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