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