2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "primitive-visual.h"
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/common/stage.h>
24 #include <dali/public-api/common/constants.h>
27 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
28 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
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.
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
62 const char * const RENDERER_TYPE_VALUE( "PRIMITIVE" );
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" );
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" );
80 const char * const POSITION( "aPosition");
81 const char * const NORMAL( "aNormal" );
82 const char * const INDICES( "aIndices" );
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
102 float xRatio = uSize.x / uObjectDimensions.x;\n
103 float yRatio = uSize.y / uObjectDimensions.y;\n
104 float scaleFactor = min( xRatio, yRatio );\n
106 vec4 normalisedVertexPosition = vec4( aPosition * scaleFactor, 1.0 );\n
107 vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
108 vertexPosition = uMvpMatrix * vertexPosition;\n
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
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
118 float lightDiffuse = max( dot( vectorToLight, normal ), 0.0 );\n
119 vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
121 gl_Position = vertexPosition;\n
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
133 gl_FragColor = vec4( vIllumination.rgb * uColor.rgb, uColor.a );\n
139 PrimitiveVisual::PrimitiveVisual( VisualFactoryCache& factoryCache )
140 : Visual::Base( 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 )
155 PrimitiveVisual::~PrimitiveVisual()
159 void PrimitiveVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap )
161 //Find out which shape to renderer.
162 Property::Value* primitiveType = propertyMap.Find( PRIMITIVE_SHAPE );
165 if( primitiveType->Get( mShape ) )
167 //Set property type as an enum.
168 if( mShape == SPHERE_LABEL )
170 mPrimitiveType = SPHERE;
172 else if( mShape == CONE_LABEL )
174 mPrimitiveType = CONE;
176 else if( mShape == CONICAL_FRUSTRUM_LABEL )
178 mPrimitiveType = CONICAL_FRUSTRUM;
180 else if( mShape == CYLINDER_LABEL )
182 mPrimitiveType = CYLINDER;
184 else if( mShape == CUBE_LABEL )
186 mPrimitiveType = CUBE;
188 else if( mShape == OCTAHEDRON_LABEL )
190 mPrimitiveType = OCTAHEDRON;
192 else if( mShape == BEVELLED_CUBE_LABEL )
194 mPrimitiveType = BEVELLED_CUBE;
198 DALI_LOG_ERROR( "No known shape in PrimitiveVisual.\n" );
203 DALI_LOG_ERROR( "Invalid type for shape in PrimitiveVisual.\n" );
208 DALI_LOG_ERROR( "Fail to provide shape to the PrimitiveVisual object.\n" );
211 //Read in other potential properties.
213 Property::Value* color = propertyMap.Find( SHAPE_COLOR );
214 if( color && !color->Get( mColor ) )
216 DALI_LOG_ERROR( "Invalid type for color in PrimitiveVisual.\n" );
219 Property::Value* slices = propertyMap.Find( SLICES );
222 if( slices->Get( mSlices ) )
225 if( mSlices > MAX_PARTITIONS )
227 mSlices = MAX_PARTITIONS;
229 else if ( mSlices < MIN_SLICES )
231 mSlices = MIN_SLICES;
236 DALI_LOG_ERROR( "Invalid type for slices in PrimitiveVisual.\n" );
240 Property::Value* stacks = propertyMap.Find( STACKS );
243 if( stacks->Get( mStacks ) )
246 if( mStacks > MAX_PARTITIONS )
248 mStacks = MAX_PARTITIONS;
250 else if ( mStacks < MIN_STACKS )
252 mStacks = MIN_STACKS;
257 DALI_LOG_ERROR( "Invalid type for stacks in PrimitiveVisual.\n" );
261 Property::Value* scaleTop = propertyMap.Find( SCALE_TOP_RADIUS );
262 if( scaleTop && !scaleTop->Get( mScaleTopRadius ) )
264 DALI_LOG_ERROR( "Invalid type for scale top radius in PrimitiveVisual.\n" );
267 Property::Value* scaleBottom = propertyMap.Find( SCALE_BOTTOM_RADIUS );
268 if( scaleBottom && !scaleBottom->Get( mScaleBottomRadius ) )
270 DALI_LOG_ERROR( "Invalid type for scale bottom radius in PrimitiveVisual.\n" );
273 Property::Value* scaleHeight = propertyMap.Find( SCALE_HEIGHT );
274 if( scaleHeight && !scaleHeight->Get( mScaleHeight ) )
276 DALI_LOG_ERROR( "Invalid type for scale height in PrimitiveVisual.\n" );
279 Property::Value* scaleRadius = propertyMap.Find( SCALE_RADIUS );
280 if( scaleRadius && !scaleRadius->Get( mScaleRadius ) )
282 DALI_LOG_ERROR( "Invalid type for scale radius in PrimitiveVisual.\n" );
285 Property::Value* dimensions = propertyMap.Find( SCALE_DIMENSIONS );
288 if( dimensions->Get( mScaleDimensions ) )
290 //If any dimension is invalid, set it to a sensible default.
291 if( mScaleDimensions.x <= 0.0 )
293 mScaleDimensions.x = 1.0;
295 if( mScaleDimensions.y <= 0.0 )
297 mScaleDimensions.y = 1.0;
299 if( mScaleDimensions.z <= 0.0 )
301 mScaleDimensions.z = 1.0;
306 DALI_LOG_ERROR( "Invalid type for scale dimensions in PrimitiveVisual.\n" );
310 Property::Value* bevel = propertyMap.Find( BEVEL_PERCENTAGE );
313 if( bevel->Get( mBevelPercentage ) )
316 if( mBevelPercentage < MIN_BEVEL_PERCENTAGE )
318 mBevelPercentage = MIN_BEVEL_PERCENTAGE;
320 else if( mBevelPercentage > MAX_BEVEL_PERCENTAGE )
322 mBevelPercentage = MAX_BEVEL_PERCENTAGE;
327 DALI_LOG_ERROR( "Invalid type for bevel percentage in PrimitiveVisual.\n" );
331 Property::Value* smoothness = propertyMap.Find( BEVEL_SMOOTHNESS );
334 if( smoothness->Get( mBevelSmoothness ) )
337 if( mBevelSmoothness < MIN_SMOOTHNESS )
339 mBevelSmoothness = MIN_SMOOTHNESS;
341 else if( mBevelSmoothness > MAX_SMOOTHNESS )
343 mBevelSmoothness = MAX_SMOOTHNESS;
348 DALI_LOG_ERROR( "Invalid type for bevel smoothness in PrimitiveVisual.\n" );
352 //Read in light position.
353 Property::Value* lightPosition = propertyMap.Find( LIGHT_POSITION_UNIFORM_NAME );
356 if( !lightPosition->Get( mLightPosition ) )
358 DALI_LOG_ERROR( "Invalid value passed for light position in MeshVisual object.\n" );
359 mLightPosition = Vector3::ZERO;
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();
368 mLightPosition = Vector3( stage.GetSize().width / 2, stage.GetSize().height / 2, stage.GetSize().width * 5 );
372 void PrimitiveVisual::SetSize( const Vector2& size )
374 Visual::Base::SetSize( size );
376 // ToDo: renderer responds to the size change
379 void PrimitiveVisual::SetClipRect( const Rect<int>& clipRect )
381 Visual::Base::SetClipRect( clipRect );
383 //ToDo: renderer responds to the clipRect change
386 void PrimitiveVisual::SetOffset( const Vector2& offset )
388 //ToDo: renderer applies the offset
391 void PrimitiveVisual::DoSetOnStage( Actor& actor )
393 InitializeRenderer();
396 void PrimitiveVisual::DoCreatePropertyMap( Property::Map& map ) const
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 );
414 void PrimitiveVisual::InitializeRenderer()
426 mImpl->mRenderer = Renderer::New( mGeometry, mShader );
429 void PrimitiveVisual::UpdateShaderUniforms()
431 Stage stage = Stage::GetCurrent();
432 float width = stage.GetSize().width;
433 float height = stage.GetSize().height;
435 //Flip model to account for DALi starting with (0, 0) at the top left.
437 scaleMatrix.SetIdentityAndScale( Vector3( 1.0, -1.0, 1.0 ) );
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 );
446 void PrimitiveVisual::CreateShader()
448 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
449 UpdateShaderUniforms();
452 void PrimitiveVisual::CreateGeometry()
454 Dali::Vector<Vertex> vertices;
455 Dali::Vector<unsigned short> indices;
457 switch( mPrimitiveType )
461 CreateSphere( vertices, indices, mSlices, mStacks );
466 //Create a conic with zero top radius.
467 CreateConic( vertices, indices, 0, mScaleBottomRadius, mScaleHeight, mSlices );
470 case CONICAL_FRUSTRUM:
472 CreateConic( vertices, indices, mScaleTopRadius, mScaleBottomRadius, mScaleHeight, mSlices );
477 //Create a conic with equal radii on the top and bottom.
478 CreateConic( vertices, indices, mScaleRadius, mScaleRadius, mScaleHeight, mSlices );
483 //Create a cube by creating a bevelled cube with minimum bevel.
484 CreateBevelledCube( vertices, indices, mScaleDimensions, 0.0, 0.0 );
489 //Create an octahedron by creating a bevelled cube with maximum bevel.
490 CreateBevelledCube( vertices, indices, mScaleDimensions, 1.0, mBevelSmoothness );
495 CreateBevelledCube( vertices, indices, mScaleDimensions, mBevelPercentage, mBevelSmoothness );
500 mGeometry = Geometry::New();
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() );
509 mGeometry.AddVertexBuffer( surfaceVertices );
511 //Indices for triangle formulation
512 mGeometry.SetIndexBuffer( &indices[0], indices.Size() );
515 void PrimitiveVisual::CreateSphere( Vector<Vertex>& vertices, Vector<unsigned short>& indices, int slices, int stacks )
517 ComputeSphereVertices( vertices, slices, stacks );
518 FormSphereTriangles( indices, slices, stacks );
520 mObjectDimensions = Vector3::ONE;
523 void PrimitiveVisual::CreateConic( Vector<Vertex>& vertices, Vector<unsigned short>& indices, float scaleTopRadius,
524 float scaleBottomRadius, float scaleHeight, int slices )
526 ComputeConicVertices( vertices, scaleTopRadius, scaleBottomRadius, scaleHeight, slices );
527 FormConicTriangles( indices, scaleTopRadius, scaleBottomRadius, slices );
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 );
534 mObjectDimensions = Vector3( xDimension / largestDimension, yDimension / largestDimension,
535 xDimension / largestDimension );
538 void PrimitiveVisual::CreateBevelledCube( Vector<Vertex>& vertices, Vector<unsigned short>& indices,
539 Vector3 dimensions, float bevelPercentage, float bevelSmoothness )
541 dimensions.Normalize();
543 if( bevelPercentage <= MIN_BEVEL_PERCENTAGE ) //No bevel, form a cube.
545 ComputeCubeVertices( vertices, dimensions );
546 FormCubeTriangles( indices );
548 else if( bevelPercentage >= MAX_BEVEL_PERCENTAGE ) //Max bevel, form an octahedron.
550 ComputeOctahedronVertices( vertices, dimensions, bevelSmoothness );
551 FormOctahedronTriangles( indices );
553 else //In between, form a bevelled cube.
555 ComputeBevelledCubeVertices( vertices, dimensions, bevelPercentage, bevelSmoothness );
556 FormBevelledCubeTriangles( indices );
559 mObjectDimensions = dimensions;
562 void PrimitiveVisual::ComputeCircleTables( Vector<float>& sinTable, Vector<float>& cosTable, int divisions,
570 const float angleDivision = ( halfCircle ? 1.0f : 2.0f ) * Dali::Math::PI / ( float ) divisions;
572 sinTable.Resize( divisions );
573 cosTable.Resize( divisions );
575 for( int i = 0; i < divisions; i++ )
577 sinTable[i] = sin( angleDivision * i );
578 cosTable[i] = cos( angleDivision * i );
582 void PrimitiveVisual::ComputeSphereVertices( Vector<Vertex>& vertices, int slices, int stacks )
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;
590 ComputeCircleTables( sinTable1, cosTable1, slices, false );
591 ComputeCircleTables( sinTable2, cosTable2, stacks, true );
593 int numVertices = slices * ( stacks - 1 ) + 2;
594 vertices.Resize( numVertices );
596 int vertexIndex = 0; //Track progress through vertices.
602 vertices[vertexIndex].position = Vector3( 0.0, 0.5, 0.0 );
603 vertices[vertexIndex].normal = Vector3( 0.0, 1.0, 0.0 );
607 for( int i = 1; i < stacks; i++ )
609 for( int j = 0; j < slices; j++, vertexIndex++ )
611 x = cosTable1[j] * sinTable2[i];
613 z = sinTable1[j] * sinTable2[i];
615 vertices[vertexIndex].position = Vector3( x / 2.0f, y / 2.0f, z / 2.0f );
616 vertices[vertexIndex].normal = Vector3( x, y, z );
621 vertices[vertexIndex].position = Vector3( 0.0, -0.5, 0.0 );
622 vertices[vertexIndex].normal = Vector3( 0.0, -1.0, 0.0 );
625 void PrimitiveVisual::FormSphereTriangles( Vector<unsigned short>& indices, int slices, int stacks )
629 //Set indices to placeholder "error" values.
630 //This will display nothing, which is the expected behaviour for this edge case.
635 int numTriangles = 2 * slices * ( stacks - 1 );
637 indices.Resize( 3 * numTriangles );
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;
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 )
646 indices[indiceIndex] = 0;
647 indices[indiceIndex + 1] = i;
650 //End, so loop around.
651 indices[indiceIndex + 2] = 1;
655 indices[indiceIndex + 2] = i + 1;
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 )
662 for( int j = 0; j < slices; j++, indiceIndex += 6 )
664 if( j == slices - 1 )
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;
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;
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 )
689 indices[indiceIndex] = previousCycleBeginning + slices;
690 indices[indiceIndex + 1] = previousCycleBeginning + i;
691 if( i == slices - 1 )
693 //End, so loop around.
694 indices[indiceIndex + 2] = previousCycleBeginning;
698 indices[indiceIndex + 2] = previousCycleBeginning + i + 1;
703 void PrimitiveVisual::ComputeConicVertices( Vector<Vertex>& vertices, float scaleTopRadius,
704 float scaleBottomRadius, float scaleHeight, int slices )
706 int vertexIndex = 0; //Track progress through vertices.
707 Vector<float> sinTable;
708 Vector<float> cosTable;
710 ComputeCircleTables( sinTable, cosTable, slices, false );
712 int numVertices = 2; //Always will have one at the top and one at the bottom.
714 //Add vertices for each circle. Need two per point for different face normals.
715 if( scaleTopRadius > 0.0 )
717 numVertices += 2 * slices;
719 if( scaleBottomRadius > 0.0 )
721 numVertices += 2 * slices;
724 vertices.Resize( numVertices );
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;
732 //Dimensions for vertex coordinates. Y is constant, and so can be initialised now.
734 float y = scaleHeight / biggestObjectDimension / 2.0f;
738 vertices[0].position = Vector3( 0, y, 0 );
739 vertices[0].normal = Vector3( 0, 1, 0 );
743 if( scaleTopRadius > 0.0 )
745 //Loop around the circle.
746 for( int i = 0; i < slices; i++, vertexIndex++ )
748 x = sinTable[i] * scaleTopRadius;
749 z = cosTable[i] * scaleTopRadius;
751 //Upward-facing normal.
752 vertices[vertexIndex].position = Vector3( x, y, z );
753 vertices[vertexIndex].normal = Vector3( 0, 1, 0 );
755 //Outward-facing normal.
756 vertices[vertexIndex + slices].position = Vector3( x, y, z );
757 vertices[vertexIndex + slices].normal = Vector3( x, 0, z );
760 vertexIndex += slices;
764 if( scaleBottomRadius > 0.0 )
766 //Loop around the circle.
767 for( int i = 0; i < slices; i++, vertexIndex++ )
769 x = sinTable[i] * scaleBottomRadius;
770 z = cosTable[i] * scaleBottomRadius;
772 //Outward-facing normal.
773 vertices[vertexIndex].position = Vector3( x, -y, z );
774 vertices[vertexIndex].normal = Vector3( x, 0, z );
776 //Downward-facing normal.
777 vertices[vertexIndex + slices].position = Vector3( x, -y, z );
778 vertices[vertexIndex + slices].normal = Vector3( 0, -1, 0 );
781 vertexIndex += slices;
785 vertices[vertexIndex].position = Vector3( 0, -y, 0 );
786 vertices[vertexIndex].normal = Vector3( 0, -1, 0 );
790 void PrimitiveVisual::FormConicTriangles( Vector<unsigned short>& indices, float scaleTopRadius,
791 float scaleBottomRadius, int slices )
793 int indiceIndex = 0; //Track progress through indices.
794 int numTriangles = 0;
795 bool coneTop = scaleTopRadius <= 0.0;
796 bool coneBottom = scaleBottomRadius <= 0.0;
798 if( coneTop && coneBottom )
800 //Set indices to placeholder "error" values.
801 //This will display nothing, which is the expected behaviour for this edge case.
808 numTriangles += 2 * slices;
812 numTriangles += 2 * slices;
815 indices.Resize( 3 * numTriangles );
817 //Switch on the type of conic we have.
818 if( !coneTop && !coneBottom )
820 //Top circle. Start at index of first outer point and go around.
821 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
823 indices[indiceIndex] = 0;
824 indices[indiceIndex + 1] = i;
827 //End, so loop around.
828 indices[indiceIndex + 2] = 1;
832 indices[indiceIndex + 2] = i + 1;
836 int topCycleBeginning = slices + 1;
837 int bottomCycleBeginning = topCycleBeginning + slices;
840 for( int i = 0; i < slices; i++, indiceIndex += 6 )
842 if( i == slices - 1 )
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;
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;
863 int bottomFaceCycleBeginning = bottomCycleBeginning + slices;
866 for( int i = 0; i < slices; i++, indiceIndex += 3 )
868 indices[indiceIndex] = bottomFaceCycleBeginning;
869 indices[indiceIndex + 1] = bottomFaceCycleBeginning + i;
870 if( i == slices - 1 )
872 //End, so loop around.
873 indices[indiceIndex + 2] = bottomFaceCycleBeginning;
877 indices[indiceIndex + 2] = bottomFaceCycleBeginning + i + 1;
881 else if( !coneTop || !coneBottom )
883 //Top circle/edges. Start at index of first outer point and go around.
884 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
886 indices[indiceIndex] = 0;
887 indices[indiceIndex + 1] = i;
890 //End, so loop around.
891 indices[indiceIndex + 2] = 1;
895 indices[indiceIndex + 2] = i + 1;
899 //Bottom circle/edges. Start at index of first outer point and go around.
900 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
902 indices[indiceIndex] = 2 * slices + 1;
903 indices[indiceIndex + 1] = slices + i;
906 //End, so loop around.
907 indices[indiceIndex + 2] = slices + 1;
911 indices[indiceIndex + 2] = slices + i + 1;
917 void PrimitiveVisual::ComputeCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions )
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;
925 vertices.Resize( numVertices );
927 Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
929 Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
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 );
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 );
948 //Top face, upward normals.
949 for( int i = 0; i < 4; i++, vertexIndex++ )
951 vertices[vertexIndex].position = positions[i];
952 vertices[vertexIndex].normal = normals[0];
955 //Top face, outward normals.
956 for( int i = 0; i < 4; i++, vertexIndex += 2 )
958 vertices[vertexIndex].position = positions[i];
959 vertices[vertexIndex].normal = normals[i + 1];
963 //End, so loop around.
964 vertices[vertexIndex + 1].position = positions[0];
968 vertices[vertexIndex + 1].position = positions[i + 1];
970 vertices[vertexIndex + 1].normal = normals[i + 1];
973 //Bottom face, outward normals.
974 for( int i = 0; i < 4; i++, vertexIndex += 2 )
976 vertices[vertexIndex].position = positions[i + 4];
977 vertices[vertexIndex].normal = normals[i + 1];
981 //End, so loop around.
982 vertices[vertexIndex + 1].position = positions[4];
986 vertices[vertexIndex + 1].position = positions[i + 5];
988 vertices[vertexIndex + 1].normal = normals[i + 1];
991 //Bottom face, downward normals.
992 for( int i = 0; i < 4; i++, vertexIndex++ )
994 vertices[vertexIndex].position = positions[i + 4];
995 vertices[vertexIndex].normal = normals[5];
1000 void PrimitiveVisual::FormCubeTriangles( Vector<unsigned short>& indices )
1002 int numTriangles = 12;
1003 int triangleIndex = 0; //Track progress through indices.
1005 indices.Resize( 3 * numTriangles );
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;
1016 int topFaceStart = 4;
1017 int bottomFaceStart = 12;
1020 for( int i = 0; i < 8; i += 2, triangleIndex += 6 )
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;
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;
1039 void PrimitiveVisual::ComputeOctahedronVertices( Vector<Vertex>& vertices, Vector3 dimensions, float smoothness )
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;
1047 vertices.Resize( numVertices );
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.
1053 Vector<Vector3> outerNormals; //Holds normals that point outwards at each vertex.
1054 outerNormals.Resize( 6 );
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 );
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 );
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 );
1079 //Loop through top faces.
1080 for( int i = 0; i < 4; i++, vertexIndex += 3 )
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);
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);
1103 //Loop through bottom faces.
1104 for( int i = 0; i < 4; i++, vertexIndex += 3 )
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);
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);
1128 void PrimitiveVisual::FormOctahedronTriangles( Vector<unsigned short>& indices )
1130 int numTriangles = 8;
1131 int numIndices = numTriangles * 3;
1133 indices.Resize( numIndices );
1135 for( unsigned short i = 0; i < numIndices; i++ )
1141 void PrimitiveVisual::ComputeBevelledCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions,
1142 float bevelPercentage, float bevelSmoothness )
1144 int numPositions = 24;
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.
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;
1155 float outerX = 0.5 * dimensions.x;
1156 float outerY = 0.5 * dimensions.y;
1157 float outerZ = 0.5 * dimensions.z;
1159 float bevelX = outerX - ( 0.5 * minDimension - bevelAmount );
1160 float bevelY = outerY - ( 0.5 * minDimension - bevelAmount );
1161 float bevelZ = outerZ - ( 0.5 * minDimension - bevelAmount );
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 );
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 );
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 );
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 );
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 );
1204 normals[0 ] = Vector3( 0, 1, 0 );
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 );
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 );
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 );
1236 //Bottom face normal.
1237 normals[25] = Vector3( 0, -1, 0 );
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 );
1247 //Topmost face vertices.
1248 for( int i = 0; i < 4; i++, vertexIndex++ )
1250 vertices[vertexIndex].position = positions[i];
1251 vertices[vertexIndex].normal = normals[normalIndex];
1256 //Top slope vertices.
1257 for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
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);
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);
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);
1293 int secondCycleBeginning = 4;
1294 int thirdCycleBeginning = secondCycleBeginning + 8;
1295 int bottomCycleBeginning = thirdCycleBeginning + 8;
1298 for( int i = 0; i < 8; i++, vertexIndex += 4, normalIndex++ )
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];
1312 else if( (i % 2) == 0 )
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);
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];
1338 //Bottom slope vertices.
1339 for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
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);
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);
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);
1375 //Bottom-most face vertices.
1376 for( int i = 0; i < 4; i++, vertexIndex++ )
1378 vertices[vertexIndex].position = positions[ bottomCycleBeginning + i];
1379 vertices[vertexIndex].normal = normals[normalIndex];
1385 void PrimitiveVisual::FormBevelledCubeTriangles( Vector<unsigned short>& indices )
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.
1391 indices.Resize( 3 * numTriangles );
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;
1404 for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1407 indices[indiceIndex ] = vertexIndex;
1408 indices[indiceIndex + 1] = vertexIndex + 1;
1409 indices[indiceIndex + 2] = vertexIndex + 2;
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;
1421 for( int i = 0; i < 8; i++, indiceIndex += 6, vertexIndex += 4 )
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;
1432 for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1435 indices[indiceIndex ] = vertexIndex;
1436 indices[indiceIndex + 1] = vertexIndex + 1;
1437 indices[indiceIndex + 2] = vertexIndex + 2;
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;
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;
1459 } // namespace Internal
1461 } // namespace Toolkit