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