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