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 );
427 mImpl->mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
430 void PrimitiveVisual::UpdateShaderUniforms()
432 Stage stage = Stage::GetCurrent();
433 float width = stage.GetSize().width;
434 float height = stage.GetSize().height;
436 //Flip model to account for DALi starting with (0, 0) at the top left.
438 scaleMatrix.SetIdentityAndScale( Vector3( 1.0, -1.0, 1.0 ) );
440 mShader.RegisterProperty( STAGE_OFFSET_UNIFORM_NAME, Vector2( width, height ) / 2.0f );
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 );
447 void PrimitiveVisual::CreateShader()
449 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
450 UpdateShaderUniforms();
453 void PrimitiveVisual::CreateGeometry()
455 Dali::Vector<Vertex> vertices;
456 Dali::Vector<unsigned short> indices;
458 switch( mPrimitiveType )
462 CreateSphere( vertices, indices, mSlices, mStacks );
467 //Create a conic with zero top radius.
468 CreateConic( vertices, indices, 0, mScaleBottomRadius, mScaleHeight, mSlices );
471 case CONICAL_FRUSTRUM:
473 CreateConic( vertices, indices, mScaleTopRadius, mScaleBottomRadius, mScaleHeight, mSlices );
478 //Create a conic with equal radii on the top and bottom.
479 CreateConic( vertices, indices, mScaleRadius, mScaleRadius, mScaleHeight, mSlices );
484 //Create a cube by creating a bevelled cube with minimum bevel.
485 CreateBevelledCube( vertices, indices, mScaleDimensions, 0.0, 0.0 );
490 //Create an octahedron by creating a bevelled cube with maximum bevel.
491 CreateBevelledCube( vertices, indices, mScaleDimensions, 1.0, mBevelSmoothness );
496 CreateBevelledCube( vertices, indices, mScaleDimensions, mBevelPercentage, mBevelSmoothness );
501 mGeometry = Geometry::New();
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() );
510 mGeometry.AddVertexBuffer( surfaceVertices );
512 //Indices for triangle formulation
513 mGeometry.SetIndexBuffer( &indices[0], indices.Size() );
516 void PrimitiveVisual::CreateSphere( Vector<Vertex>& vertices, Vector<unsigned short>& indices, int slices, int stacks )
518 ComputeSphereVertices( vertices, slices, stacks );
519 FormSphereTriangles( indices, slices, stacks );
521 mObjectDimensions = Vector3::ONE;
524 void PrimitiveVisual::CreateConic( Vector<Vertex>& vertices, Vector<unsigned short>& indices, float scaleTopRadius,
525 float scaleBottomRadius, float scaleHeight, int slices )
527 ComputeConicVertices( vertices, scaleTopRadius, scaleBottomRadius, scaleHeight, slices );
528 FormConicTriangles( indices, scaleTopRadius, scaleBottomRadius, slices );
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 );
535 mObjectDimensions = Vector3( xDimension / largestDimension, yDimension / largestDimension,
536 xDimension / largestDimension );
539 void PrimitiveVisual::CreateBevelledCube( Vector<Vertex>& vertices, Vector<unsigned short>& indices,
540 Vector3 dimensions, float bevelPercentage, float bevelSmoothness )
542 dimensions.Normalize();
544 if( bevelPercentage <= MIN_BEVEL_PERCENTAGE ) //No bevel, form a cube.
546 ComputeCubeVertices( vertices, dimensions );
547 FormCubeTriangles( indices );
549 else if( bevelPercentage >= MAX_BEVEL_PERCENTAGE ) //Max bevel, form an octahedron.
551 ComputeOctahedronVertices( vertices, dimensions, bevelSmoothness );
552 FormOctahedronTriangles( indices );
554 else //In between, form a bevelled cube.
556 ComputeBevelledCubeVertices( vertices, dimensions, bevelPercentage, bevelSmoothness );
557 FormBevelledCubeTriangles( indices );
560 mObjectDimensions = dimensions;
563 void PrimitiveVisual::ComputeCircleTables( Vector<float>& sinTable, Vector<float>& cosTable, int divisions,
571 const float angleDivision = ( halfCircle ? 1.0f : 2.0f ) * Dali::Math::PI / ( float ) divisions;
573 sinTable.Resize( divisions );
574 cosTable.Resize( divisions );
576 for( int i = 0; i < divisions; i++ )
578 sinTable[i] = sin( angleDivision * i );
579 cosTable[i] = cos( angleDivision * i );
583 void PrimitiveVisual::ComputeSphereVertices( Vector<Vertex>& vertices, int slices, int stacks )
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;
591 ComputeCircleTables( sinTable1, cosTable1, slices, false );
592 ComputeCircleTables( sinTable2, cosTable2, stacks, true );
594 int numVertices = slices * ( stacks - 1 ) + 2;
595 vertices.Resize( numVertices );
597 int vertexIndex = 0; //Track progress through vertices.
603 vertices[vertexIndex].position = Vector3( 0.0, 0.5, 0.0 );
604 vertices[vertexIndex].normal = Vector3( 0.0, 1.0, 0.0 );
608 for( int i = 1; i < stacks; i++ )
610 for( int j = 0; j < slices; j++, vertexIndex++ )
612 x = cosTable1[j] * sinTable2[i];
614 z = sinTable1[j] * sinTable2[i];
616 vertices[vertexIndex].position = Vector3( x / 2.0f, y / 2.0f, z / 2.0f );
617 vertices[vertexIndex].normal = Vector3( x, y, z );
622 vertices[vertexIndex].position = Vector3( 0.0, -0.5, 0.0 );
623 vertices[vertexIndex].normal = Vector3( 0.0, -1.0, 0.0 );
626 void PrimitiveVisual::FormSphereTriangles( Vector<unsigned short>& indices, int slices, int stacks )
630 //Set indices to placeholder "error" values.
631 //This will display nothing, which is the expected behaviour for this edge case.
636 int numTriangles = 2 * slices * ( stacks - 1 );
638 indices.Resize( 3 * numTriangles );
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;
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 )
647 indices[indiceIndex] = 0;
650 //End, so loop around.
651 indices[indiceIndex + 1] = 1;
655 indices[indiceIndex + 1] = i + 1;
657 indices[indiceIndex + 2] = i;
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 )
663 for( int j = 0; j < slices; j++, indiceIndex += 6 )
665 if( j == slices - 1 )
667 //End, so loop around.
668 indices[indiceIndex] = previousCycleBeginning + j;
669 indices[indiceIndex + 1] = previousCycleBeginning;
670 indices[indiceIndex + 2] = currentCycleBeginning + j;
671 indices[indiceIndex + 3] = currentCycleBeginning + j;
672 indices[indiceIndex + 4] = previousCycleBeginning;
673 indices[indiceIndex + 5] = currentCycleBeginning;
677 indices[indiceIndex] = previousCycleBeginning + j;
678 indices[indiceIndex + 1] = previousCycleBeginning + 1 + j;
679 indices[indiceIndex + 2] = currentCycleBeginning + j;
680 indices[indiceIndex + 3] = currentCycleBeginning + j;
681 indices[indiceIndex + 4] = previousCycleBeginning + 1 + j;
682 indices[indiceIndex + 5] = currentCycleBeginning + 1 + j;
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 )
690 indices[indiceIndex] = previousCycleBeginning + slices;
691 indices[indiceIndex + 1] = previousCycleBeginning + i;
692 if( i == slices - 1 )
694 //End, so loop around.
695 indices[indiceIndex + 2] = previousCycleBeginning;
699 indices[indiceIndex + 2] = previousCycleBeginning + i + 1;
704 void PrimitiveVisual::ComputeConicVertices( Vector<Vertex>& vertices, float scaleTopRadius,
705 float scaleBottomRadius, float scaleHeight, int slices )
707 int vertexIndex = 0; //Track progress through vertices.
708 Vector<float> sinTable;
709 Vector<float> cosTable;
711 ComputeCircleTables( sinTable, cosTable, slices, false );
713 int numVertices = 2; //Always will have one at the top and one at the bottom.
715 //Add vertices for each circle. Need two per point for different face normals.
716 if( scaleTopRadius > 0.0 )
718 numVertices += 2 * slices;
720 if( scaleBottomRadius > 0.0 )
722 numVertices += 2 * slices;
725 vertices.Resize( numVertices );
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;
733 //Dimensions for vertex coordinates. Y is constant, and so can be initialised now.
735 float y = scaleHeight / biggestObjectDimension / 2.0f;
739 vertices[0].position = Vector3( 0, y, 0 );
740 vertices[0].normal = Vector3( 0, 1, 0 );
744 if( scaleTopRadius > 0.0 )
746 //Loop around the circle.
747 for( int i = 0; i < slices; i++, vertexIndex++ )
749 x = sinTable[i] * scaleTopRadius;
750 z = cosTable[i] * scaleTopRadius;
752 //Upward-facing normal.
753 vertices[vertexIndex].position = Vector3( x, y, z );
754 vertices[vertexIndex].normal = Vector3( 0, 1, 0 );
756 //Outward-facing normal.
757 vertices[vertexIndex + slices].position = Vector3( x, y, z );
758 vertices[vertexIndex + slices].normal = Vector3( x, 0, z );
761 vertexIndex += slices;
765 if( scaleBottomRadius > 0.0 )
767 //Loop around the circle.
768 for( int i = 0; i < slices; i++, vertexIndex++ )
770 x = sinTable[i] * scaleBottomRadius;
771 z = cosTable[i] * scaleBottomRadius;
773 //Outward-facing normal.
774 vertices[vertexIndex].position = Vector3( x, -y, z );
775 vertices[vertexIndex].normal = Vector3( x, 0, z );
777 //Downward-facing normal.
778 vertices[vertexIndex + slices].position = Vector3( x, -y, z );
779 vertices[vertexIndex + slices].normal = Vector3( 0, -1, 0 );
782 vertexIndex += slices;
786 vertices[vertexIndex].position = Vector3( 0, -y, 0 );
787 vertices[vertexIndex].normal = Vector3( 0, -1, 0 );
791 void PrimitiveVisual::FormConicTriangles( Vector<unsigned short>& indices, float scaleTopRadius,
792 float scaleBottomRadius, int slices )
794 int indiceIndex = 0; //Track progress through indices.
795 int numTriangles = 0;
796 bool coneTop = scaleTopRadius <= 0.0;
797 bool coneBottom = scaleBottomRadius <= 0.0;
799 if( coneTop && coneBottom )
801 //Set indices to placeholder "error" values.
802 //This will display nothing, which is the expected behaviour for this edge case.
809 numTriangles += 2 * slices;
813 numTriangles += 2 * slices;
816 indices.Resize( 3 * numTriangles );
818 //Switch on the type of conic we have.
819 if( !coneTop && !coneBottom )
821 //Top circle. Start at index of first outer point and go around.
822 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
824 indices[indiceIndex] = 0;
825 indices[indiceIndex + 1] = i;
828 //End, so loop around.
829 indices[indiceIndex + 2] = 1;
833 indices[indiceIndex + 2] = i + 1;
837 int topCycleBeginning = slices + 1;
838 int bottomCycleBeginning = topCycleBeginning + slices;
841 for( int i = 0; i < slices; i++, indiceIndex += 6 )
843 if( i == slices - 1 )
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;
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;
864 int bottomFaceCycleBeginning = bottomCycleBeginning + slices;
867 for( int i = 0; i < slices; i++, indiceIndex += 3 )
869 indices[indiceIndex] = bottomFaceCycleBeginning;
870 if( i == slices - 1 )
872 //End, so loop around.
873 indices[indiceIndex + 1] = bottomFaceCycleBeginning;
877 indices[indiceIndex + 1] = bottomFaceCycleBeginning + i + 1;
879 indices[indiceIndex + 2] = bottomFaceCycleBeginning + i;
882 else if( !coneTop || !coneBottom )
884 //Top circle/edges. Start at index of first outer point and go around.
885 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
887 indices[indiceIndex] = 0;
888 indices[indiceIndex + 1] = i;
891 //End, so loop around.
892 indices[indiceIndex + 2] = 1;
896 indices[indiceIndex + 2] = i + 1;
900 //Bottom circle/edges. Start at index of first outer point and go around.
901 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
903 indices[indiceIndex] = 2 * slices + 1;
906 //End, so loop around.
907 indices[indiceIndex + 1] = slices + 1;
911 indices[indiceIndex + 1] = slices + i + 1;
913 indices[indiceIndex + 2] = slices + i;
918 void PrimitiveVisual::ComputeCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions )
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;
926 vertices.Resize( numVertices );
928 Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
930 Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
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 );
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 );
949 //Top face, upward normals.
950 for( int i = 0; i < 4; i++, vertexIndex++ )
952 vertices[vertexIndex].position = positions[i];
953 vertices[vertexIndex].normal = normals[0];
956 //Top face, outward normals.
957 for( int i = 0; i < 4; i++, vertexIndex += 2 )
959 vertices[vertexIndex].position = positions[i];
960 vertices[vertexIndex].normal = normals[i + 1];
964 //End, so loop around.
965 vertices[vertexIndex + 1].position = positions[0];
969 vertices[vertexIndex + 1].position = positions[i + 1];
971 vertices[vertexIndex + 1].normal = normals[i + 1];
974 //Bottom face, outward normals.
975 for( int i = 0; i < 4; i++, vertexIndex += 2 )
977 vertices[vertexIndex].position = positions[i + 4];
978 vertices[vertexIndex].normal = normals[i + 1];
982 //End, so loop around.
983 vertices[vertexIndex + 1].position = positions[4];
987 vertices[vertexIndex + 1].position = positions[i + 5];
989 vertices[vertexIndex + 1].normal = normals[i + 1];
992 //Bottom face, downward normals.
993 for( int i = 0; i < 4; i++, vertexIndex++ )
995 vertices[vertexIndex].position = positions[i + 4];
996 vertices[vertexIndex].normal = normals[5];
1001 void PrimitiveVisual::FormCubeTriangles( Vector<unsigned short>& indices )
1003 int numTriangles = 12;
1004 int triangleIndex = 0; //Track progress through indices.
1006 indices.Resize( 3 * numTriangles );
1009 indices[triangleIndex] = 0;
1010 indices[triangleIndex + 1] = 2;
1011 indices[triangleIndex + 2] = 1;
1012 indices[triangleIndex + 3] = 2;
1013 indices[triangleIndex + 4] = 0;
1014 indices[triangleIndex + 5] = 3;
1017 int topFaceStart = 4;
1018 int bottomFaceStart = 12;
1021 for( int i = 0; i < 8; i += 2, triangleIndex += 6 )
1023 indices[triangleIndex ] = i + topFaceStart;
1024 indices[triangleIndex + 1] = i + topFaceStart + 1;
1025 indices[triangleIndex + 2] = i + bottomFaceStart + 1;
1026 indices[triangleIndex + 3] = i + topFaceStart;
1027 indices[triangleIndex + 4] = i + bottomFaceStart + 1;
1028 indices[triangleIndex + 5] = i + bottomFaceStart;
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;
1040 void PrimitiveVisual::ComputeOctahedronVertices( Vector<Vertex>& vertices, Vector3 dimensions, float smoothness )
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;
1048 vertices.Resize( numVertices );
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.
1054 Vector<Vector3> outerNormals; //Holds normals that point outwards at each vertex.
1055 outerNormals.Resize( 6 );
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 );
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 );
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 );
1080 //Loop through top faces.
1081 for( int i = 0; i < 4; i++, vertexIndex += 3 )
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[1];
1089 vertices[vertexIndex + 1].normal = outerNormals[1] * smoothness + normals[i] * (1 - smoothness);
1090 vertices[vertexIndex + 2].position = positions[i + 1];
1091 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1095 vertices[vertexIndex ].position = positions[0];
1096 vertices[vertexIndex ].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1097 vertices[vertexIndex + 1].position = positions[i + 2];
1098 vertices[vertexIndex + 1].normal = outerNormals[i + 2] * smoothness + normals[i] * (1 - smoothness);
1099 vertices[vertexIndex + 2].position = positions[i + 1];
1100 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1104 //Loop through bottom faces.
1105 for( int i = 0; i < 4; i++, vertexIndex += 3 )
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);
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);
1129 void PrimitiveVisual::FormOctahedronTriangles( Vector<unsigned short>& indices )
1131 int numTriangles = 8;
1132 int numIndices = numTriangles * 3;
1134 indices.Resize( numIndices );
1136 for( unsigned short i = 0; i < numIndices; i++ )
1142 void PrimitiveVisual::ComputeBevelledCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions,
1143 float bevelPercentage, float bevelSmoothness )
1145 int numPositions = 24;
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.
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;
1156 float outerX = 0.5 * dimensions.x;
1157 float outerY = 0.5 * dimensions.y;
1158 float outerZ = 0.5 * dimensions.z;
1160 float bevelX = outerX - ( 0.5 * minDimension - bevelAmount );
1161 float bevelY = outerY - ( 0.5 * minDimension - bevelAmount );
1162 float bevelZ = outerZ - ( 0.5 * minDimension - bevelAmount );
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 );
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 );
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 );
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 );
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 );
1205 normals[0 ] = Vector3( 0, 1, 0 );
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 );
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 );
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 );
1237 //Bottom face normal.
1238 normals[25] = Vector3( 0, -1, 0 );
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 );
1248 //Topmost face vertices.
1249 for( int i = 0; i < 4; i++, vertexIndex++ )
1251 vertices[vertexIndex].position = positions[i];
1252 vertices[vertexIndex].normal = normals[normalIndex];
1257 //Top slope vertices.
1258 for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
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);
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);
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);
1294 int secondCycleBeginning = 4;
1295 int thirdCycleBeginning = secondCycleBeginning + 8;
1296 int bottomCycleBeginning = thirdCycleBeginning + 8;
1299 for( int i = 0; i < 8; i++, vertexIndex += 4, normalIndex++ )
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];
1313 else if( (i % 2) == 0 )
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);
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];
1339 //Bottom slope vertices.
1340 for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
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);
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);
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);
1376 //Bottom-most face vertices.
1377 for( int i = 0; i < 4; i++, vertexIndex++ )
1379 vertices[vertexIndex].position = positions[ bottomCycleBeginning + i];
1380 vertices[vertexIndex].normal = normals[normalIndex];
1386 void PrimitiveVisual::FormBevelledCubeTriangles( Vector<unsigned short>& indices )
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.
1392 indices.Resize( 3 * numTriangles );
1395 indices[indiceIndex ] = vertexIndex;
1396 indices[indiceIndex + 1] = vertexIndex + 2;
1397 indices[indiceIndex + 2] = vertexIndex + 1;
1398 indices[indiceIndex + 3] = vertexIndex + 0;
1399 indices[indiceIndex + 4] = vertexIndex + 3;
1400 indices[indiceIndex + 5] = vertexIndex + 2;
1405 for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1408 indices[indiceIndex ] = vertexIndex;
1409 indices[indiceIndex + 1] = vertexIndex + 2;
1410 indices[indiceIndex + 2] = vertexIndex + 1;
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 + 6;
1418 indices[indiceIndex + 8] = vertexIndex + 5;
1422 for( int i = 0; i < 8; i++, indiceIndex += 6, vertexIndex += 4 )
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 + 3;
1429 indices[indiceIndex + 5] = vertexIndex + 2;
1433 for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1436 indices[indiceIndex ] = vertexIndex;
1437 indices[indiceIndex + 1] = vertexIndex + 1;
1438 indices[indiceIndex + 2] = vertexIndex + 2;
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 + 6;
1446 indices[indiceIndex + 8] = vertexIndex + 5;
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;
1460 } // namespace Internal
1462 } // namespace Toolkit