2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/public-api/rendering/renderer.h>
20 #include <dali-toolkit/dali-toolkit.h>
26 #include "shared/view.h"
27 #include "shared/utility.h"
34 const char* IMAGES[] =
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",
43 const unsigned int NUMBER_OF_SAMPLES(sizeof(IMAGES)/sizeof(const char*));
46 #define MAKE_SHADER(A)#A
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;
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);
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 ) );
71 vTexCoord = aTexCoord;
72 gl_Position = vertexPosition;
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;
84 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4(vGlobColor, 1.0) ;
88 } // anonymous namespace
90 // This example shows how to use a simple mesh
92 class ExampleController : public ConnectionTracker
97 * The example controller constructor.
98 * @param[in] application The application instance
100 ExampleController( Application& application )
101 : mApplication( application ),
104 // Connect to the Application's Init signal
105 mApplication.InitSignal().Connect( this, &ExampleController::Create );
106 memset(mDepthIndices, 0, sizeof(mDepthIndices));
110 * The example controller destructor
114 // Nothing to do here;
118 * Invoked upon creation of application
119 * @param[in] application The application instance
121 void Create( Application& application )
123 Stage stage = Stage::GetCurrent();
124 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
126 mStageSize = stage.GetSize();
128 // The Init signal is received once (only) during the Application lifetime
130 // Hide the indicator bar
131 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
133 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
134 mGeometry = DemoHelper::CreateTexturedQuad();
136 TextureSet firstTextureSet;
138 for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
140 Texture texture = DemoHelper::LoadTexture( IMAGES[i] );
141 TextureSet textureSet = TextureSet::New();
142 textureSet.SetTexture( 0u, texture );
143 if( i==0 ) { firstTextureSet = textureSet; }
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);
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 );
159 meshActor.SetOpacity( i%2?0.7f:1.0f );
161 meshActor.RegisterProperty("uHue", i/(float)NUMBER_OF_SAMPLES);
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 );
170 mActors[NUMBER_OF_SAMPLES-2].GetRendererAt(0).SetTextures( firstTextureSet );
172 stage.GetRootLayer().TouchSignal().Connect(this, &ExampleController::OnStageTouched);
181 printf("Children Z ordered back to front\n");
186 printf("All children set to same Z=0\n");
191 printf("Children Z ordered front to back\n");
196 for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
198 printf("DepthIndex[%d]=%d\n", i, mDepthIndices[i]);
203 bool OnTouched( Actor actor, const TouchData& event )
205 if( event.GetState( 0 ) == PointState::UP )
207 int index = actor.GetProperty<int>(actor.GetPropertyIndex("index"));
209 int newDepthIndex = (mDepthIndices[index] + 10) % 30;
210 mDepthIndices[index] = newDepthIndex;
212 Renderer renderer = actor.GetRendererAt(0);
213 renderer.SetProperty( Renderer::Property::DEPTH_INDEX, newDepthIndex);
220 bool OnStageTouched( Actor rootLayer, const TouchData& event )
222 if( event.GetState( 0 ) == PointState::UP )
229 for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
231 Actor child = rootLayer.GetChildAt(i);
240 for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
242 Actor child = rootLayer.GetChildAt(i);
243 child.SetZ( 100-i*10 );
251 for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
253 Actor child = rootLayer.GetChildAt(i);
265 * Invoked whenever the quit button is clicked
266 * @param[in] button the quit button
268 bool OnQuitButtonClicked( Toolkit::Button button )
270 // quit the application
275 void OnKeyEvent(const KeyEvent& event)
277 if(event.state == KeyEvent::Down)
279 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
288 Application& mApplication; ///< Application instance
289 Vector3 mStageSize; ///< The size of the stage
294 int mDepthIndices[NUMBER_OF_SAMPLES];
295 Actor mActors[NUMBER_OF_SAMPLES];
299 void RunTest( Application& application )
301 ExampleController test( application );
303 application.MainLoop();
306 // Entry point for Linux & SLP applications
308 int DALI_EXPORT_API main( int argc, char **argv )
310 Application application = Application::New( &argc, &argv );
312 RunTest( application );