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