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-toolkit/dali-toolkit.h>
20 #include <dali/devel-api/adaptor-framework/bitmap-loader.h>
23 #include "renderer-stencil-shaders.h"
24 #include "shared/view.h"
25 #include "shared/utility.h"
34 // Application constants:
35 const char * const APPLICATION_TITLE( "Renderer Stencil API Demo" );
36 const char * const BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
39 const char * const CUBE_TEXTURE( DEMO_IMAGE_DIR "people-medium-1.jpg" );
40 const char * const FLOOR_TEXTURE( DEMO_IMAGE_DIR "wood.png" );
42 // Scale dimensions: These values are relative to the stage size. EG. width = 0.32f * stageSize.
43 const float CUBE_WIDTH_SCALE( 0.32f ); ///< The width (and height + depth) of the main and reflection cubes.
44 const Vector2 FLOOR_DIMENSION_SCALE( 0.67f, 0.017f ); ///< The width and height of the floor object.
46 // Configurable animation characteristics:
47 const float ANIMATION_ROTATION_DURATION( 10.0f ); ///< Time in seconds to rotate the scene 360 degrees around Y.
48 const float ANIMATION_BOUNCE_TOTAL_TIME( 1.6f ); ///< Time in seconds to perform 1 full bounce animation cycle.
49 const float ANIMATION_BOUNCE_DEFORMATION_TIME( 0.4f ); ///< Time in seconds that the cube deformation animation will occur for (on contact with the floor).
50 const float ANIMATION_BOUNCE_DEFORMATION_PERCENT( 20.0f ); ///< Percentage (of the cube's size) to deform the cube by (on contact with floor).
51 const float ANIMATION_BOUNCE_HEIGHT_PERCENT( 40.0f ); ///< Percentage (of the cube's size) to bounce up in to the air by.
53 // Base colors for the objects:
54 const Vector4 TEXT_COLOR( 1.0f, 1.0f, 1.0f, 1.0f ); ///< White.
55 const Vector4 CUBE_COLOR( 1.0f, 1.0f, 1.0f, 1.0f ); ///< White.
56 const Vector4 FLOOR_COLOR( 1.0f, 1.0f, 1.0f, 1.0f ); ///< White.
57 const Vector4 REFLECTION_COLOR( 0.6f, 0.6f, 0.6f, 0.6f ); ///< Note that alpha is not 1.0f, to make the blend more photo-realistic.
59 // We need to control the draw order as we are controlling both the stencil and depth buffer per renderer.
60 const int DEPTH_INDEX_GRANULARITY( 10000 ); ///< This value is the gap in depth-index in-between each renderer.
62 } // Anonymous namespace
65 * @brief This example shows how to manipulate stencil and depth buffer properties within the Renderer API.
67 class RendererStencilExample : public ConnectionTracker
73 * @param[in] application The DALi application object
75 RendererStencilExample( Application& application )
76 : mApplication( application )
78 // Connect to the Application's Init signal.
79 mApplication.InitSignal().Connect( this, &RendererStencilExample::Create );
83 * @brief Destructor (non-virtual).
85 ~RendererStencilExample()
92 * @brief Enum to facilitate more readable use of the cube array.
96 MAIN_CUBE, ///< The main cube that bounces above the floor object.
97 REFLECTION_CUBE ///< The reflected cube object.
101 * @brief Struct to store the position, normal and texture coordinates of a single vertex.
103 struct TexturedVertex
107 Vector2 textureCoord;
111 * @brief This is the main scene setup method for this demo.
112 * This is called via the Init signal which is received once (only) during the Application lifetime.
113 * @param[in] application The DALi application object
115 void Create( Application& application )
117 Stage stage = Stage::GetCurrent();
119 // Hide the indicator bar
120 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
122 // Creates the background image.
123 Toolkit::Control background = Dali::Toolkit::Control::New();
124 background.SetAnchorPoint( Dali::AnchorPoint::CENTER );
125 background.SetParentOrigin( Dali::ParentOrigin::CENTER );
126 background.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::ALL_DIMENSIONS );
127 Dali::Property::Map map;
128 map["rendererType"] = "IMAGE";
129 map["url"] = BACKGROUND_IMAGE;
130 background.SetProperty( Dali::Toolkit::Control::Property::BACKGROUND, map );
131 stage.Add( background );
133 // Create a TextLabel for the application title.
134 Toolkit::TextLabel label = Toolkit::TextLabel::New( APPLICATION_TITLE );
135 label.SetAnchorPoint( AnchorPoint::TOP_CENTER );
136 // Set the parent origin to a small percentage below the top (so the demo will scale for different resolutions).
137 label.SetParentOrigin( Vector3( 0.5f, 0.03f, 0.5f ) );
138 label.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
139 label.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
140 label.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, TEXT_COLOR );
143 // Layer to hold the 3D scene.
144 Layer layer = Layer::New();
145 layer.SetAnchorPoint( AnchorPoint::CENTER );
146 // Set the parent origin to a small percentage below the center (so the demo will scale for different resolutions).
147 layer.SetParentOrigin( Vector3( 0.5f, 0.58f, 0.5f ) );
148 layer.SetBehavior( Layer::LAYER_2D );
149 layer.SetDepthTestDisabled( false );
153 // Make the demo scalable with different resolutions by basing
154 // the cube size on a percentage of the stage size.
155 float scaleSize( std::min( stage.GetSize().width, stage.GetSize().height ) );
156 float cubeWidth( scaleSize * CUBE_WIDTH_SCALE );
157 Vector3 cubeSize( cubeWidth, cubeWidth, cubeWidth );
158 // Create the geometry for the cube, and the texture.
159 Geometry cubeGeometry = CreateCubeVertices( Vector3::ONE, false );
160 TextureSet cubeTextureSet = CreateTextureSet( CUBE_TEXTURE );
161 // Create the cube object and add it.
162 // Note: The cube is anchored around its base for animation purposes, so the position can be zero.
163 mCubes[ MAIN_CUBE ] = CreateMainCubeObject( cubeGeometry, cubeSize, cubeTextureSet );
164 layer.Add( mCubes[ MAIN_CUBE ] );
167 float floorWidth( scaleSize * FLOOR_DIMENSION_SCALE.x );
168 Vector3 floorSize( floorWidth, scaleSize * FLOOR_DIMENSION_SCALE.y, floorWidth );
169 // Create the floor object using the cube geometry with a new size, and add it.
170 Actor floorObject( CreateFloorObject( cubeGeometry, floorSize ) );
171 layer.Add( floorObject );
174 Vector3 planeSize( floorWidth, floorWidth, 0.0f );
175 // Create the stencil plane object, and add it.
176 Actor stencilPlaneObject( CreateStencilPlaneObject( planeSize ) );
177 layer.Add( stencilPlaneObject );
180 // Create the reflection cube object and add it.
181 // Note: The cube is anchored around its base for animation purposes, so the position can be zero.
182 mCubes[ REFLECTION_CUBE ] = CreateReflectionCubeObject( cubeSize, cubeTextureSet );
183 layer.Add( mCubes[ REFLECTION_CUBE ] );
185 // Rotate the layer so we can see some of the top of the cube for a more 3D effect.
186 layer.SetProperty( Actor::Property::ORIENTATION, Quaternion( Degree( -24.0f ), Degree( 0.0f ), Degree( 0.0f ) ) );
188 // Set up the rotation on the Y axis.
189 mRotationAnimation = Animation::New( ANIMATION_ROTATION_DURATION );
190 float fullRotation = 360.0f;
191 mRotationAnimation.AnimateBy( Property( mCubes[ MAIN_CUBE ], Actor::Property::ORIENTATION ),
192 Quaternion( Degree( 0.0f ), Degree( fullRotation ), Degree( 0.0f ) ) );
193 mRotationAnimation.AnimateBy( Property( floorObject, Actor::Property::ORIENTATION ),
194 Quaternion( Degree( 0.0f ), Degree( fullRotation ), Degree( 0.0f ) ) );
195 // Note the stencil is pre-rotated by 90 degrees on X, so we rotate relatively on its Z axis for an equivalent Y rotation.
196 mRotationAnimation.AnimateBy( Property( stencilPlaneObject, Actor::Property::ORIENTATION ),
197 Quaternion( Degree( 0.0f ), Degree( 0.0f ), Degree( fullRotation ) ) );
198 mRotationAnimation.AnimateBy( Property( mCubes[ REFLECTION_CUBE ], Actor::Property::ORIENTATION ),
199 Quaternion( Degree( 0.0f ), Degree( fullRotation ), Degree( 0.0f ) ) );
200 mRotationAnimation.SetLooping( true );
202 // Set up the cube bouncing animation.
203 float totalTime = ANIMATION_BOUNCE_TOTAL_TIME;
204 float deformationTime = ANIMATION_BOUNCE_DEFORMATION_TIME;
205 // Percentage based amounts allows the bounce and deformation to scale for different resolution screens.
206 float deformationAmount = ANIMATION_BOUNCE_DEFORMATION_PERCENT / 100.0f;
207 float heightChange = ( cubeSize.y * ANIMATION_BOUNCE_HEIGHT_PERCENT ) / 100.0f;
209 // Animation pre-calculations:
210 float halfTime = totalTime / 2.0f;
211 float halfDeformationTime = deformationTime / 2.0f;
213 // First position the cubes at the top of the animation cycle.
214 mCubes[ MAIN_CUBE ].SetProperty( Actor::Property::POSITION_Y, -heightChange );
215 mCubes[ REFLECTION_CUBE ].SetProperty( Actor::Property::POSITION_Y, heightChange );
217 mBounceAnimation = Animation::New( totalTime );
219 // The animations for the main and reflected cubes are almost identical, so we combine the code to do both.
220 for( int cube = 0; cube < 2; ++cube )
222 // If iterating on the reflection cube, adjust the heightChange variable so the below code can be reused.
225 heightChange = -heightChange;
228 // 1st TimePeriod: Start moving down with increasing speed, until it is time to distort the cube due to impact.
229 mBounceAnimation.AnimateBy( Property( mCubes[ cube ], Actor::Property::POSITION_Y ), heightChange, AlphaFunction::EASE_IN_SQUARE, TimePeriod( 0.0f, halfTime - halfDeformationTime ) );
231 // 2nd TimePeriod: The cube is touching the floor, start deforming it - then un-deform it again.
232 mBounceAnimation.AnimateBy( Property( mCubes[ cube ], Actor::Property::SCALE_X ), deformationAmount, AlphaFunction::BOUNCE, TimePeriod( halfTime - halfDeformationTime, deformationTime ) );
233 mBounceAnimation.AnimateBy( Property( mCubes[ cube ], Actor::Property::SCALE_Z ), deformationAmount, AlphaFunction::BOUNCE, TimePeriod( halfTime - halfDeformationTime, deformationTime ) );
234 mBounceAnimation.AnimateBy( Property( mCubes[ cube ], Actor::Property::SCALE_Y ), -deformationAmount, AlphaFunction::BOUNCE, TimePeriod( halfTime - halfDeformationTime, deformationTime ) );
236 // 3rd TimePeriod: Start moving up with decreasing speed, until at the apex of the animation.
237 mBounceAnimation.AnimateBy( Property( mCubes[ cube ], Actor::Property::POSITION_Y ), -heightChange, AlphaFunction::EASE_OUT_SQUARE, TimePeriod( halfTime + halfDeformationTime, halfTime - halfDeformationTime ) );
240 mBounceAnimation.SetLooping( true );
242 // Start the animations.
243 mRotationAnimation.Play();
244 mBounceAnimation.Play();
246 // Respond to a click anywhere on the stage
247 stage.GetRootLayer().TouchSignal().Connect( this, &RendererStencilExample::OnTouch );
248 // Connect signals to allow Back and Escape to exit.
249 stage.KeyEventSignal().Connect( this, &RendererStencilExample::OnKeyEvent );
254 // Methods to setup each component of the 3D scene:
257 * @brief Creates the Main cube object.
258 * This creates the renderer from existing geometry (as the cubes geometry is shared).
259 * The texture is set and all relevant renderer properties are set-up.
260 * @param[in] geometry Pre-calculated cube geometry
261 * @param[in] size The desired cube size
262 * @param[in] textureSet A pre-existing TextureSet with a texture set up, to be applied to the cube
263 * @return An actor set-up containing the main cube object
265 Actor CreateMainCubeObject( Geometry& geometry, Vector3 size, TextureSet& textureSet )
267 Toolkit::Control container = Toolkit::Control::New();
268 container.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
269 container.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
270 container.SetSize( size );
271 container.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
273 // Create a renderer from the geometry and add the texture.
274 Renderer renderer = CreateRenderer( geometry, size, true, CUBE_COLOR );
275 renderer.SetTextures( textureSet );
277 // Setup the renderer properties:
278 // We are writing to the color buffer & culling back faces (no stencil is used for the main cube).
279 renderer.SetProperty( Renderer::Property::RENDER_MODE, RenderMode::COLOR );
280 renderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
282 // We do need to write to the depth buffer as other objects need to appear underneath this cube.
283 renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
284 // We do not need to test the depth buffer as we are culling the back faces.
285 renderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::OFF );
287 // This object must be rendered 1st.
288 renderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 * DEPTH_INDEX_GRANULARITY );
290 container.AddRenderer( renderer );
295 * @brief Creates the Floor object.
296 * This creates the renderer from existing geometry (as the cube geometry can be re-used).
297 * The texture is created and set and all relevant renderer properties are set-up.
298 * @param[in] geometry Pre-calculated cube geometry
299 * @param[in] size The desired floor size
300 * @return An actor set-up containing the floor object
302 Actor CreateFloorObject( Geometry& geometry, Vector3 size )
304 Toolkit::Control container = Toolkit::Control::New();
305 container.SetAnchorPoint( AnchorPoint::TOP_CENTER );
306 container.SetParentOrigin( ParentOrigin::TOP_CENTER );
307 container.SetSize( size );
308 container.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
310 // Create a renderer from the geometry and add the texture.
311 TextureSet planeTextureSet = CreateTextureSet( FLOOR_TEXTURE );
312 Renderer renderer = CreateRenderer( geometry, size, true, FLOOR_COLOR );
313 renderer.SetTextures( planeTextureSet );
315 // Setup the renderer properties:
316 // We are writing to the color buffer & culling back faces as we are NOT doing depth write (no stencil is used for the floor).
317 renderer.SetProperty( Renderer::Property::RENDER_MODE, RenderMode::COLOR );
318 renderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
320 // We do not write to the depth buffer as its not needed.
321 renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::OFF );
322 // We do need to test the depth buffer as we need the floor to be underneath the cube.
323 renderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON );
325 // This object must be rendered 2nd.
326 renderer.SetProperty( Renderer::Property::DEPTH_INDEX, 1 * DEPTH_INDEX_GRANULARITY );
328 container.AddRenderer( renderer );
333 * @brief Creates the Stencil-Plane object.
334 * This is places on the floor object to allow the reflection to be drawn on to the floor.
335 * This creates the geometry and renderer.
336 * All relevant renderer properties are set-up.
337 * @param[in] size The desired plane size
338 * @return An actor set-up containing the stencil-plane object
340 Actor CreateStencilPlaneObject( Vector3 size )
342 Toolkit::Control container = Toolkit::Control::New();
343 container.SetAnchorPoint( AnchorPoint::CENTER );
344 container.SetParentOrigin( ParentOrigin::CENTER );
345 container.SetSize( size );
346 container.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
348 // We rotate the plane as the geometry is created flat in X & Y. We want it to span X & Z axis.
349 container.SetProperty( Actor::Property::ORIENTATION, Quaternion( Degree( -90.0f ), Degree( 0.0f ), Degree( 0.0f ) ) );
351 // Create geometry for a flat plane.
352 Geometry planeGeometry = CreatePlaneVertices( Vector2::ONE );
353 // Create a renderer from the geometry.
354 Renderer renderer = CreateRenderer( planeGeometry, size, false, Vector4::ONE );
356 // Setup the renderer properties:
357 // The stencil plane is only for stencilling.
358 renderer.SetProperty( Renderer::Property::RENDER_MODE, RenderMode::STENCIL );
360 renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION, StencilFunction::ALWAYS );
361 renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION_REFERENCE, 1 );
362 renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION_MASK, 0xFF );
363 renderer.SetProperty( Renderer::Property::STENCIL_OPERATION_ON_FAIL, StencilOperation::KEEP );
364 renderer.SetProperty( Renderer::Property::STENCIL_OPERATION_ON_Z_FAIL, StencilOperation::KEEP );
365 renderer.SetProperty( Renderer::Property::STENCIL_OPERATION_ON_Z_PASS, StencilOperation::REPLACE );
366 renderer.SetProperty( Renderer::Property::STENCIL_MASK, 0xFF );
368 // We don't want to write to the depth buffer, as this would block the reflection being drawn.
369 renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::OFF );
370 // We test the depth buffer as we want the stencil to only exist underneath the cube.
371 renderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON );
373 // This object must be rendered 3rd.
374 renderer.SetProperty( Renderer::Property::DEPTH_INDEX, 2 * DEPTH_INDEX_GRANULARITY );
376 container.AddRenderer( renderer );
381 * @brief Creates the Reflection cube object.
382 * This creates new geometry (as the texture UVs are different to the main cube).
383 * The renderer is then created.
384 * The texture is set and all relevant renderer properties are set-up.
385 * @param[in] size The desired cube size
386 * @param[in] textureSet A pre-existing TextureSet with a texture set up, to be applied to the cube
387 * @return An actor set-up containing the reflection cube object
389 Actor CreateReflectionCubeObject( Vector3 size, TextureSet& textureSet )
391 Toolkit::Control container = Toolkit::Control::New();
392 container.SetAnchorPoint( AnchorPoint::TOP_CENTER );
393 container.SetParentOrigin( ParentOrigin::TOP_CENTER );
394 container.SetSize( size );
395 container.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
397 // Create the cube geometry of unity size.
398 // The "true" specifies we want the texture UVs flipped vertically as this is the reflection cube.
399 Geometry reflectedCubeGeometry = CreateCubeVertices( Vector3::ONE, true );
400 // Create a renderer from the geometry and add the texture.
401 Renderer renderer = CreateRenderer( reflectedCubeGeometry, size, true, REFLECTION_COLOR );
402 renderer.SetTextures( textureSet );
404 // Setup the renderer properties:
405 // Write to color buffer so reflection is visible.
406 // Also enable the stencil buffer, as we will be testing against it to only draw to areas within the stencil.
407 renderer.SetProperty( Renderer::Property::RENDER_MODE, RenderMode::COLOR_STENCIL );
408 // We cull to skip drawing the back faces.
409 renderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
411 // We use blending to blend the reflection with the floor texture.
412 renderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
413 renderer.SetProperty( Renderer::Property::BLEND_EQUATION_RGB, BlendEquation::ADD );
414 renderer.SetProperty( Renderer::Property::BLEND_EQUATION_ALPHA, BlendEquation::ADD );
415 renderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_RGB, BlendFactor::ONE );
417 // Enable stencil. Here we only draw to areas within the stencil.
418 renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION, StencilFunction::EQUAL );
419 renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION_REFERENCE, 1 );
420 renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION_MASK, 0xff );
421 // Don't write to the stencil.
422 renderer.SetProperty( Renderer::Property::STENCIL_MASK, 0x00 );
424 // We don't need to write to the depth buffer, as we are culling.
425 renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::OFF );
426 // We need to test the depth buffer as we need the reflection to be underneath the cube.
427 renderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON );
429 // This object must be rendered last.
430 renderer.SetProperty( Renderer::Property::DEPTH_INDEX, 3 * DEPTH_INDEX_GRANULARITY );
432 container.AddRenderer( renderer );
439 * @brief Creates a geometry object from vertices and indices.
440 * @param[in] vertices The object vertices
441 * @param[in] indices The object indices
442 * @return A geometry object
444 Geometry CreateTexturedGeometry( Vector<TexturedVertex>& vertices, Vector<unsigned short>& indices )
447 Property::Map vertexFormat;
448 vertexFormat[POSITION] = Property::VECTOR3;
449 vertexFormat[NORMAL] = Property::VECTOR3;
450 vertexFormat[TEXTURE] = Property::VECTOR2;
452 PropertyBuffer surfaceVertices = PropertyBuffer::New( vertexFormat );
453 surfaceVertices.SetData( &vertices[0u], vertices.Size() );
455 Geometry geometry = Geometry::New();
456 geometry.AddVertexBuffer( surfaceVertices );
458 // Indices for triangle formulation
459 geometry.SetIndexBuffer( &indices[0u], indices.Size() );
464 * @brief Creates a renderer from a geometry object.
465 * @param[in] geometry The geometry to use
466 * @param[in] dimensions The dimensions (will be passed in to the shader)
467 * @param[in] textured Set to true to use the texture versions of the shaders
468 * @param[in] color The base color for the renderer
469 * @return A renderer object
471 Renderer CreateRenderer( Geometry geometry, Vector3 dimensions, bool textured, Vector4 color )
473 Stage stage = Stage::GetCurrent();
478 shader = Shader::New( VERTEX_SHADER_TEXTURED, FRAGMENT_SHADER_TEXTURED );
482 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
485 // Here we modify the light position based on half the stage size as a pre-calculation step.
486 // This avoids the work having to be done in the shader.
487 shader.RegisterProperty( LIGHT_POSITION_UNIFORM_NAME, Vector3( -stage.GetSize().width / 2.0f, -stage.GetSize().width / 2.0f, 1000.0f ) );
488 shader.RegisterProperty( COLOR_UNIFORM_NAME, color );
489 shader.RegisterProperty( OBJECT_DIMENSIONS_UNIFORM_NAME, dimensions );
491 return Renderer::New( geometry, shader );
495 * @brief Helper method to create a TextureSet from an image URL.
496 * @param[in] url An image URL
497 * @return A TextureSet object
499 TextureSet CreateTextureSet( const char* url )
501 TextureSet textureSet = TextureSet::New();
505 Texture texture = DemoHelper::LoadTexture( url );
508 textureSet.SetTexture( 0u, texture );
515 // Geometry Creation:
518 * @brief Creates a geometry object for a flat plane.
519 * The plane is oriented in X & Y axis (Z is 0).
520 * @param[in] dimensions The desired plane dimensions
521 * @return A Geometry object
523 Geometry CreatePlaneVertices( Vector2 dimensions )
525 Vector<TexturedVertex> vertices;
526 Vector<unsigned short> indices;
527 vertices.Resize( 4u );
528 indices.Resize( 6u );
530 float scaledX = 0.5f * dimensions.x;
531 float scaledY = 0.5f * dimensions.y;
533 vertices[0].position = Vector3( -scaledX, -scaledY, 0.0f );
534 vertices[0].textureCoord = Vector2( 0.0, 0.0f );
535 vertices[1].position = Vector3( scaledX, -scaledY, 0.0f );
536 vertices[1].textureCoord = Vector2( 1.0, 0.0f );
537 vertices[2].position = Vector3( scaledX, scaledY, 0.0f );
538 vertices[2].textureCoord = Vector2( 1.0, 1.0f );
539 vertices[3].position = Vector3( -scaledX, scaledY, 0.0f );
540 vertices[3].textureCoord = Vector2( 0.0, 1.0f );
542 // All vertices have the same normal.
543 for( int i = 0; i < 4; ++i )
545 vertices[i].normal = Vector3( 0.0f, 0.0f, -1.0f );
555 // Use the helper method to create the geometry object.
556 return CreateTexturedGeometry( vertices, indices );
560 * @brief Creates a geometry object for a cube (or cuboid).
561 * @param[in] dimensions The desired cube dimensions
562 * @param[in] reflectVerticalUVs Set to True to force the UVs to be vertically flipped
563 * @return A Geometry object
565 Geometry CreateCubeVertices( Vector3 dimensions, bool reflectVerticalUVs )
567 Vector<TexturedVertex> vertices;
568 Vector<unsigned short> indices;
569 int vertexIndex = 0u; // Tracks progress through vertices.
570 float scaledX = 0.5f * dimensions.x;
571 float scaledY = 0.5f * dimensions.y;
572 float scaledZ = 0.5f * dimensions.z;
573 float verticalTextureCoord = reflectVerticalUVs ? 0.0f : 1.0f;
575 vertices.Resize( 4u * 6u ); // 4 vertices x 6 faces
577 Vector<Vector3> positions; // Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
578 positions.Resize( 8u );
579 Vector<Vector3> normals; // Stores normals, which are shared between vertexes of the same face.
580 normals.Resize( 6u );
582 positions[0] = Vector3( -scaledX, scaledY, -scaledZ );
583 positions[1] = Vector3( scaledX, scaledY, -scaledZ );
584 positions[2] = Vector3( scaledX, scaledY, scaledZ );
585 positions[3] = Vector3( -scaledX, scaledY, scaledZ );
586 positions[4] = Vector3( -scaledX, -scaledY, -scaledZ );
587 positions[5] = Vector3( scaledX, -scaledY, -scaledZ );
588 positions[6] = Vector3( scaledX, -scaledY, scaledZ );
589 positions[7] = Vector3( -scaledX, -scaledY, scaledZ );
591 normals[0] = Vector3( 0, 1, 0 );
592 normals[1] = Vector3( 0, 0, -1 );
593 normals[2] = Vector3( 1, 0, 0 );
594 normals[3] = Vector3( 0, 0, 1 );
595 normals[4] = Vector3( -1, 0, 0 );
596 normals[5] = Vector3( 0, -1, 0 );
598 // Top face, upward normals.
599 for( int i = 0; i < 4; ++i, ++vertexIndex )
601 vertices[vertexIndex].position = positions[i];
602 vertices[vertexIndex].normal = normals[0];
603 // The below logic forms the correct U/V pairs for a quad when "i" goes from 0 to 3.
604 vertices[vertexIndex].textureCoord = Vector2( ( i == 1 || i == 2 ) ? 1.0f : 0.0f, ( i == 2 || i == 3 ) ? 1.0f : 0.0f );
607 // Top face, outward normals.
608 for( int i = 0; i < 4; ++i, vertexIndex += 2 )
610 vertices[vertexIndex].position = positions[i];
611 vertices[vertexIndex].normal = normals[i + 1];
615 // End, so loop around.
616 vertices[vertexIndex + 1].position = positions[0];
620 vertices[vertexIndex + 1].position = positions[i + 1];
622 vertices[vertexIndex + 1].normal = normals[i + 1];
624 vertices[vertexIndex].textureCoord = Vector2( 0.0f, verticalTextureCoord );
625 vertices[vertexIndex+1].textureCoord = Vector2( 1.0f, verticalTextureCoord );
628 // Flip the vertical texture coord for the UV values of the bottom points.
629 verticalTextureCoord = 1.0f - verticalTextureCoord;
631 // Bottom face, outward normals.
632 for( int i = 0; i < 4; ++i, vertexIndex += 2 )
634 vertices[vertexIndex].position = positions[i + 4];
635 vertices[vertexIndex].normal = normals[i + 1];
639 // End, so loop around.
640 vertices[vertexIndex + 1].position = positions[4];
644 vertices[vertexIndex + 1].position = positions[i + 5];
646 vertices[vertexIndex + 1].normal = normals[i + 1];
648 vertices[vertexIndex].textureCoord = Vector2( 0.0f, verticalTextureCoord );
649 vertices[vertexIndex+1].textureCoord = Vector2( 1.0f, verticalTextureCoord );
652 // Bottom face, downward normals.
653 for( int i = 0; i < 4; ++i, ++vertexIndex )
655 // Reverse positions for bottom face to keep triangles clockwise (for culling).
656 vertices[vertexIndex].position = positions[ 7 - i ];
657 vertices[vertexIndex].normal = normals[5];
658 // The below logic forms the correct U/V pairs for a quad when "i" goes from 0 to 3.
659 vertices[vertexIndex].textureCoord = Vector2( ( i == 1 || i == 2 ) ? 1.0f : 0.0f, ( i == 2 || i == 3 ) ? 1.0f : 0.0f );
662 // Create cube indices.
663 int triangleIndex = 0u; //Track progress through indices.
664 indices.Resize( 3u * 12u ); // 3 points x 12 triangles.
667 indices[triangleIndex] = 0;
668 indices[triangleIndex + 1] = 1;
669 indices[triangleIndex + 2] = 2;
670 indices[triangleIndex + 3] = 2;
671 indices[triangleIndex + 4] = 3;
672 indices[triangleIndex + 5] = 0;
675 int topFaceStart = 4u;
676 int bottomFaceStart = topFaceStart + 8u;
679 for( int i = 0; i < 8; i += 2, triangleIndex += 6 )
681 indices[triangleIndex ] = i + topFaceStart;
682 indices[triangleIndex + 1] = i + bottomFaceStart + 1;
683 indices[triangleIndex + 2] = i + topFaceStart + 1;
684 indices[triangleIndex + 3] = i + topFaceStart;
685 indices[triangleIndex + 4] = i + bottomFaceStart;
686 indices[triangleIndex + 5] = i + bottomFaceStart + 1;
690 indices[triangleIndex] = 20;
691 indices[triangleIndex + 1] = 21;
692 indices[triangleIndex + 2] = 22;
693 indices[triangleIndex + 3] = 22;
694 indices[triangleIndex + 4] = 23;
695 indices[triangleIndex + 5] = 20;
697 // Use the helper method to create the geometry object.
698 return CreateTexturedGeometry( vertices, indices );
704 * @brief OnTouch signal handler.
705 * @param[in] actor The actor that has been touched
706 * @param[in] touch The touch information
707 * @return True if the event has been handled
709 bool OnTouch( Actor actor, const TouchData& touch )
711 // Quit the application.
717 * @brief OnKeyEvent signal handler.
718 * @param[in] event The key event information
720 void OnKeyEvent( const KeyEvent& event )
722 if( event.state == KeyEvent::Down )
724 if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
735 Application& mApplication; ///< The DALi application object
736 Toolkit::Control mView; ///< The view used to show the background
738 Animation mRotationAnimation; ///< The animation to spin the cube & floor
739 Animation mBounceAnimation; ///< The animation to bounce the cube
740 Actor mCubes[2]; ///< The cube object containers
745 * @brief Creates an instance of the example object and runs it.
746 * @param[in] application The DALi application object
748 void RunExample( Application& application )
750 RendererStencilExample example( application );
752 application.MainLoop();
756 * @brief Entry point for Linux & Tizen applications
757 * @param[in] argc The executables argument count
758 * @param[in] argv The executables argument vector
759 * @return The executables exit code (0)
761 int DALI_EXPORT_API main( int argc, char **argv )
763 Application application = Application::New( &argc, &argv );
765 RunExample( application );