Automatically dis/connect registered visuals to stage
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / primitive / primitive-visual.cpp
1 /*
2  * Copyright (c) 2016 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 // CLASS HEADER
19 #include "primitive-visual.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/common/stage.h>
24 #include <dali/public-api/common/constants.h>
25 #include <dali/devel-api/scripting/enum-helper.h>
26 #include <dali/devel-api/scripting/scripting.h>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Internal
38 {
39
40 namespace
41 {
42
43 // shapes
44 DALI_ENUM_TO_STRING_TABLE_BEGIN( SHAPE_TYPE )
45 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, SPHERE )
46 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CONICAL_FRUSTRUM )
47 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CONE )
48 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CYLINDER )
49 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CUBE )
50 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, OCTAHEDRON )
51 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, BEVELLED_CUBE )
52 DALI_ENUM_TO_STRING_TABLE_END( SHAPE_TYPE )
53
54 //Property names
55 const char * const PRIMITIVE_SHAPE( "shape" );
56 const char * const SHAPE_COLOR( "mixColor" );
57 const char * const SLICES( "slices" );
58 const char * const STACKS( "stacks" );
59 const char * const SCALE_TOP_RADIUS( "scaleTopRadius" );
60 const char * const SCALE_BOTTOM_RADIUS( "scaleBottomRadius" );
61 const char * const SCALE_HEIGHT( "scaleHeight" );
62 const char * const SCALE_RADIUS( "scaleRadius" );
63 const char * const SCALE_DIMENSIONS( "scaleDimensions" );
64 const char * const BEVEL_PERCENTAGE( "bevelPercentage" );
65 const char * const BEVEL_SMOOTHNESS( "bevelSmoothness" );
66 const char * const LIGHT_POSITION_UNIFORM_NAME( "lightPosition" );
67
68 //Primitive property defaults
69 const int     DEFAULT_SLICES =              128; ///< For spheres and conics
70 const int     DEFAULT_STACKS =              128; ///< For spheres and conics
71 const float   DEFAULT_SCALE_TOP_RADIUS =    1.0; ///< For conical frustrums
72 const float   DEFAULT_SCALE_BOTTOM_RADIUS = 1.5; ///< For cones and conical frustrums
73 const float   DEFAULT_SCALE_HEIGHT =        3.0; ///< For all conics
74 const float   DEFAULT_SCALE_RADIUS =        1.0; ///< For cylinders
75 const float   DEFAULT_BEVEL_PERCENTAGE =    0.0; ///< For bevelled cubes
76 const float   DEFAULT_BEVEL_SMOOTHNESS =    0.0; ///< For bevelled cubes
77 const Vector4 DEFAULT_COLOR =               Vector4( 0.5, 0.5, 0.5, 1.0 ); ///< Grey, for all.
78
79 //Property limits
80 const int   MIN_SLICES =           3;   ///< Minimum number of slices for spheres and conics
81 const int   MIN_STACKS =           2;   ///< Minimum number of stacks for spheres and conics
82 const int   MAX_PARTITIONS =       255; ///< Maximum number of slices or stacks for spheres and conics
83 const float MIN_BEVEL_PERCENTAGE = 0.0; ///< Minimum bevel percentage for bevelled cubes
84 const float MAX_BEVEL_PERCENTAGE = 1.0; ///< Maximum bevel percentage for bevelled cubes
85 const float MIN_SMOOTHNESS =       0.0; ///< Minimum bevel smoothness for bevelled cubes
86 const float MAX_SMOOTHNESS =       1.0; ///< Maximum bevel smoothness for bevelled cubes
87
88 //Specific shape labels.
89 const char * const SPHERE_LABEL( "SPHERE" );
90 const char * const CONE_LABEL( "CONE" );
91 const char * const CONICAL_FRUSTRUM_LABEL( "CONICAL_FRUSTRUM" );
92 const char * const CYLINDER_LABEL( "CYLINDER" );
93 const char * const CUBE_LABEL( "CUBE" );
94 const char * const OCTAHEDRON_LABEL( "OCTAHEDRON" );
95 const char * const BEVELLED_CUBE_LABEL( "BEVELLED_CUBE" );
96
97 //Shader properties
98 const char * const OBJECT_MATRIX_UNIFORM_NAME( "uObjectMatrix" );
99 const char * const COLOR_UNIFORM_NAME( "mixColor" );
100 const char * const OBJECT_DIMENSIONS_UNIFORM_NAME( "uObjectDimensions" );
101 const char * const STAGE_OFFSET_UNIFORM_NAME( "uStageOffset" );
102
103 //Vertex properties
104 const char * const POSITION( "aPosition");
105 const char * const NORMAL( "aNormal" );
106 const char * const INDICES( "aIndices" );
107
108 //A simple shader that applies diffuse lighting to a mono-coloured object.
109 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
110   attribute highp   vec3 aPosition;\n
111   attribute highp   vec2 aTexCoord;\n
112   attribute highp   vec3 aNormal;\n
113   varying   mediump vec3 vIllumination;\n
114   uniform   mediump vec3 uSize;\n
115   uniform   mediump vec3 uObjectDimensions;\n
116   uniform   mediump mat4 uMvpMatrix;\n
117   uniform   mediump mat4 uModelView;\n
118   uniform   mediump mat4 uViewMatrix;\n
119   uniform   mediump mat3 uNormalMatrix;\n
120   uniform   mediump mat4 uObjectMatrix;\n
121   uniform   mediump vec3 lightPosition;\n
122   uniform   mediump vec2 uStageOffset;\n
123
124   void main()\n
125   {\n
126     float xRatio = uSize.x / uObjectDimensions.x;\n
127     float yRatio = uSize.y / uObjectDimensions.y;\n
128     float scaleFactor = min( xRatio, yRatio );\n
129
130     vec4 normalisedVertexPosition = vec4( aPosition * scaleFactor, 1.0 );\n
131     vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
132     vertexPosition = uMvpMatrix * vertexPosition;\n
133
134     //Illumination in Model-View space - Transform attributes and uniforms\n
135     vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n
136     vec3 normal = uNormalMatrix * mat3( uObjectMatrix ) * aNormal;\n
137
138     vec4 mvLightPosition = vec4( ( lightPosition.xy - uStageOffset ), lightPosition.z, 1.0 );\n
139     mvLightPosition = uViewMatrix * mvLightPosition;\n
140     vec3 vectorToLight = normalize( mvLightPosition.xyz - mvVertexPosition.xyz );\n
141
142     float lightDiffuse = max( dot( vectorToLight, normal ), 0.0 );\n
143     vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
144
145     gl_Position = vertexPosition;\n
146   }\n
147 );
148
149 //Very simple fragment shader that merely applies the vertex shading to the color at each fragment.
150 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
151   precision mediump float;\n
152   varying   mediump vec3  vIllumination;\n
153   uniform   lowp    vec4  uColor;\n
154   uniform   lowp    vec4  mixColor;\n
155
156   void main()\n
157   {\n
158     vec4 baseColor = mixColor * uColor;\n
159     gl_FragColor = vec4( vIllumination.rgb * baseColor.rgb, baseColor.a );\n
160   }\n
161 );
162
163 } // namespace
164
165 PrimitiveVisual::PrimitiveVisual( VisualFactoryCache& factoryCache )
166 : Visual::Base( factoryCache ),
167   mColor( DEFAULT_COLOR ),
168   mScaleDimensions( Vector3::ONE ),
169   mScaleTopRadius( DEFAULT_SCALE_TOP_RADIUS ),
170   mScaleBottomRadius( DEFAULT_SCALE_BOTTOM_RADIUS ),
171   mScaleHeight( DEFAULT_SCALE_HEIGHT ),
172   mScaleRadius( DEFAULT_SCALE_RADIUS ),
173   mBevelPercentage( DEFAULT_BEVEL_PERCENTAGE ),
174   mBevelSmoothness( DEFAULT_BEVEL_SMOOTHNESS ),
175   mSlices( DEFAULT_SLICES ),
176   mStacks( DEFAULT_STACKS ),
177   mPrimitiveType( Toolkit::PrimitiveVisual::Shape::SPHERE )
178 {
179 }
180
181 PrimitiveVisual::~PrimitiveVisual()
182 {
183 }
184
185 void PrimitiveVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap )
186 {
187   //Find out which shape to renderer.
188   Property::Value* primitiveTypeValue = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SHAPE, PRIMITIVE_SHAPE );
189   if( primitiveTypeValue )
190   {
191     Scripting::GetEnumerationProperty( *primitiveTypeValue, SHAPE_TYPE_TABLE, SHAPE_TYPE_TABLE_COUNT, mPrimitiveType );
192   }
193   else
194   {
195     DALI_LOG_ERROR( "Fail to provide shape to the PrimitiveVisual object.\n" );
196   }
197
198   //Read in other potential properties.
199
200   Property::Value* color = propertyMap.Find( Toolkit::PrimitiveVisual::Property::MIX_COLOR, SHAPE_COLOR );
201   if( color && !color->Get( mColor ) )
202   {
203     DALI_LOG_ERROR( "Invalid type for color in PrimitiveVisual.\n" );
204   }
205
206   Property::Value* slices = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SLICES, SLICES );
207   if( slices )
208   {
209     if( slices->Get( mSlices ) )
210     {
211       //Clamp value.
212       if( mSlices > MAX_PARTITIONS )
213       {
214         mSlices = MAX_PARTITIONS;
215         DALI_LOG_WARNING( "Value for slices clamped.\n" );
216       }
217       else if ( mSlices < MIN_SLICES )
218       {
219         mSlices = MIN_SLICES;
220         DALI_LOG_WARNING( "Value for slices clamped.\n" );
221       }
222     }
223     else
224     {
225       DALI_LOG_ERROR( "Invalid type for slices in PrimitiveVisual.\n" );
226     }
227   }
228
229   Property::Value* stacks = propertyMap.Find( Toolkit::PrimitiveVisual::Property::STACKS, STACKS );
230   if( stacks )
231   {
232     if( stacks->Get( mStacks ) )
233     {
234       //Clamp value.
235       if( mStacks > MAX_PARTITIONS )
236       {
237         mStacks = MAX_PARTITIONS;
238         DALI_LOG_WARNING( "Value for stacks clamped.\n" );
239       }
240       else if ( mStacks < MIN_STACKS )
241       {
242         mStacks = MIN_STACKS;
243         DALI_LOG_WARNING( "Value for stacks clamped.\n" );
244       }
245     }
246     else
247     {
248       DALI_LOG_ERROR( "Invalid type for stacks in PrimitiveVisual.\n" );
249     }
250   }
251
252   Property::Value* scaleTop = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_TOP_RADIUS, SCALE_TOP_RADIUS );
253   if( scaleTop && !scaleTop->Get( mScaleTopRadius ) )
254   {
255     DALI_LOG_ERROR( "Invalid type for scale top radius in PrimitiveVisual.\n" );
256   }
257
258   Property::Value* scaleBottom = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_BOTTOM_RADIUS, SCALE_BOTTOM_RADIUS );
259   if( scaleBottom && !scaleBottom->Get( mScaleBottomRadius ) )
260   {
261     DALI_LOG_ERROR( "Invalid type for scale bottom radius in PrimitiveVisual.\n" );
262   }
263
264   Property::Value* scaleHeight = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_HEIGHT, SCALE_HEIGHT );
265   if( scaleHeight && !scaleHeight->Get( mScaleHeight ) )
266   {
267     DALI_LOG_ERROR( "Invalid type for scale height in PrimitiveVisual.\n" );
268   }
269
270   Property::Value* scaleRadius = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_RADIUS, SCALE_RADIUS );
271   if( scaleRadius && !scaleRadius->Get( mScaleRadius ) )
272   {
273     DALI_LOG_ERROR( "Invalid type for scale radius in PrimitiveVisual.\n" );
274   }
275
276   Property::Value* dimensions = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_DIMENSIONS, SCALE_DIMENSIONS );
277   if( dimensions )
278   {
279     if( dimensions->Get( mScaleDimensions ) )
280     {
281       //If any dimension is invalid, set it to a sensible default.
282       if( mScaleDimensions.x <= 0.0 )
283       {
284         mScaleDimensions.x = 1.0;
285         DALI_LOG_WARNING( "Value for scale dimensions clamped. Must be greater than zero.\n" );
286       }
287       if( mScaleDimensions.y <= 0.0 )
288       {
289         mScaleDimensions.y = 1.0;
290         DALI_LOG_WARNING( "Value for scale dimensions clamped. Must be greater than zero.\n" );
291       }
292       if( mScaleDimensions.z <= 0.0 )
293       {
294         mScaleDimensions.z = 1.0;
295         DALI_LOG_WARNING( "Value for scale dimensions clamped. Must be greater than zero.\n" );
296       }
297     }
298     else
299     {
300       DALI_LOG_ERROR( "Invalid type for scale dimensions in PrimitiveVisual.\n" );
301     }
302   }
303
304   Property::Value* bevel = propertyMap.Find( Toolkit::PrimitiveVisual::Property::BEVEL_PERCENTAGE, BEVEL_PERCENTAGE );
305   if( bevel )
306   {
307     if( bevel->Get( mBevelPercentage ) )
308     {
309       //Clamp value.
310       if( mBevelPercentage < MIN_BEVEL_PERCENTAGE )
311       {
312         mBevelPercentage = MIN_BEVEL_PERCENTAGE;
313         DALI_LOG_WARNING( "Value for bevel percentage clamped.\n" );
314       }
315       else if( mBevelPercentage > MAX_BEVEL_PERCENTAGE )
316       {
317         mBevelPercentage = MAX_BEVEL_PERCENTAGE;
318         DALI_LOG_WARNING( "Value for bevel percentage clamped.\n" );
319       }
320     }
321     else
322     {
323       DALI_LOG_ERROR( "Invalid type for bevel percentage in PrimitiveVisual.\n" );
324     }
325   }
326
327   Property::Value* smoothness = propertyMap.Find( Toolkit::PrimitiveVisual::Property::BEVEL_SMOOTHNESS, BEVEL_SMOOTHNESS );
328   if( smoothness )
329   {
330     if( smoothness->Get( mBevelSmoothness ) )
331     {
332       //Clamp value.
333       if( mBevelSmoothness < MIN_SMOOTHNESS )
334       {
335         mBevelSmoothness = MIN_SMOOTHNESS;
336         DALI_LOG_WARNING( "Value for bevel smoothness clamped.\n" );
337       }
338       else if( mBevelSmoothness > MAX_SMOOTHNESS )
339       {
340         mBevelSmoothness = MAX_SMOOTHNESS;
341         DALI_LOG_WARNING( "Value for bevel smoothness clamped.\n" );
342       }
343     }
344     else
345     {
346       DALI_LOG_ERROR( "Invalid type for bevel smoothness in PrimitiveVisual.\n" );
347     }
348   }
349
350   //Read in light position.
351   Property::Value* lightPosition = propertyMap.Find( Toolkit::PrimitiveVisual::Property::LIGHT_POSITION, LIGHT_POSITION_UNIFORM_NAME );
352   if( lightPosition )
353   {
354     if( !lightPosition->Get( mLightPosition ) )
355     {
356       DALI_LOG_ERROR( "Invalid value passed for light position in MeshVisual object.\n" );
357       mLightPosition = Vector3::ZERO;
358     }
359   }
360   else
361   {
362     //Default behaviour is to place the light directly in front of the object,
363     // at a reasonable distance to light everything on screen.
364     Stage stage = Stage::GetCurrent();
365
366     mLightPosition = Vector3( stage.GetSize().width / 2, stage.GetSize().height / 2, stage.GetSize().width * 5 );
367   }
368 }
369
370 void PrimitiveVisual::SetSize( const Vector2& size )
371 {
372   Visual::Base::SetSize( size );
373
374   // ToDo: renderer responds to the size change
375 }
376
377 void PrimitiveVisual::GetNaturalSize( Vector2& naturalSize ) const
378 {
379   naturalSize.x = mObjectDimensions.x;
380   naturalSize.y = mObjectDimensions.y;
381 }
382
383 void PrimitiveVisual::DoSetOnStage( Actor& actor )
384 {
385   InitializeRenderer();
386
387   actor.AddRenderer( mImpl->mRenderer );
388 }
389
390 void PrimitiveVisual::DoCreatePropertyMap( Property::Map& map ) const
391 {
392   map.Clear();
393   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::PRIMITIVE );
394   map.Insert( Toolkit::PrimitiveVisual::Property::SHAPE, mPrimitiveType );
395   map.Insert( Toolkit::PrimitiveVisual::Property::MIX_COLOR, mColor );
396   map.Insert( Toolkit::PrimitiveVisual::Property::SLICES, mSlices );
397   map.Insert( Toolkit::PrimitiveVisual::Property::STACKS, mStacks );
398   map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_TOP_RADIUS, mScaleTopRadius );
399   map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_BOTTOM_RADIUS, mScaleBottomRadius );
400   map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_HEIGHT, mScaleHeight );
401   map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_RADIUS, mScaleRadius );
402   map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_DIMENSIONS, mScaleDimensions );
403   map.Insert( Toolkit::PrimitiveVisual::Property::BEVEL_PERCENTAGE, mBevelPercentage );
404   map.Insert( Toolkit::PrimitiveVisual::Property::BEVEL_SMOOTHNESS, mBevelSmoothness );
405   map.Insert( Toolkit::PrimitiveVisual::Property::LIGHT_POSITION, mLightPosition );
406 }
407
408 void PrimitiveVisual::InitializeRenderer()
409 {
410   if( !mGeometry )
411   {
412     CreateGeometry();
413   }
414
415   if( !mShader )
416   {
417     CreateShader();
418   }
419
420   mImpl->mRenderer = Renderer::New( mGeometry, mShader );
421   mImpl->mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
422 }
423
424 void PrimitiveVisual::UpdateShaderUniforms()
425 {
426   Stage stage = Stage::GetCurrent();
427   float width = stage.GetSize().width;
428   float height = stage.GetSize().height;
429
430   //Flip model to account for DALi starting with (0, 0) at the top left.
431   Matrix scaleMatrix;
432   scaleMatrix.SetIdentityAndScale( Vector3( 1.0, -1.0, 1.0 ) );
433
434   mShader.RegisterProperty( STAGE_OFFSET_UNIFORM_NAME, Vector2( width, height ) / 2.0f );
435   mShader.RegisterProperty( LIGHT_POSITION_UNIFORM_NAME, mLightPosition );
436   mShader.RegisterProperty( OBJECT_MATRIX_UNIFORM_NAME, scaleMatrix );
437   mShader.RegisterProperty( Toolkit::PrimitiveVisual::Property::MIX_COLOR, COLOR_UNIFORM_NAME, mColor );
438   mShader.RegisterProperty( OBJECT_DIMENSIONS_UNIFORM_NAME, mObjectDimensions );
439 }
440
441 void PrimitiveVisual::CreateShader()
442 {
443   mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
444   UpdateShaderUniforms();
445 }
446
447 void PrimitiveVisual::CreateGeometry()
448 {
449   Dali::Vector<Vertex> vertices;
450   Dali::Vector<unsigned short> indices;
451
452   switch( mPrimitiveType )
453   {
454     case Toolkit::PrimitiveVisual::Shape::SPHERE:
455     {
456       CreateSphere( vertices, indices, mSlices, mStacks );
457       break;
458     }
459     case Toolkit::PrimitiveVisual::Shape::CONE:
460     {
461       //Create a conic with zero top radius.
462       CreateConic( vertices, indices, 0, mScaleBottomRadius, mScaleHeight, mSlices );
463       break;
464     }
465     case Toolkit::PrimitiveVisual::Shape::CONICAL_FRUSTRUM:
466     {
467       CreateConic( vertices, indices, mScaleTopRadius, mScaleBottomRadius, mScaleHeight, mSlices );
468       break;
469     }
470     case Toolkit::PrimitiveVisual::Shape::CYLINDER:
471     {
472       //Create a conic with equal radii on the top and bottom.
473       CreateConic( vertices, indices, mScaleRadius, mScaleRadius, mScaleHeight, mSlices );
474       break;
475     }
476     case Toolkit::PrimitiveVisual::Shape::CUBE:
477     {
478       //Create a cube by creating a bevelled cube with minimum bevel.
479       CreateBevelledCube( vertices, indices, mScaleDimensions, 0.0, 0.0 );
480       break;
481     }
482     case Toolkit::PrimitiveVisual::Shape::OCTAHEDRON:
483     {
484       //Create an octahedron by creating a bevelled cube with maximum bevel.
485       CreateBevelledCube( vertices, indices, mScaleDimensions, 1.0, mBevelSmoothness );
486       break;
487     }
488     case Toolkit::PrimitiveVisual::Shape::BEVELLED_CUBE:
489     {
490       CreateBevelledCube( vertices, indices, mScaleDimensions, mBevelPercentage, mBevelSmoothness );
491       break;
492     }
493   }
494
495   mGeometry = Geometry::New();
496
497   //Vertices
498   Property::Map vertexFormat;
499   vertexFormat[POSITION] = Property::VECTOR3;
500   vertexFormat[NORMAL] = Property::VECTOR3;
501   PropertyBuffer surfaceVertices = PropertyBuffer::New( vertexFormat );
502   surfaceVertices.SetData( &vertices[0], vertices.Size() );
503
504   mGeometry.AddVertexBuffer( surfaceVertices );
505
506   //Indices for triangle formulation
507   mGeometry.SetIndexBuffer( &indices[0], indices.Size() );
508 }
509
510 void PrimitiveVisual::CreateSphere( Vector<Vertex>& vertices, Vector<unsigned short>& indices, int slices, int stacks )
511 {
512   ComputeSphereVertices( vertices, slices, stacks );
513   FormSphereTriangles( indices, slices, stacks );
514
515   mObjectDimensions = Vector3::ONE;
516 }
517
518 void PrimitiveVisual::CreateConic( Vector<Vertex>& vertices, Vector<unsigned short>& indices, float scaleTopRadius,
519                                      float scaleBottomRadius, float scaleHeight, int slices )
520 {
521   ComputeConicVertices( vertices, scaleTopRadius, scaleBottomRadius, scaleHeight, slices );
522   FormConicTriangles( indices, scaleTopRadius, scaleBottomRadius, slices );
523
524   //Determine object dimensions, and scale them to be between 0.0 and 1.0.
525   float xDimension = std::max( scaleTopRadius, scaleBottomRadius ) * 2.0f;
526   float yDimension = scaleHeight;
527   float largestDimension = std::max( xDimension, yDimension );
528
529   mObjectDimensions = Vector3( xDimension / largestDimension, yDimension / largestDimension,
530                                xDimension / largestDimension );
531 }
532
533 void PrimitiveVisual::CreateBevelledCube( Vector<Vertex>& vertices, Vector<unsigned short>& indices,
534                                             Vector3 dimensions, float bevelPercentage, float bevelSmoothness )
535 {
536   dimensions.Normalize();
537
538   if( bevelPercentage <= MIN_BEVEL_PERCENTAGE ) //No bevel, form a cube.
539   {
540     ComputeCubeVertices( vertices, dimensions );
541     FormCubeTriangles( indices );
542   }
543   else if( bevelPercentage >= MAX_BEVEL_PERCENTAGE ) //Max bevel, form an octahedron.
544   {
545     ComputeOctahedronVertices( vertices, dimensions, bevelSmoothness );
546     FormOctahedronTriangles( indices );
547   }
548   else //In between, form a bevelled cube.
549   {
550     ComputeBevelledCubeVertices( vertices, dimensions, bevelPercentage, bevelSmoothness );
551     FormBevelledCubeTriangles( indices );
552   }
553
554   mObjectDimensions = dimensions;
555 }
556
557 void PrimitiveVisual::ComputeCircleTables( Vector<float>& sinTable, Vector<float>& cosTable, int divisions,
558                                              bool halfCircle )
559 {
560   if( divisions < 0 )
561   {
562     return;
563   }
564
565   const float angleDivision = ( halfCircle ? 1.0f : 2.0f ) * Dali::Math::PI / ( float ) divisions;
566
567   sinTable.Resize( divisions );
568   cosTable.Resize( divisions );
569
570   for( int i = 0; i < divisions; i++ )
571   {
572     sinTable[i] = sin( angleDivision * i );
573     cosTable[i] = cos( angleDivision * i );
574   }
575 }
576
577 void PrimitiveVisual::ComputeSphereVertices( Vector<Vertex>& vertices, int slices, int stacks )
578 {
579   //Tables for calculating slices angles and stacks angles, respectively.
580   Vector<float> sinTable1;
581   Vector<float> cosTable1;
582   Vector<float> sinTable2;
583   Vector<float> cosTable2;
584
585   ComputeCircleTables( sinTable1, cosTable1, slices, false );
586   ComputeCircleTables( sinTable2, cosTable2, stacks, true );
587
588   int numVertices = slices * ( stacks - 1 ) + 2;
589   vertices.Resize( numVertices );
590
591   int vertexIndex = 0;  //Track progress through vertices.
592   float x;
593   float y;
594   float z;
595
596   //Top stack.
597   vertices[vertexIndex].position = Vector3( 0.0, 0.5, 0.0 );
598   vertices[vertexIndex].normal =   Vector3( 0.0, 1.0, 0.0 );
599   vertexIndex++;
600
601   //Middle stacks.
602   for( int i = 1; i < stacks; i++ )
603   {
604     for( int j = 0; j < slices; j++, vertexIndex++ )
605     {
606       x = cosTable1[j] * sinTable2[i];
607       y = cosTable2[i];
608       z = sinTable1[j] * sinTable2[i];
609
610       vertices[vertexIndex].position = Vector3( x / 2.0f, y / 2.0f, z / 2.0f );
611       vertices[vertexIndex].normal = Vector3( x, y, z );
612     }
613   }
614
615   //Bottom stack.
616   vertices[vertexIndex].position = Vector3( 0.0, -0.5, 0.0 );
617   vertices[vertexIndex].normal =   Vector3( 0.0, -1.0, 0.0 );
618 }
619
620 void PrimitiveVisual::FormSphereTriangles( Vector<unsigned short>& indices, int slices, int stacks )
621 {
622   if( stacks <= 1 )
623   {
624     //Set indices to placeholder "error" values.
625     //This will display nothing, which is the expected behaviour for this edge case.
626     indices.Resize( 3 );
627     return;
628   }
629
630   int numTriangles = 2 * slices * ( stacks - 1 );
631
632   indices.Resize( 3 * numTriangles );
633
634   int indiceIndex = 0;  //Used to keep track of progress through indices.
635   int previousCycleBeginning = 1;  //Stores the index of the vertex that started the cycle of the previous stack.
636   int currentCycleBeginning = 1 + slices;
637
638   //Top stack. Loop from index 1 to index slices, as not counting the very first vertex.
639   for( int i = 1; i <= slices; i++, indiceIndex += 3 )
640   {
641     indices[indiceIndex] = 0;
642     if( i == slices )
643     {
644       //End, so loop around.
645       indices[indiceIndex + 1] = 1;
646     }
647     else
648     {
649       indices[indiceIndex + 1] = i + 1;
650     }
651     indices[indiceIndex + 2] = i;
652   }
653
654   //Middle Stacks. Want to form triangles between the top and bottom stacks, so loop up to the number of stacks - 2.
655   for( int i = 0; i < stacks - 2; i++, previousCycleBeginning += slices, currentCycleBeginning += slices )
656   {
657     for( int j = 0; j < slices; j++, indiceIndex += 6 )
658     {
659       if( j == slices - 1 )
660       {
661         //End, so loop around.
662         indices[indiceIndex] =     previousCycleBeginning + j;
663         indices[indiceIndex + 1] = previousCycleBeginning;
664         indices[indiceIndex + 2] = currentCycleBeginning + j;
665         indices[indiceIndex + 3] = currentCycleBeginning + j;
666         indices[indiceIndex + 4] = previousCycleBeginning;
667         indices[indiceIndex + 5] = currentCycleBeginning;
668       }
669       else
670       {
671         indices[indiceIndex] =     previousCycleBeginning + j;
672         indices[indiceIndex + 1] = previousCycleBeginning + 1 + j;
673         indices[indiceIndex + 2] = currentCycleBeginning + j;
674         indices[indiceIndex + 3] = currentCycleBeginning + j;
675         indices[indiceIndex + 4] = previousCycleBeginning + 1 + j;
676         indices[indiceIndex + 5] = currentCycleBeginning + 1 + j;
677       }
678     }
679   }
680
681   //Bottom stack. Loop around the last stack from the previous loop, and go up to the penultimate vertex.
682   for( int i = 0; i < slices; i++, indiceIndex += 3 )
683   {
684     indices[indiceIndex] = previousCycleBeginning + slices;
685     indices[indiceIndex + 1] = previousCycleBeginning + i;
686     if( i == slices - 1 )
687     {
688       //End, so loop around.
689       indices[indiceIndex + 2] = previousCycleBeginning;
690     }
691     else
692     {
693       indices[indiceIndex + 2] = previousCycleBeginning + i + 1;
694     }
695   }
696 }
697
698 void PrimitiveVisual::ComputeConicVertices( Vector<Vertex>& vertices, float scaleTopRadius,
699                                                      float scaleBottomRadius, float scaleHeight, int slices )
700 {
701   int vertexIndex = 0;  //Track progress through vertices.
702   Vector<float> sinTable;
703   Vector<float> cosTable;
704
705   ComputeCircleTables( sinTable, cosTable, slices, false );
706
707   int numVertices = 2;  //Always will have one at the top and one at the bottom.
708
709   //Add vertices for each circle. Need two per point for different face normals.
710   if( scaleTopRadius > 0.0 )
711   {
712     numVertices += 2 * slices;
713   }
714   if( scaleBottomRadius > 0.0 )
715   {
716     numVertices += 2 * slices;
717   }
718
719   vertices.Resize( numVertices );
720
721
722   //Scale to bounding region of -0.5 to 0.5 (i.e range of 1).
723   float biggestObjectDimension = std::max( std::max( scaleTopRadius * 2.0f, scaleBottomRadius * 2.0f ), scaleHeight );
724   scaleTopRadius = scaleTopRadius / biggestObjectDimension;
725   scaleBottomRadius = scaleBottomRadius / biggestObjectDimension;
726
727   //Dimensions for vertex coordinates. Y is constant, and so can be initialised now.
728   float x;
729   float y = scaleHeight / biggestObjectDimension / 2.0f;
730   float z;
731
732   //Top center.
733   vertices[0].position = Vector3( 0, y, 0 );
734   vertices[0].normal = Vector3( 0, 1, 0 );
735   vertexIndex++;
736
737   //Top circle.
738   if( scaleTopRadius > 0.0 )
739   {
740     //Loop around the circle.
741     for( int i = 0; i < slices; i++, vertexIndex++ )
742     {
743       x = sinTable[i] * scaleTopRadius;
744       z = cosTable[i] * scaleTopRadius;
745
746       //Upward-facing normal.
747       vertices[vertexIndex].position = Vector3( x, y, z );
748       vertices[vertexIndex].normal = Vector3( 0, 1, 0 );
749
750       //Outward-facing normal.
751       vertices[vertexIndex + slices].position = Vector3( x, y, z );
752       vertices[vertexIndex + slices].normal = Vector3( x, 0, z );
753     }
754
755     vertexIndex += slices;
756   }
757
758   //Bottom circle.
759   if( scaleBottomRadius > 0.0 )
760   {
761     //Loop around the circle.
762     for( int i = 0; i < slices; i++, vertexIndex++ )
763     {
764       x = sinTable[i] * scaleBottomRadius;
765       z = cosTable[i] * scaleBottomRadius;
766
767       //Outward-facing normal.
768       vertices[vertexIndex].position = Vector3( x, -y, z );
769       vertices[vertexIndex].normal = Vector3( x, 0, z );
770
771       //Downward-facing normal.
772       vertices[vertexIndex + slices].position = Vector3( x, -y, z );
773       vertices[vertexIndex + slices].normal = Vector3( 0, -1, 0 );
774     }
775
776     vertexIndex += slices;
777   }
778
779   //Bottom center.
780   vertices[vertexIndex].position = Vector3( 0, -y, 0 );
781   vertices[vertexIndex].normal = Vector3( 0, -1, 0 );
782   vertexIndex++;
783 }
784
785 void PrimitiveVisual::FormConicTriangles( Vector<unsigned short>& indices, float scaleTopRadius,
786                                                    float scaleBottomRadius, int slices )
787 {
788   int  indiceIndex = 0;  //Track progress through indices.
789   int  numTriangles = 0;
790   bool coneTop = scaleTopRadius <= 0.0;
791   bool coneBottom = scaleBottomRadius <= 0.0;
792
793   if( coneTop && coneBottom )
794   {
795     //Set indices to placeholder "error" values.
796     //This will display nothing, which is the expected behaviour for this edge case.
797     indices.Resize( 3 );
798     return;
799   }
800
801   if( !coneTop )
802   {
803     numTriangles += 2 * slices;
804   }
805   if( !coneBottom )
806   {
807     numTriangles += 2 * slices;
808   }
809
810   indices.Resize( 3 * numTriangles );
811
812   //Switch on the type of conic we have.
813   if( !coneTop && !coneBottom )
814   {
815     //Top circle. Start at index of first outer point and go around.
816     for( int i = 1; i <= slices; i++, indiceIndex += 3 )
817     {
818       indices[indiceIndex] = 0;
819       indices[indiceIndex + 1] = i;
820       if( i == slices )
821       {
822         //End, so loop around.
823         indices[indiceIndex + 2] = 1;
824       }
825       else
826       {
827         indices[indiceIndex + 2] = i + 1;
828       }
829     }
830
831     int topCycleBeginning = slices + 1;
832     int bottomCycleBeginning = topCycleBeginning + slices;
833
834     //Vertical edges.
835     for( int i = 0; i < slices; i++, indiceIndex += 6 )
836     {
837       if( i == slices - 1 )
838       {
839         //End, so loop around.
840         indices[indiceIndex] =     topCycleBeginning + i;
841         indices[indiceIndex + 1] = bottomCycleBeginning + i;
842         indices[indiceIndex + 2] = topCycleBeginning;
843         indices[indiceIndex + 3] = bottomCycleBeginning + i;
844         indices[indiceIndex + 4] = bottomCycleBeginning;
845         indices[indiceIndex + 5] = topCycleBeginning;
846       }
847       else
848       {
849         indices[indiceIndex] =     topCycleBeginning + i;
850         indices[indiceIndex + 1] = bottomCycleBeginning + i;
851         indices[indiceIndex + 2] = topCycleBeginning + 1 + i;
852         indices[indiceIndex + 3] = bottomCycleBeginning + i;
853         indices[indiceIndex + 4] = bottomCycleBeginning + 1 + i;
854         indices[indiceIndex + 5] = topCycleBeginning + 1 + i;
855       }
856     }
857
858     int bottomFaceCycleBeginning = bottomCycleBeginning + slices;
859
860     //Bottom circle.
861     for( int i = 0; i < slices; i++, indiceIndex += 3 )
862     {
863       indices[indiceIndex] = bottomFaceCycleBeginning;
864       if( i == slices - 1 )
865       {
866         //End, so loop around.
867         indices[indiceIndex + 1] = bottomFaceCycleBeginning;
868       }
869       else
870       {
871         indices[indiceIndex + 1] = bottomFaceCycleBeginning + i + 1;
872       }
873       indices[indiceIndex + 2] = bottomFaceCycleBeginning + i;
874     }
875   }
876   else if( !coneTop || !coneBottom )
877   {
878     //Top circle/edges. Start at index of first outer point and go around.
879     for( int i = 1; i <= slices; i++, indiceIndex += 3 )
880     {
881       indices[indiceIndex] = 0;
882       indices[indiceIndex + 1] = i;
883       if( i == slices )
884       {
885         //End, so loop around.
886         indices[indiceIndex + 2] = 1;
887       }
888       else
889       {
890         indices[indiceIndex + 2] = i + 1;
891       }
892     }
893
894     //Bottom circle/edges. Start at index of first outer point and go around.
895     for( int i = 1; i <= slices; i++, indiceIndex += 3 )
896     {
897       indices[indiceIndex] = 2 * slices + 1;
898       if( i == slices )
899       {
900         //End, so loop around.
901         indices[indiceIndex + 1] = slices + 1;
902       }
903       else
904       {
905         indices[indiceIndex + 1] = slices + i + 1;
906       }
907       indices[indiceIndex + 2] = slices + i;
908     }
909   }
910 }
911
912 void PrimitiveVisual::ComputeCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions )
913 {
914   int numVertices = 4 * 6; //Four per face.
915   int vertexIndex = 0; //Tracks progress through vertices.
916   float scaledX = 0.5 * dimensions.x;
917   float scaledY = 0.5 * dimensions.y;
918   float scaledZ = 0.5 * dimensions.z;
919
920   vertices.Resize( numVertices );
921
922   Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
923   positions.Resize(8);
924   Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
925   normals.Resize(6);
926
927   positions[0] = Vector3( -scaledX,  scaledY, -scaledZ );
928   positions[1] = Vector3(  scaledX,  scaledY, -scaledZ );
929   positions[2] = Vector3(  scaledX,  scaledY,  scaledZ );
930   positions[3] = Vector3( -scaledX,  scaledY,  scaledZ );
931   positions[4] = Vector3( -scaledX, -scaledY, -scaledZ );
932   positions[5] = Vector3(  scaledX, -scaledY, -scaledZ );
933   positions[6] = Vector3(  scaledX, -scaledY,  scaledZ );
934   positions[7] = Vector3( -scaledX, -scaledY,  scaledZ );
935
936   normals[0] = Vector3(  0,  1,  0 );
937   normals[1] = Vector3(  0,  0, -1 );
938   normals[2] = Vector3(  1,  0,  0 );
939   normals[3] = Vector3(  0,  0,  1 );
940   normals[4] = Vector3( -1,  0,  0 );
941   normals[5] = Vector3(  0, -1,  0 );
942
943   //Top face, upward normals.
944   for( int i = 0; i < 4; i++, vertexIndex++ )
945   {
946     vertices[vertexIndex].position = positions[i];
947     vertices[vertexIndex].normal = normals[0];
948   }
949
950   //Top face, outward normals.
951   for( int i = 0; i < 4; i++, vertexIndex += 2 )
952   {
953     vertices[vertexIndex].position = positions[i];
954     vertices[vertexIndex].normal = normals[i + 1];
955
956     if( i == 3 )
957     {
958       //End, so loop around.
959       vertices[vertexIndex + 1].position = positions[0];
960     }
961     else
962     {
963       vertices[vertexIndex + 1].position = positions[i + 1];
964     }
965     vertices[vertexIndex + 1].normal = normals[i + 1];
966   }
967
968   //Bottom face, outward normals.
969   for( int i = 0; i < 4; i++, vertexIndex += 2 )
970   {
971     vertices[vertexIndex].position = positions[i + 4];
972     vertices[vertexIndex].normal = normals[i + 1];
973
974     if( i == 3 )
975     {
976       //End, so loop around.
977       vertices[vertexIndex + 1].position = positions[4];
978     }
979     else
980     {
981       vertices[vertexIndex + 1].position = positions[i + 5];
982     }
983     vertices[vertexIndex + 1].normal = normals[i + 1];
984   }
985
986   //Bottom face, downward normals.
987   for( int i = 0; i < 4; i++, vertexIndex++ )
988   {
989     vertices[vertexIndex].position = positions[i + 4];
990     vertices[vertexIndex].normal = normals[5];
991   }
992
993 }
994
995 void PrimitiveVisual::FormCubeTriangles( Vector<unsigned short>& indices )
996 {
997   int numTriangles = 12;
998   int triangleIndex = 0;  //Track progress through indices.
999
1000   indices.Resize( 3 * numTriangles );
1001
1002   //Top face.
1003   indices[triangleIndex] =     0;
1004   indices[triangleIndex + 1] = 2;
1005   indices[triangleIndex + 2] = 1;
1006   indices[triangleIndex + 3] = 2;
1007   indices[triangleIndex + 4] = 0;
1008   indices[triangleIndex + 5] = 3;
1009   triangleIndex += 6;
1010
1011   int topFaceStart = 4;
1012   int bottomFaceStart = 12;
1013
1014   //Side faces.
1015   for( int i = 0; i < 8; i += 2, triangleIndex += 6 )
1016   {
1017     indices[triangleIndex    ] = i + topFaceStart;
1018     indices[triangleIndex + 1] = i + topFaceStart + 1;
1019     indices[triangleIndex + 2] = i + bottomFaceStart + 1;
1020     indices[triangleIndex + 3] = i + topFaceStart;
1021     indices[triangleIndex + 4] = i + bottomFaceStart + 1;
1022     indices[triangleIndex + 5] = i + bottomFaceStart;
1023   }
1024
1025   //Bottom face.
1026   indices[triangleIndex] =     20;
1027   indices[triangleIndex + 1] = 21;
1028   indices[triangleIndex + 2] = 22;
1029   indices[triangleIndex + 3] = 22;
1030   indices[triangleIndex + 4] = 23;
1031   indices[triangleIndex + 5] = 20;
1032 }
1033
1034 void PrimitiveVisual::ComputeOctahedronVertices( Vector<Vertex>& vertices, Vector3 dimensions, float smoothness )
1035 {
1036   int numVertices = 3 * 8; //Three per face
1037   int vertexIndex = 0; //Tracks progress through vertices.
1038   float scaledX = 0.5 * dimensions.x;
1039   float scaledY = 0.5 * dimensions.y;
1040   float scaledZ = 0.5 * dimensions.z;
1041
1042   vertices.Resize( numVertices );
1043
1044   Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
1045   positions.Resize(6);
1046   Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
1047   normals.Resize(8);
1048   Vector<Vector3> outerNormals;  //Holds normals that point outwards at each vertex.
1049   outerNormals.Resize( 6 );
1050
1051   positions[0] = Vector3(  0.0,  scaledY,  0.0 );
1052   positions[1] = Vector3( -scaledX,  0.0,  0.0 );
1053   positions[2] = Vector3(  0.0,  0.0, -scaledZ );
1054   positions[3] = Vector3(  scaledX,  0.0,  0.0 );
1055   positions[4] = Vector3(  0.0,  0.0,  scaledZ );
1056   positions[5] = Vector3(  0.0, -scaledY,  0.0 );
1057
1058   normals[0] = Vector3( -1,  1, -1 );
1059   normals[1] = Vector3(  1,  1, -1 );
1060   normals[2] = Vector3(  1,  1,  1 );
1061   normals[3] = Vector3( -1,  1,  1 );
1062   normals[4] = Vector3( -1, -1, -1 );
1063   normals[5] = Vector3(  1, -1, -1 );
1064   normals[6] = Vector3(  1, -1,  1 );
1065   normals[7] = Vector3( -1, -1,  1 );
1066
1067   outerNormals[0] = Vector3(  0,  1,  0 );
1068   outerNormals[1] = Vector3( -1,  0,  0 );
1069   outerNormals[2] = Vector3(  0,  0, -1 );
1070   outerNormals[3] = Vector3(  1,  0,  0 );
1071   outerNormals[4] = Vector3(  0,  0,  1 );
1072   outerNormals[5] = Vector3(  0, -1,  0 );
1073
1074   //Loop through top faces.
1075   for( int i = 0; i < 4; i++, vertexIndex += 3 )
1076   {
1077     if( i == 3 )
1078     {
1079       //End, so loop around.
1080       vertices[vertexIndex    ].position = positions[0];
1081       vertices[vertexIndex    ].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1082       vertices[vertexIndex + 1].position = positions[1];
1083       vertices[vertexIndex + 1].normal = outerNormals[1] * smoothness + normals[i] * (1 - smoothness);
1084       vertices[vertexIndex + 2].position = positions[i + 1];
1085       vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1086     }
1087     else
1088     {
1089       vertices[vertexIndex    ].position = positions[0];
1090       vertices[vertexIndex    ].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1091       vertices[vertexIndex + 1].position = positions[i + 2];
1092       vertices[vertexIndex + 1].normal = outerNormals[i + 2] * smoothness + normals[i] * (1 - smoothness);
1093       vertices[vertexIndex + 2].position = positions[i + 1];
1094       vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1095     }
1096   }
1097
1098   //Loop through bottom faces.
1099   for( int i = 0; i < 4; i++, vertexIndex += 3 )
1100   {
1101     if( i == 3 )
1102     {
1103       //End, so loop around.
1104       vertices[vertexIndex    ].position = positions[5];
1105       vertices[vertexIndex    ].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1106       vertices[vertexIndex + 1].position = positions[i + 1];
1107       vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1108       vertices[vertexIndex + 2].position = positions[1];
1109       vertices[vertexIndex + 2].normal = outerNormals[1] * smoothness + normals[i + 4] * (1 - smoothness);
1110     }
1111     else
1112     {
1113       vertices[vertexIndex    ].position = positions[5];
1114       vertices[vertexIndex    ].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1115       vertices[vertexIndex + 1].position = positions[i + 1];
1116       vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1117       vertices[vertexIndex + 2].position = positions[i + 2];
1118       vertices[vertexIndex + 2].normal = outerNormals[i + 2] * smoothness + normals[i + 4] * (1 - smoothness);
1119     }
1120   }
1121 }
1122
1123 void PrimitiveVisual::FormOctahedronTriangles( Vector<unsigned short>& indices )
1124 {
1125   int numTriangles = 8;
1126   int numIndices = numTriangles * 3;
1127
1128   indices.Resize( numIndices );
1129
1130   for( unsigned short i = 0; i < numIndices; i++ )
1131   {
1132     indices[i] = i;
1133   }
1134 }
1135
1136 void PrimitiveVisual::ComputeBevelledCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions,
1137                                                      float bevelPercentage, float bevelSmoothness )
1138 {
1139   int numPositions = 24;
1140   int numFaces = 26;
1141   int numOuterFaces = 6;
1142   int numVertices = 6 * 4 + 12 * 4 + 8 * 3; //Six outer faces, 12 slanting rectangles, 8 slanting triangles.
1143   int vertexIndex = 0;  //Track progress through vertices.
1144   int normalIndex = 0;  //Track progress through normals, as vertices are calculated per face.
1145
1146   float minDimension = std::min( std::min( dimensions.x, dimensions.y ), dimensions.z );
1147   float bevelAmount = 0.5 * std::min( bevelPercentage, minDimension ); //Cap bevel amount if necessary.
1148
1149   //Distances from centre to outer edge points.
1150   float outerX = 0.5 * dimensions.x;
1151   float outerY = 0.5 * dimensions.y;
1152   float outerZ = 0.5 * dimensions.z;
1153
1154   //Distances from centre to bevelled points.
1155   float bevelX = outerX - bevelAmount;
1156   float bevelY = outerY - bevelAmount;
1157   float bevelZ = outerZ - bevelAmount;
1158
1159   Vector<Vector3> positions;  //Holds object points, to be shared between vertexes.
1160   positions.Resize( numPositions );
1161   Vector<Vector3> normals;  //Holds face normals, to be shared between vertexes.
1162   normals.Resize( numFaces );
1163   Vector<Vector3> outerNormals;  //Holds normals of the outermost faces specifically.
1164   outerNormals.Resize( numOuterFaces );
1165   vertices.Resize( numVertices );
1166
1167   //Topmost face positions.
1168   positions[0 ] = Vector3( -bevelX,  outerY, -bevelZ );
1169   positions[1 ] = Vector3(  bevelX,  outerY, -bevelZ );
1170   positions[2 ] = Vector3(  bevelX,  outerY,  bevelZ );
1171   positions[3 ] = Vector3( -bevelX,  outerY,  bevelZ );
1172
1173   //Second layer positions.
1174   positions[4 ] = Vector3( -outerX,  bevelY, -bevelZ );
1175   positions[5 ] = Vector3( -bevelX,  bevelY, -outerZ );
1176   positions[6 ] = Vector3(  bevelX,  bevelY, -outerZ );
1177   positions[7 ] = Vector3(  outerX,  bevelY, -bevelZ );
1178   positions[8 ] = Vector3(  outerX,  bevelY,  bevelZ );
1179   positions[9 ] = Vector3(  bevelX,  bevelY,  outerZ );
1180   positions[10] = Vector3( -bevelX,  bevelY,  outerZ );
1181   positions[11] = Vector3( -outerX,  bevelY,  bevelZ );
1182
1183   //Third layer positions.
1184   positions[12] = Vector3( -outerX, -bevelY, -bevelZ );
1185   positions[13] = Vector3( -bevelX, -bevelY, -outerZ );
1186   positions[14] = Vector3(  bevelX, -bevelY, -outerZ );
1187   positions[15] = Vector3(  outerX, -bevelY, -bevelZ );
1188   positions[16] = Vector3(  outerX, -bevelY,  bevelZ );
1189   positions[17] = Vector3(  bevelX, -bevelY,  outerZ );
1190   positions[18] = Vector3( -bevelX, -bevelY,  outerZ );
1191   positions[19] = Vector3( -outerX, -bevelY,  bevelZ );
1192
1193   //Bottom-most face positions.
1194   positions[20] = Vector3( -bevelX, -outerY, -bevelZ );
1195   positions[21] = Vector3(  bevelX, -outerY, -bevelZ );
1196   positions[22] = Vector3(  bevelX, -outerY,  bevelZ );
1197   positions[23] = Vector3( -bevelX, -outerY,  bevelZ );
1198
1199   //Top face normal.
1200   normals[0 ] = Vector3(  0,  1,  0 );
1201
1202   //Top slope normals.
1203   normals[1 ] = Vector3( -1,  1, -1 );
1204   normals[2 ] = Vector3(  0,  1, -1 );
1205   normals[3 ] = Vector3(  1,  1, -1 );
1206   normals[4 ] = Vector3(  1,  1,  0 );
1207   normals[5 ] = Vector3(  1,  1,  1 );
1208   normals[6 ] = Vector3(  0,  1,  1 );
1209   normals[7 ] = Vector3( -1,  1,  1 );
1210   normals[8 ] = Vector3( -1,  1,  0 );
1211
1212   //Side normals.
1213   normals[9 ] = Vector3( -1,  0, -1 );
1214   normals[10] = Vector3(  0,  0, -1 );
1215   normals[11] = Vector3(  1,  0, -1 );
1216   normals[12] = Vector3(  1,  0,  0 );
1217   normals[13] = Vector3(  1,  0,  1 );
1218   normals[14] = Vector3(  0,  0,  1 );
1219   normals[15] = Vector3( -1,  0,  1 );
1220   normals[16] = Vector3( -1,  0,  0 );
1221
1222   //Bottom slope normals.
1223   normals[17] = Vector3( -1, -1, -1 );
1224   normals[18] = Vector3(  0, -1, -1 );
1225   normals[19] = Vector3(  1, -1, -1 );
1226   normals[20] = Vector3(  1, -1,  0 );
1227   normals[21] = Vector3(  1, -1,  1 );
1228   normals[22] = Vector3(  0, -1,  1 );
1229   normals[23] = Vector3( -1, -1,  1 );
1230   normals[24] = Vector3( -1, -1,  0 );
1231
1232   //Bottom face normal.
1233   normals[25] = Vector3(  0, -1,  0 );
1234
1235   //Top, back, right, front, left and bottom faces, respectively.
1236   outerNormals[0] = Vector3(  0,  1,  0 );
1237   outerNormals[1] = Vector3(  0,  0, -1 );
1238   outerNormals[2] = Vector3(  1,  0,  0 );
1239   outerNormals[3] = Vector3(  0,  0,  1 );
1240   outerNormals[4] = Vector3( -1,  0,  0 );
1241   outerNormals[5] = Vector3(  0, -1,  0 );
1242
1243   //Topmost face vertices.
1244   for( int i = 0; i < 4; i++, vertexIndex++ )
1245   {
1246     vertices[vertexIndex].position = positions[i];
1247     vertices[vertexIndex].normal = normals[normalIndex];
1248   }
1249
1250   normalIndex++;
1251
1252   //Top slope vertices.
1253   for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
1254   {
1255     //Triangle part
1256     vertices[vertexIndex    ].position = positions[i];
1257     vertices[vertexIndex    ].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1258     vertices[vertexIndex + 1].position = positions[2 * i + 4];
1259     vertices[vertexIndex + 1].normal = outerNormals[( i == 0 ) ? 4 : i] * bevelSmoothness  + normals[normalIndex] * (1 - bevelSmoothness);
1260     vertices[vertexIndex + 2].position = positions[2 * i + 5];
1261     vertices[vertexIndex + 2].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1262
1263     //Rectangle part
1264     if( i == 3 )
1265     {
1266       //End, so loop around.
1267       vertices[vertexIndex + 3].position = positions[i];
1268       vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1269       vertices[vertexIndex + 4].position = positions[0];
1270       vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1271       vertices[vertexIndex + 5].position = positions[2 * i + 5];
1272       vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1273       vertices[vertexIndex + 6].position = positions[4];
1274       vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1275     }
1276     else
1277     {
1278       vertices[vertexIndex + 3].position = positions[i];
1279       vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1280       vertices[vertexIndex + 4].position = positions[i + 1];
1281       vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1282       vertices[vertexIndex + 5].position = positions[2 * i + 5];
1283       vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1284       vertices[vertexIndex + 6].position = positions[2 * i + 6];
1285       vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1286     }
1287   }
1288
1289   int secondCycleBeginning = 4;
1290   int thirdCycleBeginning = secondCycleBeginning + 8;
1291   int bottomCycleBeginning = thirdCycleBeginning + 8;
1292
1293   //Side vertices.
1294   for( int i = 0; i < 8; i++, vertexIndex += 4, normalIndex++ )
1295   {
1296     if( i == 7 )
1297     {
1298       //End, so loop around.
1299       vertices[vertexIndex    ].position = positions[secondCycleBeginning + i];
1300       vertices[vertexIndex    ].normal = normals[normalIndex];
1301       vertices[vertexIndex + 1].position = positions[secondCycleBeginning];
1302       vertices[vertexIndex + 1].normal = normals[normalIndex];
1303       vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1304       vertices[vertexIndex + 2].normal = normals[normalIndex];
1305       vertices[vertexIndex + 3].position = positions[thirdCycleBeginning];
1306       vertices[vertexIndex + 3].normal = normals[normalIndex];
1307     }
1308     else if( (i % 2) == 0 )
1309     {
1310       //'even' faces are corner ones, and need smoothing.
1311       vertices[vertexIndex    ].position = positions[secondCycleBeginning + i];
1312       vertices[vertexIndex    ].normal = outerNormals[( i == 0 ) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1313       vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1314       vertices[vertexIndex + 1].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1315       vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1316       vertices[vertexIndex + 2].normal = outerNormals[( i == 0 ) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1317       vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1318       vertices[vertexIndex + 3].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1319     }
1320     else
1321     {
1322       //'odd' faces are outer ones, and so don't need smoothing.
1323       vertices[vertexIndex    ].position = positions[secondCycleBeginning + i];
1324       vertices[vertexIndex    ].normal = normals[normalIndex];
1325       vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1326       vertices[vertexIndex + 1].normal = normals[normalIndex];
1327       vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1328       vertices[vertexIndex + 2].normal = normals[normalIndex];
1329       vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1330       vertices[vertexIndex + 3].normal = normals[normalIndex];
1331     }
1332   }
1333
1334   //Bottom slope vertices.
1335   for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
1336   {
1337     //Triangle part
1338     vertices[vertexIndex    ].position = positions[thirdCycleBeginning + 2 * i];
1339     vertices[vertexIndex    ].normal = outerNormals[( i == 0 ) ? 4 : i] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1340     vertices[vertexIndex + 1].position = positions[thirdCycleBeginning + 2 * i + 1];
1341     vertices[vertexIndex + 1].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1342     vertices[vertexIndex + 2].position = positions[bottomCycleBeginning + i];
1343     vertices[vertexIndex + 2].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1344
1345     //Rectangle part
1346     if( i == 3 )
1347     {
1348       //End, so loop around.
1349       vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1350       vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1351       vertices[vertexIndex + 4].position = positions[thirdCycleBeginning];
1352       vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1353       vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1354       vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1355       vertices[vertexIndex + 6].position = positions[bottomCycleBeginning];
1356       vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1357     }
1358     else
1359     {
1360       vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1361       vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1362       vertices[vertexIndex + 4].position = positions[thirdCycleBeginning + 2 * i + 2];
1363       vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1364       vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1365       vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1366       vertices[vertexIndex + 6].position = positions[bottomCycleBeginning + i + 1];
1367       vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1368     }
1369   }
1370
1371   //Bottom-most face vertices.
1372   for( int i = 0; i < 4; i++, vertexIndex++ )
1373   {
1374     vertices[vertexIndex].position = positions[ bottomCycleBeginning + i];
1375     vertices[vertexIndex].normal = normals[normalIndex];
1376   }
1377
1378   normalIndex++;
1379 }
1380
1381 void PrimitiveVisual::FormBevelledCubeTriangles( Vector<unsigned short>& indices )
1382 {
1383   int numTriangles = 44; //(Going from top to bottom, that's 2 + 12 + 16 + 12 + 2)
1384   int indiceIndex = 0;  //Track progress through indices.
1385   int vertexIndex = 0;  //Track progress through vertices as they're processed.
1386
1387   indices.Resize( 3 * numTriangles );
1388
1389   //Top face.
1390   indices[indiceIndex    ] = vertexIndex;
1391   indices[indiceIndex + 1] = vertexIndex + 2;
1392   indices[indiceIndex + 2] = vertexIndex + 1;
1393   indices[indiceIndex + 3] = vertexIndex + 0;
1394   indices[indiceIndex + 4] = vertexIndex + 3;
1395   indices[indiceIndex + 5] = vertexIndex + 2;
1396   indiceIndex += 6;
1397   vertexIndex += 4;
1398
1399   //Top slopes.
1400   for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1401   {
1402     //Triangle part.
1403     indices[indiceIndex    ] = vertexIndex;
1404     indices[indiceIndex + 1] = vertexIndex + 2;
1405     indices[indiceIndex + 2] = vertexIndex + 1;
1406
1407     //Rectangle part.
1408     indices[indiceIndex + 3] = vertexIndex + 3;
1409     indices[indiceIndex + 4] = vertexIndex + 4;
1410     indices[indiceIndex + 5] = vertexIndex + 5;
1411     indices[indiceIndex + 6] = vertexIndex + 4;
1412     indices[indiceIndex + 7] = vertexIndex + 6;
1413     indices[indiceIndex + 8] = vertexIndex + 5;
1414   }
1415
1416   //Side faces.
1417   for( int i = 0; i < 8; i++, indiceIndex += 6, vertexIndex += 4 )
1418   {
1419     indices[indiceIndex    ] = vertexIndex;
1420     indices[indiceIndex + 1] = vertexIndex + 1;
1421     indices[indiceIndex + 2] = vertexIndex + 2;
1422     indices[indiceIndex + 3] = vertexIndex + 1;
1423     indices[indiceIndex + 4] = vertexIndex + 3;
1424     indices[indiceIndex + 5] = vertexIndex + 2;
1425   }
1426
1427   //Bottom slopes.
1428   for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1429   {
1430     //Triangle part.
1431     indices[indiceIndex    ] = vertexIndex;
1432     indices[indiceIndex + 1] = vertexIndex + 1;
1433     indices[indiceIndex + 2] = vertexIndex + 2;
1434
1435     //Rectangle part.
1436     indices[indiceIndex + 3] = vertexIndex + 3;
1437     indices[indiceIndex + 4] = vertexIndex + 4;
1438     indices[indiceIndex + 5] = vertexIndex + 5;
1439     indices[indiceIndex + 6] = vertexIndex + 4;
1440     indices[indiceIndex + 7] = vertexIndex + 6;
1441     indices[indiceIndex + 8] = vertexIndex + 5;
1442   }
1443
1444   //Bottom face.
1445   indices[indiceIndex    ] = vertexIndex;
1446   indices[indiceIndex + 1] = vertexIndex + 1;
1447   indices[indiceIndex + 2] = vertexIndex + 2;
1448   indices[indiceIndex + 3] = vertexIndex + 0;
1449   indices[indiceIndex + 4] = vertexIndex + 2;
1450   indices[indiceIndex + 5] = vertexIndex + 3;
1451   indiceIndex += 6;
1452   vertexIndex += 4;
1453 }
1454
1455 } // namespace Internal
1456
1457 } // namespace Toolkit
1458
1459 } // namespace Dali