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