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