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