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