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