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