line mesh demo supports LINES, LINE_LOOP and LINE_STRIP modes. added radio button... 27/66027/2
authorAdam Bialogonski <adam.b@samsung.com>
Thu, 14 Apr 2016 17:26:00 +0000 (18:26 +0100)
committerAdam Bialogonski <adam.b@samsung.com>
Fri, 15 Apr 2016 16:44:05 +0000 (17:44 +0100)
Change-Id: Ifbf9ab55afa216c388e123a521e7e9a8004b4ede

examples/line-mesh/line-mesh-example.cpp

index 8916928..52f9b97 100644 (file)
@@ -32,14 +32,18 @@ namespace
 const char* VERTEX_SHADER = MAKE_SHADER(
 attribute mediump vec2    aPosition1;
 attribute mediump vec2    aPosition2;
+attribute lowp    vec3    aColor;
 uniform   mediump mat4    uMvpMatrix;
 uniform   mediump vec3    uSize;
 uniform   mediump float   uMorphAmount;
 
+varying   lowp    vec3    vColor;
+
 void main()
 {
   mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount);
   mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0);
+  vColor = aColor;
   vertexPosition.xyz *= uSize;
   vertexPosition = uMvpMatrix * vertexPosition;
   gl_Position = vertexPosition;
@@ -50,59 +54,87 @@ const char* FRAGMENT_SHADER = MAKE_SHADER(
 uniform lowp  vec4    uColor;
 uniform sampler2D     sTexture;
 
+varying   lowp        vec3 vColor;
+
 void main()
 {
-  gl_FragColor = uColor;
+  gl_FragColor = uColor * vec4( vColor, 1.0 );
 }
 );
 
+PropertyBuffer CreateIndexBuffer( Geometry::GeometryType geometryType )
+{
+  // Create indices
+  const unsigned int indexDataLines[]    =    { 0, 1, 1, 2, 2, 3, 3, 4, 4, 0 };
+  const unsigned int indexDataLoops[]    =    { 0, 1, 2, 3, 4 };
+  const unsigned int indexDataStrips[]   =    { 0, 1, 2, 3, 4, 0 };
+
+  // Create index buffer if doesn't exist
+  Property::Map indexFormat;
+  indexFormat["indices"] = Property::INTEGER;
+  PropertyBuffer indices = PropertyBuffer::New( indexFormat );
+
+  // Update buffer
+  switch( geometryType )
+  {
+    case Geometry::LINES:
+    {
+      indices.SetData( indexDataLines, sizeof(indexDataLines)/sizeof(indexDataLines[0]) );
+      break;
+    }
+    case Geometry::LINE_LOOP:
+    {
+      indices.SetData( indexDataLoops, sizeof(indexDataLoops)/sizeof(indexDataLoops[0]) );
+      break;
+    }
+    case Geometry::LINE_STRIP:
+    {
+      indices.SetData( indexDataStrips, sizeof(indexDataStrips)/sizeof(indexDataStrips[0]) );
+      break;
+    }
+    default: // this will never happen, but compilers yells
+    {
+    }
+  }
+
+  return indices;
+}
+
 Geometry CreateGeometry()
 {
   // Create vertices
-  struct Vertex { Vector2 position; };
-  Vertex pentagonVertexData[5] =
-    {
-      { Vector2(  0.0f,   1.00f) }, // 0
-      { Vector2( -0.95f,  0.31f) }, // 1
-      { Vector2( -0.59f, -0.81f) }, // 2
-      { Vector2(  0.59f, -0.81f) }, // 3
-      { Vector2(  0.95f,  0.31f) }, // 4
-    };
+  struct Vertex
+  {
+    Vector2 position1;
+    Vector2 position2;
+    Vector3 color;
+  };
 
-  Vertex pentacleVertexData[5] =
+  // Create new geometry object
+  Vertex pentagonVertexData[5] =
     {
-      { Vector2(  0.0f,  -1.00f) }, //
-      { Vector2(  0.59f,  0.81f) }, //
-      { Vector2( -0.95f, -0.31f) }, //
-      { Vector2(  0.95f, -0.31f) }, //
-      { Vector2( -0.59f,  0.81f) }, //
+      { Vector2(  0.0f,   1.00f),  Vector2(  0.0f,  -1.00f),  Vector3( 1.0f, 1.0f, 1.0f ) }, // 0
+      { Vector2( -0.95f,  0.31f),  Vector2(  0.59f,  0.81f),  Vector3( 1.0f, 0.0f, 0.0f ) }, // 1
+      { Vector2( -0.59f, -0.81f),  Vector2( -0.95f, -0.31f),  Vector3( 0.0f, 1.0f, 0.0f ) }, // 2
+      { Vector2(  0.59f, -0.81f),  Vector2(  0.95f, -0.31f),  Vector3( 0.0f, 0.0f, 1.0f ) }, // 3
+      { Vector2(  0.95f,  0.31f),  Vector2( -0.59f,  0.81f),  Vector3( 1.0f, 1.0f, 0.0f ) }, // 4
     };
 
   Property::Map pentagonVertexFormat;
   pentagonVertexFormat["aPosition1"] = Property::VECTOR2;
+  pentagonVertexFormat["aPosition2"] = Property::VECTOR2;
+  pentagonVertexFormat["aColor"] = Property::VECTOR3;
   PropertyBuffer pentagonVertices = PropertyBuffer::New( pentagonVertexFormat );
   pentagonVertices.SetData(pentagonVertexData, 5);
 
-  Property::Map pentacleVertexFormat;
-  pentacleVertexFormat["aPosition2"] = Property::VECTOR2;
-  PropertyBuffer pentacleVertices = PropertyBuffer::New( pentacleVertexFormat );
-  pentacleVertices.SetData( pentacleVertexData, 5 );
-
   // Create indices
-  unsigned int indexData[10] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 0 };
-  Property::Map indexFormat;
-  indexFormat["indices"] = Property::INTEGER;
-  PropertyBuffer indices = PropertyBuffer::New( indexFormat );
-  indices.SetData( indexData, sizeof(indexData)/sizeof(indexData[0]) );
+  PropertyBuffer indices = CreateIndexBuffer( Geometry::LINES );
 
   // Create the geometry object
   Geometry pentagonGeometry = Geometry::New();
   pentagonGeometry.AddVertexBuffer( pentagonVertices );
-  pentagonGeometry.AddVertexBuffer( pentacleVertices );
   pentagonGeometry.SetIndexBuffer( indices );
-
   pentagonGeometry.SetGeometryType( Geometry::LINES );
-
   return pentagonGeometry;
 }
 
@@ -140,21 +172,40 @@ public:
   void Create( Application& application )
   {
     Stage stage = Stage::GetCurrent();
+
+    CreateRadioButtons();
+
     stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
 
     mStageSize = stage.GetSize();
 
     // The Init signal is received once (only) during the Application lifetime
+    ReInitialise( Geometry::LINES );
 
     // Hide the indicator bar
     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
 
-    mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
+    stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
+  }
 
-    mMaterial = Material::New( mShader );
+  /**
+   * Invoked whenever application changes the type of geometry drawn
+   * @param[in] type of geometry
+   */
+  void ReInitialise( Geometry::GeometryType geometryType )
+  {
+    Stage stage = Stage::GetCurrent();
 
-    mGeometry = CreateGeometry();
+    // destroy mesh actor and its resources if already exists
+    if( mMeshActor )
+    {
+      stage.Remove( mMeshActor );
+      mMeshActor.Reset();
+    }
 
+    mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
+    mMaterial = Material::New( mShader );
+    mGeometry = CreateGeometry();
     mRenderer = Renderer::New( mGeometry, mMaterial );
 
     mMeshActor = Actor::New();
@@ -177,8 +228,48 @@ public:
     animation.AnimateBetween( Property( mMeshActor, morphAmountIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
     animation.SetLooping(true);
     animation.Play();
+  }
 
-    stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
+  /**
+   * Invoked on create
+   */
+  void CreateRadioButtons()
+  {
+    Stage stage = Stage::GetCurrent();
+
+    Toolkit::TableView modeSelectTableView = Toolkit::TableView::New( 3, 1 );
+    modeSelectTableView.SetParentOrigin( ParentOrigin::TOP_LEFT );
+    modeSelectTableView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+    modeSelectTableView.SetFitHeight( 0 );
+    modeSelectTableView.SetFitHeight( 1 );
+    modeSelectTableView.SetFitHeight( 2 );
+    modeSelectTableView.SetCellPadding( Vector2( 6.0f, 0.0f ) );
+    modeSelectTableView.SetScale( Vector3( 0.5f, 0.5f, 0.5f ));
+
+    const char* labels[] =
+    {
+      "LINES",
+      "LINE_LOOP",
+      "LINE_STRIP"
+    };
+
+    for( int i = 0; i < 3; ++i )
+    {
+      Property::Map labelMap;
+      labelMap[ "text" ]      = labels[i];
+      labelMap[ "textColor" ] = Vector4( 0.8f, 0.8f, 0.8f, 1.0f );
+
+      Dali::Toolkit::RadioButton radio = Dali::Toolkit::RadioButton::New();
+
+      radio.SetProperty( Dali::Toolkit::RadioButton::Property::LABEL, labelMap );
+      radio.SetParentOrigin( ParentOrigin::TOP_LEFT );
+      radio.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+      radio.SetSelected( i == 0 );
+      radio.PressedSignal().Connect( this, &ExampleController::OnButtonPressed );
+      mButtons[i] = radio;
+      modeSelectTableView.AddChild( radio, Toolkit::TableView::CellPosition( i,  0 ) );
+    }
+    stage.Add(modeSelectTableView);
   }
 
   /**
@@ -203,6 +294,38 @@ public:
     }
   }
 
+  bool OnButtonPressed( Toolkit::Button button )
+  {
+    const Geometry::GeometryType geomTypes[] =
+    {
+      Geometry::LINES,
+      Geometry::LINE_LOOP,
+      Geometry::LINE_STRIP
+    };
+
+    size_t index;
+    if( button == mButtons[0] )
+    {
+      index = 0;
+    }
+    else if( button == mButtons[1] )
+    {
+      index = 1;
+    }
+    else
+    {
+      index = 2;
+    }
+
+    PropertyBuffer indices = CreateIndexBuffer( geomTypes[ index ] );
+    mGeometry.SetIndexBuffer( indices );
+    mGeometry.SetGeometryType( geomTypes[ index ] );
+
+    return true;
+  }
+
+
+
 private:
 
   Application&  mApplication;                             ///< Application instance
@@ -213,6 +336,7 @@ private:
   Geometry mGeometry;
   Renderer mRenderer;
   Actor    mMeshActor;
+  Toolkit::RadioButton mButtons[3];
 };
 
 void RunTest( Application& application )