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