2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/controls/scrollable/bouncing-effect-actor.h>
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>
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);
44 #define MAKE_SHADER(A)#A
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
56 gl_Position = uMvpMatrix * vec4(mix( aPosition1, aPosition2, abs(uBounceCoefficient) )*uSize, 1.0);\n
60 // use the actor color to paint every layer
61 const char* MESH_FRAGMENT_SHADER = MAKE_SHADER(
62 uniform lowp vec4 uColor;\n
65 gl_FragColor = uColor;\n
71 Actor CreateBouncingEffectActor( Property::Index& bouncePropertyIndex )
73 // Create the bouncing mesh geometry
79 // 4 vertices 2 triangles per layer. The depth interval between each layer is 0.01
80 VertexPosition vertexData[12] = {
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 ) },
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 ) },
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 ) }
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 );
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::INTEGER;
106 PropertyBuffer indices = PropertyBuffer::New( indexFormat, 18u );
107 indices.SetData( indexData );
109 Geometry meshGeometry = Geometry::New();
110 meshGeometry.AddVertexBuffer( vertices );
111 meshGeometry.SetIndexBuffer( indices );
114 Shader shader = Shader::New( MESH_VERTEX_SHADER, MESH_FRAGMENT_SHADER );
115 Material material = Material::New( shader );
118 Renderer renderer = Renderer::New( meshGeometry, material );
121 Actor meshActor= Actor::New();
122 meshActor.AddRenderer( renderer );
125 bouncePropertyIndex = meshActor.RegisterProperty("uBounceCoefficient", 0.f);
130 } // namespace Internal
132 } // namespace Toolkit