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