Merge "Implemented n-patch rendering for NPatchRenderer." into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scrollable / bouncing-effect-actor.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/scrollable/bouncing-effect-actor.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/math/vector3.h>
23 #include <dali/public-api/object/property-map.h>
24 #include <dali/devel-api/object/property-buffer.h>
25 #include <dali/devel-api/rendering/geometry.h>
26 #include <dali/devel-api/rendering/material.h>
27 #include <dali/devel-api/rendering/renderer.h>
28 #include <dali/devel-api/rendering/shader.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 namespace
40 {
41 // Bouncing effect is presented by stacked three layers with same color and opacity
42 const float LAYER_HEIGHTS[5] =
43 {
44   1.f,
45   26.f * 4.f/ 130.f,
46   26.f * 3.f / 130.f,
47   26.f * 2.f / 130.f,
48   26.f / 130.f
49 };
50
51 #define MAKE_SHADER(A)#A
52
53 // Modify the vertex position according to the bounce coefficient;
54 const char* MESH_VERTEX_SHADER = MAKE_SHADER(
55 attribute mediump vec3    aPosition1;\n
56 attribute mediump vec3    aPosition2;\n
57 uniform   mediump mat4    uMvpMatrix;\n
58 uniform   mediump vec3    uSize;
59 uniform   mediump float   uBounceCoefficient;\n
60 \n
61 void main()\n
62 {\n
63   gl_Position = uMvpMatrix * vec4(mix( aPosition1, aPosition2, abs(uBounceCoefficient) )*uSize, 1.0);\n
64 }
65 );
66
67 // use the actor color to paint every layer
68 const char* MESH_FRAGMENT_SHADER = MAKE_SHADER(
69 uniform lowp  vec4    uColor;\n
70 void main()\n
71 {\n
72   gl_FragColor = uColor;\n
73 }\n
74 );
75
76 } // namespace Anon
77
78 Actor CreateBouncingEffectActor( Property::Index& bouncePropertyIndex )
79 {
80   // Create the bouncing mesh geometry
81   struct VertexPosition
82   {
83     Vector3 position1;
84     Vector3 position2;
85   };
86   // 4 vertices 2 triangles per layer. The depth interval between each layer is 0.01
87   VertexPosition vertexData[20] = {
88     // bottom layer
89     { Vector3( -0.5f, -0.5f, 0.f ),  Vector3( -0.5f, -0.5f, 0.f )  },
90     { Vector3( 0.5f, -0.5f, 0.f ),   Vector3( 0.5f, -0.5f, 0.f )   },
91     { Vector3( -0.5f, -0.5f, 0.f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[0], 0.f ) },
92     { Vector3( 0.5f, -0.5f, 0.f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[0], 0.f )   },
93     // mid-bottom layer
94     { Vector3( -0.5f, -0.5f, 0.01f ),  Vector3( -0.5f, -0.5f, 0.01f )  },
95     { Vector3( 0.5f, -0.5f, 0.01f ),   Vector3( 0.5f, -0.5f, 0.01f )   },
96     { Vector3( -0.5f, -0.5f, 0.01f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[1], 0.01f ) },
97     { Vector3( 0.5f, -0.5f, 0.01f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[1], 0.01f )   },
98     // middle layer
99     { Vector3( -0.5f, -0.5f, 0.02f ),  Vector3( -0.5f, -0.5f, 0.02f )  },
100     { Vector3( 0.5f, -0.5f, 0.02f ),   Vector3( 0.5f, -0.5f, 0.02f )   },
101     { Vector3( -0.5f, -0.5f, 0.02f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[2], 0.02f ) },
102     { Vector3( 0.5f, -0.5f, 0.02f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[2], 0.02f )   },
103     // mid-top layer
104     { Vector3( -0.5f, -0.5f, 0.03f ),  Vector3( -0.5f, -0.5f, 0.03f )  },
105     { Vector3( 0.5f, -0.5f, 0.03f ),   Vector3( 0.5f, -0.5f, 0.03f )   },
106     { Vector3( -0.5f, -0.5f, 0.03f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[3], 0.03f ) },
107     { Vector3( 0.5f, -0.5f, 0.03f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[3], 0.03f )   },
108     // top layer
109     { Vector3( -0.5f, -0.5f, 0.04f ),  Vector3( -0.5f, -0.5f, 0.04f )  },
110     { Vector3( 0.5f, -0.5f, 0.04f ),   Vector3( 0.5f, -0.5f, 0.04f )   },
111     { Vector3( -0.5f, -0.5f, 0.04f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[4], 0.04f ) },
112     { Vector3( 0.5f, -0.5f, 0.04f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[4], 0.04f )   }
113   };
114   Property::Map vertexFormat;
115   vertexFormat["aPosition1"] = Property::VECTOR3;
116   vertexFormat["aPosition2"] = Property::VECTOR3;
117   PropertyBuffer vertices = PropertyBuffer::New( vertexFormat, 20u );
118   vertices.SetData( vertexData );
119
120   unsigned int indexData[30] = { 0,3,1,0,2,3,4,7,5,4,6,7,8,11,9,8,10,11,12,15,13,12,14,15,16,19,17,16,18,19};
121   Property::Map indexFormat;
122   indexFormat["indices"] = Property::INTEGER;
123   PropertyBuffer indices = PropertyBuffer::New( indexFormat, 30u );
124   indices.SetData( indexData );
125
126   Geometry meshGeometry = Geometry::New();
127   meshGeometry.AddVertexBuffer( vertices );
128   meshGeometry.SetIndexBuffer( indices );
129
130   // Create material
131   Shader shader = Shader::New( MESH_VERTEX_SHADER, MESH_FRAGMENT_SHADER );
132   Material material = Material::New( shader );
133
134   // Create renderer
135   Renderer renderer = Renderer::New( meshGeometry, material );
136
137   // Create actor
138   Actor meshActor= Actor::New();
139   meshActor.AddRenderer( renderer );
140
141   // Register property
142   bouncePropertyIndex = meshActor.RegisterProperty("uBounceCoefficient", 0.f);
143
144   return meshActor;
145 }
146
147 } // namespace Internal
148
149 } // namespace Toolkit
150
151 } // namespace Dali