2 * Copyright (c) 2020 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/object/handle-devel.h>
26 #include <dali/devel-api/scripting/enum-helper.h>
27 #include <dali/devel-api/scripting/scripting.h>
30 #include <dali-toolkit/public-api/visuals/visual-properties.h>
31 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
32 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
47 DALI_ENUM_TO_STRING_TABLE_BEGIN( SHAPE_TYPE )
48 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, SPHERE )
49 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CONE )
50 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CYLINDER )
51 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CUBE )
52 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, OCTAHEDRON )
53 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, BEVELLED_CUBE )
54 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CONICAL_FRUSTUM )
55 DALI_ENUM_TO_STRING_TABLE_END( SHAPE_TYPE )
57 //Primitive property defaults
58 const int DEFAULT_SLICES = 128; ///< For spheres and conics
59 const int DEFAULT_STACKS = 128; ///< For spheres and conics
60 const float DEFAULT_SCALE_TOP_RADIUS = 1.0; ///< For conical frustums
61 const float DEFAULT_SCALE_BOTTOM_RADIUS = 1.5; ///< For cones and conical frustums
62 const float DEFAULT_SCALE_HEIGHT = 3.0; ///< For all conics
63 const float DEFAULT_SCALE_RADIUS = 1.0; ///< For cylinders
64 const float DEFAULT_BEVEL_PERCENTAGE = 0.0; ///< For bevelled cubes
65 const float DEFAULT_BEVEL_SMOOTHNESS = 0.0; ///< For bevelled cubes
66 const Vector4 DEFAULT_COLOR = Vector4( 0.5, 0.5, 0.5, 1.0 ); ///< Grey, for all.
69 const int MIN_SLICES = 3; ///< Minimum number of slices for spheres and conics
70 const int MIN_STACKS = 2; ///< Minimum number of stacks for spheres and conics
71 const int MAX_PARTITIONS = 255; ///< Maximum number of slices or stacks for spheres and conics
72 const float MIN_BEVEL_PERCENTAGE = 0.0; ///< Minimum bevel percentage for bevelled cubes
73 const float MAX_BEVEL_PERCENTAGE = 1.0; ///< Maximum bevel percentage for bevelled cubes
74 const float MIN_SMOOTHNESS = 0.0; ///< Minimum bevel smoothness for bevelled cubes
75 const float MAX_SMOOTHNESS = 1.0; ///< Maximum bevel smoothness for bevelled cubes
77 //Specific shape labels.
78 const char * const SPHERE_LABEL( "SPHERE" );
79 const char * const CONE_LABEL( "CONE" );
80 const char * const CYLINDER_LABEL( "CYLINDER" );
81 const char * const CUBE_LABEL( "CUBE" );
82 const char * const OCTAHEDRON_LABEL( "OCTAHEDRON" );
83 const char * const BEVELLED_CUBE_LABEL( "BEVELLED_CUBE" );
84 const char * const CONICAL_FRUSTUM_LABEL( "CONICAL_FRUSTUM" );
87 const char * const OBJECT_MATRIX_UNIFORM_NAME( "uObjectMatrix" );
88 const char * const OBJECT_DIMENSIONS_UNIFORM_NAME( "uObjectDimensions" );
89 const char * const STAGE_OFFSET_UNIFORM_NAME( "uStageOffset" );
92 const char * const POSITION( "aPosition");
93 const char * const NORMAL( "aNormal" );
94 const char * const INDICES( "aIndices" );
96 //A simple shader that applies diffuse lighting to a mono-coloured object.
97 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
98 attribute highp vec3 aPosition;\n
99 attribute highp vec2 aTexCoord;\n
100 attribute highp vec3 aNormal;\n
101 varying mediump vec3 vIllumination;\n
102 uniform mediump vec3 uSize;\n
103 uniform mediump vec3 uObjectDimensions;\n
104 uniform mediump mat4 uMvpMatrix;\n
105 uniform mediump mat4 uModelView;\n
106 uniform mediump mat4 uViewMatrix;\n
107 uniform mediump mat3 uNormalMatrix;\n
108 uniform mediump mat4 uObjectMatrix;\n
109 uniform mediump vec3 lightPosition;\n
110 uniform mediump vec2 uStageOffset;\n
112 //Visual size and offset
113 uniform mediump vec2 offset;\n
114 uniform mediump vec2 size;\n
115 uniform mediump vec4 offsetSizeMode;\n
116 uniform mediump vec2 origin;\n
117 uniform mediump vec2 anchorPoint;\n
119 vec4 ComputeVertexPosition()\n
121 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
122 float scaleFactor = min( visualSize.x / uObjectDimensions.x, visualSize.y / uObjectDimensions.y );\n
123 vec3 originFlipY = vec3(origin.x, -origin.y, 0.0);
124 vec3 anchorPointFlipY = vec3( anchorPoint.x, -anchorPoint.y, 0.0);
125 vec3 offset = vec3( ( offset / uSize.xy ) * offsetSizeMode.xy + offset * (1.0-offsetSizeMode.xy), 0.0) * vec3(1.0,-1.0,1.0);\n
127 return vec4( (aPosition + anchorPointFlipY)*scaleFactor + (offset + originFlipY)*uSize, 1.0 );\n
132 vec4 normalisedVertexPosition = ComputeVertexPosition();\n
133 vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
134 vertexPosition = uMvpMatrix * vertexPosition;\n
136 //Illumination in Model-View space - Transform attributes and uniforms\n
137 vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n
138 vec3 normal = uNormalMatrix * mat3( uObjectMatrix ) * aNormal;\n
140 vec4 mvLightPosition = vec4( ( lightPosition.xy - uStageOffset ), lightPosition.z, 1.0 );\n
141 mvLightPosition = uViewMatrix * mvLightPosition;\n
142 vec3 vectorToLight = normalize( mvLightPosition.xyz - mvVertexPosition.xyz );\n
144 float lightDiffuse = max( dot( vectorToLight, normal ), 0.0 );\n
145 vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
147 gl_Position = vertexPosition;\n
151 //Very simple fragment shader that merely applies the vertex shading to the color at each fragment.
152 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
153 precision mediump float;\n
154 varying mediump vec3 vIllumination;\n
155 uniform lowp vec4 uColor;\n
156 uniform lowp vec3 mixColor;\n
159 vec4 baseColor = vec4(mixColor, 1.0) * uColor;\n
160 gl_FragColor = vec4( vIllumination.rgb * baseColor.rgb, baseColor.a );\n
164 } // unnamed namespace
166 PrimitiveVisualPtr PrimitiveVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
168 PrimitiveVisualPtr primitiveVisualPtr( new PrimitiveVisual( factoryCache ) );
169 primitiveVisualPtr->SetProperties( properties );
170 return primitiveVisualPtr;
173 PrimitiveVisual::PrimitiveVisual( VisualFactoryCache& factoryCache )
174 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO, Toolkit::Visual::PRIMITIVE ),
175 mScaleDimensions( Vector3::ONE ),
176 mScaleTopRadius( DEFAULT_SCALE_TOP_RADIUS ),
177 mScaleBottomRadius( DEFAULT_SCALE_BOTTOM_RADIUS ),
178 mScaleHeight( DEFAULT_SCALE_HEIGHT ),
179 mScaleRadius( DEFAULT_SCALE_RADIUS ),
180 mBevelPercentage( DEFAULT_BEVEL_PERCENTAGE ),
181 mBevelSmoothness( DEFAULT_BEVEL_SMOOTHNESS ),
182 mSlices( DEFAULT_SLICES ),
183 mStacks( DEFAULT_STACKS ),
184 mPrimitiveType( Toolkit::PrimitiveVisual::Shape::SPHERE )
186 mImpl->mMixColor = DEFAULT_COLOR;
189 PrimitiveVisual::~PrimitiveVisual()
193 void PrimitiveVisual::DoSetProperties( const Property::Map& propertyMap )
195 //Find out which shape to renderer.
196 Property::Value* primitiveTypeValue = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SHAPE, PRIMITIVE_SHAPE );
197 if( primitiveTypeValue )
199 Scripting::GetEnumerationProperty( *primitiveTypeValue, SHAPE_TYPE_TABLE, SHAPE_TYPE_TABLE_COUNT, mPrimitiveType );
203 DALI_LOG_ERROR( "Fail to provide shape to the PrimitiveVisual object.\n" );
206 // By virtue of DoSetProperties being called last, this will override
207 // anything set by Toolkit::Visual::Property::MIX_COLOR
208 Property::Value* colorValue = propertyMap.Find( Toolkit::PrimitiveVisual::Property::MIX_COLOR, MIX_COLOR );
212 if( colorValue->Get( color ) )
214 Property::Type type = colorValue->GetType();
215 if( type == Property::VECTOR4 )
217 SetMixColor( color );
219 else if( type == Property::VECTOR3 )
221 Vector3 color3(color);
222 SetMixColor( color3 );
227 Property::Value* slices = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SLICES, SLICES );
230 if( slices->Get( mSlices ) )
233 if( mSlices > MAX_PARTITIONS )
235 mSlices = MAX_PARTITIONS;
236 DALI_LOG_WARNING( "Value for slices clamped.\n" );
238 else if ( mSlices < MIN_SLICES )
240 mSlices = MIN_SLICES;
241 DALI_LOG_WARNING( "Value for slices clamped.\n" );
246 DALI_LOG_ERROR( "Invalid type for slices in PrimitiveVisual.\n" );
250 Property::Value* stacks = propertyMap.Find( Toolkit::PrimitiveVisual::Property::STACKS, STACKS );
253 if( stacks->Get( mStacks ) )
256 if( mStacks > MAX_PARTITIONS )
258 mStacks = MAX_PARTITIONS;
259 DALI_LOG_WARNING( "Value for stacks clamped.\n" );
261 else if ( mStacks < MIN_STACKS )
263 mStacks = MIN_STACKS;
264 DALI_LOG_WARNING( "Value for stacks clamped.\n" );
269 DALI_LOG_ERROR( "Invalid type for stacks in PrimitiveVisual.\n" );
273 Property::Value* scaleTop = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_TOP_RADIUS, SCALE_TOP_RADIUS );
274 if( scaleTop && !scaleTop->Get( mScaleTopRadius ) )
276 DALI_LOG_ERROR( "Invalid type for scale top radius in PrimitiveVisual.\n" );
279 Property::Value* scaleBottom = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_BOTTOM_RADIUS, SCALE_BOTTOM_RADIUS );
280 if( scaleBottom && !scaleBottom->Get( mScaleBottomRadius ) )
282 DALI_LOG_ERROR( "Invalid type for scale bottom radius in PrimitiveVisual.\n" );
285 Property::Value* scaleHeight = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_HEIGHT, SCALE_HEIGHT );
286 if( scaleHeight && !scaleHeight->Get( mScaleHeight ) )
288 DALI_LOG_ERROR( "Invalid type for scale height in PrimitiveVisual.\n" );
291 Property::Value* scaleRadius = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_RADIUS, SCALE_RADIUS );
292 if( scaleRadius && !scaleRadius->Get( mScaleRadius ) )
294 DALI_LOG_ERROR( "Invalid type for scale radius in PrimitiveVisual.\n" );
297 Property::Value* dimensions = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_DIMENSIONS, SCALE_DIMENSIONS );
300 if( dimensions->Get( mScaleDimensions ) )
302 //If any dimension is invalid, set it to a sensible default.
303 if( mScaleDimensions.x <= 0.0 )
305 mScaleDimensions.x = 1.0;
306 DALI_LOG_WARNING( "Value for scale dimensions clamped. Must be greater than zero.\n" );
308 if( mScaleDimensions.y <= 0.0 )
310 mScaleDimensions.y = 1.0;
311 DALI_LOG_WARNING( "Value for scale dimensions clamped. Must be greater than zero.\n" );
313 if( mScaleDimensions.z <= 0.0 )
315 mScaleDimensions.z = 1.0;
316 DALI_LOG_WARNING( "Value for scale dimensions clamped. Must be greater than zero.\n" );
321 DALI_LOG_ERROR( "Invalid type for scale dimensions in PrimitiveVisual.\n" );
325 Property::Value* bevel = propertyMap.Find( Toolkit::PrimitiveVisual::Property::BEVEL_PERCENTAGE, BEVEL_PERCENTAGE );
328 if( bevel->Get( mBevelPercentage ) )
331 if( mBevelPercentage < MIN_BEVEL_PERCENTAGE )
333 mBevelPercentage = MIN_BEVEL_PERCENTAGE;
334 DALI_LOG_WARNING( "Value for bevel percentage clamped.\n" );
336 else if( mBevelPercentage > MAX_BEVEL_PERCENTAGE )
338 mBevelPercentage = MAX_BEVEL_PERCENTAGE;
339 DALI_LOG_WARNING( "Value for bevel percentage clamped.\n" );
344 DALI_LOG_ERROR( "Invalid type for bevel percentage in PrimitiveVisual.\n" );
348 Property::Value* smoothness = propertyMap.Find( Toolkit::PrimitiveVisual::Property::BEVEL_SMOOTHNESS, BEVEL_SMOOTHNESS );
351 if( smoothness->Get( mBevelSmoothness ) )
354 if( mBevelSmoothness < MIN_SMOOTHNESS )
356 mBevelSmoothness = MIN_SMOOTHNESS;
357 DALI_LOG_WARNING( "Value for bevel smoothness clamped.\n" );
359 else if( mBevelSmoothness > MAX_SMOOTHNESS )
361 mBevelSmoothness = MAX_SMOOTHNESS;
362 DALI_LOG_WARNING( "Value for bevel smoothness clamped.\n" );
367 DALI_LOG_ERROR( "Invalid type for bevel smoothness in PrimitiveVisual.\n" );
371 //Read in light position.
372 Property::Value* lightPosition = propertyMap.Find( Toolkit::PrimitiveVisual::Property::LIGHT_POSITION, LIGHT_POSITION_UNIFORM_NAME );
375 if( !lightPosition->Get( mLightPosition ) )
377 DALI_LOG_ERROR( "Invalid value passed for light position in MeshVisual object.\n" );
378 mLightPosition = Vector3::ZERO;
383 //Default behaviour is to place the light directly in front of the object,
384 // at a reasonable distance to light everything on screen.
385 Stage stage = Stage::GetCurrent();
387 mLightPosition = Vector3( stage.GetSize().width / 2, stage.GetSize().height / 2, stage.GetSize().width * 5 );
391 void PrimitiveVisual::GetNaturalSize( Vector2& naturalSize )
398 naturalSize.x = mObjectDimensions.x;
399 naturalSize.y = mObjectDimensions.y;
402 void PrimitiveVisual::DoSetOnStage( Actor& actor )
404 InitializeRenderer();
406 actor.AddRenderer( mImpl->mRenderer );
408 // Primitive generated and ready to display
409 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
412 void PrimitiveVisual::DoCreatePropertyMap( Property::Map& map ) const
415 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::PRIMITIVE );
416 map.Insert( Toolkit::PrimitiveVisual::Property::MIX_COLOR, mImpl->mMixColor );
417 map.Insert( Toolkit::PrimitiveVisual::Property::SHAPE, mPrimitiveType );
418 map.Insert( Toolkit::PrimitiveVisual::Property::SLICES, mSlices );
419 map.Insert( Toolkit::PrimitiveVisual::Property::STACKS, mStacks );
420 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_TOP_RADIUS, mScaleTopRadius );
421 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_BOTTOM_RADIUS, mScaleBottomRadius );
422 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_HEIGHT, mScaleHeight );
423 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_RADIUS, mScaleRadius );
424 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_DIMENSIONS, mScaleDimensions );
425 map.Insert( Toolkit::PrimitiveVisual::Property::BEVEL_PERCENTAGE, mBevelPercentage );
426 map.Insert( Toolkit::PrimitiveVisual::Property::BEVEL_SMOOTHNESS, mBevelSmoothness );
427 map.Insert( Toolkit::PrimitiveVisual::Property::LIGHT_POSITION, mLightPosition );
430 void PrimitiveVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
435 void PrimitiveVisual::OnSetTransform()
437 if( mImpl->mRenderer )
439 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
443 void PrimitiveVisual::InitializeRenderer()
455 mImpl->mRenderer = Renderer::New( mGeometry, mShader );
456 mImpl->mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
458 // Register transform properties
459 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
461 mImpl->mMixColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::PrimitiveVisual::Property::MIX_COLOR, MIX_COLOR, Vector3(mImpl->mMixColor) );
464 void PrimitiveVisual::UpdateShaderUniforms()
466 Stage stage = Stage::GetCurrent();
467 float width = stage.GetSize().width;
468 float height = stage.GetSize().height;
470 //Flip model to account for DALi starting with (0, 0) at the top left.
472 scaleMatrix.SetIdentityAndScale( Vector3( 1.0, -1.0, 1.0 ) );
474 mShader.RegisterProperty( STAGE_OFFSET_UNIFORM_NAME, Vector2( width, height ) / 2.0f );
475 mShader.RegisterProperty( LIGHT_POSITION_UNIFORM_NAME, mLightPosition );
476 mShader.RegisterProperty( OBJECT_MATRIX_UNIFORM_NAME, scaleMatrix );
477 mShader.RegisterProperty( OBJECT_DIMENSIONS_UNIFORM_NAME, mObjectDimensions );
480 void PrimitiveVisual::CreateShader()
482 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
483 UpdateShaderUniforms();
486 void PrimitiveVisual::CreateGeometry()
488 Dali::Vector<Vertex> vertices;
489 Dali::Vector<unsigned short> indices;
491 switch( mPrimitiveType )
493 case Toolkit::PrimitiveVisual::Shape::SPHERE:
495 CreateSphere( vertices, indices, mSlices, mStacks );
498 case Toolkit::PrimitiveVisual::Shape::CONE:
500 //Create a conic with zero top radius.
501 CreateConic( vertices, indices, 0, mScaleBottomRadius, mScaleHeight, mSlices );
504 case Toolkit::PrimitiveVisual::Shape::CYLINDER:
506 //Create a conic with equal radii on the top and bottom.
507 CreateConic( vertices, indices, mScaleRadius, mScaleRadius, mScaleHeight, mSlices );
510 case Toolkit::PrimitiveVisual::Shape::CUBE:
512 //Create a cube by creating a bevelled cube with minimum bevel.
513 CreateBevelledCube( vertices, indices, mScaleDimensions, 0.0, 0.0 );
516 case Toolkit::PrimitiveVisual::Shape::OCTAHEDRON:
518 //Create an octahedron by creating a bevelled cube with maximum bevel.
519 CreateBevelledCube( vertices, indices, mScaleDimensions, 1.0, mBevelSmoothness );
522 case Toolkit::PrimitiveVisual::Shape::BEVELLED_CUBE:
524 CreateBevelledCube( vertices, indices, mScaleDimensions, mBevelPercentage, mBevelSmoothness );
527 case Toolkit::PrimitiveVisual::Shape::CONICAL_FRUSTUM:
529 CreateConic( vertices, indices, mScaleTopRadius, mScaleBottomRadius, mScaleHeight, mSlices );
534 mGeometry = Geometry::New();
537 Property::Map vertexFormat;
538 vertexFormat[POSITION] = Property::VECTOR3;
539 vertexFormat[NORMAL] = Property::VECTOR3;
540 PropertyBuffer surfaceVertices = PropertyBuffer::New( vertexFormat );
541 surfaceVertices.SetData( &vertices[0], vertices.Size() );
543 mGeometry.AddVertexBuffer( surfaceVertices );
545 //Indices for triangle formulation
546 mGeometry.SetIndexBuffer( &indices[0], indices.Size() );
549 void PrimitiveVisual::CreateSphere( Vector<Vertex>& vertices, Vector<unsigned short>& indices, int slices, int stacks )
551 ComputeSphereVertices( vertices, slices, stacks );
552 FormSphereTriangles( indices, slices, stacks );
554 mObjectDimensions = Vector3::ONE;
557 void PrimitiveVisual::CreateConic( Vector<Vertex>& vertices, Vector<unsigned short>& indices, float scaleTopRadius,
558 float scaleBottomRadius, float scaleHeight, int slices )
560 ComputeConicVertices( vertices, scaleTopRadius, scaleBottomRadius, scaleHeight, slices );
561 FormConicTriangles( indices, scaleTopRadius, scaleBottomRadius, slices );
563 //Determine object dimensions, and scale them to be between 0.0 and 1.0.
564 float xDimension = std::max( scaleTopRadius, scaleBottomRadius ) * 2.0f;
565 float yDimension = scaleHeight;
566 float largestDimension = std::max( xDimension, yDimension );
568 mObjectDimensions = Vector3( xDimension / largestDimension, yDimension / largestDimension,
569 xDimension / largestDimension );
572 void PrimitiveVisual::CreateBevelledCube( Vector<Vertex>& vertices, Vector<unsigned short>& indices,
573 Vector3 dimensions, float bevelPercentage, float bevelSmoothness )
575 float maxDimension = std::max( std::max( dimensions.x, dimensions.y ), dimensions.z );
576 dimensions = dimensions / maxDimension;
578 if( bevelPercentage <= MIN_BEVEL_PERCENTAGE ) //No bevel, form a cube.
580 ComputeCubeVertices( vertices, dimensions );
581 FormCubeTriangles( indices );
583 else if( bevelPercentage >= MAX_BEVEL_PERCENTAGE ) //Max bevel, form an octahedron.
585 ComputeOctahedronVertices( vertices, dimensions, bevelSmoothness );
586 FormOctahedronTriangles( indices );
588 else //In between, form a bevelled cube.
590 ComputeBevelledCubeVertices( vertices, dimensions, bevelPercentage, bevelSmoothness );
591 FormBevelledCubeTriangles( indices );
594 mObjectDimensions = dimensions;
597 void PrimitiveVisual::ComputeCircleTables( Vector<float>& sinTable, Vector<float>& cosTable, int divisions,
605 const float angleDivision = ( halfCircle ? 1.0f : 2.0f ) * Dali::Math::PI / ( float ) divisions;
607 sinTable.Resize( divisions );
608 cosTable.Resize( divisions );
610 for( int i = 0; i < divisions; i++ )
612 sinTable[i] = sin( angleDivision * i );
613 cosTable[i] = cos( angleDivision * i );
617 void PrimitiveVisual::ComputeSphereVertices( Vector<Vertex>& vertices, int slices, int stacks )
619 //Tables for calculating slices angles and stacks angles, respectively.
620 Vector<float> sinTable1;
621 Vector<float> cosTable1;
622 Vector<float> sinTable2;
623 Vector<float> cosTable2;
625 ComputeCircleTables( sinTable1, cosTable1, slices, false );
626 ComputeCircleTables( sinTable2, cosTable2, stacks, true );
628 int numVertices = slices * ( stacks - 1 ) + 2;
629 vertices.Resize( numVertices );
631 int vertexIndex = 0; //Track progress through vertices.
637 vertices[vertexIndex].position = Vector3( 0.0, 0.5, 0.0 );
638 vertices[vertexIndex].normal = Vector3( 0.0, 1.0, 0.0 );
642 for( int i = 1; i < stacks; i++ )
644 for( int j = 0; j < slices; j++, vertexIndex++ )
646 x = cosTable1[j] * sinTable2[i];
648 z = sinTable1[j] * sinTable2[i];
650 vertices[vertexIndex].position = Vector3( x / 2.0f, y / 2.0f, z / 2.0f );
651 vertices[vertexIndex].normal = Vector3( x, y, z );
656 vertices[vertexIndex].position = Vector3( 0.0, -0.5, 0.0 );
657 vertices[vertexIndex].normal = Vector3( 0.0, -1.0, 0.0 );
660 void PrimitiveVisual::FormSphereTriangles( Vector<unsigned short>& indices, int slices, int stacks )
664 //Set indices to placeholder "error" values.
665 //This will display nothing, which is the expected behaviour for this edge case.
670 int numTriangles = 2 * slices * ( stacks - 1 );
672 indices.Resize( 3 * numTriangles );
674 int indiceIndex = 0; //Used to keep track of progress through indices.
675 int previousCycleBeginning = 1; //Stores the index of the vertex that started the cycle of the previous stack.
676 int currentCycleBeginning = 1 + slices;
678 //Top stack. Loop from index 1 to index slices, as not counting the very first vertex.
679 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
681 indices[indiceIndex] = 0;
684 //End, so loop around.
685 indices[indiceIndex + 1] = 1;
689 indices[indiceIndex + 1] = i + 1;
691 indices[indiceIndex + 2] = i;
694 //Middle Stacks. Want to form triangles between the top and bottom stacks, so loop up to the number of stacks - 2.
695 for( int i = 0; i < stacks - 2; i++, previousCycleBeginning += slices, currentCycleBeginning += slices )
697 for( int j = 0; j < slices; j++, indiceIndex += 6 )
699 if( j == slices - 1 )
701 //End, so loop around.
702 indices[indiceIndex] = previousCycleBeginning + j;
703 indices[indiceIndex + 1] = previousCycleBeginning;
704 indices[indiceIndex + 2] = currentCycleBeginning + j;
705 indices[indiceIndex + 3] = currentCycleBeginning + j;
706 indices[indiceIndex + 4] = previousCycleBeginning;
707 indices[indiceIndex + 5] = currentCycleBeginning;
711 indices[indiceIndex] = previousCycleBeginning + j;
712 indices[indiceIndex + 1] = previousCycleBeginning + 1 + j;
713 indices[indiceIndex + 2] = currentCycleBeginning + j;
714 indices[indiceIndex + 3] = currentCycleBeginning + j;
715 indices[indiceIndex + 4] = previousCycleBeginning + 1 + j;
716 indices[indiceIndex + 5] = currentCycleBeginning + 1 + j;
721 //Bottom stack. Loop around the last stack from the previous loop, and go up to the penultimate vertex.
722 for( int i = 0; i < slices; i++, indiceIndex += 3 )
724 indices[indiceIndex] = previousCycleBeginning + slices;
725 indices[indiceIndex + 1] = previousCycleBeginning + i;
726 if( i == slices - 1 )
728 //End, so loop around.
729 indices[indiceIndex + 2] = previousCycleBeginning;
733 indices[indiceIndex + 2] = previousCycleBeginning + i + 1;
738 void PrimitiveVisual::ComputeConicVertices( Vector<Vertex>& vertices, float scaleTopRadius,
739 float scaleBottomRadius, float scaleHeight, int slices )
741 int vertexIndex = 0; //Track progress through vertices.
742 Vector<float> sinTable;
743 Vector<float> cosTable;
745 ComputeCircleTables( sinTable, cosTable, slices, false );
747 int numVertices = 2; //Always will have one at the top and one at the bottom.
749 //Add vertices for each circle. Need two per point for different face normals.
750 if( scaleTopRadius > 0.0 )
752 numVertices += 2 * slices;
754 if( scaleBottomRadius > 0.0 )
756 numVertices += 2 * slices;
759 vertices.Resize( numVertices );
762 //Scale to bounding region of -0.5 to 0.5 (i.e range of 1).
763 float biggestObjectDimension = std::max( std::max( scaleTopRadius * 2.0f, scaleBottomRadius * 2.0f ), scaleHeight );
764 scaleTopRadius = scaleTopRadius / biggestObjectDimension;
765 scaleBottomRadius = scaleBottomRadius / biggestObjectDimension;
767 //Dimensions for vertex coordinates. Y is constant, and so can be initialised now.
769 float y = scaleHeight / biggestObjectDimension / 2.0f;
773 vertices[0].position = Vector3( 0, y, 0 );
774 vertices[0].normal = Vector3( 0, 1, 0 );
778 if( scaleTopRadius > 0.0 )
780 //Loop around the circle.
781 for( int i = 0; i < slices; i++, vertexIndex++ )
783 x = sinTable[i] * scaleTopRadius;
784 z = cosTable[i] * scaleTopRadius;
786 //Upward-facing normal.
787 vertices[vertexIndex].position = Vector3( x, y, z );
788 vertices[vertexIndex].normal = Vector3( 0, 1, 0 );
790 //Outward-facing normal.
791 vertices[vertexIndex + slices].position = Vector3( x, y, z );
792 vertices[vertexIndex + slices].normal = Vector3( x, 0, z );
795 vertexIndex += slices;
799 if( scaleBottomRadius > 0.0 )
801 //Loop around the circle.
802 for( int i = 0; i < slices; i++, vertexIndex++ )
804 x = sinTable[i] * scaleBottomRadius;
805 z = cosTable[i] * scaleBottomRadius;
807 //Outward-facing normal.
808 vertices[vertexIndex].position = Vector3( x, -y, z );
809 vertices[vertexIndex].normal = Vector3( x, 0, z );
811 //Downward-facing normal.
812 vertices[vertexIndex + slices].position = Vector3( x, -y, z );
813 vertices[vertexIndex + slices].normal = Vector3( 0, -1, 0 );
816 vertexIndex += slices;
820 vertices[vertexIndex].position = Vector3( 0, -y, 0 );
821 vertices[vertexIndex].normal = Vector3( 0, -1, 0 );
825 void PrimitiveVisual::FormConicTriangles( Vector<unsigned short>& indices, float scaleTopRadius,
826 float scaleBottomRadius, int slices )
828 int indiceIndex = 0; //Track progress through indices.
829 int numTriangles = 0;
830 bool coneTop = scaleTopRadius <= 0.0;
831 bool coneBottom = scaleBottomRadius <= 0.0;
833 if( coneTop && coneBottom )
835 //Set indices to placeholder "error" values.
836 //This will display nothing, which is the expected behaviour for this edge case.
843 numTriangles += 2 * slices;
847 numTriangles += 2 * slices;
850 indices.Resize( 3 * numTriangles );
852 //Switch on the type of conic we have.
853 if( !coneTop && !coneBottom )
855 //Top circle. Start at index of first outer point and go around.
856 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
858 indices[indiceIndex] = 0;
859 indices[indiceIndex + 1] = i;
862 //End, so loop around.
863 indices[indiceIndex + 2] = 1;
867 indices[indiceIndex + 2] = i + 1;
871 int topCycleBeginning = slices + 1;
872 int bottomCycleBeginning = topCycleBeginning + slices;
875 for( int i = 0; i < slices; i++, indiceIndex += 6 )
877 if( i == slices - 1 )
879 //End, so loop around.
880 indices[indiceIndex] = topCycleBeginning + i;
881 indices[indiceIndex + 1] = bottomCycleBeginning + i;
882 indices[indiceIndex + 2] = topCycleBeginning;
883 indices[indiceIndex + 3] = bottomCycleBeginning + i;
884 indices[indiceIndex + 4] = bottomCycleBeginning;
885 indices[indiceIndex + 5] = topCycleBeginning;
889 indices[indiceIndex] = topCycleBeginning + i;
890 indices[indiceIndex + 1] = bottomCycleBeginning + i;
891 indices[indiceIndex + 2] = topCycleBeginning + 1 + i;
892 indices[indiceIndex + 3] = bottomCycleBeginning + i;
893 indices[indiceIndex + 4] = bottomCycleBeginning + 1 + i;
894 indices[indiceIndex + 5] = topCycleBeginning + 1 + i;
898 int bottomFaceCycleBeginning = bottomCycleBeginning + slices;
901 for( int i = 0; i < slices; i++, indiceIndex += 3 )
903 indices[indiceIndex] = bottomFaceCycleBeginning;
904 if( i == slices - 1 )
906 //End, so loop around.
907 indices[indiceIndex + 1] = bottomFaceCycleBeginning;
911 indices[indiceIndex + 1] = bottomFaceCycleBeginning + i + 1;
913 indices[indiceIndex + 2] = bottomFaceCycleBeginning + i;
916 else if( !coneTop || !coneBottom )
918 //Top circle/edges. Start at index of first outer point and go around.
919 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
921 indices[indiceIndex] = 0;
922 indices[indiceIndex + 1] = i;
925 //End, so loop around.
926 indices[indiceIndex + 2] = 1;
930 indices[indiceIndex + 2] = i + 1;
934 //Bottom circle/edges. Start at index of first outer point and go around.
935 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
937 indices[indiceIndex] = 2 * slices + 1;
940 //End, so loop around.
941 indices[indiceIndex + 1] = slices + 1;
945 indices[indiceIndex + 1] = slices + i + 1;
947 indices[indiceIndex + 2] = slices + i;
952 void PrimitiveVisual::ComputeCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions )
954 int numVertices = 4 * 6; //Four per face.
955 int vertexIndex = 0; //Tracks progress through vertices.
956 float scaledX = 0.5 * dimensions.x;
957 float scaledY = 0.5 * dimensions.y;
958 float scaledZ = 0.5 * dimensions.z;
960 vertices.Resize( numVertices );
962 Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
964 Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
967 positions[0] = Vector3( -scaledX, scaledY, -scaledZ );
968 positions[1] = Vector3( scaledX, scaledY, -scaledZ );
969 positions[2] = Vector3( scaledX, scaledY, scaledZ );
970 positions[3] = Vector3( -scaledX, scaledY, scaledZ );
971 positions[4] = Vector3( -scaledX, -scaledY, -scaledZ );
972 positions[5] = Vector3( scaledX, -scaledY, -scaledZ );
973 positions[6] = Vector3( scaledX, -scaledY, scaledZ );
974 positions[7] = Vector3( -scaledX, -scaledY, scaledZ );
976 normals[0] = Vector3( 0, 1, 0 );
977 normals[1] = Vector3( 0, 0, -1 );
978 normals[2] = Vector3( 1, 0, 0 );
979 normals[3] = Vector3( 0, 0, 1 );
980 normals[4] = Vector3( -1, 0, 0 );
981 normals[5] = Vector3( 0, -1, 0 );
983 //Top face, upward normals.
984 for( int i = 0; i < 4; i++, vertexIndex++ )
986 vertices[vertexIndex].position = positions[i];
987 vertices[vertexIndex].normal = normals[0];
990 //Top face, outward normals.
991 for( int i = 0; i < 4; i++, vertexIndex += 2 )
993 vertices[vertexIndex].position = positions[i];
994 vertices[vertexIndex].normal = normals[i + 1];
998 //End, so loop around.
999 vertices[vertexIndex + 1].position = positions[0];
1003 vertices[vertexIndex + 1].position = positions[i + 1];
1005 vertices[vertexIndex + 1].normal = normals[i + 1];
1008 //Bottom face, outward normals.
1009 for( int i = 0; i < 4; i++, vertexIndex += 2 )
1011 vertices[vertexIndex].position = positions[i + 4];
1012 vertices[vertexIndex].normal = normals[i + 1];
1016 //End, so loop around.
1017 vertices[vertexIndex + 1].position = positions[4];
1021 vertices[vertexIndex + 1].position = positions[i + 5];
1023 vertices[vertexIndex + 1].normal = normals[i + 1];
1026 //Bottom face, downward normals.
1027 for( int i = 0; i < 4; i++, vertexIndex++ )
1029 vertices[vertexIndex].position = positions[i + 4];
1030 vertices[vertexIndex].normal = normals[5];
1035 void PrimitiveVisual::FormCubeTriangles( Vector<unsigned short>& indices )
1037 int numTriangles = 12;
1038 int triangleIndex = 0; //Track progress through indices.
1040 indices.Resize( 3 * numTriangles );
1043 indices[triangleIndex] = 0;
1044 indices[triangleIndex + 1] = 2;
1045 indices[triangleIndex + 2] = 1;
1046 indices[triangleIndex + 3] = 2;
1047 indices[triangleIndex + 4] = 0;
1048 indices[triangleIndex + 5] = 3;
1051 int topFaceStart = 4;
1052 int bottomFaceStart = 12;
1055 for( int i = 0; i < 8; i += 2, triangleIndex += 6 )
1057 indices[triangleIndex ] = i + topFaceStart;
1058 indices[triangleIndex + 1] = i + topFaceStart + 1;
1059 indices[triangleIndex + 2] = i + bottomFaceStart + 1;
1060 indices[triangleIndex + 3] = i + topFaceStart;
1061 indices[triangleIndex + 4] = i + bottomFaceStart + 1;
1062 indices[triangleIndex + 5] = i + bottomFaceStart;
1066 indices[triangleIndex] = 20;
1067 indices[triangleIndex + 1] = 21;
1068 indices[triangleIndex + 2] = 22;
1069 indices[triangleIndex + 3] = 22;
1070 indices[triangleIndex + 4] = 23;
1071 indices[triangleIndex + 5] = 20;
1074 void PrimitiveVisual::ComputeOctahedronVertices( Vector<Vertex>& vertices, Vector3 dimensions, float smoothness )
1076 int numVertices = 3 * 8; //Three per face
1077 int vertexIndex = 0; //Tracks progress through vertices.
1078 float scaledX = 0.5 * dimensions.x;
1079 float scaledY = 0.5 * dimensions.y;
1080 float scaledZ = 0.5 * dimensions.z;
1082 vertices.Resize( numVertices );
1084 Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
1085 positions.Resize(6);
1086 Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
1088 Vector<Vector3> outerNormals; //Holds normals that point outwards at each vertex.
1089 outerNormals.Resize( 6 );
1091 positions[0] = Vector3( 0.0, scaledY, 0.0 );
1092 positions[1] = Vector3( -scaledX, 0.0, 0.0 );
1093 positions[2] = Vector3( 0.0, 0.0, -scaledZ );
1094 positions[3] = Vector3( scaledX, 0.0, 0.0 );
1095 positions[4] = Vector3( 0.0, 0.0, scaledZ );
1096 positions[5] = Vector3( 0.0, -scaledY, 0.0 );
1098 normals[0] = Vector3( -1, 1, -1 );
1099 normals[1] = Vector3( 1, 1, -1 );
1100 normals[2] = Vector3( 1, 1, 1 );
1101 normals[3] = Vector3( -1, 1, 1 );
1102 normals[4] = Vector3( -1, -1, -1 );
1103 normals[5] = Vector3( 1, -1, -1 );
1104 normals[6] = Vector3( 1, -1, 1 );
1105 normals[7] = Vector3( -1, -1, 1 );
1107 outerNormals[0] = Vector3( 0, 1, 0 );
1108 outerNormals[1] = Vector3( -1, 0, 0 );
1109 outerNormals[2] = Vector3( 0, 0, -1 );
1110 outerNormals[3] = Vector3( 1, 0, 0 );
1111 outerNormals[4] = Vector3( 0, 0, 1 );
1112 outerNormals[5] = Vector3( 0, -1, 0 );
1114 //Loop through top faces.
1115 for( int i = 0; i < 4; i++, vertexIndex += 3 )
1119 //End, so loop around.
1120 vertices[vertexIndex ].position = positions[0];
1121 vertices[vertexIndex ].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1122 vertices[vertexIndex + 1].position = positions[1];
1123 vertices[vertexIndex + 1].normal = outerNormals[1] * smoothness + normals[i] * (1 - smoothness);
1124 vertices[vertexIndex + 2].position = positions[i + 1];
1125 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1129 vertices[vertexIndex ].position = positions[0];
1130 vertices[vertexIndex ].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1131 vertices[vertexIndex + 1].position = positions[i + 2];
1132 vertices[vertexIndex + 1].normal = outerNormals[i + 2] * smoothness + normals[i] * (1 - smoothness);
1133 vertices[vertexIndex + 2].position = positions[i + 1];
1134 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1138 //Loop through bottom faces.
1139 for( int i = 0; i < 4; i++, vertexIndex += 3 )
1143 //End, so loop around.
1144 vertices[vertexIndex ].position = positions[5];
1145 vertices[vertexIndex ].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1146 vertices[vertexIndex + 1].position = positions[i + 1];
1147 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1148 vertices[vertexIndex + 2].position = positions[1];
1149 vertices[vertexIndex + 2].normal = outerNormals[1] * smoothness + normals[i + 4] * (1 - smoothness);
1153 vertices[vertexIndex ].position = positions[5];
1154 vertices[vertexIndex ].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1155 vertices[vertexIndex + 1].position = positions[i + 1];
1156 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1157 vertices[vertexIndex + 2].position = positions[i + 2];
1158 vertices[vertexIndex + 2].normal = outerNormals[i + 2] * smoothness + normals[i + 4] * (1 - smoothness);
1163 void PrimitiveVisual::FormOctahedronTriangles( Vector<unsigned short>& indices )
1165 int numTriangles = 8;
1166 int numIndices = numTriangles * 3;
1168 indices.Resize( numIndices );
1170 for( unsigned short i = 0; i < numIndices; i++ )
1176 void PrimitiveVisual::ComputeBevelledCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions,
1177 float bevelPercentage, float bevelSmoothness )
1179 int numPositions = 24;
1181 int numOuterFaces = 6;
1182 int numVertices = 6 * 4 + 12 * 4 + 8 * 3; //Six outer faces, 12 slanting rectangles, 8 slanting triangles.
1183 int vertexIndex = 0; //Track progress through vertices.
1184 int normalIndex = 0; //Track progress through normals, as vertices are calculated per face.
1186 float minDimension = std::min( std::min( dimensions.x, dimensions.y ), dimensions.z );
1187 float bevelAmount = 0.5 * std::min( bevelPercentage, minDimension ); //Cap bevel amount if necessary.
1189 //Distances from centre to outer edge points.
1190 float outerX = 0.5 * dimensions.x;
1191 float outerY = 0.5 * dimensions.y;
1192 float outerZ = 0.5 * dimensions.z;
1194 //Distances from centre to bevelled points.
1195 float bevelX = outerX - bevelAmount;
1196 float bevelY = outerY - bevelAmount;
1197 float bevelZ = outerZ - bevelAmount;
1199 Vector<Vector3> positions; //Holds object points, to be shared between vertexes.
1200 positions.Resize( numPositions );
1201 Vector<Vector3> normals; //Holds face normals, to be shared between vertexes.
1202 normals.Resize( numFaces );
1203 Vector<Vector3> outerNormals; //Holds normals of the outermost faces specifically.
1204 outerNormals.Resize( numOuterFaces );
1205 vertices.Resize( numVertices );
1207 //Topmost face positions.
1208 positions[0 ] = Vector3( -bevelX, outerY, -bevelZ );
1209 positions[1 ] = Vector3( bevelX, outerY, -bevelZ );
1210 positions[2 ] = Vector3( bevelX, outerY, bevelZ );
1211 positions[3 ] = Vector3( -bevelX, outerY, bevelZ );
1213 //Second layer positions.
1214 positions[4 ] = Vector3( -outerX, bevelY, -bevelZ );
1215 positions[5 ] = Vector3( -bevelX, bevelY, -outerZ );
1216 positions[6 ] = Vector3( bevelX, bevelY, -outerZ );
1217 positions[7 ] = Vector3( outerX, bevelY, -bevelZ );
1218 positions[8 ] = Vector3( outerX, bevelY, bevelZ );
1219 positions[9 ] = Vector3( bevelX, bevelY, outerZ );
1220 positions[10] = Vector3( -bevelX, bevelY, outerZ );
1221 positions[11] = Vector3( -outerX, bevelY, bevelZ );
1223 //Third layer positions.
1224 positions[12] = Vector3( -outerX, -bevelY, -bevelZ );
1225 positions[13] = Vector3( -bevelX, -bevelY, -outerZ );
1226 positions[14] = Vector3( bevelX, -bevelY, -outerZ );
1227 positions[15] = Vector3( outerX, -bevelY, -bevelZ );
1228 positions[16] = Vector3( outerX, -bevelY, bevelZ );
1229 positions[17] = Vector3( bevelX, -bevelY, outerZ );
1230 positions[18] = Vector3( -bevelX, -bevelY, outerZ );
1231 positions[19] = Vector3( -outerX, -bevelY, bevelZ );
1233 //Bottom-most face positions.
1234 positions[20] = Vector3( -bevelX, -outerY, -bevelZ );
1235 positions[21] = Vector3( bevelX, -outerY, -bevelZ );
1236 positions[22] = Vector3( bevelX, -outerY, bevelZ );
1237 positions[23] = Vector3( -bevelX, -outerY, bevelZ );
1240 normals[0 ] = Vector3( 0, 1, 0 );
1242 //Top slope normals.
1243 normals[1 ] = Vector3( -1, 1, -1 );
1244 normals[2 ] = Vector3( 0, 1, -1 );
1245 normals[3 ] = Vector3( 1, 1, -1 );
1246 normals[4 ] = Vector3( 1, 1, 0 );
1247 normals[5 ] = Vector3( 1, 1, 1 );
1248 normals[6 ] = Vector3( 0, 1, 1 );
1249 normals[7 ] = Vector3( -1, 1, 1 );
1250 normals[8 ] = Vector3( -1, 1, 0 );
1253 normals[9 ] = Vector3( -1, 0, -1 );
1254 normals[10] = Vector3( 0, 0, -1 );
1255 normals[11] = Vector3( 1, 0, -1 );
1256 normals[12] = Vector3( 1, 0, 0 );
1257 normals[13] = Vector3( 1, 0, 1 );
1258 normals[14] = Vector3( 0, 0, 1 );
1259 normals[15] = Vector3( -1, 0, 1 );
1260 normals[16] = Vector3( -1, 0, 0 );
1262 //Bottom slope normals.
1263 normals[17] = Vector3( -1, -1, -1 );
1264 normals[18] = Vector3( 0, -1, -1 );
1265 normals[19] = Vector3( 1, -1, -1 );
1266 normals[20] = Vector3( 1, -1, 0 );
1267 normals[21] = Vector3( 1, -1, 1 );
1268 normals[22] = Vector3( 0, -1, 1 );
1269 normals[23] = Vector3( -1, -1, 1 );
1270 normals[24] = Vector3( -1, -1, 0 );
1272 //Bottom face normal.
1273 normals[25] = Vector3( 0, -1, 0 );
1275 //Top, back, right, front, left and bottom faces, respectively.
1276 outerNormals[0] = Vector3( 0, 1, 0 );
1277 outerNormals[1] = Vector3( 0, 0, -1 );
1278 outerNormals[2] = Vector3( 1, 0, 0 );
1279 outerNormals[3] = Vector3( 0, 0, 1 );
1280 outerNormals[4] = Vector3( -1, 0, 0 );
1281 outerNormals[5] = Vector3( 0, -1, 0 );
1283 //Topmost face vertices.
1284 for( int i = 0; i < 4; i++, vertexIndex++ )
1286 vertices[vertexIndex].position = positions[i];
1287 vertices[vertexIndex].normal = normals[normalIndex];
1292 //Top slope vertices.
1293 for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
1296 vertices[vertexIndex ].position = positions[i];
1297 vertices[vertexIndex ].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1298 vertices[vertexIndex + 1].position = positions[2 * i + 4];
1299 vertices[vertexIndex + 1].normal = outerNormals[( i == 0 ) ? 4 : i] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1300 vertices[vertexIndex + 2].position = positions[2 * i + 5];
1301 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1306 //End, so loop around.
1307 vertices[vertexIndex + 3].position = positions[i];
1308 vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1309 vertices[vertexIndex + 4].position = positions[0];
1310 vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1311 vertices[vertexIndex + 5].position = positions[2 * i + 5];
1312 vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1313 vertices[vertexIndex + 6].position = positions[4];
1314 vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1318 vertices[vertexIndex + 3].position = positions[i];
1319 vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1320 vertices[vertexIndex + 4].position = positions[i + 1];
1321 vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1322 vertices[vertexIndex + 5].position = positions[2 * i + 5];
1323 vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1324 vertices[vertexIndex + 6].position = positions[2 * i + 6];
1325 vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1329 int secondCycleBeginning = 4;
1330 int thirdCycleBeginning = secondCycleBeginning + 8;
1331 int bottomCycleBeginning = thirdCycleBeginning + 8;
1334 for( int i = 0; i < 8; i++, vertexIndex += 4, normalIndex++ )
1338 //End, so loop around.
1339 vertices[vertexIndex ].position = positions[secondCycleBeginning + i];
1340 vertices[vertexIndex ].normal = normals[normalIndex];
1341 vertices[vertexIndex + 1].position = positions[secondCycleBeginning];
1342 vertices[vertexIndex + 1].normal = normals[normalIndex];
1343 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1344 vertices[vertexIndex + 2].normal = normals[normalIndex];
1345 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning];
1346 vertices[vertexIndex + 3].normal = normals[normalIndex];
1348 else if( (i % 2) == 0 )
1350 //'even' faces are corner ones, and need smoothing.
1351 vertices[vertexIndex ].position = positions[secondCycleBeginning + i];
1352 vertices[vertexIndex ].normal = outerNormals[( i == 0 ) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1353 vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1354 vertices[vertexIndex + 1].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1355 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1356 vertices[vertexIndex + 2].normal = outerNormals[( i == 0 ) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1357 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1358 vertices[vertexIndex + 3].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1362 //'odd' faces are outer ones, and so don't need smoothing.
1363 vertices[vertexIndex ].position = positions[secondCycleBeginning + i];
1364 vertices[vertexIndex ].normal = normals[normalIndex];
1365 vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1366 vertices[vertexIndex + 1].normal = normals[normalIndex];
1367 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1368 vertices[vertexIndex + 2].normal = normals[normalIndex];
1369 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1370 vertices[vertexIndex + 3].normal = normals[normalIndex];
1374 //Bottom slope vertices.
1375 for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
1378 vertices[vertexIndex ].position = positions[thirdCycleBeginning + 2 * i];
1379 vertices[vertexIndex ].normal = outerNormals[( i == 0 ) ? 4 : i] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1380 vertices[vertexIndex + 1].position = positions[thirdCycleBeginning + 2 * i + 1];
1381 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1382 vertices[vertexIndex + 2].position = positions[bottomCycleBeginning + i];
1383 vertices[vertexIndex + 2].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1388 //End, so loop around.
1389 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1390 vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1391 vertices[vertexIndex + 4].position = positions[thirdCycleBeginning];
1392 vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1393 vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1394 vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1395 vertices[vertexIndex + 6].position = positions[bottomCycleBeginning];
1396 vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1400 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1401 vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1402 vertices[vertexIndex + 4].position = positions[thirdCycleBeginning + 2 * i + 2];
1403 vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1404 vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1405 vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1406 vertices[vertexIndex + 6].position = positions[bottomCycleBeginning + i + 1];
1407 vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1411 //Bottom-most face vertices.
1412 for( int i = 0; i < 4; i++, vertexIndex++ )
1414 vertices[vertexIndex].position = positions[ bottomCycleBeginning + i];
1415 vertices[vertexIndex].normal = normals[normalIndex];
1421 void PrimitiveVisual::FormBevelledCubeTriangles( Vector<unsigned short>& indices )
1423 int numTriangles = 44; //(Going from top to bottom, that's 2 + 12 + 16 + 12 + 2)
1424 int indiceIndex = 0; //Track progress through indices.
1425 int vertexIndex = 0; //Track progress through vertices as they're processed.
1427 indices.Resize( 3 * numTriangles );
1430 indices[indiceIndex ] = vertexIndex;
1431 indices[indiceIndex + 1] = vertexIndex + 2;
1432 indices[indiceIndex + 2] = vertexIndex + 1;
1433 indices[indiceIndex + 3] = vertexIndex + 0;
1434 indices[indiceIndex + 4] = vertexIndex + 3;
1435 indices[indiceIndex + 5] = vertexIndex + 2;
1440 for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1443 indices[indiceIndex ] = vertexIndex;
1444 indices[indiceIndex + 1] = vertexIndex + 2;
1445 indices[indiceIndex + 2] = vertexIndex + 1;
1448 indices[indiceIndex + 3] = vertexIndex + 3;
1449 indices[indiceIndex + 4] = vertexIndex + 4;
1450 indices[indiceIndex + 5] = vertexIndex + 5;
1451 indices[indiceIndex + 6] = vertexIndex + 4;
1452 indices[indiceIndex + 7] = vertexIndex + 6;
1453 indices[indiceIndex + 8] = vertexIndex + 5;
1457 for( int i = 0; i < 8; i++, indiceIndex += 6, vertexIndex += 4 )
1459 indices[indiceIndex ] = vertexIndex;
1460 indices[indiceIndex + 1] = vertexIndex + 1;
1461 indices[indiceIndex + 2] = vertexIndex + 2;
1462 indices[indiceIndex + 3] = vertexIndex + 1;
1463 indices[indiceIndex + 4] = vertexIndex + 3;
1464 indices[indiceIndex + 5] = vertexIndex + 2;
1468 for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1471 indices[indiceIndex ] = vertexIndex;
1472 indices[indiceIndex + 1] = vertexIndex + 1;
1473 indices[indiceIndex + 2] = vertexIndex + 2;
1476 indices[indiceIndex + 3] = vertexIndex + 3;
1477 indices[indiceIndex + 4] = vertexIndex + 4;
1478 indices[indiceIndex + 5] = vertexIndex + 5;
1479 indices[indiceIndex + 6] = vertexIndex + 4;
1480 indices[indiceIndex + 7] = vertexIndex + 6;
1481 indices[indiceIndex + 8] = vertexIndex + 5;
1485 indices[indiceIndex ] = vertexIndex;
1486 indices[indiceIndex + 1] = vertexIndex + 1;
1487 indices[indiceIndex + 2] = vertexIndex + 2;
1488 indices[indiceIndex + 3] = vertexIndex + 0;
1489 indices[indiceIndex + 4] = vertexIndex + 2;
1490 indices[indiceIndex + 5] = vertexIndex + 3;
1495 } // namespace Internal
1497 } // namespace Toolkit