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