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