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"
33 const char* IMAGES[] =
35 DEMO_IMAGE_DIR "people-medium-1.jpg",
36 DEMO_IMAGE_DIR "people-medium-4.jpg",
37 DEMO_IMAGE_DIR "people-medium-11.jpg",
38 DEMO_IMAGE_DIR "people-small-16.jpg",
39 DEMO_IMAGE_DIR "people-medium-15.jpg",
40 DEMO_IMAGE_DIR "people-medium-6.jpg",
42 const unsigned int NUMBER_OF_SAMPLES(sizeof(IMAGES)/sizeof(const char*));
45 #define MAKE_SHADER(A)#A
47 const char* VERTEX_SHADER = MAKE_SHADER(
48 uniform highp float uHue;
49 attribute mediump vec2 aPosition;
50 attribute highp vec2 aTexCoord;
51 varying mediump vec2 vTexCoord;
52 uniform mediump mat4 uMvpMatrix;
53 uniform mediump vec3 uSize;
54 varying mediump vec3 vGlobColor;
58 vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
59 vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
60 return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
65 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
66 vertexPosition.xyz *= uSize;
67 vertexPosition = uMvpMatrix * vertexPosition;
68 vGlobColor = hsv2rgb( vec3( clamp(uHue, 0.0, 1.0), 1.0, 1.0 ) );
70 vTexCoord = aTexCoord;
71 gl_Position = vertexPosition;
75 const char* FRAGMENT_SHADER = MAKE_SHADER(
76 varying mediump vec2 vTexCoord;
77 varying mediump vec3 vGlobColor;
78 uniform lowp vec4 uColor;
79 uniform sampler2D sTexture;
83 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4(vGlobColor, 1.0) ;
87 Geometry CreateGeometry()
90 const float halfQuadSize = .5f;
91 struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
92 TexturedQuadVertex texturedQuadVertexData[4] = {
93 { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
94 { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
95 { Vector2(-halfQuadSize, halfQuadSize), Vector2(0.f, 1.f) },
96 { Vector2( halfQuadSize, halfQuadSize), Vector2(1.f, 1.f) } };
98 Property::Map texturedQuadVertexFormat;
99 texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
100 texturedQuadVertexFormat["aTexCoord"] = Property::VECTOR2;
101 PropertyBuffer texturedQuadVertices = PropertyBuffer::New( texturedQuadVertexFormat );
102 texturedQuadVertices.SetData( texturedQuadVertexData, 4 );
105 unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
107 // Create the geometry object
108 Geometry texturedQuadGeometry = Geometry::New();
109 texturedQuadGeometry.AddVertexBuffer( texturedQuadVertices );
110 texturedQuadGeometry.SetIndexBuffer( &indexData[0], sizeof(indexData)/sizeof(unsigned short) );
112 return texturedQuadGeometry;
115 } // anonymous namespace
117 // This example shows how to use a simple mesh
119 class ExampleController : public ConnectionTracker
124 * The example controller constructor.
125 * @param[in] application The application instance
127 ExampleController( Application& application )
128 : mApplication( application ),
131 // Connect to the Application's Init signal
132 mApplication.InitSignal().Connect( this, &ExampleController::Create );
133 memset(mDepthIndices, 0, sizeof(mDepthIndices));
137 * The example controller destructor
141 // Nothing to do here;
145 * Invoked upon creation of application
146 * @param[in] application The application instance
148 void Create( Application& application )
150 Stage stage = Stage::GetCurrent();
151 stage.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
153 mStageSize = stage.GetSize();
155 // The Init signal is received once (only) during the Application lifetime
157 // Hide the indicator bar
158 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
160 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
161 mGeometry = CreateGeometry();
163 TextureSet firstTextureSet;
165 for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
167 Image image = ResourceImage::New( IMAGES[i] );
168 TextureSet textureSet = TextureSet::New();
169 textureSet.SetImage( 0u, image );
170 if( i==0 ) { firstTextureSet = textureSet; }
172 Renderer renderer = Renderer::New( mGeometry, mShader );
173 renderer.SetTextures( textureSet );
174 Actor meshActor = Actor::New();
175 mActors[i] = meshActor;
176 meshActor.AddRenderer( renderer );
177 meshActor.SetSize(175, 175);
178 meshActor.RegisterProperty("index", (int)i);
180 renderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 );
181 // Test with actor alpha
182 meshActor.SetParentOrigin( ParentOrigin::CENTER );
183 meshActor.SetAnchorPoint( AnchorPoint::CENTER );
184 meshActor.SetPosition( 40.0f*(i-(NUMBER_OF_SAMPLES*0.5f)), 40.0f*(i-(NUMBER_OF_SAMPLES*0.5f)), i*10 );
186 meshActor.SetOpacity( i%2?0.7f:1.0f );
188 meshActor.RegisterProperty("uHue", i/(float)NUMBER_OF_SAMPLES);
190 meshActor.TouchSignal().Connect(this, &ExampleController::OnTouched);
191 std::ostringstream oss;
192 oss << "Mesh Actor " << i;
193 meshActor.SetName(oss.str());
194 stage.Add( meshActor );
197 mActors[NUMBER_OF_SAMPLES-2].GetRendererAt(0).SetTextures( firstTextureSet );
199 stage.GetRootLayer().TouchSignal().Connect(this, &ExampleController::OnStageTouched);
208 printf("Children Z ordered back to front\n");
213 printf("All children set to same Z=0\n");
218 printf("Children Z ordered front to back\n");
223 for( unsigned i=0; i<NUMBER_OF_SAMPLES; ++i)
225 printf("DepthIndex[%d]=%d\n", i, mDepthIndices[i]);
230 bool OnTouched( Actor actor, const TouchData& event )
232 if( event.GetState( 0 ) == PointState::UP )
234 int index = actor.GetProperty<int>(actor.GetPropertyIndex("index"));
236 int newDepthIndex = (mDepthIndices[index] + 10) % 30;
237 mDepthIndices[index] = newDepthIndex;
239 Renderer renderer = actor.GetRendererAt(0);
240 renderer.SetProperty( Renderer::Property::DEPTH_INDEX, newDepthIndex);
247 bool OnStageTouched( Actor rootLayer, const TouchData& event )
249 if( event.GetState( 0 ) == PointState::UP )
256 for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
258 Actor child = rootLayer.GetChildAt(i);
267 for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
269 Actor child = rootLayer.GetChildAt(i);
270 child.SetZ( 100-i*10 );
278 for(unsigned int i=1; i < rootLayer.GetChildCount(); ++i)
280 Actor child = rootLayer.GetChildAt(i);
292 * Invoked whenever the quit button is clicked
293 * @param[in] button the quit button
295 bool OnQuitButtonClicked( Toolkit::Button button )
297 // quit the application
302 void OnKeyEvent(const KeyEvent& event)
304 if(event.state == KeyEvent::Down)
306 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
315 Application& mApplication; ///< Application instance
316 Vector3 mStageSize; ///< The size of the stage
321 int mDepthIndices[NUMBER_OF_SAMPLES];
322 Actor mActors[NUMBER_OF_SAMPLES];
326 void RunTest( Application& application )
328 ExampleController test( application );
330 application.MainLoop();
333 // Entry point for Linux & SLP applications
335 int DALI_EXPORT_API main( int argc, char **argv )
337 Application application = Application::New( &argc, &argv );
339 RunTest( application );