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