Rendering API clean-up
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scrollable / bouncing-effect-actor.cpp
index a4f1ffb..91e556f 100644 (file)
 #include <dali-toolkit/internal/controls/scrollable/bouncing-effect-actor.h>
 
 // EXTERNAL INCLUDES
-#include <math.h>
-#include <dali/public-api/actors/mesh-actor.h>
-#include <dali/public-api/animation/active-constraint.h>
-#include <dali/public-api/animation/constraint.h>
-#include <dali/public-api/geometry/animatable-mesh.h>
-#include <dali/public-api/shader-effects/shader-effect.h>
 #include <dali/public-api/math/vector3.h>
+#include <dali/public-api/object/property-map.h>
+#include <dali/devel-api/object/property-buffer.h>
+#include <dali/devel-api/rendering/geometry.h>
+#include <dali/devel-api/rendering/texture-set.h>
+#include <dali/devel-api/rendering/renderer.h>
+#include <dali/devel-api/rendering/shader.h>
 
 namespace Dali
 {
@@ -39,82 +39,106 @@ namespace Internal
 namespace
 {
 // Bouncing effect is presented by stacked three layers with same color and opacity
-const size_t NUM_LAYERS( 3 );
-const Vector3 LAYER_HEIGHTS( 1.f, 27.f/42.f, 13.f/42.f);
-
-// use the actor color to paint every layer
-const char* MESH_FRAGMENT_SHADER =
-"void main()\n"
-"{\n"
-"  gl_FragColor = uColor;\n"
-"}\n";
-
-// Constraint to move the vertices vertically
-struct VertexPositionConstraint
+const float LAYER_HEIGHTS[5] =
 {
-  VertexPositionConstraint( float initialY, float range )
-  : mInitialY( initialY ),
-    mRange( range )
-  {
-  }
+  1.f,
+  26.f * 4.f/ 130.f,
+  26.f * 3.f / 130.f,
+  26.f * 2.f / 130.f,
+  26.f / 130.f
+};
 
-  Vector3 operator()( const Vector3& current, const PropertyInput& bounceCoef )
-  {
-    float positionY = mInitialY + mRange * fabsf(bounceCoef.GetFloat());
-    return Vector3( current.x, positionY, current.z );
-  }
+#define MAKE_SHADER(A)#A
+
+// Modify the vertex position according to the bounce coefficient;
+const char* MESH_VERTEX_SHADER = MAKE_SHADER(
+attribute mediump vec3    aPosition1;\n
+attribute mediump vec3    aPosition2;\n
+uniform   mediump mat4    uMvpMatrix;\n
+uniform   mediump vec3    uSize;
+uniform   mediump float   uBounceCoefficient;\n
+\n
+void main()\n
+{\n
+  gl_Position = uMvpMatrix * vec4(mix( aPosition1, aPosition2, abs(uBounceCoefficient) )*uSize, 1.0);\n
+}
+);
 
-  float mInitialY;
-  float mRange;
-};
+// use the actor color to paint every layer
+const char* MESH_FRAGMENT_SHADER = MAKE_SHADER(
+uniform lowp  vec4    uColor;\n
+void main()\n
+{\n
+  gl_FragColor = uColor;\n
+}\n
+);
 
 } // namespace Anon
 
 Actor CreateBouncingEffectActor( Property::Index& bouncePropertyIndex )
 {
-  Dali::AnimatableMesh             mesh;
-  Dali::MeshActor                  meshActor;
-
-  Dali::AnimatableMesh::Faces faces;
-  faces.reserve( NUM_LAYERS * 6 ); // 2 triangles per layer
-  for( size_t i=0; i<NUM_LAYERS; i++ )
-  {
-    size_t j=i*4;
-    faces.push_back(j); faces.push_back(j+3); faces.push_back(j+1);
-    faces.push_back(j); faces.push_back(j+2); faces.push_back(j+3);
-  }
-
-  mesh = Dali::AnimatableMesh::New(NUM_LAYERS*4, faces); // 4 vertices per layer
-  for( size_t i=0;i<NUM_LAYERS;i++ )
-  {
-    size_t j=i*4;
-    float positionZ = 0.01f*static_cast<float>( i ); // the interval between each layer is 0.01
-    mesh[j  ].SetPosition( Vector3( -0.5f, -0.5f, positionZ ) );
-    mesh[j+1].SetPosition( Vector3( 0.5f, -0.5f, positionZ ) );
-    mesh[j+2].SetPosition( Vector3( -0.5f, -0.5f, positionZ ) );
-    mesh[j+3].SetPosition( Vector3( 0.5f, -0.5f, positionZ ) );
-  }
-
-  meshActor = Dali::MeshActor::New(mesh);
-  meshActor.SetAffectedByLighting(false);
-
-  Dali::ShaderEffect shaderEffect = Dali::ShaderEffect::New( "", MESH_FRAGMENT_SHADER,
-                                                             GEOMETRY_TYPE_UNTEXTURED_MESH,
-                                                             Dali::ShaderEffect::HINT_BLENDING );
-  meshActor.SetShaderEffect(shaderEffect);
-
-  // To control the movement of all vertices with one custom property
-  bouncePropertyIndex = meshActor.RegisterProperty("BounceCoeffcient", 0.f);
-  for( size_t i=0;i<NUM_LAYERS;i++ )
+  // Create the bouncing mesh geometry
+  struct VertexPosition
   {
-    size_t j=i*4;
-    mesh.ApplyConstraint( Constraint::New<Vector3>( mesh.GetPropertyIndex(j+2, AnimatableVertex::POSITION ),
-                                                    Source(meshActor, bouncePropertyIndex),
-                                                    VertexPositionConstraint(-0.5f, LAYER_HEIGHTS[i]) ) );
-    mesh.ApplyConstraint( Constraint::New<Vector3>( mesh.GetPropertyIndex(j+3,  AnimatableVertex::POSITION),
-                                                    Source(meshActor, bouncePropertyIndex),
-                                                    VertexPositionConstraint(-0.5f, LAYER_HEIGHTS[i]) ) );
-  }
+    Vector3 position1;
+    Vector3 position2;
+  };
+  // 4 vertices 2 triangles per layer. The depth interval between each layer is 0.01
+  VertexPosition vertexData[20] = {
+    // bottom layer
+    { Vector3( -0.5f, -0.5f, 0.f ),  Vector3( -0.5f, -0.5f, 0.f )  },
+    { Vector3( 0.5f, -0.5f, 0.f ),   Vector3( 0.5f, -0.5f, 0.f )   },
+    { Vector3( -0.5f, -0.5f, 0.f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[0], 0.f ) },
+    { Vector3( 0.5f, -0.5f, 0.f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[0], 0.f )   },
+    // mid-bottom layer
+    { Vector3( -0.5f, -0.5f, 0.01f ),  Vector3( -0.5f, -0.5f, 0.01f )  },
+    { Vector3( 0.5f, -0.5f, 0.01f ),   Vector3( 0.5f, -0.5f, 0.01f )   },
+    { Vector3( -0.5f, -0.5f, 0.01f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[1], 0.01f ) },
+    { Vector3( 0.5f, -0.5f, 0.01f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[1], 0.01f )   },
+    // middle layer
+    { Vector3( -0.5f, -0.5f, 0.02f ),  Vector3( -0.5f, -0.5f, 0.02f )  },
+    { Vector3( 0.5f, -0.5f, 0.02f ),   Vector3( 0.5f, -0.5f, 0.02f )   },
+    { Vector3( -0.5f, -0.5f, 0.02f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[2], 0.02f ) },
+    { Vector3( 0.5f, -0.5f, 0.02f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[2], 0.02f )   },
+    // mid-top layer
+    { Vector3( -0.5f, -0.5f, 0.03f ),  Vector3( -0.5f, -0.5f, 0.03f )  },
+    { Vector3( 0.5f, -0.5f, 0.03f ),   Vector3( 0.5f, -0.5f, 0.03f )   },
+    { Vector3( -0.5f, -0.5f, 0.03f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[3], 0.03f ) },
+    { Vector3( 0.5f, -0.5f, 0.03f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[3], 0.03f )   },
+    // top layer
+    { Vector3( -0.5f, -0.5f, 0.04f ),  Vector3( -0.5f, -0.5f, 0.04f )  },
+    { Vector3( 0.5f, -0.5f, 0.04f ),   Vector3( 0.5f, -0.5f, 0.04f )   },
+    { Vector3( -0.5f, -0.5f, 0.04f ),  Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[4], 0.04f ) },
+    { Vector3( 0.5f, -0.5f, 0.04f ),   Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[4], 0.04f )   }
+  };
+  Property::Map vertexFormat;
+  vertexFormat["aPosition1"] = Property::VECTOR3;
+  vertexFormat["aPosition2"] = Property::VECTOR3;
+  PropertyBuffer vertices = PropertyBuffer::New( vertexFormat );
+  vertices.SetData( vertexData, 20u );
+
+  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};
+  Property::Map indexFormat;
+  indexFormat["indices"] = Property::INTEGER;
+  PropertyBuffer indices = PropertyBuffer::New( indexFormat );
+  indices.SetData( indexData, 30u );
+
+  Geometry meshGeometry = Geometry::New();
+  meshGeometry.AddVertexBuffer( vertices );
+  meshGeometry.SetIndexBuffer( indices );
+
+  // Create the shader
+  Shader shader = Shader::New( MESH_VERTEX_SHADER, MESH_FRAGMENT_SHADER );
+
+  // Create renderer
+  Renderer renderer = Renderer::New( meshGeometry, shader );
+
+  // Create actor
+  Actor meshActor= Actor::New();
+  meshActor.AddRenderer( renderer );
+
+  // Register property
+  bouncePropertyIndex = meshActor.RegisterProperty("uBounceCoefficient", 0.f);
 
   return meshActor;
 }