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