Merge branch 'devel/new_mesh' 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 Vector3 LAYER_HEIGHTS( 1.f, 27.f/42.f, 13.f/42.f);
43
44 #define MAKE_SHADER(A)#A
45
46 // Modify the vertex position according to the bounce coefficient;
47 const char* MESH_VERTEX_SHADER = MAKE_SHADER(
48 attribute mediump vec3    aPosition1;\n
49 attribute mediump vec3    aPosition2;\n
50 uniform   mediump mat4    uMvpMatrix;\n
51 uniform   mediump vec3    uSize;
52 uniform   mediump float   uBounceCoefficient;\n
53 \n
54 void main()\n
55 {\n
56   gl_Position = uMvpMatrix * vec4(mix( aPosition1, aPosition2, abs(uBounceCoefficient) )*uSize, 1.0);\n
57 }
58 );
59
60 // use the actor color to paint every layer
61 const char* MESH_FRAGMENT_SHADER = MAKE_SHADER(
62 uniform lowp  vec4    uColor;\n
63 void main()\n
64 {\n
65   gl_FragColor = uColor;\n
66 }\n
67 );
68
69 } // namespace Anon
70
71 Actor CreateBouncingEffectActor( Property::Index& bouncePropertyIndex )
72 {
73   // Create the bouncing mesh geometry
74   struct VertexPosition
75   {
76     Vector3 position1;
77     Vector3 position2;
78   };
79   // 4 vertices 2 triangles per layer. The depth interval between each layer is 0.01
80   VertexPosition vertexData[12] = {
81     //bottom layer
82     { Vector3( -0.5f, -0.5f, 0.f ),  Vector3( -0.5f, -0.5f, 0.f )  },
83     { Vector3( 0.5f, -0.5f, 0.f ),   Vector3( 0.5f, -0.5f, 0.f )   },
84     { Vector3( -0.5f, -0.5f, 0.f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[0], 0.f ) },
85     { Vector3( 0.5f, -0.5f, 0.f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[0], 0.f )   },
86     // middle layer
87     { Vector3( -0.5f, -0.5f, 0.01f ),  Vector3( -0.5f, -0.5f, 0.01f )  },
88     { Vector3( 0.5f, -0.5f, 0.01f ),   Vector3( 0.5f, -0.5f, 0.01f )   },
89     { Vector3( -0.5f, -0.5f, 0.01f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[1], 0.01f ) },
90     { Vector3( 0.5f, -0.5f, 0.01f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[1], 0.01f )   },
91     // top layer
92     { Vector3( -0.5f, -0.5f, 0.02f ),  Vector3( -0.5f, -0.5f, 0.02f )  },
93     { Vector3( 0.5f, -0.5f, 0.02f ),   Vector3( 0.5f, -0.5f, 0.02f )   },
94     { Vector3( -0.5f, -0.5f, 0.02f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[2], 0.02f ) },
95     { Vector3( 0.5f, -0.5f, 0.02f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[2], 0.02f )   }
96   };
97   Property::Map vertexFormat;
98   vertexFormat["aPosition1"] = Property::VECTOR3;
99   vertexFormat["aPosition2"] = Property::VECTOR3;
100   PropertyBuffer vertices = PropertyBuffer::New( vertexFormat, 12u );
101   vertices.SetData( vertexData );
102
103   unsigned int indexData[18] = { 0,3,1,0,2,3,4,7,5,4,6,7,8,11,9,8,10,11 };
104   Property::Map indexFormat;
105   indexFormat["indices"] = Property::UNSIGNED_INTEGER;
106   PropertyBuffer indices = PropertyBuffer::New( indexFormat, 18u );
107   indices.SetData( indexData );
108
109   Geometry meshGeometry = Geometry::New();
110   meshGeometry.AddVertexBuffer( vertices );
111   meshGeometry.SetIndexBuffer( indices );
112
113   // Create material
114   Shader shader = Shader::New( MESH_VERTEX_SHADER, MESH_FRAGMENT_SHADER );
115   Material material = Material::New( shader );
116
117   // Create renderer
118   Renderer renderer = Renderer::New( meshGeometry, material );
119
120   // Create actor
121   Actor meshActor= Actor::New();
122   meshActor.AddRenderer( renderer );
123
124   // Register property
125   bouncePropertyIndex = meshActor.RegisterProperty("uBounceCoefficient", 0.f);
126
127   return meshActor;
128 }
129
130 } // namespace Internal
131
132 } // namespace Toolkit
133
134 } // namespace Dali