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