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>
25 #include <dali/devel-api/scripting/enum-helper.h>
26 #include <dali/devel-api/scripting/scripting.h>
29 #include <dali-toolkit/devel-api/visual-factory/devel-visual-properties.h>
30 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
45 DALI_ENUM_TO_STRING_TABLE_BEGIN( SHAPE_TYPE )
46 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, SPHERE )
47 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CONICAL_FRUSTRUM )
48 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CONE )
49 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CYLINDER )
50 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CUBE )
51 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, OCTAHEDRON )
52 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, BEVELLED_CUBE )
53 DALI_ENUM_TO_STRING_TABLE_END( SHAPE_TYPE )
56 const char * const PRIMITIVE_SHAPE( "shape" );
57 const char * const SHAPE_COLOR( "mixColor" );
58 const char * const SLICES( "slices" );
59 const char * const STACKS( "stacks" );
60 const char * const SCALE_TOP_RADIUS( "scaleTopRadius" );
61 const char * const SCALE_BOTTOM_RADIUS( "scaleBottomRadius" );
62 const char * const SCALE_HEIGHT( "scaleHeight" );
63 const char * const SCALE_RADIUS( "scaleRadius" );
64 const char * const SCALE_DIMENSIONS( "scaleDimensions" );
65 const char * const BEVEL_PERCENTAGE( "bevelPercentage" );
66 const char * const BEVEL_SMOOTHNESS( "bevelSmoothness" );
67 const char * const LIGHT_POSITION_UNIFORM_NAME( "lightPosition" );
69 //Primitive property defaults
70 const int DEFAULT_SLICES = 128; ///< For spheres and conics
71 const int DEFAULT_STACKS = 128; ///< For spheres and conics
72 const float DEFAULT_SCALE_TOP_RADIUS = 1.0; ///< For conical frustrums
73 const float DEFAULT_SCALE_BOTTOM_RADIUS = 1.5; ///< For cones and conical frustrums
74 const float DEFAULT_SCALE_HEIGHT = 3.0; ///< For all conics
75 const float DEFAULT_SCALE_RADIUS = 1.0; ///< For cylinders
76 const float DEFAULT_BEVEL_PERCENTAGE = 0.0; ///< For bevelled cubes
77 const float DEFAULT_BEVEL_SMOOTHNESS = 0.0; ///< For bevelled cubes
78 const Vector4 DEFAULT_COLOR = Vector4( 0.5, 0.5, 0.5, 1.0 ); ///< Grey, for all.
81 const int MIN_SLICES = 3; ///< Minimum number of slices for spheres and conics
82 const int MIN_STACKS = 2; ///< Minimum number of stacks for spheres and conics
83 const int MAX_PARTITIONS = 255; ///< Maximum number of slices or stacks for spheres and conics
84 const float MIN_BEVEL_PERCENTAGE = 0.0; ///< Minimum bevel percentage for bevelled cubes
85 const float MAX_BEVEL_PERCENTAGE = 1.0; ///< Maximum bevel percentage for bevelled cubes
86 const float MIN_SMOOTHNESS = 0.0; ///< Minimum bevel smoothness for bevelled cubes
87 const float MAX_SMOOTHNESS = 1.0; ///< Maximum bevel smoothness for bevelled cubes
89 //Specific shape labels.
90 const char * const SPHERE_LABEL( "SPHERE" );
91 const char * const CONE_LABEL( "CONE" );
92 const char * const CONICAL_FRUSTRUM_LABEL( "CONICAL_FRUSTRUM" );
93 const char * const CYLINDER_LABEL( "CYLINDER" );
94 const char * const CUBE_LABEL( "CUBE" );
95 const char * const OCTAHEDRON_LABEL( "OCTAHEDRON" );
96 const char * const BEVELLED_CUBE_LABEL( "BEVELLED_CUBE" );
99 const char * const OBJECT_MATRIX_UNIFORM_NAME( "uObjectMatrix" );
100 const char * const COLOR_UNIFORM_NAME( "mixColor" );
101 const char * const OBJECT_DIMENSIONS_UNIFORM_NAME( "uObjectDimensions" );
102 const char * const STAGE_OFFSET_UNIFORM_NAME( "uStageOffset" );
105 const char * const POSITION( "aPosition");
106 const char * const NORMAL( "aNormal" );
107 const char * const INDICES( "aIndices" );
109 //A simple shader that applies diffuse lighting to a mono-coloured object.
110 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
111 attribute highp vec3 aPosition;\n
112 attribute highp vec2 aTexCoord;\n
113 attribute highp vec3 aNormal;\n
114 varying mediump vec3 vIllumination;\n
115 uniform mediump vec3 uSize;\n
116 uniform mediump vec3 uObjectDimensions;\n
117 uniform mediump mat4 uMvpMatrix;\n
118 uniform mediump mat4 uModelView;\n
119 uniform mediump mat4 uViewMatrix;\n
120 uniform mediump mat3 uNormalMatrix;\n
121 uniform mediump mat4 uObjectMatrix;\n
122 uniform mediump vec3 lightPosition;\n
123 uniform mediump vec2 uStageOffset;\n
127 float xRatio = uSize.x / uObjectDimensions.x;\n
128 float yRatio = uSize.y / uObjectDimensions.y;\n
129 float scaleFactor = min( xRatio, yRatio );\n
131 vec4 normalisedVertexPosition = vec4( aPosition * scaleFactor, 1.0 );\n
132 vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
133 vertexPosition = uMvpMatrix * vertexPosition;\n
135 //Illumination in Model-View space - Transform attributes and uniforms\n
136 vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n
137 vec3 normal = uNormalMatrix * mat3( uObjectMatrix ) * aNormal;\n
139 vec4 mvLightPosition = vec4( ( lightPosition.xy - uStageOffset ), lightPosition.z, 1.0 );\n
140 mvLightPosition = uViewMatrix * mvLightPosition;\n
141 vec3 vectorToLight = normalize( mvLightPosition.xyz - mvVertexPosition.xyz );\n
143 float lightDiffuse = max( dot( vectorToLight, normal ), 0.0 );\n
144 vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
146 gl_Position = vertexPosition;\n
150 //Very simple fragment shader that merely applies the vertex shading to the color at each fragment.
151 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
152 precision mediump float;\n
153 varying mediump vec3 vIllumination;\n
154 uniform lowp vec4 uColor;\n
155 uniform lowp vec4 mixColor;\n
159 vec4 baseColor = mixColor * uColor;\n
160 gl_FragColor = vec4( vIllumination.rgb * baseColor.rgb, baseColor.a );\n
164 } // unnamed namespace
166 PrimitiveVisualPtr PrimitiveVisual::New( VisualFactoryCache& factoryCache )
168 return new PrimitiveVisual( factoryCache );
171 PrimitiveVisual::PrimitiveVisual( VisualFactoryCache& factoryCache )
172 : Visual::Base( factoryCache ),
173 mColor( DEFAULT_COLOR ),
174 mScaleDimensions( Vector3::ONE ),
175 mScaleTopRadius( DEFAULT_SCALE_TOP_RADIUS ),
176 mScaleBottomRadius( DEFAULT_SCALE_BOTTOM_RADIUS ),
177 mScaleHeight( DEFAULT_SCALE_HEIGHT ),
178 mScaleRadius( DEFAULT_SCALE_RADIUS ),
179 mBevelPercentage( DEFAULT_BEVEL_PERCENTAGE ),
180 mBevelSmoothness( DEFAULT_BEVEL_SMOOTHNESS ),
181 mSlices( DEFAULT_SLICES ),
182 mStacks( DEFAULT_STACKS ),
183 mPrimitiveType( Toolkit::PrimitiveVisual::Shape::SPHERE )
187 PrimitiveVisual::~PrimitiveVisual()
191 void PrimitiveVisual::DoSetProperties( const Property::Map& propertyMap )
193 //Find out which shape to renderer.
194 Property::Value* primitiveTypeValue = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SHAPE, PRIMITIVE_SHAPE );
195 if( primitiveTypeValue )
197 Scripting::GetEnumerationProperty( *primitiveTypeValue, SHAPE_TYPE_TABLE, SHAPE_TYPE_TABLE_COUNT, mPrimitiveType );
201 DALI_LOG_ERROR( "Fail to provide shape to the PrimitiveVisual object.\n" );
204 //Read in other potential properties.
206 Property::Value* color = propertyMap.Find( Toolkit::PrimitiveVisual::Property::MIX_COLOR, SHAPE_COLOR );
207 if( color && !color->Get( mColor ) )
209 DALI_LOG_ERROR( "Invalid type for color in PrimitiveVisual.\n" );
212 Property::Value* slices = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SLICES, SLICES );
215 if( slices->Get( mSlices ) )
218 if( mSlices > MAX_PARTITIONS )
220 mSlices = MAX_PARTITIONS;
221 DALI_LOG_WARNING( "Value for slices clamped.\n" );
223 else if ( mSlices < MIN_SLICES )
225 mSlices = MIN_SLICES;
226 DALI_LOG_WARNING( "Value for slices clamped.\n" );
231 DALI_LOG_ERROR( "Invalid type for slices in PrimitiveVisual.\n" );
235 Property::Value* stacks = propertyMap.Find( Toolkit::PrimitiveVisual::Property::STACKS, STACKS );
238 if( stacks->Get( mStacks ) )
241 if( mStacks > MAX_PARTITIONS )
243 mStacks = MAX_PARTITIONS;
244 DALI_LOG_WARNING( "Value for stacks clamped.\n" );
246 else if ( mStacks < MIN_STACKS )
248 mStacks = MIN_STACKS;
249 DALI_LOG_WARNING( "Value for stacks clamped.\n" );
254 DALI_LOG_ERROR( "Invalid type for stacks in PrimitiveVisual.\n" );
258 Property::Value* scaleTop = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_TOP_RADIUS, SCALE_TOP_RADIUS );
259 if( scaleTop && !scaleTop->Get( mScaleTopRadius ) )
261 DALI_LOG_ERROR( "Invalid type for scale top radius in PrimitiveVisual.\n" );
264 Property::Value* scaleBottom = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_BOTTOM_RADIUS, SCALE_BOTTOM_RADIUS );
265 if( scaleBottom && !scaleBottom->Get( mScaleBottomRadius ) )
267 DALI_LOG_ERROR( "Invalid type for scale bottom radius in PrimitiveVisual.\n" );
270 Property::Value* scaleHeight = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_HEIGHT, SCALE_HEIGHT );
271 if( scaleHeight && !scaleHeight->Get( mScaleHeight ) )
273 DALI_LOG_ERROR( "Invalid type for scale height in PrimitiveVisual.\n" );
276 Property::Value* scaleRadius = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_RADIUS, SCALE_RADIUS );
277 if( scaleRadius && !scaleRadius->Get( mScaleRadius ) )
279 DALI_LOG_ERROR( "Invalid type for scale radius in PrimitiveVisual.\n" );
282 Property::Value* dimensions = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_DIMENSIONS, SCALE_DIMENSIONS );
285 if( dimensions->Get( mScaleDimensions ) )
287 //If any dimension is invalid, set it to a sensible default.
288 if( mScaleDimensions.x <= 0.0 )
290 mScaleDimensions.x = 1.0;
291 DALI_LOG_WARNING( "Value for scale dimensions clamped. Must be greater than zero.\n" );
293 if( mScaleDimensions.y <= 0.0 )
295 mScaleDimensions.y = 1.0;
296 DALI_LOG_WARNING( "Value for scale dimensions clamped. Must be greater than zero.\n" );
298 if( mScaleDimensions.z <= 0.0 )
300 mScaleDimensions.z = 1.0;
301 DALI_LOG_WARNING( "Value for scale dimensions clamped. Must be greater than zero.\n" );
306 DALI_LOG_ERROR( "Invalid type for scale dimensions in PrimitiveVisual.\n" );
310 Property::Value* bevel = propertyMap.Find( Toolkit::PrimitiveVisual::Property::BEVEL_PERCENTAGE, BEVEL_PERCENTAGE );
313 if( bevel->Get( mBevelPercentage ) )
316 if( mBevelPercentage < MIN_BEVEL_PERCENTAGE )
318 mBevelPercentage = MIN_BEVEL_PERCENTAGE;
319 DALI_LOG_WARNING( "Value for bevel percentage clamped.\n" );
321 else if( mBevelPercentage > MAX_BEVEL_PERCENTAGE )
323 mBevelPercentage = MAX_BEVEL_PERCENTAGE;
324 DALI_LOG_WARNING( "Value for bevel percentage clamped.\n" );
329 DALI_LOG_ERROR( "Invalid type for bevel percentage in PrimitiveVisual.\n" );
333 Property::Value* smoothness = propertyMap.Find( Toolkit::PrimitiveVisual::Property::BEVEL_SMOOTHNESS, BEVEL_SMOOTHNESS );
336 if( smoothness->Get( mBevelSmoothness ) )
339 if( mBevelSmoothness < MIN_SMOOTHNESS )
341 mBevelSmoothness = MIN_SMOOTHNESS;
342 DALI_LOG_WARNING( "Value for bevel smoothness clamped.\n" );
344 else if( mBevelSmoothness > MAX_SMOOTHNESS )
346 mBevelSmoothness = MAX_SMOOTHNESS;
347 DALI_LOG_WARNING( "Value for bevel smoothness clamped.\n" );
352 DALI_LOG_ERROR( "Invalid type for bevel smoothness in PrimitiveVisual.\n" );
356 //Read in light position.
357 Property::Value* lightPosition = propertyMap.Find( Toolkit::PrimitiveVisual::Property::LIGHT_POSITION, LIGHT_POSITION_UNIFORM_NAME );
360 if( !lightPosition->Get( mLightPosition ) )
362 DALI_LOG_ERROR( "Invalid value passed for light position in MeshVisual object.\n" );
363 mLightPosition = Vector3::ZERO;
368 //Default behaviour is to place the light directly in front of the object,
369 // at a reasonable distance to light everything on screen.
370 Stage stage = Stage::GetCurrent();
372 mLightPosition = Vector3( stage.GetSize().width / 2, stage.GetSize().height / 2, stage.GetSize().width * 5 );
376 void PrimitiveVisual::SetSize( const Vector2& size )
378 Visual::Base::SetSize( size );
380 // ToDo: renderer responds to the size change
383 void PrimitiveVisual::GetNaturalSize( Vector2& naturalSize ) const
385 naturalSize.x = mObjectDimensions.x;
386 naturalSize.y = mObjectDimensions.y;
389 void PrimitiveVisual::DoSetOnStage( Actor& actor )
391 InitializeRenderer();
393 actor.AddRenderer( mImpl->mRenderer );
396 void PrimitiveVisual::DoCreatePropertyMap( Property::Map& map ) const
399 map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::PRIMITIVE );
400 map.Insert( Toolkit::PrimitiveVisual::Property::SHAPE, mPrimitiveType );
401 map.Insert( Toolkit::PrimitiveVisual::Property::MIX_COLOR, mColor );
402 map.Insert( Toolkit::PrimitiveVisual::Property::SLICES, mSlices );
403 map.Insert( Toolkit::PrimitiveVisual::Property::STACKS, mStacks );
404 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_TOP_RADIUS, mScaleTopRadius );
405 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_BOTTOM_RADIUS, mScaleBottomRadius );
406 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_HEIGHT, mScaleHeight );
407 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_RADIUS, mScaleRadius );
408 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_DIMENSIONS, mScaleDimensions );
409 map.Insert( Toolkit::PrimitiveVisual::Property::BEVEL_PERCENTAGE, mBevelPercentage );
410 map.Insert( Toolkit::PrimitiveVisual::Property::BEVEL_SMOOTHNESS, mBevelSmoothness );
411 map.Insert( Toolkit::PrimitiveVisual::Property::LIGHT_POSITION, mLightPosition );
414 void PrimitiveVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
419 Dali::Property::Value PrimitiveVisual::DoGetProperty( Dali::Property::Index index )
422 return Dali::Property::Value();
425 void PrimitiveVisual::InitializeRenderer()
437 mImpl->mRenderer = Renderer::New( mGeometry, mShader );
438 mImpl->mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
441 void PrimitiveVisual::UpdateShaderUniforms()
443 Stage stage = Stage::GetCurrent();
444 float width = stage.GetSize().width;
445 float height = stage.GetSize().height;
447 //Flip model to account for DALi starting with (0, 0) at the top left.
449 scaleMatrix.SetIdentityAndScale( Vector3( 1.0, -1.0, 1.0 ) );
451 mShader.RegisterProperty( STAGE_OFFSET_UNIFORM_NAME, Vector2( width, height ) / 2.0f );
452 mShader.RegisterProperty( LIGHT_POSITION_UNIFORM_NAME, mLightPosition );
453 mShader.RegisterProperty( OBJECT_MATRIX_UNIFORM_NAME, scaleMatrix );
454 mShader.RegisterProperty( Toolkit::PrimitiveVisual::Property::MIX_COLOR, COLOR_UNIFORM_NAME, mColor );
455 mShader.RegisterProperty( OBJECT_DIMENSIONS_UNIFORM_NAME, mObjectDimensions );
458 void PrimitiveVisual::CreateShader()
460 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
461 UpdateShaderUniforms();
464 void PrimitiveVisual::CreateGeometry()
466 Dali::Vector<Vertex> vertices;
467 Dali::Vector<unsigned short> indices;
469 switch( mPrimitiveType )
471 case Toolkit::PrimitiveVisual::Shape::SPHERE:
473 CreateSphere( vertices, indices, mSlices, mStacks );
476 case Toolkit::PrimitiveVisual::Shape::CONE:
478 //Create a conic with zero top radius.
479 CreateConic( vertices, indices, 0, mScaleBottomRadius, mScaleHeight, mSlices );
482 case Toolkit::PrimitiveVisual::Shape::CONICAL_FRUSTRUM:
484 CreateConic( vertices, indices, mScaleTopRadius, mScaleBottomRadius, mScaleHeight, mSlices );
487 case Toolkit::PrimitiveVisual::Shape::CYLINDER:
489 //Create a conic with equal radii on the top and bottom.
490 CreateConic( vertices, indices, mScaleRadius, mScaleRadius, mScaleHeight, mSlices );
493 case Toolkit::PrimitiveVisual::Shape::CUBE:
495 //Create a cube by creating a bevelled cube with minimum bevel.
496 CreateBevelledCube( vertices, indices, mScaleDimensions, 0.0, 0.0 );
499 case Toolkit::PrimitiveVisual::Shape::OCTAHEDRON:
501 //Create an octahedron by creating a bevelled cube with maximum bevel.
502 CreateBevelledCube( vertices, indices, mScaleDimensions, 1.0, mBevelSmoothness );
505 case Toolkit::PrimitiveVisual::Shape::BEVELLED_CUBE:
507 CreateBevelledCube( vertices, indices, mScaleDimensions, mBevelPercentage, mBevelSmoothness );
512 mGeometry = Geometry::New();
515 Property::Map vertexFormat;
516 vertexFormat[POSITION] = Property::VECTOR3;
517 vertexFormat[NORMAL] = Property::VECTOR3;
518 PropertyBuffer surfaceVertices = PropertyBuffer::New( vertexFormat );
519 surfaceVertices.SetData( &vertices[0], vertices.Size() );
521 mGeometry.AddVertexBuffer( surfaceVertices );
523 //Indices for triangle formulation
524 mGeometry.SetIndexBuffer( &indices[0], indices.Size() );
527 void PrimitiveVisual::CreateSphere( Vector<Vertex>& vertices, Vector<unsigned short>& indices, int slices, int stacks )
529 ComputeSphereVertices( vertices, slices, stacks );
530 FormSphereTriangles( indices, slices, stacks );
532 mObjectDimensions = Vector3::ONE;
535 void PrimitiveVisual::CreateConic( Vector<Vertex>& vertices, Vector<unsigned short>& indices, float scaleTopRadius,
536 float scaleBottomRadius, float scaleHeight, int slices )
538 ComputeConicVertices( vertices, scaleTopRadius, scaleBottomRadius, scaleHeight, slices );
539 FormConicTriangles( indices, scaleTopRadius, scaleBottomRadius, slices );
541 //Determine object dimensions, and scale them to be between 0.0 and 1.0.
542 float xDimension = std::max( scaleTopRadius, scaleBottomRadius ) * 2.0f;
543 float yDimension = scaleHeight;
544 float largestDimension = std::max( xDimension, yDimension );
546 mObjectDimensions = Vector3( xDimension / largestDimension, yDimension / largestDimension,
547 xDimension / largestDimension );
550 void PrimitiveVisual::CreateBevelledCube( Vector<Vertex>& vertices, Vector<unsigned short>& indices,
551 Vector3 dimensions, float bevelPercentage, float bevelSmoothness )
553 float maxDimension = std::max( std::max( dimensions.x, dimensions.y ), dimensions.z );
554 dimensions = dimensions / maxDimension;
556 if( bevelPercentage <= MIN_BEVEL_PERCENTAGE ) //No bevel, form a cube.
558 ComputeCubeVertices( vertices, dimensions );
559 FormCubeTriangles( indices );
561 else if( bevelPercentage >= MAX_BEVEL_PERCENTAGE ) //Max bevel, form an octahedron.
563 ComputeOctahedronVertices( vertices, dimensions, bevelSmoothness );
564 FormOctahedronTriangles( indices );
566 else //In between, form a bevelled cube.
568 ComputeBevelledCubeVertices( vertices, dimensions, bevelPercentage, bevelSmoothness );
569 FormBevelledCubeTriangles( indices );
572 mObjectDimensions = dimensions;
575 void PrimitiveVisual::ComputeCircleTables( Vector<float>& sinTable, Vector<float>& cosTable, int divisions,
583 const float angleDivision = ( halfCircle ? 1.0f : 2.0f ) * Dali::Math::PI / ( float ) divisions;
585 sinTable.Resize( divisions );
586 cosTable.Resize( divisions );
588 for( int i = 0; i < divisions; i++ )
590 sinTable[i] = sin( angleDivision * i );
591 cosTable[i] = cos( angleDivision * i );
595 void PrimitiveVisual::ComputeSphereVertices( Vector<Vertex>& vertices, int slices, int stacks )
597 //Tables for calculating slices angles and stacks angles, respectively.
598 Vector<float> sinTable1;
599 Vector<float> cosTable1;
600 Vector<float> sinTable2;
601 Vector<float> cosTable2;
603 ComputeCircleTables( sinTable1, cosTable1, slices, false );
604 ComputeCircleTables( sinTable2, cosTable2, stacks, true );
606 int numVertices = slices * ( stacks - 1 ) + 2;
607 vertices.Resize( numVertices );
609 int vertexIndex = 0; //Track progress through vertices.
615 vertices[vertexIndex].position = Vector3( 0.0, 0.5, 0.0 );
616 vertices[vertexIndex].normal = Vector3( 0.0, 1.0, 0.0 );
620 for( int i = 1; i < stacks; i++ )
622 for( int j = 0; j < slices; j++, vertexIndex++ )
624 x = cosTable1[j] * sinTable2[i];
626 z = sinTable1[j] * sinTable2[i];
628 vertices[vertexIndex].position = Vector3( x / 2.0f, y / 2.0f, z / 2.0f );
629 vertices[vertexIndex].normal = Vector3( x, y, z );
634 vertices[vertexIndex].position = Vector3( 0.0, -0.5, 0.0 );
635 vertices[vertexIndex].normal = Vector3( 0.0, -1.0, 0.0 );
638 void PrimitiveVisual::FormSphereTriangles( Vector<unsigned short>& indices, int slices, int stacks )
642 //Set indices to placeholder "error" values.
643 //This will display nothing, which is the expected behaviour for this edge case.
648 int numTriangles = 2 * slices * ( stacks - 1 );
650 indices.Resize( 3 * numTriangles );
652 int indiceIndex = 0; //Used to keep track of progress through indices.
653 int previousCycleBeginning = 1; //Stores the index of the vertex that started the cycle of the previous stack.
654 int currentCycleBeginning = 1 + slices;
656 //Top stack. Loop from index 1 to index slices, as not counting the very first vertex.
657 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
659 indices[indiceIndex] = 0;
662 //End, so loop around.
663 indices[indiceIndex + 1] = 1;
667 indices[indiceIndex + 1] = i + 1;
669 indices[indiceIndex + 2] = i;
672 //Middle Stacks. Want to form triangles between the top and bottom stacks, so loop up to the number of stacks - 2.
673 for( int i = 0; i < stacks - 2; i++, previousCycleBeginning += slices, currentCycleBeginning += slices )
675 for( int j = 0; j < slices; j++, indiceIndex += 6 )
677 if( j == slices - 1 )
679 //End, so loop around.
680 indices[indiceIndex] = previousCycleBeginning + j;
681 indices[indiceIndex + 1] = previousCycleBeginning;
682 indices[indiceIndex + 2] = currentCycleBeginning + j;
683 indices[indiceIndex + 3] = currentCycleBeginning + j;
684 indices[indiceIndex + 4] = previousCycleBeginning;
685 indices[indiceIndex + 5] = currentCycleBeginning;
689 indices[indiceIndex] = previousCycleBeginning + j;
690 indices[indiceIndex + 1] = previousCycleBeginning + 1 + j;
691 indices[indiceIndex + 2] = currentCycleBeginning + j;
692 indices[indiceIndex + 3] = currentCycleBeginning + j;
693 indices[indiceIndex + 4] = previousCycleBeginning + 1 + j;
694 indices[indiceIndex + 5] = currentCycleBeginning + 1 + j;
699 //Bottom stack. Loop around the last stack from the previous loop, and go up to the penultimate vertex.
700 for( int i = 0; i < slices; i++, indiceIndex += 3 )
702 indices[indiceIndex] = previousCycleBeginning + slices;
703 indices[indiceIndex + 1] = previousCycleBeginning + i;
704 if( i == slices - 1 )
706 //End, so loop around.
707 indices[indiceIndex + 2] = previousCycleBeginning;
711 indices[indiceIndex + 2] = previousCycleBeginning + i + 1;
716 void PrimitiveVisual::ComputeConicVertices( Vector<Vertex>& vertices, float scaleTopRadius,
717 float scaleBottomRadius, float scaleHeight, int slices )
719 int vertexIndex = 0; //Track progress through vertices.
720 Vector<float> sinTable;
721 Vector<float> cosTable;
723 ComputeCircleTables( sinTable, cosTable, slices, false );
725 int numVertices = 2; //Always will have one at the top and one at the bottom.
727 //Add vertices for each circle. Need two per point for different face normals.
728 if( scaleTopRadius > 0.0 )
730 numVertices += 2 * slices;
732 if( scaleBottomRadius > 0.0 )
734 numVertices += 2 * slices;
737 vertices.Resize( numVertices );
740 //Scale to bounding region of -0.5 to 0.5 (i.e range of 1).
741 float biggestObjectDimension = std::max( std::max( scaleTopRadius * 2.0f, scaleBottomRadius * 2.0f ), scaleHeight );
742 scaleTopRadius = scaleTopRadius / biggestObjectDimension;
743 scaleBottomRadius = scaleBottomRadius / biggestObjectDimension;
745 //Dimensions for vertex coordinates. Y is constant, and so can be initialised now.
747 float y = scaleHeight / biggestObjectDimension / 2.0f;
751 vertices[0].position = Vector3( 0, y, 0 );
752 vertices[0].normal = Vector3( 0, 1, 0 );
756 if( scaleTopRadius > 0.0 )
758 //Loop around the circle.
759 for( int i = 0; i < slices; i++, vertexIndex++ )
761 x = sinTable[i] * scaleTopRadius;
762 z = cosTable[i] * scaleTopRadius;
764 //Upward-facing normal.
765 vertices[vertexIndex].position = Vector3( x, y, z );
766 vertices[vertexIndex].normal = Vector3( 0, 1, 0 );
768 //Outward-facing normal.
769 vertices[vertexIndex + slices].position = Vector3( x, y, z );
770 vertices[vertexIndex + slices].normal = Vector3( x, 0, z );
773 vertexIndex += slices;
777 if( scaleBottomRadius > 0.0 )
779 //Loop around the circle.
780 for( int i = 0; i < slices; i++, vertexIndex++ )
782 x = sinTable[i] * scaleBottomRadius;
783 z = cosTable[i] * scaleBottomRadius;
785 //Outward-facing normal.
786 vertices[vertexIndex].position = Vector3( x, -y, z );
787 vertices[vertexIndex].normal = Vector3( x, 0, z );
789 //Downward-facing normal.
790 vertices[vertexIndex + slices].position = Vector3( x, -y, z );
791 vertices[vertexIndex + slices].normal = Vector3( 0, -1, 0 );
794 vertexIndex += slices;
798 vertices[vertexIndex].position = Vector3( 0, -y, 0 );
799 vertices[vertexIndex].normal = Vector3( 0, -1, 0 );
803 void PrimitiveVisual::FormConicTriangles( Vector<unsigned short>& indices, float scaleTopRadius,
804 float scaleBottomRadius, int slices )
806 int indiceIndex = 0; //Track progress through indices.
807 int numTriangles = 0;
808 bool coneTop = scaleTopRadius <= 0.0;
809 bool coneBottom = scaleBottomRadius <= 0.0;
811 if( coneTop && coneBottom )
813 //Set indices to placeholder "error" values.
814 //This will display nothing, which is the expected behaviour for this edge case.
821 numTriangles += 2 * slices;
825 numTriangles += 2 * slices;
828 indices.Resize( 3 * numTriangles );
830 //Switch on the type of conic we have.
831 if( !coneTop && !coneBottom )
833 //Top circle. Start at index of first outer point and go around.
834 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
836 indices[indiceIndex] = 0;
837 indices[indiceIndex + 1] = i;
840 //End, so loop around.
841 indices[indiceIndex + 2] = 1;
845 indices[indiceIndex + 2] = i + 1;
849 int topCycleBeginning = slices + 1;
850 int bottomCycleBeginning = topCycleBeginning + slices;
853 for( int i = 0; i < slices; i++, indiceIndex += 6 )
855 if( i == slices - 1 )
857 //End, so loop around.
858 indices[indiceIndex] = topCycleBeginning + i;
859 indices[indiceIndex + 1] = bottomCycleBeginning + i;
860 indices[indiceIndex + 2] = topCycleBeginning;
861 indices[indiceIndex + 3] = bottomCycleBeginning + i;
862 indices[indiceIndex + 4] = bottomCycleBeginning;
863 indices[indiceIndex + 5] = topCycleBeginning;
867 indices[indiceIndex] = topCycleBeginning + i;
868 indices[indiceIndex + 1] = bottomCycleBeginning + i;
869 indices[indiceIndex + 2] = topCycleBeginning + 1 + i;
870 indices[indiceIndex + 3] = bottomCycleBeginning + i;
871 indices[indiceIndex + 4] = bottomCycleBeginning + 1 + i;
872 indices[indiceIndex + 5] = topCycleBeginning + 1 + i;
876 int bottomFaceCycleBeginning = bottomCycleBeginning + slices;
879 for( int i = 0; i < slices; i++, indiceIndex += 3 )
881 indices[indiceIndex] = bottomFaceCycleBeginning;
882 if( i == slices - 1 )
884 //End, so loop around.
885 indices[indiceIndex + 1] = bottomFaceCycleBeginning;
889 indices[indiceIndex + 1] = bottomFaceCycleBeginning + i + 1;
891 indices[indiceIndex + 2] = bottomFaceCycleBeginning + i;
894 else if( !coneTop || !coneBottom )
896 //Top circle/edges. Start at index of first outer point and go around.
897 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
899 indices[indiceIndex] = 0;
900 indices[indiceIndex + 1] = i;
903 //End, so loop around.
904 indices[indiceIndex + 2] = 1;
908 indices[indiceIndex + 2] = i + 1;
912 //Bottom circle/edges. Start at index of first outer point and go around.
913 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
915 indices[indiceIndex] = 2 * slices + 1;
918 //End, so loop around.
919 indices[indiceIndex + 1] = slices + 1;
923 indices[indiceIndex + 1] = slices + i + 1;
925 indices[indiceIndex + 2] = slices + i;
930 void PrimitiveVisual::ComputeCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions )
932 int numVertices = 4 * 6; //Four per face.
933 int vertexIndex = 0; //Tracks progress through vertices.
934 float scaledX = 0.5 * dimensions.x;
935 float scaledY = 0.5 * dimensions.y;
936 float scaledZ = 0.5 * dimensions.z;
938 vertices.Resize( numVertices );
940 Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
942 Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
945 positions[0] = Vector3( -scaledX, scaledY, -scaledZ );
946 positions[1] = Vector3( scaledX, scaledY, -scaledZ );
947 positions[2] = Vector3( scaledX, scaledY, scaledZ );
948 positions[3] = Vector3( -scaledX, scaledY, scaledZ );
949 positions[4] = Vector3( -scaledX, -scaledY, -scaledZ );
950 positions[5] = Vector3( scaledX, -scaledY, -scaledZ );
951 positions[6] = Vector3( scaledX, -scaledY, scaledZ );
952 positions[7] = Vector3( -scaledX, -scaledY, scaledZ );
954 normals[0] = Vector3( 0, 1, 0 );
955 normals[1] = Vector3( 0, 0, -1 );
956 normals[2] = Vector3( 1, 0, 0 );
957 normals[3] = Vector3( 0, 0, 1 );
958 normals[4] = Vector3( -1, 0, 0 );
959 normals[5] = Vector3( 0, -1, 0 );
961 //Top face, upward normals.
962 for( int i = 0; i < 4; i++, vertexIndex++ )
964 vertices[vertexIndex].position = positions[i];
965 vertices[vertexIndex].normal = normals[0];
968 //Top face, outward normals.
969 for( int i = 0; i < 4; i++, vertexIndex += 2 )
971 vertices[vertexIndex].position = positions[i];
972 vertices[vertexIndex].normal = normals[i + 1];
976 //End, so loop around.
977 vertices[vertexIndex + 1].position = positions[0];
981 vertices[vertexIndex + 1].position = positions[i + 1];
983 vertices[vertexIndex + 1].normal = normals[i + 1];
986 //Bottom face, outward normals.
987 for( int i = 0; i < 4; i++, vertexIndex += 2 )
989 vertices[vertexIndex].position = positions[i + 4];
990 vertices[vertexIndex].normal = normals[i + 1];
994 //End, so loop around.
995 vertices[vertexIndex + 1].position = positions[4];
999 vertices[vertexIndex + 1].position = positions[i + 5];
1001 vertices[vertexIndex + 1].normal = normals[i + 1];
1004 //Bottom face, downward normals.
1005 for( int i = 0; i < 4; i++, vertexIndex++ )
1007 vertices[vertexIndex].position = positions[i + 4];
1008 vertices[vertexIndex].normal = normals[5];
1013 void PrimitiveVisual::FormCubeTriangles( Vector<unsigned short>& indices )
1015 int numTriangles = 12;
1016 int triangleIndex = 0; //Track progress through indices.
1018 indices.Resize( 3 * numTriangles );
1021 indices[triangleIndex] = 0;
1022 indices[triangleIndex + 1] = 2;
1023 indices[triangleIndex + 2] = 1;
1024 indices[triangleIndex + 3] = 2;
1025 indices[triangleIndex + 4] = 0;
1026 indices[triangleIndex + 5] = 3;
1029 int topFaceStart = 4;
1030 int bottomFaceStart = 12;
1033 for( int i = 0; i < 8; i += 2, triangleIndex += 6 )
1035 indices[triangleIndex ] = i + topFaceStart;
1036 indices[triangleIndex + 1] = i + topFaceStart + 1;
1037 indices[triangleIndex + 2] = i + bottomFaceStart + 1;
1038 indices[triangleIndex + 3] = i + topFaceStart;
1039 indices[triangleIndex + 4] = i + bottomFaceStart + 1;
1040 indices[triangleIndex + 5] = i + bottomFaceStart;
1044 indices[triangleIndex] = 20;
1045 indices[triangleIndex + 1] = 21;
1046 indices[triangleIndex + 2] = 22;
1047 indices[triangleIndex + 3] = 22;
1048 indices[triangleIndex + 4] = 23;
1049 indices[triangleIndex + 5] = 20;
1052 void PrimitiveVisual::ComputeOctahedronVertices( Vector<Vertex>& vertices, Vector3 dimensions, float smoothness )
1054 int numVertices = 3 * 8; //Three per face
1055 int vertexIndex = 0; //Tracks progress through vertices.
1056 float scaledX = 0.5 * dimensions.x;
1057 float scaledY = 0.5 * dimensions.y;
1058 float scaledZ = 0.5 * dimensions.z;
1060 vertices.Resize( numVertices );
1062 Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
1063 positions.Resize(6);
1064 Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
1066 Vector<Vector3> outerNormals; //Holds normals that point outwards at each vertex.
1067 outerNormals.Resize( 6 );
1069 positions[0] = Vector3( 0.0, scaledY, 0.0 );
1070 positions[1] = Vector3( -scaledX, 0.0, 0.0 );
1071 positions[2] = Vector3( 0.0, 0.0, -scaledZ );
1072 positions[3] = Vector3( scaledX, 0.0, 0.0 );
1073 positions[4] = Vector3( 0.0, 0.0, scaledZ );
1074 positions[5] = Vector3( 0.0, -scaledY, 0.0 );
1076 normals[0] = Vector3( -1, 1, -1 );
1077 normals[1] = Vector3( 1, 1, -1 );
1078 normals[2] = Vector3( 1, 1, 1 );
1079 normals[3] = Vector3( -1, 1, 1 );
1080 normals[4] = Vector3( -1, -1, -1 );
1081 normals[5] = Vector3( 1, -1, -1 );
1082 normals[6] = Vector3( 1, -1, 1 );
1083 normals[7] = Vector3( -1, -1, 1 );
1085 outerNormals[0] = Vector3( 0, 1, 0 );
1086 outerNormals[1] = Vector3( -1, 0, 0 );
1087 outerNormals[2] = Vector3( 0, 0, -1 );
1088 outerNormals[3] = Vector3( 1, 0, 0 );
1089 outerNormals[4] = Vector3( 0, 0, 1 );
1090 outerNormals[5] = Vector3( 0, -1, 0 );
1092 //Loop through top faces.
1093 for( int i = 0; i < 4; i++, vertexIndex += 3 )
1097 //End, so loop around.
1098 vertices[vertexIndex ].position = positions[0];
1099 vertices[vertexIndex ].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1100 vertices[vertexIndex + 1].position = positions[1];
1101 vertices[vertexIndex + 1].normal = outerNormals[1] * smoothness + normals[i] * (1 - smoothness);
1102 vertices[vertexIndex + 2].position = positions[i + 1];
1103 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1107 vertices[vertexIndex ].position = positions[0];
1108 vertices[vertexIndex ].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1109 vertices[vertexIndex + 1].position = positions[i + 2];
1110 vertices[vertexIndex + 1].normal = outerNormals[i + 2] * smoothness + normals[i] * (1 - smoothness);
1111 vertices[vertexIndex + 2].position = positions[i + 1];
1112 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1116 //Loop through bottom faces.
1117 for( int i = 0; i < 4; i++, vertexIndex += 3 )
1121 //End, so loop around.
1122 vertices[vertexIndex ].position = positions[5];
1123 vertices[vertexIndex ].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1124 vertices[vertexIndex + 1].position = positions[i + 1];
1125 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1126 vertices[vertexIndex + 2].position = positions[1];
1127 vertices[vertexIndex + 2].normal = outerNormals[1] * smoothness + normals[i + 4] * (1 - smoothness);
1131 vertices[vertexIndex ].position = positions[5];
1132 vertices[vertexIndex ].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1133 vertices[vertexIndex + 1].position = positions[i + 1];
1134 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1135 vertices[vertexIndex + 2].position = positions[i + 2];
1136 vertices[vertexIndex + 2].normal = outerNormals[i + 2] * smoothness + normals[i + 4] * (1 - smoothness);
1141 void PrimitiveVisual::FormOctahedronTriangles( Vector<unsigned short>& indices )
1143 int numTriangles = 8;
1144 int numIndices = numTriangles * 3;
1146 indices.Resize( numIndices );
1148 for( unsigned short i = 0; i < numIndices; i++ )
1154 void PrimitiveVisual::ComputeBevelledCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions,
1155 float bevelPercentage, float bevelSmoothness )
1157 int numPositions = 24;
1159 int numOuterFaces = 6;
1160 int numVertices = 6 * 4 + 12 * 4 + 8 * 3; //Six outer faces, 12 slanting rectangles, 8 slanting triangles.
1161 int vertexIndex = 0; //Track progress through vertices.
1162 int normalIndex = 0; //Track progress through normals, as vertices are calculated per face.
1164 float minDimension = std::min( std::min( dimensions.x, dimensions.y ), dimensions.z );
1165 float bevelAmount = 0.5 * std::min( bevelPercentage, minDimension ); //Cap bevel amount if necessary.
1167 //Distances from centre to outer edge points.
1168 float outerX = 0.5 * dimensions.x;
1169 float outerY = 0.5 * dimensions.y;
1170 float outerZ = 0.5 * dimensions.z;
1172 //Distances from centre to bevelled points.
1173 float bevelX = outerX - bevelAmount;
1174 float bevelY = outerY - bevelAmount;
1175 float bevelZ = outerZ - bevelAmount;
1177 Vector<Vector3> positions; //Holds object points, to be shared between vertexes.
1178 positions.Resize( numPositions );
1179 Vector<Vector3> normals; //Holds face normals, to be shared between vertexes.
1180 normals.Resize( numFaces );
1181 Vector<Vector3> outerNormals; //Holds normals of the outermost faces specifically.
1182 outerNormals.Resize( numOuterFaces );
1183 vertices.Resize( numVertices );
1185 //Topmost face positions.
1186 positions[0 ] = Vector3( -bevelX, outerY, -bevelZ );
1187 positions[1 ] = Vector3( bevelX, outerY, -bevelZ );
1188 positions[2 ] = Vector3( bevelX, outerY, bevelZ );
1189 positions[3 ] = Vector3( -bevelX, outerY, bevelZ );
1191 //Second layer positions.
1192 positions[4 ] = Vector3( -outerX, bevelY, -bevelZ );
1193 positions[5 ] = Vector3( -bevelX, bevelY, -outerZ );
1194 positions[6 ] = Vector3( bevelX, bevelY, -outerZ );
1195 positions[7 ] = Vector3( outerX, bevelY, -bevelZ );
1196 positions[8 ] = Vector3( outerX, bevelY, bevelZ );
1197 positions[9 ] = Vector3( bevelX, bevelY, outerZ );
1198 positions[10] = Vector3( -bevelX, bevelY, outerZ );
1199 positions[11] = Vector3( -outerX, bevelY, bevelZ );
1201 //Third layer positions.
1202 positions[12] = Vector3( -outerX, -bevelY, -bevelZ );
1203 positions[13] = Vector3( -bevelX, -bevelY, -outerZ );
1204 positions[14] = Vector3( bevelX, -bevelY, -outerZ );
1205 positions[15] = Vector3( outerX, -bevelY, -bevelZ );
1206 positions[16] = Vector3( outerX, -bevelY, bevelZ );
1207 positions[17] = Vector3( bevelX, -bevelY, outerZ );
1208 positions[18] = Vector3( -bevelX, -bevelY, outerZ );
1209 positions[19] = Vector3( -outerX, -bevelY, bevelZ );
1211 //Bottom-most face positions.
1212 positions[20] = Vector3( -bevelX, -outerY, -bevelZ );
1213 positions[21] = Vector3( bevelX, -outerY, -bevelZ );
1214 positions[22] = Vector3( bevelX, -outerY, bevelZ );
1215 positions[23] = Vector3( -bevelX, -outerY, bevelZ );
1218 normals[0 ] = Vector3( 0, 1, 0 );
1220 //Top slope normals.
1221 normals[1 ] = Vector3( -1, 1, -1 );
1222 normals[2 ] = Vector3( 0, 1, -1 );
1223 normals[3 ] = Vector3( 1, 1, -1 );
1224 normals[4 ] = Vector3( 1, 1, 0 );
1225 normals[5 ] = Vector3( 1, 1, 1 );
1226 normals[6 ] = Vector3( 0, 1, 1 );
1227 normals[7 ] = Vector3( -1, 1, 1 );
1228 normals[8 ] = Vector3( -1, 1, 0 );
1231 normals[9 ] = Vector3( -1, 0, -1 );
1232 normals[10] = Vector3( 0, 0, -1 );
1233 normals[11] = Vector3( 1, 0, -1 );
1234 normals[12] = Vector3( 1, 0, 0 );
1235 normals[13] = Vector3( 1, 0, 1 );
1236 normals[14] = Vector3( 0, 0, 1 );
1237 normals[15] = Vector3( -1, 0, 1 );
1238 normals[16] = Vector3( -1, 0, 0 );
1240 //Bottom slope normals.
1241 normals[17] = Vector3( -1, -1, -1 );
1242 normals[18] = Vector3( 0, -1, -1 );
1243 normals[19] = Vector3( 1, -1, -1 );
1244 normals[20] = Vector3( 1, -1, 0 );
1245 normals[21] = Vector3( 1, -1, 1 );
1246 normals[22] = Vector3( 0, -1, 1 );
1247 normals[23] = Vector3( -1, -1, 1 );
1248 normals[24] = Vector3( -1, -1, 0 );
1250 //Bottom face normal.
1251 normals[25] = Vector3( 0, -1, 0 );
1253 //Top, back, right, front, left and bottom faces, respectively.
1254 outerNormals[0] = Vector3( 0, 1, 0 );
1255 outerNormals[1] = Vector3( 0, 0, -1 );
1256 outerNormals[2] = Vector3( 1, 0, 0 );
1257 outerNormals[3] = Vector3( 0, 0, 1 );
1258 outerNormals[4] = Vector3( -1, 0, 0 );
1259 outerNormals[5] = Vector3( 0, -1, 0 );
1261 //Topmost face vertices.
1262 for( int i = 0; i < 4; i++, vertexIndex++ )
1264 vertices[vertexIndex].position = positions[i];
1265 vertices[vertexIndex].normal = normals[normalIndex];
1270 //Top slope vertices.
1271 for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
1274 vertices[vertexIndex ].position = positions[i];
1275 vertices[vertexIndex ].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1276 vertices[vertexIndex + 1].position = positions[2 * i + 4];
1277 vertices[vertexIndex + 1].normal = outerNormals[( i == 0 ) ? 4 : i] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1278 vertices[vertexIndex + 2].position = positions[2 * i + 5];
1279 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1284 //End, so loop around.
1285 vertices[vertexIndex + 3].position = positions[i];
1286 vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1287 vertices[vertexIndex + 4].position = positions[0];
1288 vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1289 vertices[vertexIndex + 5].position = positions[2 * i + 5];
1290 vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1291 vertices[vertexIndex + 6].position = positions[4];
1292 vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1296 vertices[vertexIndex + 3].position = positions[i];
1297 vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1298 vertices[vertexIndex + 4].position = positions[i + 1];
1299 vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1300 vertices[vertexIndex + 5].position = positions[2 * i + 5];
1301 vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1302 vertices[vertexIndex + 6].position = positions[2 * i + 6];
1303 vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1307 int secondCycleBeginning = 4;
1308 int thirdCycleBeginning = secondCycleBeginning + 8;
1309 int bottomCycleBeginning = thirdCycleBeginning + 8;
1312 for( int i = 0; i < 8; i++, vertexIndex += 4, normalIndex++ )
1316 //End, so loop around.
1317 vertices[vertexIndex ].position = positions[secondCycleBeginning + i];
1318 vertices[vertexIndex ].normal = normals[normalIndex];
1319 vertices[vertexIndex + 1].position = positions[secondCycleBeginning];
1320 vertices[vertexIndex + 1].normal = normals[normalIndex];
1321 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1322 vertices[vertexIndex + 2].normal = normals[normalIndex];
1323 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning];
1324 vertices[vertexIndex + 3].normal = normals[normalIndex];
1326 else if( (i % 2) == 0 )
1328 //'even' faces are corner ones, and need smoothing.
1329 vertices[vertexIndex ].position = positions[secondCycleBeginning + i];
1330 vertices[vertexIndex ].normal = outerNormals[( i == 0 ) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1331 vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1332 vertices[vertexIndex + 1].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1333 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1334 vertices[vertexIndex + 2].normal = outerNormals[( i == 0 ) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1335 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1336 vertices[vertexIndex + 3].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1340 //'odd' faces are outer ones, and so don't need smoothing.
1341 vertices[vertexIndex ].position = positions[secondCycleBeginning + i];
1342 vertices[vertexIndex ].normal = normals[normalIndex];
1343 vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1344 vertices[vertexIndex + 1].normal = normals[normalIndex];
1345 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1346 vertices[vertexIndex + 2].normal = normals[normalIndex];
1347 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1348 vertices[vertexIndex + 3].normal = normals[normalIndex];
1352 //Bottom slope vertices.
1353 for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
1356 vertices[vertexIndex ].position = positions[thirdCycleBeginning + 2 * i];
1357 vertices[vertexIndex ].normal = outerNormals[( i == 0 ) ? 4 : i] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1358 vertices[vertexIndex + 1].position = positions[thirdCycleBeginning + 2 * i + 1];
1359 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1360 vertices[vertexIndex + 2].position = positions[bottomCycleBeginning + i];
1361 vertices[vertexIndex + 2].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1366 //End, so loop around.
1367 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1368 vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1369 vertices[vertexIndex + 4].position = positions[thirdCycleBeginning];
1370 vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1371 vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1372 vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1373 vertices[vertexIndex + 6].position = positions[bottomCycleBeginning];
1374 vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1378 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1379 vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1380 vertices[vertexIndex + 4].position = positions[thirdCycleBeginning + 2 * i + 2];
1381 vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1382 vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1383 vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1384 vertices[vertexIndex + 6].position = positions[bottomCycleBeginning + i + 1];
1385 vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1389 //Bottom-most face vertices.
1390 for( int i = 0; i < 4; i++, vertexIndex++ )
1392 vertices[vertexIndex].position = positions[ bottomCycleBeginning + i];
1393 vertices[vertexIndex].normal = normals[normalIndex];
1399 void PrimitiveVisual::FormBevelledCubeTriangles( Vector<unsigned short>& indices )
1401 int numTriangles = 44; //(Going from top to bottom, that's 2 + 12 + 16 + 12 + 2)
1402 int indiceIndex = 0; //Track progress through indices.
1403 int vertexIndex = 0; //Track progress through vertices as they're processed.
1405 indices.Resize( 3 * numTriangles );
1408 indices[indiceIndex ] = vertexIndex;
1409 indices[indiceIndex + 1] = vertexIndex + 2;
1410 indices[indiceIndex + 2] = vertexIndex + 1;
1411 indices[indiceIndex + 3] = vertexIndex + 0;
1412 indices[indiceIndex + 4] = vertexIndex + 3;
1413 indices[indiceIndex + 5] = vertexIndex + 2;
1418 for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1421 indices[indiceIndex ] = vertexIndex;
1422 indices[indiceIndex + 1] = vertexIndex + 2;
1423 indices[indiceIndex + 2] = vertexIndex + 1;
1426 indices[indiceIndex + 3] = vertexIndex + 3;
1427 indices[indiceIndex + 4] = vertexIndex + 4;
1428 indices[indiceIndex + 5] = vertexIndex + 5;
1429 indices[indiceIndex + 6] = vertexIndex + 4;
1430 indices[indiceIndex + 7] = vertexIndex + 6;
1431 indices[indiceIndex + 8] = vertexIndex + 5;
1435 for( int i = 0; i < 8; i++, indiceIndex += 6, vertexIndex += 4 )
1437 indices[indiceIndex ] = vertexIndex;
1438 indices[indiceIndex + 1] = vertexIndex + 1;
1439 indices[indiceIndex + 2] = vertexIndex + 2;
1440 indices[indiceIndex + 3] = vertexIndex + 1;
1441 indices[indiceIndex + 4] = vertexIndex + 3;
1442 indices[indiceIndex + 5] = vertexIndex + 2;
1446 for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1449 indices[indiceIndex ] = vertexIndex;
1450 indices[indiceIndex + 1] = vertexIndex + 1;
1451 indices[indiceIndex + 2] = vertexIndex + 2;
1454 indices[indiceIndex + 3] = vertexIndex + 3;
1455 indices[indiceIndex + 4] = vertexIndex + 4;
1456 indices[indiceIndex + 5] = vertexIndex + 5;
1457 indices[indiceIndex + 6] = vertexIndex + 4;
1458 indices[indiceIndex + 7] = vertexIndex + 6;
1459 indices[indiceIndex + 8] = vertexIndex + 5;
1463 indices[indiceIndex ] = vertexIndex;
1464 indices[indiceIndex + 1] = vertexIndex + 1;
1465 indices[indiceIndex + 2] = vertexIndex + 2;
1466 indices[indiceIndex + 3] = vertexIndex + 0;
1467 indices[indiceIndex + 4] = vertexIndex + 2;
1468 indices[indiceIndex + 5] = vertexIndex + 3;
1473 } // namespace Internal
1475 } // namespace Toolkit