fa40339804f7b8ab6787c9b24365d944aa592dad
[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
388 void PrimitiveVisual::DoCreatePropertyMap( Property::Map& map ) const
389 {
390   map.Clear();
391   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::PRIMITIVE );
392   map.Insert( Toolkit::PrimitiveVisual::Property::SHAPE, mPrimitiveType );
393   map.Insert( Toolkit::PrimitiveVisual::Property::MIX_COLOR, mColor );
394   map.Insert( Toolkit::PrimitiveVisual::Property::SLICES, mSlices );
395   map.Insert( Toolkit::PrimitiveVisual::Property::STACKS, mStacks );
396   map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_TOP_RADIUS, mScaleTopRadius );
397   map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_BOTTOM_RADIUS, mScaleBottomRadius );
398   map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_HEIGHT, mScaleHeight );
399   map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_RADIUS, mScaleRadius );
400   map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_DIMENSIONS, mScaleDimensions );
401   map.Insert( Toolkit::PrimitiveVisual::Property::BEVEL_PERCENTAGE, mBevelPercentage );
402   map.Insert( Toolkit::PrimitiveVisual::Property::BEVEL_SMOOTHNESS, mBevelSmoothness );
403   map.Insert( Toolkit::PrimitiveVisual::Property::LIGHT_POSITION, mLightPosition );
404 }
405
406 void PrimitiveVisual::InitializeRenderer()
407 {
408   if( !mGeometry )
409   {
410     CreateGeometry();
411   }
412
413   if( !mShader )
414   {
415     CreateShader();
416   }
417
418   mImpl->mRenderer = Renderer::New( mGeometry, mShader );
419   mImpl->mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
420 }
421
422 void PrimitiveVisual::UpdateShaderUniforms()
423 {
424   Stage stage = Stage::GetCurrent();
425   float width = stage.GetSize().width;
426   float height = stage.GetSize().height;
427
428   //Flip model to account for DALi starting with (0, 0) at the top left.
429   Matrix scaleMatrix;
430   scaleMatrix.SetIdentityAndScale( Vector3( 1.0, -1.0, 1.0 ) );
431
432   mShader.RegisterProperty( STAGE_OFFSET_UNIFORM_NAME, Vector2( width, height ) / 2.0f );
433   mShader.RegisterProperty( LIGHT_POSITION_UNIFORM_NAME, mLightPosition );
434   mShader.RegisterProperty( OBJECT_MATRIX_UNIFORM_NAME, scaleMatrix );
435   mShader.RegisterProperty( Toolkit::PrimitiveVisual::Property::MIX_COLOR, COLOR_UNIFORM_NAME, mColor );
436   mShader.RegisterProperty( OBJECT_DIMENSIONS_UNIFORM_NAME, mObjectDimensions );
437 }
438
439 void PrimitiveVisual::CreateShader()
440 {
441   mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
442   UpdateShaderUniforms();
443 }
444
445 void PrimitiveVisual::CreateGeometry()
446 {
447   Dali::Vector<Vertex> vertices;
448   Dali::Vector<unsigned short> indices;
449
450   switch( mPrimitiveType )
451   {
452     case Toolkit::PrimitiveVisual::Shape::SPHERE:
453     {
454       CreateSphere( vertices, indices, mSlices, mStacks );
455       break;
456     }
457     case Toolkit::PrimitiveVisual::Shape::CONE:
458     {
459       //Create a conic with zero top radius.
460       CreateConic( vertices, indices, 0, mScaleBottomRadius, mScaleHeight, mSlices );
461       break;
462     }
463     case Toolkit::PrimitiveVisual::Shape::CONICAL_FRUSTRUM:
464     {
465       CreateConic( vertices, indices, mScaleTopRadius, mScaleBottomRadius, mScaleHeight, mSlices );
466       break;
467     }
468     case Toolkit::PrimitiveVisual::Shape::CYLINDER:
469     {
470       //Create a conic with equal radii on the top and bottom.
471       CreateConic( vertices, indices, mScaleRadius, mScaleRadius, mScaleHeight, mSlices );
472       break;
473     }
474     case Toolkit::PrimitiveVisual::Shape::CUBE:
475     {
476       //Create a cube by creating a bevelled cube with minimum bevel.
477       CreateBevelledCube( vertices, indices, mScaleDimensions, 0.0, 0.0 );
478       break;
479     }
480     case Toolkit::PrimitiveVisual::Shape::OCTAHEDRON:
481     {
482       //Create an octahedron by creating a bevelled cube with maximum bevel.
483       CreateBevelledCube( vertices, indices, mScaleDimensions, 1.0, mBevelSmoothness );
484       break;
485     }
486     case Toolkit::PrimitiveVisual::Shape::BEVELLED_CUBE:
487     {
488       CreateBevelledCube( vertices, indices, mScaleDimensions, mBevelPercentage, mBevelSmoothness );
489       break;
490     }
491   }
492
493   mGeometry = Geometry::New();
494
495   //Vertices
496   Property::Map vertexFormat;
497   vertexFormat[POSITION] = Property::VECTOR3;
498   vertexFormat[NORMAL] = Property::VECTOR3;
499   PropertyBuffer surfaceVertices = PropertyBuffer::New( vertexFormat );
500   surfaceVertices.SetData( &vertices[0], vertices.Size() );
501
502   mGeometry.AddVertexBuffer( surfaceVertices );
503
504   //Indices for triangle formulation
505   mGeometry.SetIndexBuffer( &indices[0], indices.Size() );
506 }
507
508 void PrimitiveVisual::CreateSphere( Vector<Vertex>& vertices, Vector<unsigned short>& indices, int slices, int stacks )
509 {
510   ComputeSphereVertices( vertices, slices, stacks );
511   FormSphereTriangles( indices, slices, stacks );
512
513   mObjectDimensions = Vector3::ONE;
514 }
515
516 void PrimitiveVisual::CreateConic( Vector<Vertex>& vertices, Vector<unsigned short>& indices, float scaleTopRadius,
517                                      float scaleBottomRadius, float scaleHeight, int slices )
518 {
519   ComputeConicVertices( vertices, scaleTopRadius, scaleBottomRadius, scaleHeight, slices );
520   FormConicTriangles( indices, scaleTopRadius, scaleBottomRadius, slices );
521
522   //Determine object dimensions, and scale them to be between 0.0 and 1.0.
523   float xDimension = std::max( scaleTopRadius, scaleBottomRadius ) * 2.0f;
524   float yDimension = scaleHeight;
525   float largestDimension = std::max( xDimension, yDimension );
526
527   mObjectDimensions = Vector3( xDimension / largestDimension, yDimension / largestDimension,
528                                xDimension / largestDimension );
529 }
530
531 void PrimitiveVisual::CreateBevelledCube( Vector<Vertex>& vertices, Vector<unsigned short>& indices,
532                                             Vector3 dimensions, float bevelPercentage, float bevelSmoothness )
533 {
534   dimensions.Normalize();
535
536   if( bevelPercentage <= MIN_BEVEL_PERCENTAGE ) //No bevel, form a cube.
537   {
538     ComputeCubeVertices( vertices, dimensions );
539     FormCubeTriangles( indices );
540   }
541   else if( bevelPercentage >= MAX_BEVEL_PERCENTAGE ) //Max bevel, form an octahedron.
542   {
543     ComputeOctahedronVertices( vertices, dimensions, bevelSmoothness );
544     FormOctahedronTriangles( indices );
545   }
546   else //In between, form a bevelled cube.
547   {
548     ComputeBevelledCubeVertices( vertices, dimensions, bevelPercentage, bevelSmoothness );
549     FormBevelledCubeTriangles( indices );
550   }
551
552   mObjectDimensions = dimensions;
553 }
554
555 void PrimitiveVisual::ComputeCircleTables( Vector<float>& sinTable, Vector<float>& cosTable, int divisions,
556                                              bool halfCircle )
557 {
558   if( divisions < 0 )
559   {
560     return;
561   }
562
563   const float angleDivision = ( halfCircle ? 1.0f : 2.0f ) * Dali::Math::PI / ( float ) divisions;
564
565   sinTable.Resize( divisions );
566   cosTable.Resize( divisions );
567
568   for( int i = 0; i < divisions; i++ )
569   {
570     sinTable[i] = sin( angleDivision * i );
571     cosTable[i] = cos( angleDivision * i );
572   }
573 }
574
575 void PrimitiveVisual::ComputeSphereVertices( Vector<Vertex>& vertices, int slices, int stacks )
576 {
577   //Tables for calculating slices angles and stacks angles, respectively.
578   Vector<float> sinTable1;
579   Vector<float> cosTable1;
580   Vector<float> sinTable2;
581   Vector<float> cosTable2;
582
583   ComputeCircleTables( sinTable1, cosTable1, slices, false );
584   ComputeCircleTables( sinTable2, cosTable2, stacks, true );
585
586   int numVertices = slices * ( stacks - 1 ) + 2;
587   vertices.Resize( numVertices );
588
589   int vertexIndex = 0;  //Track progress through vertices.
590   float x;
591   float y;
592   float z;
593
594   //Top stack.
595   vertices[vertexIndex].position = Vector3( 0.0, 0.5, 0.0 );
596   vertices[vertexIndex].normal =   Vector3( 0.0, 1.0, 0.0 );
597   vertexIndex++;
598
599   //Middle stacks.
600   for( int i = 1; i < stacks; i++ )
601   {
602     for( int j = 0; j < slices; j++, vertexIndex++ )
603     {
604       x = cosTable1[j] * sinTable2[i];
605       y = cosTable2[i];
606       z = sinTable1[j] * sinTable2[i];
607
608       vertices[vertexIndex].position = Vector3( x / 2.0f, y / 2.0f, z / 2.0f );
609       vertices[vertexIndex].normal = Vector3( x, y, z );
610     }
611   }
612
613   //Bottom stack.
614   vertices[vertexIndex].position = Vector3( 0.0, -0.5, 0.0 );
615   vertices[vertexIndex].normal =   Vector3( 0.0, -1.0, 0.0 );
616 }
617
618 void PrimitiveVisual::FormSphereTriangles( Vector<unsigned short>& indices, int slices, int stacks )
619 {
620   if( stacks <= 1 )
621   {
622     //Set indices to placeholder "error" values.
623     //This will display nothing, which is the expected behaviour for this edge case.
624     indices.Resize( 3 );
625     return;
626   }
627
628   int numTriangles = 2 * slices * ( stacks - 1 );
629
630   indices.Resize( 3 * numTriangles );
631
632   int indiceIndex = 0;  //Used to keep track of progress through indices.
633   int previousCycleBeginning = 1;  //Stores the index of the vertex that started the cycle of the previous stack.
634   int currentCycleBeginning = 1 + slices;
635
636   //Top stack. Loop from index 1 to index slices, as not counting the very first vertex.
637   for( int i = 1; i <= slices; i++, indiceIndex += 3 )
638   {
639     indices[indiceIndex] = 0;
640     if( i == slices )
641     {
642       //End, so loop around.
643       indices[indiceIndex + 1] = 1;
644     }
645     else
646     {
647       indices[indiceIndex + 1] = i + 1;
648     }
649     indices[indiceIndex + 2] = i;
650   }
651
652   //Middle Stacks. Want to form triangles between the top and bottom stacks, so loop up to the number of stacks - 2.
653   for( int i = 0; i < stacks - 2; i++, previousCycleBeginning += slices, currentCycleBeginning += slices )
654   {
655     for( int j = 0; j < slices; j++, indiceIndex += 6 )
656     {
657       if( j == slices - 1 )
658       {
659         //End, so loop around.
660         indices[indiceIndex] =     previousCycleBeginning + j;
661         indices[indiceIndex + 1] = previousCycleBeginning;
662         indices[indiceIndex + 2] = currentCycleBeginning + j;
663         indices[indiceIndex + 3] = currentCycleBeginning + j;
664         indices[indiceIndex + 4] = previousCycleBeginning;
665         indices[indiceIndex + 5] = currentCycleBeginning;
666       }
667       else
668       {
669         indices[indiceIndex] =     previousCycleBeginning + j;
670         indices[indiceIndex + 1] = previousCycleBeginning + 1 + j;
671         indices[indiceIndex + 2] = currentCycleBeginning + j;
672         indices[indiceIndex + 3] = currentCycleBeginning + j;
673         indices[indiceIndex + 4] = previousCycleBeginning + 1 + j;
674         indices[indiceIndex + 5] = currentCycleBeginning + 1 + j;
675       }
676     }
677   }
678
679   //Bottom stack. Loop around the last stack from the previous loop, and go up to the penultimate vertex.
680   for( int i = 0; i < slices; i++, indiceIndex += 3 )
681   {
682     indices[indiceIndex] = previousCycleBeginning + slices;
683     indices[indiceIndex + 1] = previousCycleBeginning + i;
684     if( i == slices - 1 )
685     {
686       //End, so loop around.
687       indices[indiceIndex + 2] = previousCycleBeginning;
688     }
689     else
690     {
691       indices[indiceIndex + 2] = previousCycleBeginning + i + 1;
692     }
693   }
694 }
695
696 void PrimitiveVisual::ComputeConicVertices( Vector<Vertex>& vertices, float scaleTopRadius,
697                                                      float scaleBottomRadius, float scaleHeight, int slices )
698 {
699   int vertexIndex = 0;  //Track progress through vertices.
700   Vector<float> sinTable;
701   Vector<float> cosTable;
702
703   ComputeCircleTables( sinTable, cosTable, slices, false );
704
705   int numVertices = 2;  //Always will have one at the top and one at the bottom.
706
707   //Add vertices for each circle. Need two per point for different face normals.
708   if( scaleTopRadius > 0.0 )
709   {
710     numVertices += 2 * slices;
711   }
712   if( scaleBottomRadius > 0.0 )
713   {
714     numVertices += 2 * slices;
715   }
716
717   vertices.Resize( numVertices );
718
719
720   //Scale to bounding region of -0.5 to 0.5 (i.e range of 1).
721   float biggestObjectDimension = std::max( std::max( scaleTopRadius * 2.0f, scaleBottomRadius * 2.0f ), scaleHeight );
722   scaleTopRadius = scaleTopRadius / biggestObjectDimension;
723   scaleBottomRadius = scaleBottomRadius / biggestObjectDimension;
724
725   //Dimensions for vertex coordinates. Y is constant, and so can be initialised now.
726   float x;
727   float y = scaleHeight / biggestObjectDimension / 2.0f;
728   float z;
729
730   //Top center.
731   vertices[0].position = Vector3( 0, y, 0 );
732   vertices[0].normal = Vector3( 0, 1, 0 );
733   vertexIndex++;
734
735   //Top circle.
736   if( scaleTopRadius > 0.0 )
737   {
738     //Loop around the circle.
739     for( int i = 0; i < slices; i++, vertexIndex++ )
740     {
741       x = sinTable[i] * scaleTopRadius;
742       z = cosTable[i] * scaleTopRadius;
743
744       //Upward-facing normal.
745       vertices[vertexIndex].position = Vector3( x, y, z );
746       vertices[vertexIndex].normal = Vector3( 0, 1, 0 );
747
748       //Outward-facing normal.
749       vertices[vertexIndex + slices].position = Vector3( x, y, z );
750       vertices[vertexIndex + slices].normal = Vector3( x, 0, z );
751     }
752
753     vertexIndex += slices;
754   }
755
756   //Bottom circle.
757   if( scaleBottomRadius > 0.0 )
758   {
759     //Loop around the circle.
760     for( int i = 0; i < slices; i++, vertexIndex++ )
761     {
762       x = sinTable[i] * scaleBottomRadius;
763       z = cosTable[i] * scaleBottomRadius;
764
765       //Outward-facing normal.
766       vertices[vertexIndex].position = Vector3( x, -y, z );
767       vertices[vertexIndex].normal = Vector3( x, 0, z );
768
769       //Downward-facing normal.
770       vertices[vertexIndex + slices].position = Vector3( x, -y, z );
771       vertices[vertexIndex + slices].normal = Vector3( 0, -1, 0 );
772     }
773
774     vertexIndex += slices;
775   }
776
777   //Bottom center.
778   vertices[vertexIndex].position = Vector3( 0, -y, 0 );
779   vertices[vertexIndex].normal = Vector3( 0, -1, 0 );
780   vertexIndex++;
781 }
782
783 void PrimitiveVisual::FormConicTriangles( Vector<unsigned short>& indices, float scaleTopRadius,
784                                                    float scaleBottomRadius, int slices )
785 {
786   int  indiceIndex = 0;  //Track progress through indices.
787   int  numTriangles = 0;
788   bool coneTop = scaleTopRadius <= 0.0;
789   bool coneBottom = scaleBottomRadius <= 0.0;
790
791   if( coneTop && coneBottom )
792   {
793     //Set indices to placeholder "error" values.
794     //This will display nothing, which is the expected behaviour for this edge case.
795     indices.Resize( 3 );
796     return;
797   }
798
799   if( !coneTop )
800   {
801     numTriangles += 2 * slices;
802   }
803   if( !coneBottom )
804   {
805     numTriangles += 2 * slices;
806   }
807
808   indices.Resize( 3 * numTriangles );
809
810   //Switch on the type of conic we have.
811   if( !coneTop && !coneBottom )
812   {
813     //Top circle. Start at index of first outer point and go around.
814     for( int i = 1; i <= slices; i++, indiceIndex += 3 )
815     {
816       indices[indiceIndex] = 0;
817       indices[indiceIndex + 1] = i;
818       if( i == slices )
819       {
820         //End, so loop around.
821         indices[indiceIndex + 2] = 1;
822       }
823       else
824       {
825         indices[indiceIndex + 2] = i + 1;
826       }
827     }
828
829     int topCycleBeginning = slices + 1;
830     int bottomCycleBeginning = topCycleBeginning + slices;
831
832     //Vertical edges.
833     for( int i = 0; i < slices; i++, indiceIndex += 6 )
834     {
835       if( i == slices - 1 )
836       {
837         //End, so loop around.
838         indices[indiceIndex] =     topCycleBeginning + i;
839         indices[indiceIndex + 1] = bottomCycleBeginning + i;
840         indices[indiceIndex + 2] = topCycleBeginning;
841         indices[indiceIndex + 3] = bottomCycleBeginning + i;
842         indices[indiceIndex + 4] = bottomCycleBeginning;
843         indices[indiceIndex + 5] = topCycleBeginning;
844       }
845       else
846       {
847         indices[indiceIndex] =     topCycleBeginning + i;
848         indices[indiceIndex + 1] = bottomCycleBeginning + i;
849         indices[indiceIndex + 2] = topCycleBeginning + 1 + i;
850         indices[indiceIndex + 3] = bottomCycleBeginning + i;
851         indices[indiceIndex + 4] = bottomCycleBeginning + 1 + i;
852         indices[indiceIndex + 5] = topCycleBeginning + 1 + i;
853       }
854     }
855
856     int bottomFaceCycleBeginning = bottomCycleBeginning + slices;
857
858     //Bottom circle.
859     for( int i = 0; i < slices; i++, indiceIndex += 3 )
860     {
861       indices[indiceIndex] = bottomFaceCycleBeginning;
862       if( i == slices - 1 )
863       {
864         //End, so loop around.
865         indices[indiceIndex + 1] = bottomFaceCycleBeginning;
866       }
867       else
868       {
869         indices[indiceIndex + 1] = bottomFaceCycleBeginning + i + 1;
870       }
871       indices[indiceIndex + 2] = bottomFaceCycleBeginning + i;
872     }
873   }
874   else if( !coneTop || !coneBottom )
875   {
876     //Top circle/edges. Start at index of first outer point and go around.
877     for( int i = 1; i <= slices; i++, indiceIndex += 3 )
878     {
879       indices[indiceIndex] = 0;
880       indices[indiceIndex + 1] = i;
881       if( i == slices )
882       {
883         //End, so loop around.
884         indices[indiceIndex + 2] = 1;
885       }
886       else
887       {
888         indices[indiceIndex + 2] = i + 1;
889       }
890     }
891
892     //Bottom circle/edges. Start at index of first outer point and go around.
893     for( int i = 1; i <= slices; i++, indiceIndex += 3 )
894     {
895       indices[indiceIndex] = 2 * slices + 1;
896       if( i == slices )
897       {
898         //End, so loop around.
899         indices[indiceIndex + 1] = slices + 1;
900       }
901       else
902       {
903         indices[indiceIndex + 1] = slices + i + 1;
904       }
905       indices[indiceIndex + 2] = slices + i;
906     }
907   }
908 }
909
910 void PrimitiveVisual::ComputeCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions )
911 {
912   int numVertices = 4 * 6; //Four per face.
913   int vertexIndex = 0; //Tracks progress through vertices.
914   float scaledX = 0.5 * dimensions.x;
915   float scaledY = 0.5 * dimensions.y;
916   float scaledZ = 0.5 * dimensions.z;
917
918   vertices.Resize( numVertices );
919
920   Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
921   positions.Resize(8);
922   Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
923   normals.Resize(6);
924
925   positions[0] = Vector3( -scaledX,  scaledY, -scaledZ );
926   positions[1] = Vector3(  scaledX,  scaledY, -scaledZ );
927   positions[2] = Vector3(  scaledX,  scaledY,  scaledZ );
928   positions[3] = Vector3( -scaledX,  scaledY,  scaledZ );
929   positions[4] = Vector3( -scaledX, -scaledY, -scaledZ );
930   positions[5] = Vector3(  scaledX, -scaledY, -scaledZ );
931   positions[6] = Vector3(  scaledX, -scaledY,  scaledZ );
932   positions[7] = Vector3( -scaledX, -scaledY,  scaledZ );
933
934   normals[0] = Vector3(  0,  1,  0 );
935   normals[1] = Vector3(  0,  0, -1 );
936   normals[2] = Vector3(  1,  0,  0 );
937   normals[3] = Vector3(  0,  0,  1 );
938   normals[4] = Vector3( -1,  0,  0 );
939   normals[5] = Vector3(  0, -1,  0 );
940
941   //Top face, upward normals.
942   for( int i = 0; i < 4; i++, vertexIndex++ )
943   {
944     vertices[vertexIndex].position = positions[i];
945     vertices[vertexIndex].normal = normals[0];
946   }
947
948   //Top face, outward normals.
949   for( int i = 0; i < 4; i++, vertexIndex += 2 )
950   {
951     vertices[vertexIndex].position = positions[i];
952     vertices[vertexIndex].normal = normals[i + 1];
953
954     if( i == 3 )
955     {
956       //End, so loop around.
957       vertices[vertexIndex + 1].position = positions[0];
958     }
959     else
960     {
961       vertices[vertexIndex + 1].position = positions[i + 1];
962     }
963     vertices[vertexIndex + 1].normal = normals[i + 1];
964   }
965
966   //Bottom face, outward normals.
967   for( int i = 0; i < 4; i++, vertexIndex += 2 )
968   {
969     vertices[vertexIndex].position = positions[i + 4];
970     vertices[vertexIndex].normal = normals[i + 1];
971
972     if( i == 3 )
973     {
974       //End, so loop around.
975       vertices[vertexIndex + 1].position = positions[4];
976     }
977     else
978     {
979       vertices[vertexIndex + 1].position = positions[i + 5];
980     }
981     vertices[vertexIndex + 1].normal = normals[i + 1];
982   }
983
984   //Bottom face, downward normals.
985   for( int i = 0; i < 4; i++, vertexIndex++ )
986   {
987     vertices[vertexIndex].position = positions[i + 4];
988     vertices[vertexIndex].normal = normals[5];
989   }
990
991 }
992
993 void PrimitiveVisual::FormCubeTriangles( Vector<unsigned short>& indices )
994 {
995   int numTriangles = 12;
996   int triangleIndex = 0;  //Track progress through indices.
997
998   indices.Resize( 3 * numTriangles );
999
1000   //Top face.
1001   indices[triangleIndex] =     0;
1002   indices[triangleIndex + 1] = 2;
1003   indices[triangleIndex + 2] = 1;
1004   indices[triangleIndex + 3] = 2;
1005   indices[triangleIndex + 4] = 0;
1006   indices[triangleIndex + 5] = 3;
1007   triangleIndex += 6;
1008
1009   int topFaceStart = 4;
1010   int bottomFaceStart = 12;
1011
1012   //Side faces.
1013   for( int i = 0; i < 8; i += 2, triangleIndex += 6 )
1014   {
1015     indices[triangleIndex    ] = i + topFaceStart;
1016     indices[triangleIndex + 1] = i + topFaceStart + 1;
1017     indices[triangleIndex + 2] = i + bottomFaceStart + 1;
1018     indices[triangleIndex + 3] = i + topFaceStart;
1019     indices[triangleIndex + 4] = i + bottomFaceStart + 1;
1020     indices[triangleIndex + 5] = i + bottomFaceStart;
1021   }
1022
1023   //Bottom face.
1024   indices[triangleIndex] =     20;
1025   indices[triangleIndex + 1] = 21;
1026   indices[triangleIndex + 2] = 22;
1027   indices[triangleIndex + 3] = 22;
1028   indices[triangleIndex + 4] = 23;
1029   indices[triangleIndex + 5] = 20;
1030 }
1031
1032 void PrimitiveVisual::ComputeOctahedronVertices( Vector<Vertex>& vertices, Vector3 dimensions, float smoothness )
1033 {
1034   int numVertices = 3 * 8; //Three per face
1035   int vertexIndex = 0; //Tracks progress through vertices.
1036   float scaledX = 0.5 * dimensions.x;
1037   float scaledY = 0.5 * dimensions.y;
1038   float scaledZ = 0.5 * dimensions.z;
1039
1040   vertices.Resize( numVertices );
1041
1042   Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
1043   positions.Resize(6);
1044   Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
1045   normals.Resize(8);
1046   Vector<Vector3> outerNormals;  //Holds normals that point outwards at each vertex.
1047   outerNormals.Resize( 6 );
1048
1049   positions[0] = Vector3(  0.0,  scaledY,  0.0 );
1050   positions[1] = Vector3( -scaledX,  0.0,  0.0 );
1051   positions[2] = Vector3(  0.0,  0.0, -scaledZ );
1052   positions[3] = Vector3(  scaledX,  0.0,  0.0 );
1053   positions[4] = Vector3(  0.0,  0.0,  scaledZ );
1054   positions[5] = Vector3(  0.0, -scaledY,  0.0 );
1055
1056   normals[0] = Vector3( -1,  1, -1 );
1057   normals[1] = Vector3(  1,  1, -1 );
1058   normals[2] = Vector3(  1,  1,  1 );
1059   normals[3] = Vector3( -1,  1,  1 );
1060   normals[4] = Vector3( -1, -1, -1 );
1061   normals[5] = Vector3(  1, -1, -1 );
1062   normals[6] = Vector3(  1, -1,  1 );
1063   normals[7] = Vector3( -1, -1,  1 );
1064
1065   outerNormals[0] = Vector3(  0,  1,  0 );
1066   outerNormals[1] = Vector3( -1,  0,  0 );
1067   outerNormals[2] = Vector3(  0,  0, -1 );
1068   outerNormals[3] = Vector3(  1,  0,  0 );
1069   outerNormals[4] = Vector3(  0,  0,  1 );
1070   outerNormals[5] = Vector3(  0, -1,  0 );
1071
1072   //Loop through top faces.
1073   for( int i = 0; i < 4; i++, vertexIndex += 3 )
1074   {
1075     if( i == 3 )
1076     {
1077       //End, so loop around.
1078       vertices[vertexIndex    ].position = positions[0];
1079       vertices[vertexIndex    ].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1080       vertices[vertexIndex + 1].position = positions[1];
1081       vertices[vertexIndex + 1].normal = outerNormals[1] * smoothness + normals[i] * (1 - smoothness);
1082       vertices[vertexIndex + 2].position = positions[i + 1];
1083       vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1084     }
1085     else
1086     {
1087       vertices[vertexIndex    ].position = positions[0];
1088       vertices[vertexIndex    ].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1089       vertices[vertexIndex + 1].position = positions[i + 2];
1090       vertices[vertexIndex + 1].normal = outerNormals[i + 2] * smoothness + normals[i] * (1 - smoothness);
1091       vertices[vertexIndex + 2].position = positions[i + 1];
1092       vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1093     }
1094   }
1095
1096   //Loop through bottom faces.
1097   for( int i = 0; i < 4; i++, vertexIndex += 3 )
1098   {
1099     if( i == 3 )
1100     {
1101       //End, so loop around.
1102       vertices[vertexIndex    ].position = positions[5];
1103       vertices[vertexIndex    ].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1104       vertices[vertexIndex + 1].position = positions[i + 1];
1105       vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1106       vertices[vertexIndex + 2].position = positions[1];
1107       vertices[vertexIndex + 2].normal = outerNormals[1] * smoothness + normals[i + 4] * (1 - smoothness);
1108     }
1109     else
1110     {
1111       vertices[vertexIndex    ].position = positions[5];
1112       vertices[vertexIndex    ].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1113       vertices[vertexIndex + 1].position = positions[i + 1];
1114       vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1115       vertices[vertexIndex + 2].position = positions[i + 2];
1116       vertices[vertexIndex + 2].normal = outerNormals[i + 2] * smoothness + normals[i + 4] * (1 - smoothness);
1117     }
1118   }
1119 }
1120
1121 void PrimitiveVisual::FormOctahedronTriangles( Vector<unsigned short>& indices )
1122 {
1123   int numTriangles = 8;
1124   int numIndices = numTriangles * 3;
1125
1126   indices.Resize( numIndices );
1127
1128   for( unsigned short i = 0; i < numIndices; i++ )
1129   {
1130     indices[i] = i;
1131   }
1132 }
1133
1134 void PrimitiveVisual::ComputeBevelledCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions,
1135                                                      float bevelPercentage, float bevelSmoothness )
1136 {
1137   int numPositions = 24;
1138   int numFaces = 26;
1139   int numOuterFaces = 6;
1140   int numVertices = 6 * 4 + 12 * 4 + 8 * 3; //Six outer faces, 12 slanting rectangles, 8 slanting triangles.
1141   int vertexIndex = 0;  //Track progress through vertices.
1142   int normalIndex = 0;  //Track progress through normals, as vertices are calculated per face.
1143
1144   float minDimension = std::min( std::min( dimensions.x, dimensions.y ), dimensions.z );
1145   float bevelAmount = 0.5 * std::min( bevelPercentage, minDimension ); //Cap bevel amount if necessary.
1146
1147   //Distances from centre to outer edge points.
1148   float outerX = 0.5 * dimensions.x;
1149   float outerY = 0.5 * dimensions.y;
1150   float outerZ = 0.5 * dimensions.z;
1151
1152   //Distances from centre to bevelled points.
1153   float bevelX = outerX - bevelAmount;
1154   float bevelY = outerY - bevelAmount;
1155   float bevelZ = outerZ - bevelAmount;
1156
1157   Vector<Vector3> positions;  //Holds object points, to be shared between vertexes.
1158   positions.Resize( numPositions );
1159   Vector<Vector3> normals;  //Holds face normals, to be shared between vertexes.
1160   normals.Resize( numFaces );
1161   Vector<Vector3> outerNormals;  //Holds normals of the outermost faces specifically.
1162   outerNormals.Resize( numOuterFaces );
1163   vertices.Resize( numVertices );
1164
1165   //Topmost face positions.
1166   positions[0 ] = Vector3( -bevelX,  outerY, -bevelZ );
1167   positions[1 ] = Vector3(  bevelX,  outerY, -bevelZ );
1168   positions[2 ] = Vector3(  bevelX,  outerY,  bevelZ );
1169   positions[3 ] = Vector3( -bevelX,  outerY,  bevelZ );
1170
1171   //Second layer positions.
1172   positions[4 ] = Vector3( -outerX,  bevelY, -bevelZ );
1173   positions[5 ] = Vector3( -bevelX,  bevelY, -outerZ );
1174   positions[6 ] = Vector3(  bevelX,  bevelY, -outerZ );
1175   positions[7 ] = Vector3(  outerX,  bevelY, -bevelZ );
1176   positions[8 ] = Vector3(  outerX,  bevelY,  bevelZ );
1177   positions[9 ] = Vector3(  bevelX,  bevelY,  outerZ );
1178   positions[10] = Vector3( -bevelX,  bevelY,  outerZ );
1179   positions[11] = Vector3( -outerX,  bevelY,  bevelZ );
1180
1181   //Third layer positions.
1182   positions[12] = Vector3( -outerX, -bevelY, -bevelZ );
1183   positions[13] = Vector3( -bevelX, -bevelY, -outerZ );
1184   positions[14] = Vector3(  bevelX, -bevelY, -outerZ );
1185   positions[15] = Vector3(  outerX, -bevelY, -bevelZ );
1186   positions[16] = Vector3(  outerX, -bevelY,  bevelZ );
1187   positions[17] = Vector3(  bevelX, -bevelY,  outerZ );
1188   positions[18] = Vector3( -bevelX, -bevelY,  outerZ );
1189   positions[19] = Vector3( -outerX, -bevelY,  bevelZ );
1190
1191   //Bottom-most face positions.
1192   positions[20] = Vector3( -bevelX, -outerY, -bevelZ );
1193   positions[21] = Vector3(  bevelX, -outerY, -bevelZ );
1194   positions[22] = Vector3(  bevelX, -outerY,  bevelZ );
1195   positions[23] = Vector3( -bevelX, -outerY,  bevelZ );
1196
1197   //Top face normal.
1198   normals[0 ] = Vector3(  0,  1,  0 );
1199
1200   //Top slope normals.
1201   normals[1 ] = Vector3( -1,  1, -1 );
1202   normals[2 ] = Vector3(  0,  1, -1 );
1203   normals[3 ] = Vector3(  1,  1, -1 );
1204   normals[4 ] = Vector3(  1,  1,  0 );
1205   normals[5 ] = Vector3(  1,  1,  1 );
1206   normals[6 ] = Vector3(  0,  1,  1 );
1207   normals[7 ] = Vector3( -1,  1,  1 );
1208   normals[8 ] = Vector3( -1,  1,  0 );
1209
1210   //Side normals.
1211   normals[9 ] = Vector3( -1,  0, -1 );
1212   normals[10] = Vector3(  0,  0, -1 );
1213   normals[11] = Vector3(  1,  0, -1 );
1214   normals[12] = Vector3(  1,  0,  0 );
1215   normals[13] = Vector3(  1,  0,  1 );
1216   normals[14] = Vector3(  0,  0,  1 );
1217   normals[15] = Vector3( -1,  0,  1 );
1218   normals[16] = Vector3( -1,  0,  0 );
1219
1220   //Bottom slope normals.
1221   normals[17] = Vector3( -1, -1, -1 );
1222   normals[18] = Vector3(  0, -1, -1 );
1223   normals[19] = Vector3(  1, -1, -1 );
1224   normals[20] = Vector3(  1, -1,  0 );
1225   normals[21] = Vector3(  1, -1,  1 );
1226   normals[22] = Vector3(  0, -1,  1 );
1227   normals[23] = Vector3( -1, -1,  1 );
1228   normals[24] = Vector3( -1, -1,  0 );
1229
1230   //Bottom face normal.
1231   normals[25] = Vector3(  0, -1,  0 );
1232
1233   //Top, back, right, front, left and bottom faces, respectively.
1234   outerNormals[0] = Vector3(  0,  1,  0 );
1235   outerNormals[1] = Vector3(  0,  0, -1 );
1236   outerNormals[2] = Vector3(  1,  0,  0 );
1237   outerNormals[3] = Vector3(  0,  0,  1 );
1238   outerNormals[4] = Vector3( -1,  0,  0 );
1239   outerNormals[5] = Vector3(  0, -1,  0 );
1240
1241   //Topmost face vertices.
1242   for( int i = 0; i < 4; i++, vertexIndex++ )
1243   {
1244     vertices[vertexIndex].position = positions[i];
1245     vertices[vertexIndex].normal = normals[normalIndex];
1246   }
1247
1248   normalIndex++;
1249
1250   //Top slope vertices.
1251   for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
1252   {
1253     //Triangle part
1254     vertices[vertexIndex    ].position = positions[i];
1255     vertices[vertexIndex    ].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1256     vertices[vertexIndex + 1].position = positions[2 * i + 4];
1257     vertices[vertexIndex + 1].normal = outerNormals[( i == 0 ) ? 4 : i] * bevelSmoothness  + normals[normalIndex] * (1 - bevelSmoothness);
1258     vertices[vertexIndex + 2].position = positions[2 * i + 5];
1259     vertices[vertexIndex + 2].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1260
1261     //Rectangle part
1262     if( i == 3 )
1263     {
1264       //End, so loop around.
1265       vertices[vertexIndex + 3].position = positions[i];
1266       vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1267       vertices[vertexIndex + 4].position = positions[0];
1268       vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1269       vertices[vertexIndex + 5].position = positions[2 * i + 5];
1270       vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1271       vertices[vertexIndex + 6].position = positions[4];
1272       vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1273     }
1274     else
1275     {
1276       vertices[vertexIndex + 3].position = positions[i];
1277       vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1278       vertices[vertexIndex + 4].position = positions[i + 1];
1279       vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1280       vertices[vertexIndex + 5].position = positions[2 * i + 5];
1281       vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1282       vertices[vertexIndex + 6].position = positions[2 * i + 6];
1283       vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1284     }
1285   }
1286
1287   int secondCycleBeginning = 4;
1288   int thirdCycleBeginning = secondCycleBeginning + 8;
1289   int bottomCycleBeginning = thirdCycleBeginning + 8;
1290
1291   //Side vertices.
1292   for( int i = 0; i < 8; i++, vertexIndex += 4, normalIndex++ )
1293   {
1294     if( i == 7 )
1295     {
1296       //End, so loop around.
1297       vertices[vertexIndex    ].position = positions[secondCycleBeginning + i];
1298       vertices[vertexIndex    ].normal = normals[normalIndex];
1299       vertices[vertexIndex + 1].position = positions[secondCycleBeginning];
1300       vertices[vertexIndex + 1].normal = normals[normalIndex];
1301       vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1302       vertices[vertexIndex + 2].normal = normals[normalIndex];
1303       vertices[vertexIndex + 3].position = positions[thirdCycleBeginning];
1304       vertices[vertexIndex + 3].normal = normals[normalIndex];
1305     }
1306     else if( (i % 2) == 0 )
1307     {
1308       //'even' faces are corner ones, and need smoothing.
1309       vertices[vertexIndex    ].position = positions[secondCycleBeginning + i];
1310       vertices[vertexIndex    ].normal = outerNormals[( i == 0 ) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1311       vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1312       vertices[vertexIndex + 1].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1313       vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1314       vertices[vertexIndex + 2].normal = outerNormals[( i == 0 ) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1315       vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1316       vertices[vertexIndex + 3].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1317     }
1318     else
1319     {
1320       //'odd' faces are outer ones, and so don't need smoothing.
1321       vertices[vertexIndex    ].position = positions[secondCycleBeginning + i];
1322       vertices[vertexIndex    ].normal = normals[normalIndex];
1323       vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1324       vertices[vertexIndex + 1].normal = normals[normalIndex];
1325       vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1326       vertices[vertexIndex + 2].normal = normals[normalIndex];
1327       vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1328       vertices[vertexIndex + 3].normal = normals[normalIndex];
1329     }
1330   }
1331
1332   //Bottom slope vertices.
1333   for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
1334   {
1335     //Triangle part
1336     vertices[vertexIndex    ].position = positions[thirdCycleBeginning + 2 * i];
1337     vertices[vertexIndex    ].normal = outerNormals[( i == 0 ) ? 4 : i] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1338     vertices[vertexIndex + 1].position = positions[thirdCycleBeginning + 2 * i + 1];
1339     vertices[vertexIndex + 1].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1340     vertices[vertexIndex + 2].position = positions[bottomCycleBeginning + i];
1341     vertices[vertexIndex + 2].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1342
1343     //Rectangle part
1344     if( i == 3 )
1345     {
1346       //End, so loop around.
1347       vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1348       vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1349       vertices[vertexIndex + 4].position = positions[thirdCycleBeginning];
1350       vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1351       vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1352       vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1353       vertices[vertexIndex + 6].position = positions[bottomCycleBeginning];
1354       vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1355     }
1356     else
1357     {
1358       vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1359       vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1360       vertices[vertexIndex + 4].position = positions[thirdCycleBeginning + 2 * i + 2];
1361       vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1362       vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1363       vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1364       vertices[vertexIndex + 6].position = positions[bottomCycleBeginning + i + 1];
1365       vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1366     }
1367   }
1368
1369   //Bottom-most face vertices.
1370   for( int i = 0; i < 4; i++, vertexIndex++ )
1371   {
1372     vertices[vertexIndex].position = positions[ bottomCycleBeginning + i];
1373     vertices[vertexIndex].normal = normals[normalIndex];
1374   }
1375
1376   normalIndex++;
1377 }
1378
1379 void PrimitiveVisual::FormBevelledCubeTriangles( Vector<unsigned short>& indices )
1380 {
1381   int numTriangles = 44; //(Going from top to bottom, that's 2 + 12 + 16 + 12 + 2)
1382   int indiceIndex = 0;  //Track progress through indices.
1383   int vertexIndex = 0;  //Track progress through vertices as they're processed.
1384
1385   indices.Resize( 3 * numTriangles );
1386
1387   //Top face.
1388   indices[indiceIndex    ] = vertexIndex;
1389   indices[indiceIndex + 1] = vertexIndex + 2;
1390   indices[indiceIndex + 2] = vertexIndex + 1;
1391   indices[indiceIndex + 3] = vertexIndex + 0;
1392   indices[indiceIndex + 4] = vertexIndex + 3;
1393   indices[indiceIndex + 5] = vertexIndex + 2;
1394   indiceIndex += 6;
1395   vertexIndex += 4;
1396
1397   //Top slopes.
1398   for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1399   {
1400     //Triangle part.
1401     indices[indiceIndex    ] = vertexIndex;
1402     indices[indiceIndex + 1] = vertexIndex + 2;
1403     indices[indiceIndex + 2] = vertexIndex + 1;
1404
1405     //Rectangle part.
1406     indices[indiceIndex + 3] = vertexIndex + 3;
1407     indices[indiceIndex + 4] = vertexIndex + 4;
1408     indices[indiceIndex + 5] = vertexIndex + 5;
1409     indices[indiceIndex + 6] = vertexIndex + 4;
1410     indices[indiceIndex + 7] = vertexIndex + 6;
1411     indices[indiceIndex + 8] = vertexIndex + 5;
1412   }
1413
1414   //Side faces.
1415   for( int i = 0; i < 8; i++, indiceIndex += 6, vertexIndex += 4 )
1416   {
1417     indices[indiceIndex    ] = vertexIndex;
1418     indices[indiceIndex + 1] = vertexIndex + 1;
1419     indices[indiceIndex + 2] = vertexIndex + 2;
1420     indices[indiceIndex + 3] = vertexIndex + 1;
1421     indices[indiceIndex + 4] = vertexIndex + 3;
1422     indices[indiceIndex + 5] = vertexIndex + 2;
1423   }
1424
1425   //Bottom slopes.
1426   for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1427   {
1428     //Triangle part.
1429     indices[indiceIndex    ] = vertexIndex;
1430     indices[indiceIndex + 1] = vertexIndex + 1;
1431     indices[indiceIndex + 2] = vertexIndex + 2;
1432
1433     //Rectangle part.
1434     indices[indiceIndex + 3] = vertexIndex + 3;
1435     indices[indiceIndex + 4] = vertexIndex + 4;
1436     indices[indiceIndex + 5] = vertexIndex + 5;
1437     indices[indiceIndex + 6] = vertexIndex + 4;
1438     indices[indiceIndex + 7] = vertexIndex + 6;
1439     indices[indiceIndex + 8] = vertexIndex + 5;
1440   }
1441
1442   //Bottom face.
1443   indices[indiceIndex    ] = vertexIndex;
1444   indices[indiceIndex + 1] = vertexIndex + 1;
1445   indices[indiceIndex + 2] = vertexIndex + 2;
1446   indices[indiceIndex + 3] = vertexIndex + 0;
1447   indices[indiceIndex + 4] = vertexIndex + 2;
1448   indices[indiceIndex + 5] = vertexIndex + 3;
1449   indiceIndex += 6;
1450   vertexIndex += 4;
1451 }
1452
1453 } // namespace Internal
1454
1455 } // namespace Toolkit
1456
1457 } // namespace Dali