2 * Copyright (c) 2015 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/devel-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 Geometry CreateGeometry()
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) } };
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 );
106 unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
108 // Create the geometry object
109 Geometry texturedQuadGeometry = Geometry::New();
110 texturedQuadGeometry.AddVertexBuffer( texturedQuadVertices );
111 texturedQuadGeometry.SetIndexBuffer( &indexData[0], sizeof(indexData)/sizeof(unsigned short) );
113 return texturedQuadGeometry;
116 } // anonymous namespace
118 // This example shows how to use a simple mesh
120 class ExampleController : public ConnectionTracker
125 * The example controller constructor.
126 * @param[in] application The application instance
128 ExampleController( Application& application )
129 : mApplication( application ),
132 // Connect to the Application's Init signal
133 mApplication.InitSignal().Connect( this, &ExampleController::Create );
134 memset(mDepthIndices, 0, sizeof(mDepthIndices));
138 * The example controller destructor
142 // Nothing to do here;
146 * Invoked upon creation of application
147 * @param[in] application The application instance
149 void Create( Application& application )
151 Stage stage = Stage::GetCurrent();
152 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
154 mStageSize = stage.GetSize();
156 // The Init signal is received once (only) during the Application lifetime
158 // Hide the indicator bar
159 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
161 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
162 mGeometry = CreateGeometry();
164 TextureSet firstTextureSet;
166 for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
168 Texture texture = DemoHelper::LoadTexture( IMAGES[i] );
169 TextureSet textureSet = TextureSet::New();
170 textureSet.SetTexture( 0u, texture );
171 if( i==0 ) { firstTextureSet = textureSet; }
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);
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 );
187 meshActor.SetOpacity( i%2?0.7f:1.0f );
189 meshActor.RegisterProperty("uHue", i/(float)NUMBER_OF_SAMPLES);
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 );
198 mActors[NUMBER_OF_SAMPLES-2].GetRendererAt(0).SetTextures( firstTextureSet );
200 stage.GetRootLayer().TouchSignal().Connect(this, &ExampleController::OnStageTouched);
209 printf("Children Z ordered back to front\n");
214 printf("All children set to same Z=0\n");
219 printf("Children Z ordered front to back\n");
224 for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
226 printf("DepthIndex[%d]=%d\n", i, mDepthIndices[i]);
231 bool OnTouched( Actor actor, const TouchData& event )
233 if( event.GetState( 0 ) == PointState::UP )
235 int index = actor.GetProperty<int>(actor.GetPropertyIndex("index"));
237 int newDepthIndex = (mDepthIndices[index] + 10) % 30;
238 mDepthIndices[index] = newDepthIndex;
240 Renderer renderer = actor.GetRendererAt(0);
241 renderer.SetProperty( Renderer::Property::DEPTH_INDEX, newDepthIndex);
248 bool OnStageTouched( Actor rootLayer, const TouchData& event )
250 if( event.GetState( 0 ) == PointState::UP )
257 for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
259 Actor child = rootLayer.GetChildAt(i);
268 for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
270 Actor child = rootLayer.GetChildAt(i);
271 child.SetZ( 100-i*10 );
279 for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
281 Actor child = rootLayer.GetChildAt(i);
293 * Invoked whenever the quit button is clicked
294 * @param[in] button the quit button
296 bool OnQuitButtonClicked( Toolkit::Button button )
298 // quit the application
303 void OnKeyEvent(const KeyEvent& event)
305 if(event.state == KeyEvent::Down)
307 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
316 Application& mApplication; ///< Application instance
317 Vector3 mStageSize; ///< The size of the stage
322 int mDepthIndices[NUMBER_OF_SAMPLES];
323 Actor mActors[NUMBER_OF_SAMPLES];
327 void RunTest( Application& application )
329 ExampleController test( application );
331 application.MainLoop();
334 // Entry point for Linux & SLP applications
336 int DALI_EXPORT_API main( int argc, char **argv )
338 Application application = Application::New( &argc, &argv );
340 RunTest( application );