2 * Copyright (c) 2018 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, CONICAL_FRUSTRUM ) // deprecated
50 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CONE )
51 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CYLINDER )
52 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CUBE )
53 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, OCTAHEDRON )
54 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, BEVELLED_CUBE )
55 DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::PrimitiveVisual::Shape, CONICAL_FRUSTUM )
56 DALI_ENUM_TO_STRING_TABLE_END( SHAPE_TYPE )
59 const char * const PRIMITIVE_SHAPE( "shape" );
60 const char * const SLICES( "slices" );
61 const char * const STACKS( "stacks" );
62 const char * const SCALE_TOP_RADIUS( "scaleTopRadius" );
63 const char * const SCALE_BOTTOM_RADIUS( "scaleBottomRadius" );
64 const char * const SCALE_HEIGHT( "scaleHeight" );
65 const char * const SCALE_RADIUS( "scaleRadius" );
66 const char * const SCALE_DIMENSIONS( "scaleDimensions" );
67 const char * const BEVEL_PERCENTAGE( "bevelPercentage" );
68 const char * const BEVEL_SMOOTHNESS( "bevelSmoothness" );
69 const char * const LIGHT_POSITION_UNIFORM_NAME( "lightPosition" );
71 //Primitive property defaults
72 const int DEFAULT_SLICES = 128; ///< For spheres and conics
73 const int DEFAULT_STACKS = 128; ///< For spheres and conics
74 const float DEFAULT_SCALE_TOP_RADIUS = 1.0; ///< For conical frustums
75 const float DEFAULT_SCALE_BOTTOM_RADIUS = 1.5; ///< For cones and conical frustums
76 const float DEFAULT_SCALE_HEIGHT = 3.0; ///< For all conics
77 const float DEFAULT_SCALE_RADIUS = 1.0; ///< For cylinders
78 const float DEFAULT_BEVEL_PERCENTAGE = 0.0; ///< For bevelled cubes
79 const float DEFAULT_BEVEL_SMOOTHNESS = 0.0; ///< For bevelled cubes
80 const Vector4 DEFAULT_COLOR = Vector4( 0.5, 0.5, 0.5, 1.0 ); ///< Grey, for all.
83 const int MIN_SLICES = 3; ///< Minimum number of slices for spheres and conics
84 const int MIN_STACKS = 2; ///< Minimum number of stacks for spheres and conics
85 const int MAX_PARTITIONS = 255; ///< Maximum number of slices or stacks for spheres and conics
86 const float MIN_BEVEL_PERCENTAGE = 0.0; ///< Minimum bevel percentage for bevelled cubes
87 const float MAX_BEVEL_PERCENTAGE = 1.0; ///< Maximum bevel percentage for bevelled cubes
88 const float MIN_SMOOTHNESS = 0.0; ///< Minimum bevel smoothness for bevelled cubes
89 const float MAX_SMOOTHNESS = 1.0; ///< Maximum bevel smoothness for bevelled cubes
91 //Specific shape labels.
92 const char * const SPHERE_LABEL( "SPHERE" );
93 const char * const CONE_LABEL( "CONE" );
94 const char * const CONICAL_FRUSTRUM_LABEL( "CONICAL_FRUSTRUM" ); // deprecated
95 const char * const CYLINDER_LABEL( "CYLINDER" );
96 const char * const CUBE_LABEL( "CUBE" );
97 const char * const OCTAHEDRON_LABEL( "OCTAHEDRON" );
98 const char * const BEVELLED_CUBE_LABEL( "BEVELLED_CUBE" );
99 const char * const CONICAL_FRUSTUM_LABEL( "CONICAL_FRUSTUM" );
102 const char * const OBJECT_MATRIX_UNIFORM_NAME( "uObjectMatrix" );
103 const char * const OBJECT_DIMENSIONS_UNIFORM_NAME( "uObjectDimensions" );
104 const char * const STAGE_OFFSET_UNIFORM_NAME( "uStageOffset" );
107 const char * const POSITION( "aPosition");
108 const char * const NORMAL( "aNormal" );
109 const char * const INDICES( "aIndices" );
111 //A simple shader that applies diffuse lighting to a mono-coloured object.
112 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
113 attribute highp vec3 aPosition;\n
114 attribute highp vec2 aTexCoord;\n
115 attribute highp vec3 aNormal;\n
116 varying mediump vec3 vIllumination;\n
117 uniform mediump vec3 uSize;\n
118 uniform mediump vec3 uObjectDimensions;\n
119 uniform mediump mat4 uMvpMatrix;\n
120 uniform mediump mat4 uModelView;\n
121 uniform mediump mat4 uViewMatrix;\n
122 uniform mediump mat3 uNormalMatrix;\n
123 uniform mediump mat4 uObjectMatrix;\n
124 uniform mediump vec3 lightPosition;\n
125 uniform mediump vec2 uStageOffset;\n
127 //Visual size and offset
128 uniform mediump vec2 offset;\n
129 uniform mediump vec2 size;\n
130 uniform mediump vec4 offsetSizeMode;\n
131 uniform mediump vec2 origin;\n
132 uniform mediump vec2 anchorPoint;\n
134 vec4 ComputeVertexPosition()\n
136 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
137 float scaleFactor = min( visualSize.x / uObjectDimensions.x, visualSize.y / uObjectDimensions.y );\n
138 vec3 originFlipY = vec3(origin.x, -origin.y, 0.0);
139 vec3 anchorPointFlipY = vec3( anchorPoint.x, -anchorPoint.y, 0.0);
140 vec3 offset = vec3( ( offset / uSize.xy ) * offsetSizeMode.xy + offset * (1.0-offsetSizeMode.xy), 0.0) * vec3(1.0,-1.0,1.0);\n
142 return vec4( (aPosition + anchorPointFlipY)*scaleFactor + (offset + originFlipY)*uSize, 1.0 );\n
147 vec4 normalisedVertexPosition = ComputeVertexPosition();\n
148 vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
149 vertexPosition = uMvpMatrix * vertexPosition;\n
151 //Illumination in Model-View space - Transform attributes and uniforms\n
152 vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n
153 vec3 normal = uNormalMatrix * mat3( uObjectMatrix ) * aNormal;\n
155 vec4 mvLightPosition = vec4( ( lightPosition.xy - uStageOffset ), lightPosition.z, 1.0 );\n
156 mvLightPosition = uViewMatrix * mvLightPosition;\n
157 vec3 vectorToLight = normalize( mvLightPosition.xyz - mvVertexPosition.xyz );\n
159 float lightDiffuse = max( dot( vectorToLight, normal ), 0.0 );\n
160 vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
162 gl_Position = vertexPosition;\n
166 //Very simple fragment shader that merely applies the vertex shading to the color at each fragment.
167 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
168 precision mediump float;\n
169 varying mediump vec3 vIllumination;\n
170 uniform lowp vec4 uColor;\n
171 uniform lowp vec3 mixColor;\n
174 vec4 baseColor = vec4(mixColor, 1.0) * uColor;\n
175 gl_FragColor = vec4( vIllumination.rgb * baseColor.rgb, baseColor.a );\n
179 } // unnamed namespace
181 PrimitiveVisualPtr PrimitiveVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
183 PrimitiveVisualPtr primitiveVisualPtr( new PrimitiveVisual( factoryCache ) );
184 primitiveVisualPtr->SetProperties( properties );
185 return primitiveVisualPtr;
188 PrimitiveVisual::PrimitiveVisual( VisualFactoryCache& factoryCache )
189 : Visual::Base( factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ),
190 mScaleDimensions( Vector3::ONE ),
191 mScaleTopRadius( DEFAULT_SCALE_TOP_RADIUS ),
192 mScaleBottomRadius( DEFAULT_SCALE_BOTTOM_RADIUS ),
193 mScaleHeight( DEFAULT_SCALE_HEIGHT ),
194 mScaleRadius( DEFAULT_SCALE_RADIUS ),
195 mBevelPercentage( DEFAULT_BEVEL_PERCENTAGE ),
196 mBevelSmoothness( DEFAULT_BEVEL_SMOOTHNESS ),
197 mSlices( DEFAULT_SLICES ),
198 mStacks( DEFAULT_STACKS ),
199 mPrimitiveType( Toolkit::PrimitiveVisual::Shape::SPHERE )
201 mImpl->mMixColor = DEFAULT_COLOR;
204 PrimitiveVisual::~PrimitiveVisual()
208 void PrimitiveVisual::DoSetProperties( const Property::Map& propertyMap )
210 //Find out which shape to renderer.
211 Property::Value* primitiveTypeValue = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SHAPE, PRIMITIVE_SHAPE );
212 if( primitiveTypeValue )
214 Scripting::GetEnumerationProperty( *primitiveTypeValue, SHAPE_TYPE_TABLE, SHAPE_TYPE_TABLE_COUNT, mPrimitiveType );
218 DALI_LOG_ERROR( "Fail to provide shape to the PrimitiveVisual object.\n" );
221 // By virtue of DoSetProperties being called last, this will override
222 // anything set by Toolkit::Visual::Property::MIX_COLOR
223 Property::Value* colorValue = propertyMap.Find( Toolkit::PrimitiveVisual::Property::MIX_COLOR, MIX_COLOR );
227 if( colorValue->Get( color ) )
229 Property::Type type = colorValue->GetType();
230 if( type == Property::VECTOR4 )
232 SetMixColor( color );
234 else if( type == Property::VECTOR3 )
236 Vector3 color3(color);
237 SetMixColor( color3 );
242 Property::Value* slices = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SLICES, SLICES );
245 if( slices->Get( mSlices ) )
248 if( mSlices > MAX_PARTITIONS )
250 mSlices = MAX_PARTITIONS;
251 DALI_LOG_WARNING( "Value for slices clamped.\n" );
253 else if ( mSlices < MIN_SLICES )
255 mSlices = MIN_SLICES;
256 DALI_LOG_WARNING( "Value for slices clamped.\n" );
261 DALI_LOG_ERROR( "Invalid type for slices in PrimitiveVisual.\n" );
265 Property::Value* stacks = propertyMap.Find( Toolkit::PrimitiveVisual::Property::STACKS, STACKS );
268 if( stacks->Get( mStacks ) )
271 if( mStacks > MAX_PARTITIONS )
273 mStacks = MAX_PARTITIONS;
274 DALI_LOG_WARNING( "Value for stacks clamped.\n" );
276 else if ( mStacks < MIN_STACKS )
278 mStacks = MIN_STACKS;
279 DALI_LOG_WARNING( "Value for stacks clamped.\n" );
284 DALI_LOG_ERROR( "Invalid type for stacks in PrimitiveVisual.\n" );
288 Property::Value* scaleTop = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_TOP_RADIUS, SCALE_TOP_RADIUS );
289 if( scaleTop && !scaleTop->Get( mScaleTopRadius ) )
291 DALI_LOG_ERROR( "Invalid type for scale top radius in PrimitiveVisual.\n" );
294 Property::Value* scaleBottom = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_BOTTOM_RADIUS, SCALE_BOTTOM_RADIUS );
295 if( scaleBottom && !scaleBottom->Get( mScaleBottomRadius ) )
297 DALI_LOG_ERROR( "Invalid type for scale bottom radius in PrimitiveVisual.\n" );
300 Property::Value* scaleHeight = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_HEIGHT, SCALE_HEIGHT );
301 if( scaleHeight && !scaleHeight->Get( mScaleHeight ) )
303 DALI_LOG_ERROR( "Invalid type for scale height in PrimitiveVisual.\n" );
306 Property::Value* scaleRadius = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_RADIUS, SCALE_RADIUS );
307 if( scaleRadius && !scaleRadius->Get( mScaleRadius ) )
309 DALI_LOG_ERROR( "Invalid type for scale radius in PrimitiveVisual.\n" );
312 Property::Value* dimensions = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_DIMENSIONS, SCALE_DIMENSIONS );
315 if( dimensions->Get( mScaleDimensions ) )
317 //If any dimension is invalid, set it to a sensible default.
318 if( mScaleDimensions.x <= 0.0 )
320 mScaleDimensions.x = 1.0;
321 DALI_LOG_WARNING( "Value for scale dimensions clamped. Must be greater than zero.\n" );
323 if( mScaleDimensions.y <= 0.0 )
325 mScaleDimensions.y = 1.0;
326 DALI_LOG_WARNING( "Value for scale dimensions clamped. Must be greater than zero.\n" );
328 if( mScaleDimensions.z <= 0.0 )
330 mScaleDimensions.z = 1.0;
331 DALI_LOG_WARNING( "Value for scale dimensions clamped. Must be greater than zero.\n" );
336 DALI_LOG_ERROR( "Invalid type for scale dimensions in PrimitiveVisual.\n" );
340 Property::Value* bevel = propertyMap.Find( Toolkit::PrimitiveVisual::Property::BEVEL_PERCENTAGE, BEVEL_PERCENTAGE );
343 if( bevel->Get( mBevelPercentage ) )
346 if( mBevelPercentage < MIN_BEVEL_PERCENTAGE )
348 mBevelPercentage = MIN_BEVEL_PERCENTAGE;
349 DALI_LOG_WARNING( "Value for bevel percentage clamped.\n" );
351 else if( mBevelPercentage > MAX_BEVEL_PERCENTAGE )
353 mBevelPercentage = MAX_BEVEL_PERCENTAGE;
354 DALI_LOG_WARNING( "Value for bevel percentage clamped.\n" );
359 DALI_LOG_ERROR( "Invalid type for bevel percentage in PrimitiveVisual.\n" );
363 Property::Value* smoothness = propertyMap.Find( Toolkit::PrimitiveVisual::Property::BEVEL_SMOOTHNESS, BEVEL_SMOOTHNESS );
366 if( smoothness->Get( mBevelSmoothness ) )
369 if( mBevelSmoothness < MIN_SMOOTHNESS )
371 mBevelSmoothness = MIN_SMOOTHNESS;
372 DALI_LOG_WARNING( "Value for bevel smoothness clamped.\n" );
374 else if( mBevelSmoothness > MAX_SMOOTHNESS )
376 mBevelSmoothness = MAX_SMOOTHNESS;
377 DALI_LOG_WARNING( "Value for bevel smoothness clamped.\n" );
382 DALI_LOG_ERROR( "Invalid type for bevel smoothness in PrimitiveVisual.\n" );
386 //Read in light position.
387 Property::Value* lightPosition = propertyMap.Find( Toolkit::PrimitiveVisual::Property::LIGHT_POSITION, LIGHT_POSITION_UNIFORM_NAME );
390 if( !lightPosition->Get( mLightPosition ) )
392 DALI_LOG_ERROR( "Invalid value passed for light position in MeshVisual object.\n" );
393 mLightPosition = Vector3::ZERO;
398 //Default behaviour is to place the light directly in front of the object,
399 // at a reasonable distance to light everything on screen.
400 Stage stage = Stage::GetCurrent();
402 mLightPosition = Vector3( stage.GetSize().width / 2, stage.GetSize().height / 2, stage.GetSize().width * 5 );
406 void PrimitiveVisual::GetNaturalSize( Vector2& naturalSize )
413 naturalSize.x = mObjectDimensions.x;
414 naturalSize.y = mObjectDimensions.y;
417 void PrimitiveVisual::DoSetOnStage( Actor& actor )
419 InitializeRenderer();
421 actor.AddRenderer( mImpl->mRenderer );
423 // Primitive generated and ready to display
424 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
427 void PrimitiveVisual::DoCreatePropertyMap( Property::Map& map ) const
430 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::PRIMITIVE );
431 map.Insert( Toolkit::PrimitiveVisual::Property::MIX_COLOR, mImpl->mMixColor );
432 map.Insert( Toolkit::PrimitiveVisual::Property::SHAPE, mPrimitiveType );
433 map.Insert( Toolkit::PrimitiveVisual::Property::SLICES, mSlices );
434 map.Insert( Toolkit::PrimitiveVisual::Property::STACKS, mStacks );
435 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_TOP_RADIUS, mScaleTopRadius );
436 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_BOTTOM_RADIUS, mScaleBottomRadius );
437 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_HEIGHT, mScaleHeight );
438 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_RADIUS, mScaleRadius );
439 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_DIMENSIONS, mScaleDimensions );
440 map.Insert( Toolkit::PrimitiveVisual::Property::BEVEL_PERCENTAGE, mBevelPercentage );
441 map.Insert( Toolkit::PrimitiveVisual::Property::BEVEL_SMOOTHNESS, mBevelSmoothness );
442 map.Insert( Toolkit::PrimitiveVisual::Property::LIGHT_POSITION, mLightPosition );
445 void PrimitiveVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
450 void PrimitiveVisual::OnSetTransform()
452 if( mImpl->mRenderer )
454 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
458 void PrimitiveVisual::InitializeRenderer()
470 mImpl->mRenderer = Renderer::New( mGeometry, mShader );
471 mImpl->mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
473 // Register transform properties
474 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
476 mImpl->mMixColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::PrimitiveVisual::Property::MIX_COLOR, MIX_COLOR, Vector3(mImpl->mMixColor) );
479 void PrimitiveVisual::UpdateShaderUniforms()
481 Stage stage = Stage::GetCurrent();
482 float width = stage.GetSize().width;
483 float height = stage.GetSize().height;
485 //Flip model to account for DALi starting with (0, 0) at the top left.
487 scaleMatrix.SetIdentityAndScale( Vector3( 1.0, -1.0, 1.0 ) );
489 mShader.RegisterProperty( STAGE_OFFSET_UNIFORM_NAME, Vector2( width, height ) / 2.0f );
490 mShader.RegisterProperty( LIGHT_POSITION_UNIFORM_NAME, mLightPosition );
491 mShader.RegisterProperty( OBJECT_MATRIX_UNIFORM_NAME, scaleMatrix );
492 mShader.RegisterProperty( OBJECT_DIMENSIONS_UNIFORM_NAME, mObjectDimensions );
495 void PrimitiveVisual::CreateShader()
497 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
498 UpdateShaderUniforms();
501 void PrimitiveVisual::CreateGeometry()
503 Dali::Vector<Vertex> vertices;
504 Dali::Vector<unsigned short> indices;
506 switch( mPrimitiveType )
508 case Toolkit::PrimitiveVisual::Shape::SPHERE:
510 CreateSphere( vertices, indices, mSlices, mStacks );
513 case Toolkit::PrimitiveVisual::Shape::CONE:
515 //Create a conic with zero top radius.
516 CreateConic( vertices, indices, 0, mScaleBottomRadius, mScaleHeight, mSlices );
519 case Toolkit::PrimitiveVisual::Shape::CONICAL_FRUSTRUM: // deprecated
521 CreateConic( vertices, indices, mScaleTopRadius, mScaleBottomRadius, mScaleHeight, mSlices );
524 case Toolkit::PrimitiveVisual::Shape::CYLINDER:
526 //Create a conic with equal radii on the top and bottom.
527 CreateConic( vertices, indices, mScaleRadius, mScaleRadius, mScaleHeight, mSlices );
530 case Toolkit::PrimitiveVisual::Shape::CUBE:
532 //Create a cube by creating a bevelled cube with minimum bevel.
533 CreateBevelledCube( vertices, indices, mScaleDimensions, 0.0, 0.0 );
536 case Toolkit::PrimitiveVisual::Shape::OCTAHEDRON:
538 //Create an octahedron by creating a bevelled cube with maximum bevel.
539 CreateBevelledCube( vertices, indices, mScaleDimensions, 1.0, mBevelSmoothness );
542 case Toolkit::PrimitiveVisual::Shape::BEVELLED_CUBE:
544 CreateBevelledCube( vertices, indices, mScaleDimensions, mBevelPercentage, mBevelSmoothness );
547 case Toolkit::PrimitiveVisual::Shape::CONICAL_FRUSTUM:
549 CreateConic( vertices, indices, mScaleTopRadius, mScaleBottomRadius, mScaleHeight, mSlices );
554 mGeometry = Geometry::New();
557 Property::Map vertexFormat;
558 vertexFormat[POSITION] = Property::VECTOR3;
559 vertexFormat[NORMAL] = Property::VECTOR3;
560 PropertyBuffer surfaceVertices = PropertyBuffer::New( vertexFormat );
561 surfaceVertices.SetData( &vertices[0], vertices.Size() );
563 mGeometry.AddVertexBuffer( surfaceVertices );
565 //Indices for triangle formulation
566 mGeometry.SetIndexBuffer( &indices[0], indices.Size() );
569 void PrimitiveVisual::CreateSphere( Vector<Vertex>& vertices, Vector<unsigned short>& indices, int slices, int stacks )
571 ComputeSphereVertices( vertices, slices, stacks );
572 FormSphereTriangles( indices, slices, stacks );
574 mObjectDimensions = Vector3::ONE;
577 void PrimitiveVisual::CreateConic( Vector<Vertex>& vertices, Vector<unsigned short>& indices, float scaleTopRadius,
578 float scaleBottomRadius, float scaleHeight, int slices )
580 ComputeConicVertices( vertices, scaleTopRadius, scaleBottomRadius, scaleHeight, slices );
581 FormConicTriangles( indices, scaleTopRadius, scaleBottomRadius, slices );
583 //Determine object dimensions, and scale them to be between 0.0 and 1.0.
584 float xDimension = std::max( scaleTopRadius, scaleBottomRadius ) * 2.0f;
585 float yDimension = scaleHeight;
586 float largestDimension = std::max( xDimension, yDimension );
588 mObjectDimensions = Vector3( xDimension / largestDimension, yDimension / largestDimension,
589 xDimension / largestDimension );
592 void PrimitiveVisual::CreateBevelledCube( Vector<Vertex>& vertices, Vector<unsigned short>& indices,
593 Vector3 dimensions, float bevelPercentage, float bevelSmoothness )
595 float maxDimension = std::max( std::max( dimensions.x, dimensions.y ), dimensions.z );
596 dimensions = dimensions / maxDimension;
598 if( bevelPercentage <= MIN_BEVEL_PERCENTAGE ) //No bevel, form a cube.
600 ComputeCubeVertices( vertices, dimensions );
601 FormCubeTriangles( indices );
603 else if( bevelPercentage >= MAX_BEVEL_PERCENTAGE ) //Max bevel, form an octahedron.
605 ComputeOctahedronVertices( vertices, dimensions, bevelSmoothness );
606 FormOctahedronTriangles( indices );
608 else //In between, form a bevelled cube.
610 ComputeBevelledCubeVertices( vertices, dimensions, bevelPercentage, bevelSmoothness );
611 FormBevelledCubeTriangles( indices );
614 mObjectDimensions = dimensions;
617 void PrimitiveVisual::ComputeCircleTables( Vector<float>& sinTable, Vector<float>& cosTable, int divisions,
625 const float angleDivision = ( halfCircle ? 1.0f : 2.0f ) * Dali::Math::PI / ( float ) divisions;
627 sinTable.Resize( divisions );
628 cosTable.Resize( divisions );
630 for( int i = 0; i < divisions; i++ )
632 sinTable[i] = sin( angleDivision * i );
633 cosTable[i] = cos( angleDivision * i );
637 void PrimitiveVisual::ComputeSphereVertices( Vector<Vertex>& vertices, int slices, int stacks )
639 //Tables for calculating slices angles and stacks angles, respectively.
640 Vector<float> sinTable1;
641 Vector<float> cosTable1;
642 Vector<float> sinTable2;
643 Vector<float> cosTable2;
645 ComputeCircleTables( sinTable1, cosTable1, slices, false );
646 ComputeCircleTables( sinTable2, cosTable2, stacks, true );
648 int numVertices = slices * ( stacks - 1 ) + 2;
649 vertices.Resize( numVertices );
651 int vertexIndex = 0; //Track progress through vertices.
657 vertices[vertexIndex].position = Vector3( 0.0, 0.5, 0.0 );
658 vertices[vertexIndex].normal = Vector3( 0.0, 1.0, 0.0 );
662 for( int i = 1; i < stacks; i++ )
664 for( int j = 0; j < slices; j++, vertexIndex++ )
666 x = cosTable1[j] * sinTable2[i];
668 z = sinTable1[j] * sinTable2[i];
670 vertices[vertexIndex].position = Vector3( x / 2.0f, y / 2.0f, z / 2.0f );
671 vertices[vertexIndex].normal = Vector3( x, y, z );
676 vertices[vertexIndex].position = Vector3( 0.0, -0.5, 0.0 );
677 vertices[vertexIndex].normal = Vector3( 0.0, -1.0, 0.0 );
680 void PrimitiveVisual::FormSphereTriangles( Vector<unsigned short>& indices, int slices, int stacks )
684 //Set indices to placeholder "error" values.
685 //This will display nothing, which is the expected behaviour for this edge case.
690 int numTriangles = 2 * slices * ( stacks - 1 );
692 indices.Resize( 3 * numTriangles );
694 int indiceIndex = 0; //Used to keep track of progress through indices.
695 int previousCycleBeginning = 1; //Stores the index of the vertex that started the cycle of the previous stack.
696 int currentCycleBeginning = 1 + slices;
698 //Top stack. Loop from index 1 to index slices, as not counting the very first vertex.
699 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
701 indices[indiceIndex] = 0;
704 //End, so loop around.
705 indices[indiceIndex + 1] = 1;
709 indices[indiceIndex + 1] = i + 1;
711 indices[indiceIndex + 2] = i;
714 //Middle Stacks. Want to form triangles between the top and bottom stacks, so loop up to the number of stacks - 2.
715 for( int i = 0; i < stacks - 2; i++, previousCycleBeginning += slices, currentCycleBeginning += slices )
717 for( int j = 0; j < slices; j++, indiceIndex += 6 )
719 if( j == slices - 1 )
721 //End, so loop around.
722 indices[indiceIndex] = previousCycleBeginning + j;
723 indices[indiceIndex + 1] = previousCycleBeginning;
724 indices[indiceIndex + 2] = currentCycleBeginning + j;
725 indices[indiceIndex + 3] = currentCycleBeginning + j;
726 indices[indiceIndex + 4] = previousCycleBeginning;
727 indices[indiceIndex + 5] = currentCycleBeginning;
731 indices[indiceIndex] = previousCycleBeginning + j;
732 indices[indiceIndex + 1] = previousCycleBeginning + 1 + j;
733 indices[indiceIndex + 2] = currentCycleBeginning + j;
734 indices[indiceIndex + 3] = currentCycleBeginning + j;
735 indices[indiceIndex + 4] = previousCycleBeginning + 1 + j;
736 indices[indiceIndex + 5] = currentCycleBeginning + 1 + j;
741 //Bottom stack. Loop around the last stack from the previous loop, and go up to the penultimate vertex.
742 for( int i = 0; i < slices; i++, indiceIndex += 3 )
744 indices[indiceIndex] = previousCycleBeginning + slices;
745 indices[indiceIndex + 1] = previousCycleBeginning + i;
746 if( i == slices - 1 )
748 //End, so loop around.
749 indices[indiceIndex + 2] = previousCycleBeginning;
753 indices[indiceIndex + 2] = previousCycleBeginning + i + 1;
758 void PrimitiveVisual::ComputeConicVertices( Vector<Vertex>& vertices, float scaleTopRadius,
759 float scaleBottomRadius, float scaleHeight, int slices )
761 int vertexIndex = 0; //Track progress through vertices.
762 Vector<float> sinTable;
763 Vector<float> cosTable;
765 ComputeCircleTables( sinTable, cosTable, slices, false );
767 int numVertices = 2; //Always will have one at the top and one at the bottom.
769 //Add vertices for each circle. Need two per point for different face normals.
770 if( scaleTopRadius > 0.0 )
772 numVertices += 2 * slices;
774 if( scaleBottomRadius > 0.0 )
776 numVertices += 2 * slices;
779 vertices.Resize( numVertices );
782 //Scale to bounding region of -0.5 to 0.5 (i.e range of 1).
783 float biggestObjectDimension = std::max( std::max( scaleTopRadius * 2.0f, scaleBottomRadius * 2.0f ), scaleHeight );
784 scaleTopRadius = scaleTopRadius / biggestObjectDimension;
785 scaleBottomRadius = scaleBottomRadius / biggestObjectDimension;
787 //Dimensions for vertex coordinates. Y is constant, and so can be initialised now.
789 float y = scaleHeight / biggestObjectDimension / 2.0f;
793 vertices[0].position = Vector3( 0, y, 0 );
794 vertices[0].normal = Vector3( 0, 1, 0 );
798 if( scaleTopRadius > 0.0 )
800 //Loop around the circle.
801 for( int i = 0; i < slices; i++, vertexIndex++ )
803 x = sinTable[i] * scaleTopRadius;
804 z = cosTable[i] * scaleTopRadius;
806 //Upward-facing normal.
807 vertices[vertexIndex].position = Vector3( x, y, z );
808 vertices[vertexIndex].normal = Vector3( 0, 1, 0 );
810 //Outward-facing normal.
811 vertices[vertexIndex + slices].position = Vector3( x, y, z );
812 vertices[vertexIndex + slices].normal = Vector3( x, 0, z );
815 vertexIndex += slices;
819 if( scaleBottomRadius > 0.0 )
821 //Loop around the circle.
822 for( int i = 0; i < slices; i++, vertexIndex++ )
824 x = sinTable[i] * scaleBottomRadius;
825 z = cosTable[i] * scaleBottomRadius;
827 //Outward-facing normal.
828 vertices[vertexIndex].position = Vector3( x, -y, z );
829 vertices[vertexIndex].normal = Vector3( x, 0, z );
831 //Downward-facing normal.
832 vertices[vertexIndex + slices].position = Vector3( x, -y, z );
833 vertices[vertexIndex + slices].normal = Vector3( 0, -1, 0 );
836 vertexIndex += slices;
840 vertices[vertexIndex].position = Vector3( 0, -y, 0 );
841 vertices[vertexIndex].normal = Vector3( 0, -1, 0 );
845 void PrimitiveVisual::FormConicTriangles( Vector<unsigned short>& indices, float scaleTopRadius,
846 float scaleBottomRadius, int slices )
848 int indiceIndex = 0; //Track progress through indices.
849 int numTriangles = 0;
850 bool coneTop = scaleTopRadius <= 0.0;
851 bool coneBottom = scaleBottomRadius <= 0.0;
853 if( coneTop && coneBottom )
855 //Set indices to placeholder "error" values.
856 //This will display nothing, which is the expected behaviour for this edge case.
863 numTriangles += 2 * slices;
867 numTriangles += 2 * slices;
870 indices.Resize( 3 * numTriangles );
872 //Switch on the type of conic we have.
873 if( !coneTop && !coneBottom )
875 //Top circle. Start at index of first outer point and go around.
876 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
878 indices[indiceIndex] = 0;
879 indices[indiceIndex + 1] = i;
882 //End, so loop around.
883 indices[indiceIndex + 2] = 1;
887 indices[indiceIndex + 2] = i + 1;
891 int topCycleBeginning = slices + 1;
892 int bottomCycleBeginning = topCycleBeginning + slices;
895 for( int i = 0; i < slices; i++, indiceIndex += 6 )
897 if( i == slices - 1 )
899 //End, so loop around.
900 indices[indiceIndex] = topCycleBeginning + i;
901 indices[indiceIndex + 1] = bottomCycleBeginning + i;
902 indices[indiceIndex + 2] = topCycleBeginning;
903 indices[indiceIndex + 3] = bottomCycleBeginning + i;
904 indices[indiceIndex + 4] = bottomCycleBeginning;
905 indices[indiceIndex + 5] = topCycleBeginning;
909 indices[indiceIndex] = topCycleBeginning + i;
910 indices[indiceIndex + 1] = bottomCycleBeginning + i;
911 indices[indiceIndex + 2] = topCycleBeginning + 1 + i;
912 indices[indiceIndex + 3] = bottomCycleBeginning + i;
913 indices[indiceIndex + 4] = bottomCycleBeginning + 1 + i;
914 indices[indiceIndex + 5] = topCycleBeginning + 1 + i;
918 int bottomFaceCycleBeginning = bottomCycleBeginning + slices;
921 for( int i = 0; i < slices; i++, indiceIndex += 3 )
923 indices[indiceIndex] = bottomFaceCycleBeginning;
924 if( i == slices - 1 )
926 //End, so loop around.
927 indices[indiceIndex + 1] = bottomFaceCycleBeginning;
931 indices[indiceIndex + 1] = bottomFaceCycleBeginning + i + 1;
933 indices[indiceIndex + 2] = bottomFaceCycleBeginning + i;
936 else if( !coneTop || !coneBottom )
938 //Top circle/edges. Start at index of first outer point and go around.
939 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
941 indices[indiceIndex] = 0;
942 indices[indiceIndex + 1] = i;
945 //End, so loop around.
946 indices[indiceIndex + 2] = 1;
950 indices[indiceIndex + 2] = i + 1;
954 //Bottom circle/edges. Start at index of first outer point and go around.
955 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
957 indices[indiceIndex] = 2 * slices + 1;
960 //End, so loop around.
961 indices[indiceIndex + 1] = slices + 1;
965 indices[indiceIndex + 1] = slices + i + 1;
967 indices[indiceIndex + 2] = slices + i;
972 void PrimitiveVisual::ComputeCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions )
974 int numVertices = 4 * 6; //Four per face.
975 int vertexIndex = 0; //Tracks progress through vertices.
976 float scaledX = 0.5 * dimensions.x;
977 float scaledY = 0.5 * dimensions.y;
978 float scaledZ = 0.5 * dimensions.z;
980 vertices.Resize( numVertices );
982 Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
984 Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
987 positions[0] = Vector3( -scaledX, scaledY, -scaledZ );
988 positions[1] = Vector3( scaledX, scaledY, -scaledZ );
989 positions[2] = Vector3( scaledX, scaledY, scaledZ );
990 positions[3] = Vector3( -scaledX, scaledY, scaledZ );
991 positions[4] = Vector3( -scaledX, -scaledY, -scaledZ );
992 positions[5] = Vector3( scaledX, -scaledY, -scaledZ );
993 positions[6] = Vector3( scaledX, -scaledY, scaledZ );
994 positions[7] = Vector3( -scaledX, -scaledY, scaledZ );
996 normals[0] = Vector3( 0, 1, 0 );
997 normals[1] = Vector3( 0, 0, -1 );
998 normals[2] = Vector3( 1, 0, 0 );
999 normals[3] = Vector3( 0, 0, 1 );
1000 normals[4] = Vector3( -1, 0, 0 );
1001 normals[5] = Vector3( 0, -1, 0 );
1003 //Top face, upward normals.
1004 for( int i = 0; i < 4; i++, vertexIndex++ )
1006 vertices[vertexIndex].position = positions[i];
1007 vertices[vertexIndex].normal = normals[0];
1010 //Top face, outward normals.
1011 for( int i = 0; i < 4; i++, vertexIndex += 2 )
1013 vertices[vertexIndex].position = positions[i];
1014 vertices[vertexIndex].normal = normals[i + 1];
1018 //End, so loop around.
1019 vertices[vertexIndex + 1].position = positions[0];
1023 vertices[vertexIndex + 1].position = positions[i + 1];
1025 vertices[vertexIndex + 1].normal = normals[i + 1];
1028 //Bottom face, outward normals.
1029 for( int i = 0; i < 4; i++, vertexIndex += 2 )
1031 vertices[vertexIndex].position = positions[i + 4];
1032 vertices[vertexIndex].normal = normals[i + 1];
1036 //End, so loop around.
1037 vertices[vertexIndex + 1].position = positions[4];
1041 vertices[vertexIndex + 1].position = positions[i + 5];
1043 vertices[vertexIndex + 1].normal = normals[i + 1];
1046 //Bottom face, downward normals.
1047 for( int i = 0; i < 4; i++, vertexIndex++ )
1049 vertices[vertexIndex].position = positions[i + 4];
1050 vertices[vertexIndex].normal = normals[5];
1055 void PrimitiveVisual::FormCubeTriangles( Vector<unsigned short>& indices )
1057 int numTriangles = 12;
1058 int triangleIndex = 0; //Track progress through indices.
1060 indices.Resize( 3 * numTriangles );
1063 indices[triangleIndex] = 0;
1064 indices[triangleIndex + 1] = 2;
1065 indices[triangleIndex + 2] = 1;
1066 indices[triangleIndex + 3] = 2;
1067 indices[triangleIndex + 4] = 0;
1068 indices[triangleIndex + 5] = 3;
1071 int topFaceStart = 4;
1072 int bottomFaceStart = 12;
1075 for( int i = 0; i < 8; i += 2, triangleIndex += 6 )
1077 indices[triangleIndex ] = i + topFaceStart;
1078 indices[triangleIndex + 1] = i + topFaceStart + 1;
1079 indices[triangleIndex + 2] = i + bottomFaceStart + 1;
1080 indices[triangleIndex + 3] = i + topFaceStart;
1081 indices[triangleIndex + 4] = i + bottomFaceStart + 1;
1082 indices[triangleIndex + 5] = i + bottomFaceStart;
1086 indices[triangleIndex] = 20;
1087 indices[triangleIndex + 1] = 21;
1088 indices[triangleIndex + 2] = 22;
1089 indices[triangleIndex + 3] = 22;
1090 indices[triangleIndex + 4] = 23;
1091 indices[triangleIndex + 5] = 20;
1094 void PrimitiveVisual::ComputeOctahedronVertices( Vector<Vertex>& vertices, Vector3 dimensions, float smoothness )
1096 int numVertices = 3 * 8; //Three per face
1097 int vertexIndex = 0; //Tracks progress through vertices.
1098 float scaledX = 0.5 * dimensions.x;
1099 float scaledY = 0.5 * dimensions.y;
1100 float scaledZ = 0.5 * dimensions.z;
1102 vertices.Resize( numVertices );
1104 Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
1105 positions.Resize(6);
1106 Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
1108 Vector<Vector3> outerNormals; //Holds normals that point outwards at each vertex.
1109 outerNormals.Resize( 6 );
1111 positions[0] = Vector3( 0.0, scaledY, 0.0 );
1112 positions[1] = Vector3( -scaledX, 0.0, 0.0 );
1113 positions[2] = Vector3( 0.0, 0.0, -scaledZ );
1114 positions[3] = Vector3( scaledX, 0.0, 0.0 );
1115 positions[4] = Vector3( 0.0, 0.0, scaledZ );
1116 positions[5] = Vector3( 0.0, -scaledY, 0.0 );
1118 normals[0] = Vector3( -1, 1, -1 );
1119 normals[1] = Vector3( 1, 1, -1 );
1120 normals[2] = Vector3( 1, 1, 1 );
1121 normals[3] = Vector3( -1, 1, 1 );
1122 normals[4] = Vector3( -1, -1, -1 );
1123 normals[5] = Vector3( 1, -1, -1 );
1124 normals[6] = Vector3( 1, -1, 1 );
1125 normals[7] = Vector3( -1, -1, 1 );
1127 outerNormals[0] = Vector3( 0, 1, 0 );
1128 outerNormals[1] = Vector3( -1, 0, 0 );
1129 outerNormals[2] = Vector3( 0, 0, -1 );
1130 outerNormals[3] = Vector3( 1, 0, 0 );
1131 outerNormals[4] = Vector3( 0, 0, 1 );
1132 outerNormals[5] = Vector3( 0, -1, 0 );
1134 //Loop through top faces.
1135 for( int i = 0; i < 4; i++, vertexIndex += 3 )
1139 //End, so loop around.
1140 vertices[vertexIndex ].position = positions[0];
1141 vertices[vertexIndex ].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1142 vertices[vertexIndex + 1].position = positions[1];
1143 vertices[vertexIndex + 1].normal = outerNormals[1] * smoothness + normals[i] * (1 - smoothness);
1144 vertices[vertexIndex + 2].position = positions[i + 1];
1145 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1149 vertices[vertexIndex ].position = positions[0];
1150 vertices[vertexIndex ].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1151 vertices[vertexIndex + 1].position = positions[i + 2];
1152 vertices[vertexIndex + 1].normal = outerNormals[i + 2] * smoothness + normals[i] * (1 - smoothness);
1153 vertices[vertexIndex + 2].position = positions[i + 1];
1154 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1158 //Loop through bottom faces.
1159 for( int i = 0; i < 4; i++, vertexIndex += 3 )
1163 //End, so loop around.
1164 vertices[vertexIndex ].position = positions[5];
1165 vertices[vertexIndex ].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1166 vertices[vertexIndex + 1].position = positions[i + 1];
1167 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1168 vertices[vertexIndex + 2].position = positions[1];
1169 vertices[vertexIndex + 2].normal = outerNormals[1] * smoothness + normals[i + 4] * (1 - smoothness);
1173 vertices[vertexIndex ].position = positions[5];
1174 vertices[vertexIndex ].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1175 vertices[vertexIndex + 1].position = positions[i + 1];
1176 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1177 vertices[vertexIndex + 2].position = positions[i + 2];
1178 vertices[vertexIndex + 2].normal = outerNormals[i + 2] * smoothness + normals[i + 4] * (1 - smoothness);
1183 void PrimitiveVisual::FormOctahedronTriangles( Vector<unsigned short>& indices )
1185 int numTriangles = 8;
1186 int numIndices = numTriangles * 3;
1188 indices.Resize( numIndices );
1190 for( unsigned short i = 0; i < numIndices; i++ )
1196 void PrimitiveVisual::ComputeBevelledCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions,
1197 float bevelPercentage, float bevelSmoothness )
1199 int numPositions = 24;
1201 int numOuterFaces = 6;
1202 int numVertices = 6 * 4 + 12 * 4 + 8 * 3; //Six outer faces, 12 slanting rectangles, 8 slanting triangles.
1203 int vertexIndex = 0; //Track progress through vertices.
1204 int normalIndex = 0; //Track progress through normals, as vertices are calculated per face.
1206 float minDimension = std::min( std::min( dimensions.x, dimensions.y ), dimensions.z );
1207 float bevelAmount = 0.5 * std::min( bevelPercentage, minDimension ); //Cap bevel amount if necessary.
1209 //Distances from centre to outer edge points.
1210 float outerX = 0.5 * dimensions.x;
1211 float outerY = 0.5 * dimensions.y;
1212 float outerZ = 0.5 * dimensions.z;
1214 //Distances from centre to bevelled points.
1215 float bevelX = outerX - bevelAmount;
1216 float bevelY = outerY - bevelAmount;
1217 float bevelZ = outerZ - bevelAmount;
1219 Vector<Vector3> positions; //Holds object points, to be shared between vertexes.
1220 positions.Resize( numPositions );
1221 Vector<Vector3> normals; //Holds face normals, to be shared between vertexes.
1222 normals.Resize( numFaces );
1223 Vector<Vector3> outerNormals; //Holds normals of the outermost faces specifically.
1224 outerNormals.Resize( numOuterFaces );
1225 vertices.Resize( numVertices );
1227 //Topmost face positions.
1228 positions[0 ] = Vector3( -bevelX, outerY, -bevelZ );
1229 positions[1 ] = Vector3( bevelX, outerY, -bevelZ );
1230 positions[2 ] = Vector3( bevelX, outerY, bevelZ );
1231 positions[3 ] = Vector3( -bevelX, outerY, bevelZ );
1233 //Second layer positions.
1234 positions[4 ] = Vector3( -outerX, bevelY, -bevelZ );
1235 positions[5 ] = Vector3( -bevelX, bevelY, -outerZ );
1236 positions[6 ] = Vector3( bevelX, bevelY, -outerZ );
1237 positions[7 ] = Vector3( outerX, bevelY, -bevelZ );
1238 positions[8 ] = Vector3( outerX, bevelY, bevelZ );
1239 positions[9 ] = Vector3( bevelX, bevelY, outerZ );
1240 positions[10] = Vector3( -bevelX, bevelY, outerZ );
1241 positions[11] = Vector3( -outerX, bevelY, bevelZ );
1243 //Third layer positions.
1244 positions[12] = Vector3( -outerX, -bevelY, -bevelZ );
1245 positions[13] = Vector3( -bevelX, -bevelY, -outerZ );
1246 positions[14] = Vector3( bevelX, -bevelY, -outerZ );
1247 positions[15] = Vector3( outerX, -bevelY, -bevelZ );
1248 positions[16] = Vector3( outerX, -bevelY, bevelZ );
1249 positions[17] = Vector3( bevelX, -bevelY, outerZ );
1250 positions[18] = Vector3( -bevelX, -bevelY, outerZ );
1251 positions[19] = Vector3( -outerX, -bevelY, bevelZ );
1253 //Bottom-most face positions.
1254 positions[20] = Vector3( -bevelX, -outerY, -bevelZ );
1255 positions[21] = Vector3( bevelX, -outerY, -bevelZ );
1256 positions[22] = Vector3( bevelX, -outerY, bevelZ );
1257 positions[23] = Vector3( -bevelX, -outerY, bevelZ );
1260 normals[0 ] = Vector3( 0, 1, 0 );
1262 //Top slope normals.
1263 normals[1 ] = Vector3( -1, 1, -1 );
1264 normals[2 ] = Vector3( 0, 1, -1 );
1265 normals[3 ] = Vector3( 1, 1, -1 );
1266 normals[4 ] = Vector3( 1, 1, 0 );
1267 normals[5 ] = Vector3( 1, 1, 1 );
1268 normals[6 ] = Vector3( 0, 1, 1 );
1269 normals[7 ] = Vector3( -1, 1, 1 );
1270 normals[8 ] = Vector3( -1, 1, 0 );
1273 normals[9 ] = Vector3( -1, 0, -1 );
1274 normals[10] = Vector3( 0, 0, -1 );
1275 normals[11] = Vector3( 1, 0, -1 );
1276 normals[12] = Vector3( 1, 0, 0 );
1277 normals[13] = Vector3( 1, 0, 1 );
1278 normals[14] = Vector3( 0, 0, 1 );
1279 normals[15] = Vector3( -1, 0, 1 );
1280 normals[16] = Vector3( -1, 0, 0 );
1282 //Bottom slope normals.
1283 normals[17] = Vector3( -1, -1, -1 );
1284 normals[18] = Vector3( 0, -1, -1 );
1285 normals[19] = Vector3( 1, -1, -1 );
1286 normals[20] = Vector3( 1, -1, 0 );
1287 normals[21] = Vector3( 1, -1, 1 );
1288 normals[22] = Vector3( 0, -1, 1 );
1289 normals[23] = Vector3( -1, -1, 1 );
1290 normals[24] = Vector3( -1, -1, 0 );
1292 //Bottom face normal.
1293 normals[25] = Vector3( 0, -1, 0 );
1295 //Top, back, right, front, left and bottom faces, respectively.
1296 outerNormals[0] = Vector3( 0, 1, 0 );
1297 outerNormals[1] = Vector3( 0, 0, -1 );
1298 outerNormals[2] = Vector3( 1, 0, 0 );
1299 outerNormals[3] = Vector3( 0, 0, 1 );
1300 outerNormals[4] = Vector3( -1, 0, 0 );
1301 outerNormals[5] = Vector3( 0, -1, 0 );
1303 //Topmost face vertices.
1304 for( int i = 0; i < 4; i++, vertexIndex++ )
1306 vertices[vertexIndex].position = positions[i];
1307 vertices[vertexIndex].normal = normals[normalIndex];
1312 //Top slope vertices.
1313 for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
1316 vertices[vertexIndex ].position = positions[i];
1317 vertices[vertexIndex ].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1318 vertices[vertexIndex + 1].position = positions[2 * i + 4];
1319 vertices[vertexIndex + 1].normal = outerNormals[( i == 0 ) ? 4 : i] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1320 vertices[vertexIndex + 2].position = positions[2 * i + 5];
1321 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1326 //End, so loop around.
1327 vertices[vertexIndex + 3].position = positions[i];
1328 vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1329 vertices[vertexIndex + 4].position = positions[0];
1330 vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1331 vertices[vertexIndex + 5].position = positions[2 * i + 5];
1332 vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1333 vertices[vertexIndex + 6].position = positions[4];
1334 vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1338 vertices[vertexIndex + 3].position = positions[i];
1339 vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1340 vertices[vertexIndex + 4].position = positions[i + 1];
1341 vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1342 vertices[vertexIndex + 5].position = positions[2 * i + 5];
1343 vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1344 vertices[vertexIndex + 6].position = positions[2 * i + 6];
1345 vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1349 int secondCycleBeginning = 4;
1350 int thirdCycleBeginning = secondCycleBeginning + 8;
1351 int bottomCycleBeginning = thirdCycleBeginning + 8;
1354 for( int i = 0; i < 8; i++, vertexIndex += 4, normalIndex++ )
1358 //End, so loop around.
1359 vertices[vertexIndex ].position = positions[secondCycleBeginning + i];
1360 vertices[vertexIndex ].normal = normals[normalIndex];
1361 vertices[vertexIndex + 1].position = positions[secondCycleBeginning];
1362 vertices[vertexIndex + 1].normal = normals[normalIndex];
1363 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1364 vertices[vertexIndex + 2].normal = normals[normalIndex];
1365 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning];
1366 vertices[vertexIndex + 3].normal = normals[normalIndex];
1368 else if( (i % 2) == 0 )
1370 //'even' faces are corner ones, and need smoothing.
1371 vertices[vertexIndex ].position = positions[secondCycleBeginning + i];
1372 vertices[vertexIndex ].normal = outerNormals[( i == 0 ) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1373 vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1374 vertices[vertexIndex + 1].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1375 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1376 vertices[vertexIndex + 2].normal = outerNormals[( i == 0 ) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1377 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1378 vertices[vertexIndex + 3].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1382 //'odd' faces are outer ones, and so don't need smoothing.
1383 vertices[vertexIndex ].position = positions[secondCycleBeginning + i];
1384 vertices[vertexIndex ].normal = normals[normalIndex];
1385 vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1386 vertices[vertexIndex + 1].normal = normals[normalIndex];
1387 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1388 vertices[vertexIndex + 2].normal = normals[normalIndex];
1389 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1390 vertices[vertexIndex + 3].normal = normals[normalIndex];
1394 //Bottom slope vertices.
1395 for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
1398 vertices[vertexIndex ].position = positions[thirdCycleBeginning + 2 * i];
1399 vertices[vertexIndex ].normal = outerNormals[( i == 0 ) ? 4 : i] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1400 vertices[vertexIndex + 1].position = positions[thirdCycleBeginning + 2 * i + 1];
1401 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1402 vertices[vertexIndex + 2].position = positions[bottomCycleBeginning + i];
1403 vertices[vertexIndex + 2].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1408 //End, so loop around.
1409 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1410 vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1411 vertices[vertexIndex + 4].position = positions[thirdCycleBeginning];
1412 vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1413 vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1414 vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1415 vertices[vertexIndex + 6].position = positions[bottomCycleBeginning];
1416 vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1420 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1421 vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1422 vertices[vertexIndex + 4].position = positions[thirdCycleBeginning + 2 * i + 2];
1423 vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1424 vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1425 vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1426 vertices[vertexIndex + 6].position = positions[bottomCycleBeginning + i + 1];
1427 vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1431 //Bottom-most face vertices.
1432 for( int i = 0; i < 4; i++, vertexIndex++ )
1434 vertices[vertexIndex].position = positions[ bottomCycleBeginning + i];
1435 vertices[vertexIndex].normal = normals[normalIndex];
1441 void PrimitiveVisual::FormBevelledCubeTriangles( Vector<unsigned short>& indices )
1443 int numTriangles = 44; //(Going from top to bottom, that's 2 + 12 + 16 + 12 + 2)
1444 int indiceIndex = 0; //Track progress through indices.
1445 int vertexIndex = 0; //Track progress through vertices as they're processed.
1447 indices.Resize( 3 * numTriangles );
1450 indices[indiceIndex ] = vertexIndex;
1451 indices[indiceIndex + 1] = vertexIndex + 2;
1452 indices[indiceIndex + 2] = vertexIndex + 1;
1453 indices[indiceIndex + 3] = vertexIndex + 0;
1454 indices[indiceIndex + 4] = vertexIndex + 3;
1455 indices[indiceIndex + 5] = vertexIndex + 2;
1460 for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1463 indices[indiceIndex ] = vertexIndex;
1464 indices[indiceIndex + 1] = vertexIndex + 2;
1465 indices[indiceIndex + 2] = vertexIndex + 1;
1468 indices[indiceIndex + 3] = vertexIndex + 3;
1469 indices[indiceIndex + 4] = vertexIndex + 4;
1470 indices[indiceIndex + 5] = vertexIndex + 5;
1471 indices[indiceIndex + 6] = vertexIndex + 4;
1472 indices[indiceIndex + 7] = vertexIndex + 6;
1473 indices[indiceIndex + 8] = vertexIndex + 5;
1477 for( int i = 0; i < 8; i++, indiceIndex += 6, vertexIndex += 4 )
1479 indices[indiceIndex ] = vertexIndex;
1480 indices[indiceIndex + 1] = vertexIndex + 1;
1481 indices[indiceIndex + 2] = vertexIndex + 2;
1482 indices[indiceIndex + 3] = vertexIndex + 1;
1483 indices[indiceIndex + 4] = vertexIndex + 3;
1484 indices[indiceIndex + 5] = vertexIndex + 2;
1488 for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1491 indices[indiceIndex ] = vertexIndex;
1492 indices[indiceIndex + 1] = vertexIndex + 1;
1493 indices[indiceIndex + 2] = vertexIndex + 2;
1496 indices[indiceIndex + 3] = vertexIndex + 3;
1497 indices[indiceIndex + 4] = vertexIndex + 4;
1498 indices[indiceIndex + 5] = vertexIndex + 5;
1499 indices[indiceIndex + 6] = vertexIndex + 4;
1500 indices[indiceIndex + 7] = vertexIndex + 6;
1501 indices[indiceIndex + 8] = vertexIndex + 5;
1505 indices[indiceIndex ] = vertexIndex;
1506 indices[indiceIndex + 1] = vertexIndex + 1;
1507 indices[indiceIndex + 2] = vertexIndex + 2;
1508 indices[indiceIndex + 3] = vertexIndex + 0;
1509 indices[indiceIndex + 4] = vertexIndex + 2;
1510 indices[indiceIndex + 5] = vertexIndex + 3;
1515 } // namespace Internal
1517 } // namespace Toolkit