Rendering API clean-up
[platform/core/uifw/dali-demo.git] / examples / line-mesh / line-mesh-example.cpp
1 /*
2  * Copyright (c) 2015 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 // EXTERNAL INCLUDES
19 #include <dali/devel-api/rendering/renderer.h>
20 #include <dali-toolkit/dali-toolkit.h>
21
22 // INTERNAL INCLUDES
23 #include "shared/view.h"
24
25 using namespace Dali;
26
27 namespace
28 {
29
30 #define MAKE_SHADER(A)#A
31
32 const char* VERTEX_SHADER = MAKE_SHADER(
33 attribute mediump vec2    aPosition1;
34 attribute mediump vec2    aPosition2;
35 attribute lowp    vec3    aColor;
36 uniform   mediump mat4    uMvpMatrix;
37 uniform   mediump vec3    uSize;
38 uniform   mediump float   uMorphAmount;
39
40 varying   lowp    vec3    vColor;
41
42 void main()
43 {
44   mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount);
45   mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0);
46   vColor = aColor;
47   vertexPosition.xyz *= uSize;
48   vertexPosition = uMvpMatrix * vertexPosition;
49   gl_Position = vertexPosition;
50 }
51 );
52
53 const char* FRAGMENT_SHADER = MAKE_SHADER(
54 uniform lowp  vec4    uColor;
55 uniform sampler2D     sTexture;
56
57 varying   lowp        vec3 vColor;
58
59 void main()
60 {
61   gl_FragColor = uColor * vec4( vColor, 1.0 );
62 }
63 );
64
65 PropertyBuffer CreateIndexBuffer( Geometry::GeometryType geometryType )
66 {
67   // Create indices
68   const unsigned int indexDataLines[]    =    { 0, 1, 1, 2, 2, 3, 3, 4, 4, 0 };
69   const unsigned int indexDataLoops[]    =    { 0, 1, 2, 3, 4 };
70   const unsigned int indexDataStrips[]   =    { 0, 1, 2, 3, 4, 0 };
71
72   // Create index buffer if doesn't exist
73   Property::Map indexFormat;
74   indexFormat["indices"] = Property::INTEGER;
75   PropertyBuffer indices = PropertyBuffer::New( indexFormat );
76
77   // Update buffer
78   switch( geometryType )
79   {
80     case Geometry::LINES:
81     {
82       indices.SetData( indexDataLines, sizeof(indexDataLines)/sizeof(indexDataLines[0]) );
83       break;
84     }
85     case Geometry::LINE_LOOP:
86     {
87       indices.SetData( indexDataLoops, sizeof(indexDataLoops)/sizeof(indexDataLoops[0]) );
88       break;
89     }
90     case Geometry::LINE_STRIP:
91     {
92       indices.SetData( indexDataStrips, sizeof(indexDataStrips)/sizeof(indexDataStrips[0]) );
93       break;
94     }
95     default: // this will never happen, but compilers yells
96     {
97     }
98   }
99
100   return indices;
101 }
102
103 Geometry CreateGeometry()
104 {
105   // Create vertices
106   struct Vertex
107   {
108     Vector2 position1;
109     Vector2 position2;
110     Vector3 color;
111   };
112
113   // Create new geometry object
114   Vertex pentagonVertexData[5] =
115     {
116       { Vector2(  0.0f,   1.00f),  Vector2(  0.0f,  -1.00f),  Vector3( 1.0f, 1.0f, 1.0f ) }, // 0
117       { Vector2( -0.95f,  0.31f),  Vector2(  0.59f,  0.81f),  Vector3( 1.0f, 0.0f, 0.0f ) }, // 1
118       { Vector2( -0.59f, -0.81f),  Vector2( -0.95f, -0.31f),  Vector3( 0.0f, 1.0f, 0.0f ) }, // 2
119       { Vector2(  0.59f, -0.81f),  Vector2(  0.95f, -0.31f),  Vector3( 0.0f, 0.0f, 1.0f ) }, // 3
120       { Vector2(  0.95f,  0.31f),  Vector2( -0.59f,  0.81f),  Vector3( 1.0f, 1.0f, 0.0f ) }, // 4
121     };
122
123   Property::Map pentagonVertexFormat;
124   pentagonVertexFormat["aPosition1"] = Property::VECTOR2;
125   pentagonVertexFormat["aPosition2"] = Property::VECTOR2;
126   pentagonVertexFormat["aColor"] = Property::VECTOR3;
127   PropertyBuffer pentagonVertices = PropertyBuffer::New( pentagonVertexFormat );
128   pentagonVertices.SetData(pentagonVertexData, 5);
129
130   // Create indices
131   PropertyBuffer indices = CreateIndexBuffer( Geometry::LINES );
132
133   // Create the geometry object
134   Geometry pentagonGeometry = Geometry::New();
135   pentagonGeometry.AddVertexBuffer( pentagonVertices );
136   pentagonGeometry.SetIndexBuffer( indices );
137   pentagonGeometry.SetGeometryType( Geometry::LINES );
138   return pentagonGeometry;
139 }
140
141 } // anonymous namespace
142
143 // This example shows how to morph between 2 meshes with the same number of
144 // vertices.
145 class ExampleController : public ConnectionTracker
146 {
147 public:
148
149   /**
150    * The example controller constructor.
151    * @param[in] application The application instance
152    */
153   ExampleController( Application& application )
154   : mApplication( application )
155   {
156     // Connect to the Application's Init signal
157     mApplication.InitSignal().Connect( this, &ExampleController::Create );
158   }
159
160   /**
161    * The example controller destructor
162    */
163   ~ExampleController()
164   {
165     // Nothing to do here;
166   }
167
168   /**
169    * Invoked upon creation of application
170    * @param[in] application The application instance
171    */
172   void Create( Application& application )
173   {
174     Stage stage = Stage::GetCurrent();
175
176     CreateRadioButtons();
177
178     stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
179
180     mStageSize = stage.GetSize();
181
182     // The Init signal is received once (only) during the Application lifetime
183     ReInitialise( Geometry::LINES );
184
185     // Hide the indicator bar
186     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
187
188     stage.SetBackgroundColor(Vector4(0.0f, 0.2f, 0.2f, 1.0f));
189   }
190
191   /**
192    * Invoked whenever application changes the type of geometry drawn
193    * @param[in] type of geometry
194    */
195   void ReInitialise( Geometry::GeometryType geometryType )
196   {
197     Stage stage = Stage::GetCurrent();
198
199     // destroy mesh actor and its resources if already exists
200     if( mMeshActor )
201     {
202       stage.Remove( mMeshActor );
203       mMeshActor.Reset();
204     }
205
206     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
207     mGeometry = CreateGeometry();
208     mRenderer = Renderer::New( mGeometry, mShader );
209
210     mMeshActor = Actor::New();
211     mMeshActor.AddRenderer( mRenderer );
212     mMeshActor.SetSize(200, 200);
213
214     Property::Index morphAmountIndex = mMeshActor.RegisterProperty( "uMorphAmount", 0.0f );
215
216     mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
217
218     mMeshActor.SetParentOrigin( ParentOrigin::CENTER );
219     mMeshActor.SetAnchorPoint( AnchorPoint::CENTER );
220     stage.Add( mMeshActor );
221
222     Animation  animation = Animation::New(5);
223     KeyFrames keyFrames = KeyFrames::New();
224     keyFrames.Add(0.0f, 0.0f);
225     keyFrames.Add(1.0f, 1.0f);
226
227     animation.AnimateBetween( Property( mMeshActor, morphAmountIndex ), keyFrames, AlphaFunction(AlphaFunction::SIN) );
228     animation.SetLooping(true);
229     animation.Play();
230   }
231
232   /**
233    * Invoked on create
234    */
235   void CreateRadioButtons()
236   {
237     Stage stage = Stage::GetCurrent();
238
239     Toolkit::TableView modeSelectTableView = Toolkit::TableView::New( 3, 1 );
240     modeSelectTableView.SetParentOrigin( ParentOrigin::TOP_LEFT );
241     modeSelectTableView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
242     modeSelectTableView.SetFitHeight( 0 );
243     modeSelectTableView.SetFitHeight( 1 );
244     modeSelectTableView.SetFitHeight( 2 );
245     modeSelectTableView.SetCellPadding( Vector2( 6.0f, 0.0f ) );
246     modeSelectTableView.SetScale( Vector3( 0.5f, 0.5f, 0.5f ));
247
248     const char* labels[] =
249     {
250       "LINES",
251       "LINE_LOOP",
252       "LINE_STRIP"
253     };
254
255     for( int i = 0; i < 3; ++i )
256     {
257       Property::Map labelMap;
258       labelMap[ "text" ]      = labels[i];
259       labelMap[ "textColor" ] = Vector4( 0.8f, 0.8f, 0.8f, 1.0f );
260
261       Dali::Toolkit::RadioButton radio = Dali::Toolkit::RadioButton::New();
262
263       radio.SetProperty( Dali::Toolkit::RadioButton::Property::LABEL, labelMap );
264       radio.SetParentOrigin( ParentOrigin::TOP_LEFT );
265       radio.SetAnchorPoint( AnchorPoint::TOP_LEFT );
266       radio.SetSelected( i == 0 );
267       radio.PressedSignal().Connect( this, &ExampleController::OnButtonPressed );
268       mButtons[i] = radio;
269       modeSelectTableView.AddChild( radio, Toolkit::TableView::CellPosition( i,  0 ) );
270     }
271     stage.Add(modeSelectTableView);
272   }
273
274   /**
275    * Invoked whenever the quit button is clicked
276    * @param[in] button the quit button
277    */
278   bool OnQuitButtonClicked( Toolkit::Button button )
279   {
280     // quit the application
281     mApplication.Quit();
282     return true;
283   }
284
285   void OnKeyEvent(const KeyEvent& event)
286   {
287     if(event.state == KeyEvent::Down)
288     {
289       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
290       {
291         mApplication.Quit();
292       }
293     }
294   }
295
296   bool OnButtonPressed( Toolkit::Button button )
297   {
298     const Geometry::GeometryType geomTypes[] =
299     {
300       Geometry::LINES,
301       Geometry::LINE_LOOP,
302       Geometry::LINE_STRIP
303     };
304
305     size_t index;
306     if( button == mButtons[0] )
307     {
308       index = 0;
309     }
310     else if( button == mButtons[1] )
311     {
312       index = 1;
313     }
314     else
315     {
316       index = 2;
317     }
318
319     PropertyBuffer indices = CreateIndexBuffer( geomTypes[ index ] );
320     mGeometry.SetIndexBuffer( indices );
321     mGeometry.SetGeometryType( geomTypes[ index ] );
322
323     return true;
324   }
325
326
327
328 private:
329
330   Application&  mApplication;                             ///< Application instance
331   Vector3 mStageSize;                                     ///< The size of the stage
332
333   Shader   mShader;
334   Geometry mGeometry;
335   Renderer mRenderer;
336   Actor    mMeshActor;
337   Toolkit::RadioButton mButtons[3];
338 };
339
340 void RunTest( Application& application )
341 {
342   ExampleController test( application );
343
344   application.MainLoop();
345 }
346
347 // Entry point for Linux & SLP applications
348 //
349 int main( int argc, char **argv )
350 {
351   Application application = Application::New( &argc, &argv );
352
353   RunTest( application );
354
355   return 0;
356 }