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