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