Further Setter/Getter public API removal from Dali::Actor
[platform/core/uifw/dali-demo.git] / examples / renderer-stencil / renderer-stencil-example.cpp
1 /*
2  * Copyright (c) 2017 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-toolkit/dali-toolkit.h>
20
21 // INTERNAL INCLUDES
22 #include "renderer-stencil-shaders.h"
23 #include "shared/view.h"
24 #include "shared/utility.h"
25
26 using namespace Dali;
27
28 namespace
29 {
30
31 // Constants:
32
33 // Application constants:
34 const char * const APPLICATION_TITLE( "Renderer Stencil API Demo" );
35 const char * const BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
36
37 // Texture filenames:
38 const char * const CUBE_TEXTURE( DEMO_IMAGE_DIR "people-medium-1.jpg" );
39 const char * const FLOOR_TEXTURE( DEMO_IMAGE_DIR "wood.png" );
40
41 // Scale dimensions: These values are relative to the stage size. EG. width = 0.32f * stageSize.
42 const float   CUBE_WIDTH_SCALE( 0.32f );                   ///< The width (and height + depth) of the main and reflection cubes.
43 const Vector2 FLOOR_DIMENSION_SCALE( 0.67f, 0.017f );      ///< The width and height of the floor object.
44
45 // Configurable animation characteristics:
46 const float ANIMATION_ROTATION_DURATION( 10.0f );          ///< Time in seconds to rotate the scene 360 degrees around Y.
47 const float ANIMATION_BOUNCE_TOTAL_TIME( 1.6f );           ///< Time in seconds to perform 1 full bounce animation cycle.
48 const float ANIMATION_BOUNCE_DEFORMATION_TIME( 0.4f );     ///< Time in seconds that the cube deformation animation will occur for (on contact with the floor).
49 const float ANIMATION_BOUNCE_DEFORMATION_PERCENT( 20.0f ); ///< Percentage (of the cube's size) to deform the cube by (on contact with floor).
50 const float ANIMATION_BOUNCE_HEIGHT_PERCENT( 40.0f );      ///< Percentage (of the cube's size) to bounce up in to the air by.
51
52 // Base colors for the objects:
53 const Vector4 TEXT_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );        ///< White.
54 const Vector4 CUBE_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );        ///< White.
55 const Vector4 FLOOR_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );       ///< White.
56 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.
57
58 // We need to control the draw order as we are controlling both the stencil and depth buffer per renderer.
59 const int DEPTH_INDEX_GRANULARITY( 10000 );                ///< This value is the gap in depth-index in-between each renderer.
60
61 } // Anonymous namespace
62
63 /**
64  * @brief This example shows how to manipulate stencil and depth buffer properties within the Renderer API.
65  */
66 class RendererStencilExample : public ConnectionTracker
67 {
68 public:
69
70   /**
71    * @brief Constructor.
72    * @param[in] application The DALi application object
73    */
74   RendererStencilExample( Application& application )
75   : mApplication( application )
76   {
77     // Connect to the Application's Init signal.
78     mApplication.InitSignal().Connect( this, &RendererStencilExample::Create );
79   }
80
81   /**
82    * @brief Destructor (non-virtual).
83    */
84   ~RendererStencilExample()
85   {
86   }
87
88 private:
89
90   /**
91    * @brief Enum to facilitate more readable use of the cube array.
92    */
93   enum CubeType
94   {
95     MAIN_CUBE,      ///< The main cube that bounces above the floor object.
96     REFLECTION_CUBE ///< The reflected cube object.
97   };
98
99   /**
100    * @brief Struct to store the position, normal and texture coordinates of a single vertex.
101    */
102   struct TexturedVertex
103   {
104     Vector3 position;
105     Vector3 normal;
106     Vector2 textureCoord;
107   };
108
109   /**
110    * @brief This is the main scene setup method for this demo.
111    * This is called via the Init signal which is received once (only) during the Application lifetime.
112    * @param[in] application The DALi application object
113    */
114   void Create( Application& application )
115   {
116     Stage stage = Stage::GetCurrent();
117
118     // Hide the indicator bar
119     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
120
121     // Use a gradient visual to render the background gradient.
122     Toolkit::Control background = Dali::Toolkit::Control::New();
123     background.SetProperty( Actor::Property::ANCHOR_POINT, Dali::AnchorPoint::CENTER );
124     background.SetProperty( Actor::Property::PARENT_ORIGIN, Dali::ParentOrigin::CENTER );
125     background.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::ALL_DIMENSIONS );
126
127     // Set up the background gradient.
128     Property::Array stopOffsets;
129     stopOffsets.PushBack( 0.0f );
130     stopOffsets.PushBack( 1.0f );
131     Property::Array stopColors;
132     stopColors.PushBack( Vector4( 0.17f, 0.24f, 0.35f, 1.0f ) ); // Dark, medium saturated blue  ( top   of screen)
133     stopColors.PushBack( Vector4( 0.45f, 0.70f, 0.80f, 1.0f ) ); // Medium bright, pastel blue   (bottom of screen)
134     const float percentageStageHeight = stage.GetSize().height * 0.7f;
135
136     background.SetProperty( Toolkit::Control::Property::BACKGROUND, Dali::Property::Map()
137       .Add( Toolkit::Visual::Property::TYPE, Dali::Toolkit::Visual::GRADIENT )
138       .Add( Toolkit::GradientVisual::Property::STOP_OFFSET, stopOffsets )
139       .Add( Toolkit::GradientVisual::Property::STOP_COLOR, stopColors )
140       .Add( Toolkit::GradientVisual::Property::START_POSITION, Vector2( 0.0f, -percentageStageHeight ) )
141       .Add( Toolkit::GradientVisual::Property::END_POSITION, Vector2( 0.0f, percentageStageHeight ) )
142       .Add( Toolkit::GradientVisual::Property::UNITS, Toolkit::GradientVisual::Units::USER_SPACE ) );
143
144     stage.Add( background );
145
146     // Create a TextLabel for the application title.
147     Toolkit::TextLabel label = Toolkit::TextLabel::New( APPLICATION_TITLE );
148     label.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
149     // Set the parent origin to a small percentage below the top (so the demo will scale for different resolutions).
150     label.SetProperty( Actor::Property::PARENT_ORIGIN, Vector3( 0.5f, 0.03f, 0.5f ) );
151     label.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
152     label.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
153     label.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, TEXT_COLOR );
154     stage.Add( label );
155
156     // Layer to hold the 3D scene.
157     Layer layer = Layer::New();
158     layer.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
159     // Set the parent origin to a small percentage below the center (so the demo will scale for different resolutions).
160     layer.SetProperty( Actor::Property::PARENT_ORIGIN, Vector3( 0.5f, 0.58f, 0.5f ) );
161     layer.SetBehavior( Layer::LAYER_UI );
162     layer.SetDepthTestDisabled( false );
163     stage.Add( layer );
164
165     // Main cube:
166     // Make the demo scalable with different resolutions by basing
167     // the cube size on a percentage of the stage size.
168     float scaleSize( std::min( stage.GetSize().width, stage.GetSize().height ) );
169     float cubeWidth( scaleSize * CUBE_WIDTH_SCALE );
170     Vector3 cubeSize( cubeWidth, cubeWidth, cubeWidth );
171     // Create the geometry for the cube, and the texture.
172     Geometry cubeGeometry = CreateCubeVertices( Vector3::ONE, false );
173     TextureSet cubeTextureSet = CreateTextureSet( CUBE_TEXTURE );
174     // Create the cube object and add it.
175     // Note: The cube is anchored around its base for animation purposes, so the position can be zero.
176     mCubes[ MAIN_CUBE ] = CreateMainCubeObject( cubeGeometry, cubeSize, cubeTextureSet );
177     layer.Add( mCubes[ MAIN_CUBE ] );
178
179     // Floor:
180     float floorWidth( scaleSize * FLOOR_DIMENSION_SCALE.x );
181     Vector3 floorSize( floorWidth, scaleSize * FLOOR_DIMENSION_SCALE.y, floorWidth );
182     // Create the floor object using the cube geometry with a new size, and add it.
183     Actor floorObject( CreateFloorObject( cubeGeometry, floorSize ) );
184     layer.Add( floorObject );
185
186     // Stencil:
187     Vector3 planeSize( floorWidth, floorWidth, 0.0f );
188     // Create the stencil plane object, and add it.
189     Actor stencilPlaneObject( CreateStencilPlaneObject( planeSize ) );
190     layer.Add( stencilPlaneObject );
191
192     // Reflection cube:
193     // Create the reflection cube object and add it.
194     // Note: The cube is anchored around its base for animation purposes, so the position can be zero.
195     mCubes[ REFLECTION_CUBE ] = CreateReflectionCubeObject( cubeSize, cubeTextureSet );
196     layer.Add( mCubes[ REFLECTION_CUBE ] );
197
198     // Rotate the layer so we can see some of the top of the cube for a more 3D effect.
199     layer.SetProperty( Actor::Property::ORIENTATION, Quaternion( Degree( -24.0f ), Degree( 0.0f ), Degree( 0.0f ) ) );
200
201     // Set up the rotation on the Y axis.
202     mRotationAnimation = Animation::New( ANIMATION_ROTATION_DURATION );
203     float fullRotation = 360.0f;
204     mRotationAnimation.AnimateBy( Property( mCubes[ MAIN_CUBE ], Actor::Property::ORIENTATION ),
205                                  Quaternion( Degree( 0.0f ), Degree( fullRotation ), Degree( 0.0f ) ) );
206     mRotationAnimation.AnimateBy( Property( floorObject, Actor::Property::ORIENTATION ),
207                                  Quaternion( Degree( 0.0f ), Degree( fullRotation ), Degree( 0.0f ) ) );
208     // Note the stencil is pre-rotated by 90 degrees on X, so we rotate relatively on its Z axis for an equivalent Y rotation.
209     mRotationAnimation.AnimateBy( Property( stencilPlaneObject, Actor::Property::ORIENTATION ),
210                                  Quaternion( Degree( 0.0f ), Degree( 0.0f ), Degree( fullRotation ) ) );
211     mRotationAnimation.AnimateBy( Property( mCubes[ REFLECTION_CUBE ], Actor::Property::ORIENTATION ),
212                                  Quaternion( Degree( 0.0f ), Degree( fullRotation ), Degree( 0.0f ) ) );
213     mRotationAnimation.SetLooping( true );
214
215     // Set up the cube bouncing animation.
216     float totalTime = ANIMATION_BOUNCE_TOTAL_TIME;
217     float deformationTime = ANIMATION_BOUNCE_DEFORMATION_TIME;
218     // Percentage based amounts allows the bounce and deformation to scale for different resolution screens.
219     float deformationAmount = ANIMATION_BOUNCE_DEFORMATION_PERCENT / 100.0f;
220     float heightChange = ( cubeSize.y * ANIMATION_BOUNCE_HEIGHT_PERCENT ) / 100.0f;
221
222     // Animation pre-calculations:
223     float halfTime = totalTime / 2.0f;
224     float halfDeformationTime = deformationTime / 2.0f;
225
226     // First position the cubes at the top of the animation cycle.
227     mCubes[ MAIN_CUBE ].SetProperty(       Actor::Property::POSITION_Y, -heightChange );
228     mCubes[ REFLECTION_CUBE ].SetProperty( Actor::Property::POSITION_Y,  heightChange );
229
230     mBounceAnimation = Animation::New( totalTime );
231
232     // The animations for the main and reflected cubes are almost identical, so we combine the code to do both.
233     for( int cube = 0; cube < 2; ++cube )
234     {
235       // If iterating on the reflection cube, adjust the heightChange variable so the below code can be reused.
236       if( cube == 1 )
237       {
238         heightChange = -heightChange;
239       }
240
241       // 1st TimePeriod: Start moving down with increasing speed, until it is time to distort the cube due to impact.
242       mBounceAnimation.AnimateBy( Property( mCubes[ cube ], Actor::Property::POSITION_Y ),  heightChange, AlphaFunction::EASE_IN_SQUARE, TimePeriod( 0.0f, halfTime - halfDeformationTime ) );
243
244       // 2nd TimePeriod: The cube is touching the floor, start deforming it - then un-deform it again.
245       mBounceAnimation.AnimateBy( Property( mCubes[ cube ], Actor::Property::SCALE_X ),  deformationAmount, AlphaFunction::BOUNCE, TimePeriod( halfTime - halfDeformationTime, deformationTime ) );
246       mBounceAnimation.AnimateBy( Property( mCubes[ cube ], Actor::Property::SCALE_Z ),  deformationAmount, AlphaFunction::BOUNCE, TimePeriod( halfTime - halfDeformationTime, deformationTime ) );
247       mBounceAnimation.AnimateBy( Property( mCubes[ cube ], Actor::Property::SCALE_Y ), -deformationAmount, AlphaFunction::BOUNCE, TimePeriod( halfTime - halfDeformationTime, deformationTime ) );
248
249       // 3rd TimePeriod: Start moving up with decreasing speed, until at the apex of the animation.
250       mBounceAnimation.AnimateBy( Property( mCubes[ cube ], Actor::Property::POSITION_Y ), -heightChange, AlphaFunction::EASE_OUT_SQUARE, TimePeriod( halfTime + halfDeformationTime, halfTime - halfDeformationTime ) );
251     }
252
253     mBounceAnimation.SetLooping( true );
254
255     // Start the animations.
256     mRotationAnimation.Play();
257     mBounceAnimation.Play();
258
259     // Respond to a click anywhere on the stage
260     stage.GetRootLayer().TouchSignal().Connect( this, &RendererStencilExample::OnTouch );
261     // Connect signals to allow Back and Escape to exit.
262     stage.KeyEventSignal().Connect( this, &RendererStencilExample::OnKeyEvent );
263   }
264
265 private:
266
267   // Methods to setup each component of the 3D scene:
268
269   /**
270    * @brief Creates the Main cube object.
271    * This creates the renderer from existing geometry (as the cubes geometry is shared).
272    * The texture is set and all relevant renderer properties are set-up.
273    * @param[in] geometry Pre-calculated cube geometry
274    * @param[in] size The desired cube size
275    * @param[in] textureSet A pre-existing TextureSet with a texture set up, to be applied to the cube
276    * @return An actor set-up containing the main cube object
277    */
278   Actor CreateMainCubeObject( Geometry& geometry, Vector3 size, TextureSet& textureSet )
279   {
280     Toolkit::Control container = Toolkit::Control::New();
281     container.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
282     container.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER );
283     container.SetProperty( Actor::Property::SIZE, Vector2( size ) );
284     container.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
285
286     // Create a renderer from the geometry and add the texture.
287     Renderer renderer = CreateRenderer( geometry, size, true, CUBE_COLOR );
288     renderer.SetTextures( textureSet );
289
290     // Setup the renderer properties:
291     // We are writing to the color buffer & culling back faces (no stencil is used for the main cube).
292     renderer.SetProperty( Renderer::Property::RENDER_MODE, RenderMode::COLOR );
293     renderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
294
295     // We do need to write to the depth buffer as other objects need to appear underneath this cube.
296     renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
297     // We do not need to test the depth buffer as we are culling the back faces.
298     renderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::OFF );
299
300     // This object must be rendered 1st.
301     renderer.SetProperty( Renderer::Property::DEPTH_INDEX, 0 * DEPTH_INDEX_GRANULARITY );
302
303     container.AddRenderer( renderer );
304     return container;
305   }
306
307   /**
308    * @brief Creates the Floor object.
309    * This creates the renderer from existing geometry (as the cube geometry can be re-used).
310    * The texture is created and set and all relevant renderer properties are set-up.
311    * @param[in] geometry Pre-calculated cube geometry
312    * @param[in] size The desired floor size
313    * @return An actor set-up containing the floor object
314    */
315   Actor CreateFloorObject( Geometry& geometry, Vector3 size )
316   {
317     Toolkit::Control container = Toolkit::Control::New();
318     container.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
319     container.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
320     container.SetProperty( Actor::Property::SIZE, Vector2( size ) );
321     container.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
322
323     // Create a renderer from the geometry and add the texture.
324     TextureSet planeTextureSet = CreateTextureSet( FLOOR_TEXTURE );
325     Renderer renderer = CreateRenderer( geometry, size, true, FLOOR_COLOR );
326     renderer.SetTextures( planeTextureSet );
327
328     // Setup the renderer properties:
329     // We are writing to the color buffer & culling back faces as we are NOT doing depth write (no stencil is used for the floor).
330     renderer.SetProperty( Renderer::Property::RENDER_MODE, RenderMode::COLOR );
331     renderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
332
333     // We do not write to the depth buffer as its not needed.
334     renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::OFF );
335     // We do need to test the depth buffer as we need the floor to be underneath the cube.
336     renderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON );
337
338     // This object must be rendered 2nd.
339     renderer.SetProperty( Renderer::Property::DEPTH_INDEX, 1 * DEPTH_INDEX_GRANULARITY );
340
341     container.AddRenderer( renderer );
342     return container;
343   }
344
345   /**
346    * @brief Creates the Stencil-Plane object.
347    * This is places on the floor object to allow the reflection to be drawn on to the floor.
348    * This creates the geometry and renderer.
349    * All relevant renderer properties are set-up.
350    * @param[in] size The desired plane size
351    * @return An actor set-up containing the stencil-plane object
352    */
353   Actor CreateStencilPlaneObject( Vector3 size )
354   {
355     Toolkit::Control container = Toolkit::Control::New();
356     container.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
357     container.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
358     container.SetProperty( Actor::Property::SIZE, Vector2( size ) );
359     container.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
360
361     // We rotate the plane as the geometry is created flat in X & Y. We want it to span X & Z axis.
362     container.SetProperty( Actor::Property::ORIENTATION, Quaternion( Degree( -90.0f ), Degree( 0.0f ), Degree( 0.0f ) ) );
363
364     // Create geometry for a flat plane.
365     Geometry planeGeometry = CreatePlaneVertices( Vector2::ONE );
366     // Create a renderer from the geometry.
367     Renderer renderer = CreateRenderer( planeGeometry, size, false, Vector4::ONE );
368
369     // Setup the renderer properties:
370     // The stencil plane is only for stencilling.
371     renderer.SetProperty( Renderer::Property::RENDER_MODE, RenderMode::STENCIL );
372
373     renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION, StencilFunction::ALWAYS );
374     renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION_REFERENCE, 1 );
375     renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION_MASK, 0xFF );
376     renderer.SetProperty( Renderer::Property::STENCIL_OPERATION_ON_FAIL, StencilOperation::KEEP );
377     renderer.SetProperty( Renderer::Property::STENCIL_OPERATION_ON_Z_FAIL, StencilOperation::KEEP );
378     renderer.SetProperty( Renderer::Property::STENCIL_OPERATION_ON_Z_PASS, StencilOperation::REPLACE );
379     renderer.SetProperty( Renderer::Property::STENCIL_MASK, 0xFF );
380
381     // We don't want to write to the depth buffer, as this would block the reflection being drawn.
382     renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::OFF );
383     // We test the depth buffer as we want the stencil to only exist underneath the cube.
384     renderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON );
385
386     // This object must be rendered 3rd.
387     renderer.SetProperty( Renderer::Property::DEPTH_INDEX, 2 * DEPTH_INDEX_GRANULARITY );
388
389     container.AddRenderer( renderer );
390     return container;
391   }
392
393   /**
394    * @brief Creates the Reflection cube object.
395    * This creates new geometry (as the texture UVs are different to the main cube).
396    * The renderer is then created.
397    * The texture is set and all relevant renderer properties are set-up.
398    * @param[in] size The desired cube size
399    * @param[in] textureSet A pre-existing TextureSet with a texture set up, to be applied to the cube
400    * @return An actor set-up containing the reflection cube object
401    */
402   Actor CreateReflectionCubeObject( Vector3 size, TextureSet& textureSet )
403   {
404     Toolkit::Control container = Toolkit::Control::New();
405     container.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
406     container.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
407     container.SetProperty( Actor::Property::SIZE, Vector2( size ) );
408     container.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
409
410     // Create the cube geometry of unity size.
411     // The "true" specifies we want the texture UVs flipped vertically as this is the reflection cube.
412     Geometry reflectedCubeGeometry = CreateCubeVertices( Vector3::ONE, true );
413     // Create a renderer from the geometry and add the texture.
414     Renderer renderer = CreateRenderer( reflectedCubeGeometry, size, true, REFLECTION_COLOR );
415     renderer.SetTextures( textureSet );
416
417     // Setup the renderer properties:
418     // Write to color buffer so reflection is visible.
419     // Also enable the stencil buffer, as we will be testing against it to only draw to areas within the stencil.
420     renderer.SetProperty( Renderer::Property::RENDER_MODE, RenderMode::COLOR_STENCIL );
421     // We cull to skip drawing the back faces.
422     renderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
423
424     // We use blending to blend the reflection with the floor texture.
425     renderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
426     renderer.SetProperty( Renderer::Property::BLEND_EQUATION_RGB, BlendEquation::ADD );
427     renderer.SetProperty( Renderer::Property::BLEND_EQUATION_ALPHA, BlendEquation::ADD );
428     renderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_RGB, BlendFactor::ONE );
429
430     // Enable stencil. Here we only draw to areas within the stencil.
431     renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION, StencilFunction::EQUAL );
432     renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION_REFERENCE, 1 );
433     renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION_MASK, 0xff );
434     // Don't write to the stencil.
435     renderer.SetProperty( Renderer::Property::STENCIL_MASK, 0x00 );
436
437     // We don't need to write to the depth buffer, as we are culling.
438     renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::OFF );
439     // We need to test the depth buffer as we need the reflection to be underneath the cube.
440     renderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON );
441
442     // This object must be rendered last.
443     renderer.SetProperty( Renderer::Property::DEPTH_INDEX, 3 * DEPTH_INDEX_GRANULARITY );
444
445     container.AddRenderer( renderer );
446     return container;
447   }
448
449   // Methods:
450
451   /**
452    * @brief Creates a geometry object from vertices and indices.
453    * @param[in] vertices The object vertices
454    * @param[in] indices The object indices
455    * @return A geometry object
456    */
457   Geometry CreateTexturedGeometry( Vector<TexturedVertex>& vertices, Vector<unsigned short>& indices )
458   {
459     // Vertices
460     Property::Map vertexFormat;
461     vertexFormat[POSITION] = Property::VECTOR3;
462     vertexFormat[NORMAL] =   Property::VECTOR3;
463     vertexFormat[TEXTURE] =  Property::VECTOR2;
464
465     PropertyBuffer surfaceVertices = PropertyBuffer::New( vertexFormat );
466     surfaceVertices.SetData( &vertices[0u], vertices.Size() );
467
468     Geometry geometry = Geometry::New();
469     geometry.AddVertexBuffer( surfaceVertices );
470
471     // Indices for triangle formulation
472     geometry.SetIndexBuffer( &indices[0u], indices.Size() );
473     return geometry;
474   }
475
476   /**
477    * @brief Creates a renderer from a geometry object.
478    * @param[in] geometry The geometry to use
479    * @param[in] dimensions The dimensions (will be passed in to the shader)
480    * @param[in] textured Set to true to use the texture versions of the shaders
481    * @param[in] color The base color for the renderer
482    * @return A renderer object
483    */
484   Renderer CreateRenderer( Geometry geometry, Vector3 dimensions, bool textured, Vector4 color )
485   {
486     Stage stage = Stage::GetCurrent();
487     Shader shader;
488
489     if( textured )
490     {
491       shader = Shader::New( VERTEX_SHADER_TEXTURED, FRAGMENT_SHADER_TEXTURED );
492     }
493     else
494     {
495       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
496     }
497
498     // Here we modify the light position based on half the stage size as a pre-calculation step.
499     // This avoids the work having to be done in the shader.
500     shader.RegisterProperty( LIGHT_POSITION_UNIFORM_NAME, Vector3( -stage.GetSize().width / 2.0f, -stage.GetSize().width / 2.0f, 1000.0f ) );
501     shader.RegisterProperty( COLOR_UNIFORM_NAME, color );
502     shader.RegisterProperty( OBJECT_DIMENSIONS_UNIFORM_NAME, dimensions );
503
504     return Renderer::New( geometry, shader );
505   }
506
507   /**
508    * @brief Helper method to create a TextureSet from an image URL.
509    * @param[in] url An image URL
510    * @return A TextureSet object
511    */
512   TextureSet CreateTextureSet( const char* url )
513   {
514     TextureSet textureSet = TextureSet::New();
515
516     if( textureSet )
517     {
518       Texture texture = DemoHelper::LoadTexture( url );
519       if( texture )
520       {
521         textureSet.SetTexture( 0u, texture );
522       }
523     }
524
525     return textureSet;
526   }
527
528   // Geometry Creation:
529
530   /**
531    * @brief Creates a geometry object for a flat plane.
532    * The plane is oriented in X & Y axis (Z is 0).
533    * @param[in] dimensions The desired plane dimensions
534    * @return A Geometry object
535    */
536   Geometry CreatePlaneVertices( Vector2 dimensions )
537   {
538     Vector<TexturedVertex> vertices;
539     Vector<unsigned short> indices;
540     vertices.Resize( 4u );
541     indices.Resize( 6u );
542
543     float scaledX = 0.5f * dimensions.x;
544     float scaledY = 0.5f * dimensions.y;
545
546     vertices[0].position     = Vector3( -scaledX, -scaledY, 0.0f );
547     vertices[0].textureCoord = Vector2( 0.0, 0.0f );
548     vertices[1].position     = Vector3(  scaledX, -scaledY, 0.0f );
549     vertices[1].textureCoord = Vector2( 1.0, 0.0f );
550     vertices[2].position     = Vector3(  scaledX,  scaledY, 0.0f );
551     vertices[2].textureCoord = Vector2( 1.0, 1.0f );
552     vertices[3].position     = Vector3( -scaledX,  scaledY, 0.0f );
553     vertices[3].textureCoord = Vector2( 0.0, 1.0f );
554
555     // All vertices have the same normal.
556     for( int i = 0; i < 4; ++i )
557     {
558       vertices[i].normal = Vector3( 0.0f, 0.0f, -1.0f );
559     }
560
561     indices[0] = 0;
562     indices[1] = 1;
563     indices[2] = 2;
564     indices[3] = 2;
565     indices[4] = 3;
566     indices[5] = 0;
567
568     // Use the helper method to create the geometry object.
569     return CreateTexturedGeometry( vertices, indices );
570   }
571
572   /**
573    * @brief Creates a geometry object for a cube (or cuboid).
574    * @param[in] dimensions The desired cube dimensions
575    * @param[in] reflectVerticalUVs Set to True to force the UVs to be vertically flipped
576    * @return A Geometry object
577    */
578   Geometry CreateCubeVertices( Vector3 dimensions, bool reflectVerticalUVs )
579   {
580     Vector<TexturedVertex> vertices;
581     Vector<unsigned short> indices;
582     int vertexIndex = 0u; // Tracks progress through vertices.
583     float scaledX = 0.5f * dimensions.x;
584     float scaledY = 0.5f * dimensions.y;
585     float scaledZ = 0.5f * dimensions.z;
586     float verticalTextureCoord = reflectVerticalUVs ? 0.0f : 1.0f;
587
588     vertices.Resize( 4u * 6u ); // 4 vertices x 6 faces
589
590     Vector<Vector3> positions;  // Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
591     positions.Resize( 8u );
592     Vector<Vector3> normals;    // Stores normals, which are shared between vertexes of the same face.
593     normals.Resize( 6u );
594
595     positions[0] = Vector3( -scaledX,  scaledY, -scaledZ );
596     positions[1] = Vector3(  scaledX,  scaledY, -scaledZ );
597     positions[2] = Vector3(  scaledX,  scaledY,  scaledZ );
598     positions[3] = Vector3( -scaledX,  scaledY,  scaledZ );
599     positions[4] = Vector3( -scaledX, -scaledY, -scaledZ );
600     positions[5] = Vector3(  scaledX, -scaledY, -scaledZ );
601     positions[6] = Vector3(  scaledX, -scaledY,  scaledZ );
602     positions[7] = Vector3( -scaledX, -scaledY,  scaledZ );
603
604     normals[0] = Vector3(  0,  1,  0 );
605     normals[1] = Vector3(  0,  0, -1 );
606     normals[2] = Vector3(  1,  0,  0 );
607     normals[3] = Vector3(  0,  0,  1 );
608     normals[4] = Vector3( -1,  0,  0 );
609     normals[5] = Vector3(  0, -1,  0 );
610
611     // Top face, upward normals.
612     for( int i = 0; i < 4; ++i, ++vertexIndex )
613     {
614       vertices[vertexIndex].position = positions[i];
615       vertices[vertexIndex].normal = normals[0];
616       // The below logic forms the correct U/V pairs for a quad when "i" goes from 0 to 3.
617       vertices[vertexIndex].textureCoord = Vector2( ( i == 1 || i == 2 ) ? 1.0f : 0.0f, ( i == 2 || i == 3 ) ? 1.0f : 0.0f );
618     }
619
620     // Top face, outward normals.
621     for( int i = 0; i < 4; ++i, vertexIndex += 2 )
622     {
623       vertices[vertexIndex].position = positions[i];
624       vertices[vertexIndex].normal = normals[i + 1];
625
626       if( i == 3 )
627       {
628         // End, so loop around.
629         vertices[vertexIndex + 1].position = positions[0];
630       }
631       else
632       {
633         vertices[vertexIndex + 1].position = positions[i + 1];
634       }
635       vertices[vertexIndex + 1].normal = normals[i + 1];
636
637       vertices[vertexIndex].textureCoord = Vector2( 0.0f, verticalTextureCoord );
638       vertices[vertexIndex+1].textureCoord = Vector2( 1.0f, verticalTextureCoord );
639     }
640
641     // Flip the vertical texture coord for the UV values of the bottom points.
642     verticalTextureCoord = 1.0f - verticalTextureCoord;
643
644     // Bottom face, outward normals.
645     for( int i = 0; i < 4; ++i, vertexIndex += 2 )
646     {
647       vertices[vertexIndex].position = positions[i + 4];
648       vertices[vertexIndex].normal = normals[i + 1];
649
650       if( i == 3 )
651       {
652         // End, so loop around.
653         vertices[vertexIndex + 1].position = positions[4];
654       }
655       else
656       {
657         vertices[vertexIndex + 1].position = positions[i + 5];
658       }
659       vertices[vertexIndex + 1].normal = normals[i + 1];
660
661       vertices[vertexIndex].textureCoord = Vector2( 0.0f, verticalTextureCoord );
662       vertices[vertexIndex+1].textureCoord = Vector2( 1.0f, verticalTextureCoord );
663     }
664
665     // Bottom face, downward normals.
666     for( int i = 0; i < 4; ++i, ++vertexIndex )
667     {
668       // Reverse positions for bottom face to keep triangles clockwise (for culling).
669       vertices[vertexIndex].position = positions[ 7 - i ];
670       vertices[vertexIndex].normal = normals[5];
671       // The below logic forms the correct U/V pairs for a quad when "i" goes from 0 to 3.
672       vertices[vertexIndex].textureCoord = Vector2( ( i == 1 || i == 2 ) ? 1.0f : 0.0f, ( i == 2 || i == 3 ) ? 1.0f : 0.0f );
673     }
674
675     // Create cube indices.
676     int triangleIndex = 0u;     //Track progress through indices.
677     indices.Resize( 3u * 12u ); // 3 points x 12 triangles.
678
679     // Top face.
680     indices[triangleIndex] =     0;
681     indices[triangleIndex + 1] = 1;
682     indices[triangleIndex + 2] = 2;
683     indices[triangleIndex + 3] = 2;
684     indices[triangleIndex + 4] = 3;
685     indices[triangleIndex + 5] = 0;
686     triangleIndex += 6;
687
688     int topFaceStart = 4u;
689     int bottomFaceStart = topFaceStart + 8u;
690
691     // Side faces.
692     for( int i = 0; i < 8; i += 2, triangleIndex += 6 )
693     {
694       indices[triangleIndex    ] = i + topFaceStart;
695       indices[triangleIndex + 1] = i + bottomFaceStart + 1;
696       indices[triangleIndex + 2] = i + topFaceStart + 1;
697       indices[triangleIndex + 3] = i + topFaceStart;
698       indices[triangleIndex + 4] = i + bottomFaceStart;
699       indices[triangleIndex + 5] = i + bottomFaceStart + 1;
700     }
701
702     // Bottom face.
703     indices[triangleIndex] =     20;
704     indices[triangleIndex + 1] = 21;
705     indices[triangleIndex + 2] = 22;
706     indices[triangleIndex + 3] = 22;
707     indices[triangleIndex + 4] = 23;
708     indices[triangleIndex + 5] = 20;
709
710     // Use the helper method to create the geometry object.
711     return CreateTexturedGeometry( vertices, indices );
712   }
713
714   // Signal handlers:
715
716   /**
717    * @brief OnTouch signal handler.
718    * @param[in] actor The actor that has been touched
719    * @param[in] touch The touch information
720    * @return True if the event has been handled
721    */
722   bool OnTouch( Actor actor, const TouchData& touch )
723   {
724     // Quit the application.
725     mApplication.Quit();
726     return true;
727   }
728
729   /**
730    * @brief OnKeyEvent signal handler.
731    * @param[in] event The key event information
732    */
733   void OnKeyEvent( const KeyEvent& event )
734   {
735     if( event.state == KeyEvent::Down )
736     {
737       if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
738       {
739         mApplication.Quit();
740       }
741     }
742   }
743
744 private:
745
746   // Member variables:
747
748   Application&     mApplication;       ///< The DALi application object
749   Toolkit::Control mView;              ///< The view used to show the background
750
751   Animation        mRotationAnimation; ///< The animation to spin the cube & floor
752   Animation        mBounceAnimation;   ///< The animation to bounce the cube
753   Actor            mCubes[2];          ///< The cube object containers
754 };
755
756 int DALI_EXPORT_API main( int argc, char **argv )
757 {
758   Application application = Application::New( &argc, &argv );
759   RendererStencilExample example( application );
760   application.MainLoop();
761   return 0;
762 }