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