Remove ResourceImage usage from demos
[platform/core/uifw/dali-demo.git] / examples / mesh-sorting / mesh-sorting-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 #include <stdio.h>
22 #include <sstream>
23 #include <cstring>
24
25 // INTERNAL INCLUDES
26 #include "shared/view.h"
27 #include "shared/utility.h"
28
29 using namespace Dali;
30
31 namespace
32 {
33
34 const char* IMAGES[] =
35 {
36   DEMO_IMAGE_DIR "people-medium-1.jpg",
37   DEMO_IMAGE_DIR "people-medium-4.jpg",
38   DEMO_IMAGE_DIR "people-medium-11.jpg",
39   DEMO_IMAGE_DIR "people-small-16.jpg",
40   DEMO_IMAGE_DIR "people-medium-15.jpg",
41   DEMO_IMAGE_DIR "people-medium-6.jpg",
42 };
43 const unsigned int NUMBER_OF_SAMPLES(sizeof(IMAGES)/sizeof(const char*));
44
45
46 #define MAKE_SHADER(A)#A
47
48 const char* VERTEX_SHADER = MAKE_SHADER(
49 uniform   highp   float   uHue;
50 attribute mediump vec2    aPosition;
51 attribute highp   vec2    aTexCoord;
52 varying   mediump vec2    vTexCoord;
53 uniform   mediump mat4    uMvpMatrix;
54 uniform   mediump vec3    uSize;
55 varying   mediump vec3    vGlobColor;
56
57 vec3 hsv2rgb(vec3 c)
58 {
59   vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
60   vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
61   return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
62 }
63
64 void main()
65 {
66   mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
67   vertexPosition.xyz *= uSize;
68   vertexPosition = uMvpMatrix * vertexPosition;
69   vGlobColor = hsv2rgb( vec3( clamp(uHue, 0.0, 1.0), 1.0, 1.0 ) );
70
71   vTexCoord = aTexCoord;
72   gl_Position = vertexPosition;
73 }
74 );
75
76 const char* FRAGMENT_SHADER = MAKE_SHADER(
77 varying mediump vec2  vTexCoord;
78 varying mediump vec3  vGlobColor;
79 uniform lowp    vec4  uColor;
80 uniform sampler2D     sTexture;
81
82 void main()
83 {
84   gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4(vGlobColor, 1.0) ;
85 }
86 );
87
88 Geometry CreateGeometry()
89 {
90   // Create vertices
91   const float halfQuadSize = .5f;
92   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
93   TexturedQuadVertex texturedQuadVertexData[4] = {
94     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
95     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
96     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
97     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
98
99   Property::Map texturedQuadVertexFormat;
100   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
101   texturedQuadVertexFormat["aTexCoord"] = Property::VECTOR2;
102   PropertyBuffer texturedQuadVertices = PropertyBuffer::New( texturedQuadVertexFormat );
103   texturedQuadVertices.SetData( texturedQuadVertexData, 4 );
104
105   // Create indices
106   unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
107
108   // Create the geometry object
109   Geometry texturedQuadGeometry = Geometry::New();
110   texturedQuadGeometry.AddVertexBuffer( texturedQuadVertices );
111   texturedQuadGeometry.SetIndexBuffer( &indexData[0], sizeof(indexData)/sizeof(unsigned short) );
112
113   return texturedQuadGeometry;
114 }
115
116 } // anonymous namespace
117
118 // This example shows how to use a simple mesh
119 //
120 class ExampleController : public ConnectionTracker
121 {
122 public:
123
124   /**
125    * The example controller constructor.
126    * @param[in] application The application instance
127    */
128   ExampleController( Application& application )
129   : mApplication( application ),
130     mZMode(0)
131   {
132     // Connect to the Application's Init signal
133     mApplication.InitSignal().Connect( this, &ExampleController::Create );
134     memset(mDepthIndices, 0, sizeof(mDepthIndices));
135   }
136
137   /**
138    * The example controller destructor
139    */
140   ~ExampleController()
141   {
142     // Nothing to do here;
143   }
144
145   /**
146    * Invoked upon creation of application
147    * @param[in] application The application instance
148    */
149   void Create( Application& application )
150   {
151     Stage stage = Stage::GetCurrent();
152     stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
153
154     mStageSize = stage.GetSize();
155
156     // The Init signal is received once (only) during the Application lifetime
157
158     // Hide the indicator bar
159     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
160
161     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
162     mGeometry = CreateGeometry();
163
164     TextureSet firstTextureSet;
165
166     for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
167     {
168       Texture texture = DemoHelper::LoadTexture( IMAGES[i] );
169       TextureSet textureSet = TextureSet::New();
170       textureSet.SetTexture( 0u, texture );
171       if( i==0 ) { firstTextureSet = textureSet; }
172
173       Renderer renderer = Renderer::New( mGeometry, mShader );
174       renderer.SetTextures( textureSet );
175       Actor meshActor = Actor::New();
176       mActors[i] = meshActor;
177       meshActor.AddRenderer( renderer );
178       meshActor.SetSize(175, 175);
179       meshActor.RegisterProperty("index", (int)i);
180
181       renderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
182       // Test with actor alpha
183       meshActor.SetParentOrigin( ParentOrigin::CENTER );
184       meshActor.SetAnchorPoint( AnchorPoint::CENTER );
185       meshActor.SetPosition( 40.0f*(i-(NUMBER_OF_SAMPLES*0.5f)), 40.0f*(i-(NUMBER_OF_SAMPLES*0.5f)), i*10 );
186
187       meshActor.SetOpacity( i%2?0.7f:1.0f );
188
189       meshActor.RegisterProperty("uHue", i/(float)NUMBER_OF_SAMPLES);
190
191       meshActor.TouchSignal().Connect(this, &ExampleController::OnTouched);
192       std::ostringstream oss;
193       oss << "Mesh Actor " << i;
194       meshActor.SetName(oss.str());
195       stage.Add( meshActor );
196     }
197
198     mActors[NUMBER_OF_SAMPLES-2].GetRendererAt(0).SetTextures( firstTextureSet );
199
200     stage.GetRootLayer().TouchSignal().Connect(this, &ExampleController::OnStageTouched);
201   }
202
203   void PrintDepths()
204   {
205     switch( mZMode )
206     {
207       case 0:
208       {
209         printf("Children Z ordered back to front\n");
210         break;
211       }
212       case 1:
213       {
214         printf("All children set to same Z=0\n");
215         break;
216       }
217       case 2:
218       {
219         printf("Children Z ordered front to back\n");
220         break;
221       }
222     }
223
224     for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
225     {
226       printf("DepthIndex[%d]=%d\n", i, mDepthIndices[i]);
227     }
228     printf("\n");
229   }
230
231   bool OnTouched( Actor actor, const TouchData& event )
232   {
233     if( event.GetState( 0 ) == PointState::UP )
234     {
235       int index = actor.GetProperty<int>(actor.GetPropertyIndex("index"));
236
237       int newDepthIndex = (mDepthIndices[index] + 10) % 30;
238       mDepthIndices[index] = newDepthIndex;
239
240       Renderer renderer = actor.GetRendererAt(0);
241       renderer.SetProperty( Renderer::Property::DEPTH_INDEX, newDepthIndex);
242
243       PrintDepths();
244     }
245     return true;
246   }
247
248   bool OnStageTouched( Actor rootLayer, const TouchData& event )
249   {
250     if( event.GetState( 0 ) == PointState::UP )
251     {
252       switch( mZMode )
253       {
254         case 0:
255         {
256           mZMode = 1;
257           for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
258           {
259             Actor child = rootLayer.GetChildAt(i);
260             child.SetZ( 0.0f );
261           }
262           PrintDepths();
263           break;
264         }
265         case 1:
266         {
267           mZMode = 2;
268           for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
269           {
270             Actor child = rootLayer.GetChildAt(i);
271             child.SetZ( 100-i*10 );
272           }
273           PrintDepths();
274           break;
275         }
276         case 2:
277         {
278           mZMode = 0;
279           for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
280           {
281             Actor child = rootLayer.GetChildAt(i);
282             child.SetZ( i*10 );
283           }
284           PrintDepths();
285           break;
286         }
287       }
288     }
289     return true;
290   }
291
292   /**
293    * Invoked whenever the quit button is clicked
294    * @param[in] button the quit button
295    */
296   bool OnQuitButtonClicked( Toolkit::Button button )
297   {
298     // quit the application
299     mApplication.Quit();
300     return true;
301   }
302
303   void OnKeyEvent(const KeyEvent& event)
304   {
305     if(event.state == KeyEvent::Down)
306     {
307       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
308       {
309         mApplication.Quit();
310       }
311     }
312   }
313
314 private:
315
316   Application&  mApplication;                             ///< Application instance
317   Vector3 mStageSize;                                     ///< The size of the stage
318
319   Shader   mShader;
320   Geometry mGeometry;
321
322   int mDepthIndices[NUMBER_OF_SAMPLES];
323   Actor mActors[NUMBER_OF_SAMPLES];
324   int mZMode;
325 };
326
327 void RunTest( Application& application )
328 {
329   ExampleController test( application );
330
331   application.MainLoop();
332 }
333
334 // Entry point for Linux & SLP applications
335 //
336 int DALI_EXPORT_API main( int argc, char **argv )
337 {
338   Application application = Application::New( &argc, &argv );
339
340   RunTest( application );
341
342   return 0;
343 }