0b94968bbb95812199693d1523b2e94b03ce5102
[platform/core/uifw/dali-demo.git] / examples / mesh-sorting / mesh-sorting-example.cpp
1 /*
2  * Copyright (c) 2016 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/public-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 } // anonymous namespace
89
90 // This example shows how to use a simple mesh
91 //
92 class ExampleController : public ConnectionTracker
93 {
94 public:
95
96   /**
97    * The example controller constructor.
98    * @param[in] application The application instance
99    */
100   ExampleController( Application& application )
101   : mApplication( application ),
102     mZMode(0)
103   {
104     // Connect to the Application's Init signal
105     mApplication.InitSignal().Connect( this, &ExampleController::Create );
106     memset(mDepthIndices, 0, sizeof(mDepthIndices));
107   }
108
109   /**
110    * The example controller destructor
111    */
112   ~ExampleController()
113   {
114     // Nothing to do here;
115   }
116
117   /**
118    * Invoked upon creation of application
119    * @param[in] application The application instance
120    */
121   void Create( Application& application )
122   {
123     Stage stage = Stage::GetCurrent();
124     stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
125
126     mStageSize = stage.GetSize();
127
128     // The Init signal is received once (only) during the Application lifetime
129
130     // Hide the indicator bar
131     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
132
133     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
134     mGeometry = DemoHelper::CreateTexturedQuad();
135
136     TextureSet firstTextureSet;
137
138     for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
139     {
140       Texture texture = DemoHelper::LoadTexture( IMAGES[i] );
141       TextureSet textureSet = TextureSet::New();
142       textureSet.SetTexture( 0u, texture );
143       if( i==0 ) { firstTextureSet = textureSet; }
144
145       Renderer renderer = Renderer::New( mGeometry, mShader );
146       renderer.SetTextures( textureSet );
147       Actor meshActor = Actor::New();
148       mActors[i] = meshActor;
149       meshActor.AddRenderer( renderer );
150       meshActor.SetSize(175, 175);
151       meshActor.RegisterProperty("index", (int)i);
152
153       renderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
154       // Test with actor alpha
155       meshActor.SetParentOrigin( ParentOrigin::CENTER );
156       meshActor.SetAnchorPoint( AnchorPoint::CENTER );
157       meshActor.SetPosition( 40.0f*(i-(NUMBER_OF_SAMPLES*0.5f)), 40.0f*(i-(NUMBER_OF_SAMPLES*0.5f)), i*10 );
158
159       meshActor.SetOpacity( i%2?0.7f:1.0f );
160
161       meshActor.RegisterProperty("uHue", i/(float)NUMBER_OF_SAMPLES);
162
163       meshActor.TouchSignal().Connect(this, &ExampleController::OnTouched);
164       std::ostringstream oss;
165       oss << "Mesh Actor " << i;
166       meshActor.SetName(oss.str());
167       stage.Add( meshActor );
168     }
169
170     mActors[NUMBER_OF_SAMPLES-2].GetRendererAt(0).SetTextures( firstTextureSet );
171
172     stage.GetRootLayer().TouchSignal().Connect(this, &ExampleController::OnStageTouched);
173   }
174
175   void PrintDepths()
176   {
177     switch( mZMode )
178     {
179       case 0:
180       {
181         printf("Children Z ordered back to front\n");
182         break;
183       }
184       case 1:
185       {
186         printf("All children set to same Z=0\n");
187         break;
188       }
189       case 2:
190       {
191         printf("Children Z ordered front to back\n");
192         break;
193       }
194     }
195
196     for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
197     {
198       printf("DepthIndex[%d]=%d\n", i, mDepthIndices[i]);
199     }
200     printf("\n");
201   }
202
203   bool OnTouched( Actor actor, const TouchData& event )
204   {
205     if( event.GetState( 0 ) == PointState::UP )
206     {
207       int index = actor.GetProperty<int>(actor.GetPropertyIndex("index"));
208
209       int newDepthIndex = (mDepthIndices[index] + 10) % 30;
210       mDepthIndices[index] = newDepthIndex;
211
212       Renderer renderer = actor.GetRendererAt(0);
213       renderer.SetProperty( Renderer::Property::DEPTH_INDEX, newDepthIndex);
214
215       PrintDepths();
216     }
217     return true;
218   }
219
220   bool OnStageTouched( Actor rootLayer, const TouchData& event )
221   {
222     if( event.GetState( 0 ) == PointState::UP )
223     {
224       switch( mZMode )
225       {
226         case 0:
227         {
228           mZMode = 1;
229           for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
230           {
231             Actor child = rootLayer.GetChildAt(i);
232             child.SetZ( 0.0f );
233           }
234           PrintDepths();
235           break;
236         }
237         case 1:
238         {
239           mZMode = 2;
240           for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
241           {
242             Actor child = rootLayer.GetChildAt(i);
243             child.SetZ( 100-i*10 );
244           }
245           PrintDepths();
246           break;
247         }
248         case 2:
249         {
250           mZMode = 0;
251           for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
252           {
253             Actor child = rootLayer.GetChildAt(i);
254             child.SetZ( i*10 );
255           }
256           PrintDepths();
257           break;
258         }
259       }
260     }
261     return true;
262   }
263
264   /**
265    * Invoked whenever the quit button is clicked
266    * @param[in] button the quit button
267    */
268   bool OnQuitButtonClicked( Toolkit::Button button )
269   {
270     // quit the application
271     mApplication.Quit();
272     return true;
273   }
274
275   void OnKeyEvent(const KeyEvent& event)
276   {
277     if(event.state == KeyEvent::Down)
278     {
279       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
280       {
281         mApplication.Quit();
282       }
283     }
284   }
285
286 private:
287
288   Application&  mApplication;                             ///< Application instance
289   Vector3 mStageSize;                                     ///< The size of the stage
290
291   Shader   mShader;
292   Geometry mGeometry;
293
294   int mDepthIndices[NUMBER_OF_SAMPLES];
295   Actor mActors[NUMBER_OF_SAMPLES];
296   int mZMode;
297 };
298
299 void RunTest( Application& application )
300 {
301   ExampleController test( application );
302
303   application.MainLoop();
304 }
305
306 // Entry point for Linux & SLP applications
307 //
308 int DALI_EXPORT_API main( int argc, char **argv )
309 {
310   Application application = Application::New( &argc, &argv );
311
312   RunTest( application );
313
314   return 0;
315 }