2 * Copyright (c) 2017 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/devel-api/visuals/visual-properties-devel.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 )
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_TABLE_END( SHAPE_TYPE )
58 const char * const PRIMITIVE_SHAPE( "shape" );
59 const char * const SLICES( "slices" );
60 const char * const STACKS( "stacks" );
61 const char * const SCALE_TOP_RADIUS( "scaleTopRadius" );
62 const char * const SCALE_BOTTOM_RADIUS( "scaleBottomRadius" );
63 const char * const SCALE_HEIGHT( "scaleHeight" );
64 const char * const SCALE_RADIUS( "scaleRadius" );
65 const char * const SCALE_DIMENSIONS( "scaleDimensions" );
66 const char * const BEVEL_PERCENTAGE( "bevelPercentage" );
67 const char * const BEVEL_SMOOTHNESS( "bevelSmoothness" );
68 const char * const LIGHT_POSITION_UNIFORM_NAME( "lightPosition" );
70 //Primitive property defaults
71 const int DEFAULT_SLICES = 128; ///< For spheres and conics
72 const int DEFAULT_STACKS = 128; ///< For spheres and conics
73 const float DEFAULT_SCALE_TOP_RADIUS = 1.0; ///< For conical frustrums
74 const float DEFAULT_SCALE_BOTTOM_RADIUS = 1.5; ///< For cones and conical frustrums
75 const float DEFAULT_SCALE_HEIGHT = 3.0; ///< For all conics
76 const float DEFAULT_SCALE_RADIUS = 1.0; ///< For cylinders
77 const float DEFAULT_BEVEL_PERCENTAGE = 0.0; ///< For bevelled cubes
78 const float DEFAULT_BEVEL_SMOOTHNESS = 0.0; ///< For bevelled cubes
79 const Vector4 DEFAULT_COLOR = Vector4( 0.5, 0.5, 0.5, 1.0 ); ///< Grey, for all.
82 const int MIN_SLICES = 3; ///< Minimum number of slices for spheres and conics
83 const int MIN_STACKS = 2; ///< Minimum number of stacks for spheres and conics
84 const int MAX_PARTITIONS = 255; ///< Maximum number of slices or stacks for spheres and conics
85 const float MIN_BEVEL_PERCENTAGE = 0.0; ///< Minimum bevel percentage for bevelled cubes
86 const float MAX_BEVEL_PERCENTAGE = 1.0; ///< Maximum bevel percentage for bevelled cubes
87 const float MIN_SMOOTHNESS = 0.0; ///< Minimum bevel smoothness for bevelled cubes
88 const float MAX_SMOOTHNESS = 1.0; ///< Maximum bevel smoothness for bevelled cubes
90 //Specific shape labels.
91 const char * const SPHERE_LABEL( "SPHERE" );
92 const char * const CONE_LABEL( "CONE" );
93 const char * const CONICAL_FRUSTRUM_LABEL( "CONICAL_FRUSTRUM" );
94 const char * const CYLINDER_LABEL( "CYLINDER" );
95 const char * const CUBE_LABEL( "CUBE" );
96 const char * const OCTAHEDRON_LABEL( "OCTAHEDRON" );
97 const char * const BEVELLED_CUBE_LABEL( "BEVELLED_CUBE" );
100 const char * const OBJECT_MATRIX_UNIFORM_NAME( "uObjectMatrix" );
101 const char * const OBJECT_DIMENSIONS_UNIFORM_NAME( "uObjectDimensions" );
102 const char * const STAGE_OFFSET_UNIFORM_NAME( "uStageOffset" );
105 const char * const POSITION( "aPosition");
106 const char * const NORMAL( "aNormal" );
107 const char * const INDICES( "aIndices" );
109 //A simple shader that applies diffuse lighting to a mono-coloured object.
110 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
111 attribute highp vec3 aPosition;\n
112 attribute highp vec2 aTexCoord;\n
113 attribute highp vec3 aNormal;\n
114 varying mediump vec3 vIllumination;\n
115 uniform mediump vec3 uSize;\n
116 uniform mediump vec3 uObjectDimensions;\n
117 uniform mediump mat4 uMvpMatrix;\n
118 uniform mediump mat4 uModelView;\n
119 uniform mediump mat4 uViewMatrix;\n
120 uniform mediump mat3 uNormalMatrix;\n
121 uniform mediump mat4 uObjectMatrix;\n
122 uniform mediump vec3 lightPosition;\n
123 uniform mediump vec2 uStageOffset;\n
125 //Visual size and offset
126 uniform mediump vec2 offset;\n
127 uniform mediump vec2 size;\n
128 uniform mediump vec4 offsetSizeMode;\n
129 uniform mediump vec2 origin;\n
130 uniform mediump vec2 anchorPoint;\n
132 vec4 ComputeVertexPosition()\n
134 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
135 float scaleFactor = min( visualSize.x / uObjectDimensions.x, visualSize.y / uObjectDimensions.y );\n
136 vec3 originFlipY = vec3(origin.x, -origin.y, 0.0);
137 vec3 anchorPointFlipY = vec3( anchorPoint.x, -anchorPoint.y, 0.0);
138 vec3 offset = vec3( ( offset / uSize.xy ) * offsetSizeMode.xy + offset * (1.0-offsetSizeMode.xy), 0.0) * vec3(1.0,-1.0,1.0);\n
140 return vec4( (aPosition + anchorPointFlipY)*scaleFactor + (offset + originFlipY)*uSize, 1.0 );\n
145 vec4 normalisedVertexPosition = ComputeVertexPosition();\n
146 vec4 vertexPosition = uObjectMatrix * normalisedVertexPosition;\n
147 vertexPosition = uMvpMatrix * vertexPosition;\n
149 //Illumination in Model-View space - Transform attributes and uniforms\n
150 vec4 mvVertexPosition = uModelView * normalisedVertexPosition;\n
151 vec3 normal = uNormalMatrix * mat3( uObjectMatrix ) * aNormal;\n
153 vec4 mvLightPosition = vec4( ( lightPosition.xy - uStageOffset ), lightPosition.z, 1.0 );\n
154 mvLightPosition = uViewMatrix * mvLightPosition;\n
155 vec3 vectorToLight = normalize( mvLightPosition.xyz - mvVertexPosition.xyz );\n
157 float lightDiffuse = max( dot( vectorToLight, normal ), 0.0 );\n
158 vIllumination = vec3( lightDiffuse * 0.5 + 0.5 );\n
160 gl_Position = vertexPosition;\n
164 //Very simple fragment shader that merely applies the vertex shading to the color at each fragment.
165 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
166 precision mediump float;\n
167 varying mediump vec3 vIllumination;\n
168 uniform lowp vec4 uColor;\n
169 uniform lowp vec3 mixColor;\n
170 uniform lowp float opacity;\n
173 vec4 baseColor = vec4(mixColor, opacity) * uColor;\n
174 gl_FragColor = vec4( vIllumination.rgb * baseColor.rgb, baseColor.a );\n
178 } // unnamed namespace
180 PrimitiveVisualPtr PrimitiveVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
182 PrimitiveVisualPtr primitiveVisualPtr( new PrimitiveVisual( factoryCache ) );
183 primitiveVisualPtr->SetProperties( properties );
184 return primitiveVisualPtr;
187 PrimitiveVisual::PrimitiveVisual( VisualFactoryCache& factoryCache )
188 : Visual::Base( factoryCache ),
189 mScaleDimensions( Vector3::ONE ),
190 mScaleTopRadius( DEFAULT_SCALE_TOP_RADIUS ),
191 mScaleBottomRadius( DEFAULT_SCALE_BOTTOM_RADIUS ),
192 mScaleHeight( DEFAULT_SCALE_HEIGHT ),
193 mScaleRadius( DEFAULT_SCALE_RADIUS ),
194 mBevelPercentage( DEFAULT_BEVEL_PERCENTAGE ),
195 mBevelSmoothness( DEFAULT_BEVEL_SMOOTHNESS ),
196 mSlices( DEFAULT_SLICES ),
197 mStacks( DEFAULT_STACKS ),
198 mPrimitiveType( Toolkit::PrimitiveVisual::Shape::SPHERE )
200 mImpl->mMixColor = DEFAULT_COLOR;
203 PrimitiveVisual::~PrimitiveVisual()
207 void PrimitiveVisual::DoSetProperties( const Property::Map& propertyMap )
209 //Find out which shape to renderer.
210 Property::Value* primitiveTypeValue = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SHAPE, PRIMITIVE_SHAPE );
211 if( primitiveTypeValue )
213 Scripting::GetEnumerationProperty( *primitiveTypeValue, SHAPE_TYPE_TABLE, SHAPE_TYPE_TABLE_COUNT, mPrimitiveType );
217 DALI_LOG_ERROR( "Fail to provide shape to the PrimitiveVisual object.\n" );
220 // By virtue of DoSetProperties being called last, this will override
221 // anything set by DevelVisual::Property::MIX_COLOR
222 Property::Value* colorValue = propertyMap.Find( Toolkit::PrimitiveVisual::Property::MIX_COLOR, MIX_COLOR );
226 if( colorValue->Get( color ) )
228 Property::Type type = colorValue->GetType();
229 if( type == Property::VECTOR4 )
231 SetMixColor( color );
233 else if( type == Property::VECTOR3 )
235 Vector3 color3(color);
236 SetMixColor( color3 );
241 Property::Value* slices = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SLICES, SLICES );
244 if( slices->Get( mSlices ) )
247 if( mSlices > MAX_PARTITIONS )
249 mSlices = MAX_PARTITIONS;
250 DALI_LOG_WARNING( "Value for slices clamped.\n" );
252 else if ( mSlices < MIN_SLICES )
254 mSlices = MIN_SLICES;
255 DALI_LOG_WARNING( "Value for slices clamped.\n" );
260 DALI_LOG_ERROR( "Invalid type for slices in PrimitiveVisual.\n" );
264 Property::Value* stacks = propertyMap.Find( Toolkit::PrimitiveVisual::Property::STACKS, STACKS );
267 if( stacks->Get( mStacks ) )
270 if( mStacks > MAX_PARTITIONS )
272 mStacks = MAX_PARTITIONS;
273 DALI_LOG_WARNING( "Value for stacks clamped.\n" );
275 else if ( mStacks < MIN_STACKS )
277 mStacks = MIN_STACKS;
278 DALI_LOG_WARNING( "Value for stacks clamped.\n" );
283 DALI_LOG_ERROR( "Invalid type for stacks in PrimitiveVisual.\n" );
287 Property::Value* scaleTop = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_TOP_RADIUS, SCALE_TOP_RADIUS );
288 if( scaleTop && !scaleTop->Get( mScaleTopRadius ) )
290 DALI_LOG_ERROR( "Invalid type for scale top radius in PrimitiveVisual.\n" );
293 Property::Value* scaleBottom = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_BOTTOM_RADIUS, SCALE_BOTTOM_RADIUS );
294 if( scaleBottom && !scaleBottom->Get( mScaleBottomRadius ) )
296 DALI_LOG_ERROR( "Invalid type for scale bottom radius in PrimitiveVisual.\n" );
299 Property::Value* scaleHeight = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_HEIGHT, SCALE_HEIGHT );
300 if( scaleHeight && !scaleHeight->Get( mScaleHeight ) )
302 DALI_LOG_ERROR( "Invalid type for scale height in PrimitiveVisual.\n" );
305 Property::Value* scaleRadius = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_RADIUS, SCALE_RADIUS );
306 if( scaleRadius && !scaleRadius->Get( mScaleRadius ) )
308 DALI_LOG_ERROR( "Invalid type for scale radius in PrimitiveVisual.\n" );
311 Property::Value* dimensions = propertyMap.Find( Toolkit::PrimitiveVisual::Property::SCALE_DIMENSIONS, SCALE_DIMENSIONS );
314 if( dimensions->Get( mScaleDimensions ) )
316 //If any dimension is invalid, set it to a sensible default.
317 if( mScaleDimensions.x <= 0.0 )
319 mScaleDimensions.x = 1.0;
320 DALI_LOG_WARNING( "Value for scale dimensions clamped. Must be greater than zero.\n" );
322 if( mScaleDimensions.y <= 0.0 )
324 mScaleDimensions.y = 1.0;
325 DALI_LOG_WARNING( "Value for scale dimensions clamped. Must be greater than zero.\n" );
327 if( mScaleDimensions.z <= 0.0 )
329 mScaleDimensions.z = 1.0;
330 DALI_LOG_WARNING( "Value for scale dimensions clamped. Must be greater than zero.\n" );
335 DALI_LOG_ERROR( "Invalid type for scale dimensions in PrimitiveVisual.\n" );
339 Property::Value* bevel = propertyMap.Find( Toolkit::PrimitiveVisual::Property::BEVEL_PERCENTAGE, BEVEL_PERCENTAGE );
342 if( bevel->Get( mBevelPercentage ) )
345 if( mBevelPercentage < MIN_BEVEL_PERCENTAGE )
347 mBevelPercentage = MIN_BEVEL_PERCENTAGE;
348 DALI_LOG_WARNING( "Value for bevel percentage clamped.\n" );
350 else if( mBevelPercentage > MAX_BEVEL_PERCENTAGE )
352 mBevelPercentage = MAX_BEVEL_PERCENTAGE;
353 DALI_LOG_WARNING( "Value for bevel percentage clamped.\n" );
358 DALI_LOG_ERROR( "Invalid type for bevel percentage in PrimitiveVisual.\n" );
362 Property::Value* smoothness = propertyMap.Find( Toolkit::PrimitiveVisual::Property::BEVEL_SMOOTHNESS, BEVEL_SMOOTHNESS );
365 if( smoothness->Get( mBevelSmoothness ) )
368 if( mBevelSmoothness < MIN_SMOOTHNESS )
370 mBevelSmoothness = MIN_SMOOTHNESS;
371 DALI_LOG_WARNING( "Value for bevel smoothness clamped.\n" );
373 else if( mBevelSmoothness > MAX_SMOOTHNESS )
375 mBevelSmoothness = MAX_SMOOTHNESS;
376 DALI_LOG_WARNING( "Value for bevel smoothness clamped.\n" );
381 DALI_LOG_ERROR( "Invalid type for bevel smoothness in PrimitiveVisual.\n" );
385 //Read in light position.
386 Property::Value* lightPosition = propertyMap.Find( Toolkit::PrimitiveVisual::Property::LIGHT_POSITION, LIGHT_POSITION_UNIFORM_NAME );
389 if( !lightPosition->Get( mLightPosition ) )
391 DALI_LOG_ERROR( "Invalid value passed for light position in MeshVisual object.\n" );
392 mLightPosition = Vector3::ZERO;
397 //Default behaviour is to place the light directly in front of the object,
398 // at a reasonable distance to light everything on screen.
399 Stage stage = Stage::GetCurrent();
401 mLightPosition = Vector3( stage.GetSize().width / 2, stage.GetSize().height / 2, stage.GetSize().width * 5 );
405 void PrimitiveVisual::GetNaturalSize( Vector2& naturalSize )
412 naturalSize.x = mObjectDimensions.x;
413 naturalSize.y = mObjectDimensions.y;
416 void PrimitiveVisual::DoSetOnStage( Actor& actor )
418 InitializeRenderer();
420 actor.AddRenderer( mImpl->mRenderer );
422 // Primitive generated and ready to display
426 void PrimitiveVisual::DoCreatePropertyMap( Property::Map& map ) const
429 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::PRIMITIVE );
430 map.Insert( Toolkit::PrimitiveVisual::Property::MIX_COLOR, mImpl->mMixColor );
431 map.Insert( Toolkit::PrimitiveVisual::Property::SHAPE, mPrimitiveType );
432 map.Insert( Toolkit::PrimitiveVisual::Property::SLICES, mSlices );
433 map.Insert( Toolkit::PrimitiveVisual::Property::STACKS, mStacks );
434 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_TOP_RADIUS, mScaleTopRadius );
435 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_BOTTOM_RADIUS, mScaleBottomRadius );
436 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_HEIGHT, mScaleHeight );
437 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_RADIUS, mScaleRadius );
438 map.Insert( Toolkit::PrimitiveVisual::Property::SCALE_DIMENSIONS, mScaleDimensions );
439 map.Insert( Toolkit::PrimitiveVisual::Property::BEVEL_PERCENTAGE, mBevelPercentage );
440 map.Insert( Toolkit::PrimitiveVisual::Property::BEVEL_SMOOTHNESS, mBevelSmoothness );
441 map.Insert( Toolkit::PrimitiveVisual::Property::LIGHT_POSITION, mLightPosition );
444 void PrimitiveVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
449 void PrimitiveVisual::OnSetTransform()
451 if( mImpl->mRenderer )
453 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
457 void PrimitiveVisual::InitializeRenderer()
469 mImpl->mRenderer = Renderer::New( mGeometry, mShader );
470 mImpl->mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
472 // Register transform properties
473 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
475 mImpl->mMixColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::PrimitiveVisual::Property::MIX_COLOR, MIX_COLOR, Vector3(mImpl->mMixColor) );
478 void PrimitiveVisual::UpdateShaderUniforms()
480 Stage stage = Stage::GetCurrent();
481 float width = stage.GetSize().width;
482 float height = stage.GetSize().height;
484 //Flip model to account for DALi starting with (0, 0) at the top left.
486 scaleMatrix.SetIdentityAndScale( Vector3( 1.0, -1.0, 1.0 ) );
488 mShader.RegisterProperty( STAGE_OFFSET_UNIFORM_NAME, Vector2( width, height ) / 2.0f );
489 mShader.RegisterProperty( LIGHT_POSITION_UNIFORM_NAME, mLightPosition );
490 mShader.RegisterProperty( OBJECT_MATRIX_UNIFORM_NAME, scaleMatrix );
491 mShader.RegisterProperty( OBJECT_DIMENSIONS_UNIFORM_NAME, mObjectDimensions );
494 void PrimitiveVisual::CreateShader()
496 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
497 UpdateShaderUniforms();
500 void PrimitiveVisual::CreateGeometry()
502 Dali::Vector<Vertex> vertices;
503 Dali::Vector<unsigned short> indices;
505 switch( mPrimitiveType )
507 case Toolkit::PrimitiveVisual::Shape::SPHERE:
509 CreateSphere( vertices, indices, mSlices, mStacks );
512 case Toolkit::PrimitiveVisual::Shape::CONE:
514 //Create a conic with zero top radius.
515 CreateConic( vertices, indices, 0, mScaleBottomRadius, mScaleHeight, mSlices );
518 case Toolkit::PrimitiveVisual::Shape::CONICAL_FRUSTRUM:
520 CreateConic( vertices, indices, mScaleTopRadius, mScaleBottomRadius, mScaleHeight, mSlices );
523 case Toolkit::PrimitiveVisual::Shape::CYLINDER:
525 //Create a conic with equal radii on the top and bottom.
526 CreateConic( vertices, indices, mScaleRadius, mScaleRadius, mScaleHeight, mSlices );
529 case Toolkit::PrimitiveVisual::Shape::CUBE:
531 //Create a cube by creating a bevelled cube with minimum bevel.
532 CreateBevelledCube( vertices, indices, mScaleDimensions, 0.0, 0.0 );
535 case Toolkit::PrimitiveVisual::Shape::OCTAHEDRON:
537 //Create an octahedron by creating a bevelled cube with maximum bevel.
538 CreateBevelledCube( vertices, indices, mScaleDimensions, 1.0, mBevelSmoothness );
541 case Toolkit::PrimitiveVisual::Shape::BEVELLED_CUBE:
543 CreateBevelledCube( vertices, indices, mScaleDimensions, mBevelPercentage, mBevelSmoothness );
548 mGeometry = Geometry::New();
551 Property::Map vertexFormat;
552 vertexFormat[POSITION] = Property::VECTOR3;
553 vertexFormat[NORMAL] = Property::VECTOR3;
554 PropertyBuffer surfaceVertices = PropertyBuffer::New( vertexFormat );
555 surfaceVertices.SetData( &vertices[0], vertices.Size() );
557 mGeometry.AddVertexBuffer( surfaceVertices );
559 //Indices for triangle formulation
560 mGeometry.SetIndexBuffer( &indices[0], indices.Size() );
563 void PrimitiveVisual::CreateSphere( Vector<Vertex>& vertices, Vector<unsigned short>& indices, int slices, int stacks )
565 ComputeSphereVertices( vertices, slices, stacks );
566 FormSphereTriangles( indices, slices, stacks );
568 mObjectDimensions = Vector3::ONE;
571 void PrimitiveVisual::CreateConic( Vector<Vertex>& vertices, Vector<unsigned short>& indices, float scaleTopRadius,
572 float scaleBottomRadius, float scaleHeight, int slices )
574 ComputeConicVertices( vertices, scaleTopRadius, scaleBottomRadius, scaleHeight, slices );
575 FormConicTriangles( indices, scaleTopRadius, scaleBottomRadius, slices );
577 //Determine object dimensions, and scale them to be between 0.0 and 1.0.
578 float xDimension = std::max( scaleTopRadius, scaleBottomRadius ) * 2.0f;
579 float yDimension = scaleHeight;
580 float largestDimension = std::max( xDimension, yDimension );
582 mObjectDimensions = Vector3( xDimension / largestDimension, yDimension / largestDimension,
583 xDimension / largestDimension );
586 void PrimitiveVisual::CreateBevelledCube( Vector<Vertex>& vertices, Vector<unsigned short>& indices,
587 Vector3 dimensions, float bevelPercentage, float bevelSmoothness )
589 float maxDimension = std::max( std::max( dimensions.x, dimensions.y ), dimensions.z );
590 dimensions = dimensions / maxDimension;
592 if( bevelPercentage <= MIN_BEVEL_PERCENTAGE ) //No bevel, form a cube.
594 ComputeCubeVertices( vertices, dimensions );
595 FormCubeTriangles( indices );
597 else if( bevelPercentage >= MAX_BEVEL_PERCENTAGE ) //Max bevel, form an octahedron.
599 ComputeOctahedronVertices( vertices, dimensions, bevelSmoothness );
600 FormOctahedronTriangles( indices );
602 else //In between, form a bevelled cube.
604 ComputeBevelledCubeVertices( vertices, dimensions, bevelPercentage, bevelSmoothness );
605 FormBevelledCubeTriangles( indices );
608 mObjectDimensions = dimensions;
611 void PrimitiveVisual::ComputeCircleTables( Vector<float>& sinTable, Vector<float>& cosTable, int divisions,
619 const float angleDivision = ( halfCircle ? 1.0f : 2.0f ) * Dali::Math::PI / ( float ) divisions;
621 sinTable.Resize( divisions );
622 cosTable.Resize( divisions );
624 for( int i = 0; i < divisions; i++ )
626 sinTable[i] = sin( angleDivision * i );
627 cosTable[i] = cos( angleDivision * i );
631 void PrimitiveVisual::ComputeSphereVertices( Vector<Vertex>& vertices, int slices, int stacks )
633 //Tables for calculating slices angles and stacks angles, respectively.
634 Vector<float> sinTable1;
635 Vector<float> cosTable1;
636 Vector<float> sinTable2;
637 Vector<float> cosTable2;
639 ComputeCircleTables( sinTable1, cosTable1, slices, false );
640 ComputeCircleTables( sinTable2, cosTable2, stacks, true );
642 int numVertices = slices * ( stacks - 1 ) + 2;
643 vertices.Resize( numVertices );
645 int vertexIndex = 0; //Track progress through vertices.
651 vertices[vertexIndex].position = Vector3( 0.0, 0.5, 0.0 );
652 vertices[vertexIndex].normal = Vector3( 0.0, 1.0, 0.0 );
656 for( int i = 1; i < stacks; i++ )
658 for( int j = 0; j < slices; j++, vertexIndex++ )
660 x = cosTable1[j] * sinTable2[i];
662 z = sinTable1[j] * sinTable2[i];
664 vertices[vertexIndex].position = Vector3( x / 2.0f, y / 2.0f, z / 2.0f );
665 vertices[vertexIndex].normal = Vector3( x, y, z );
670 vertices[vertexIndex].position = Vector3( 0.0, -0.5, 0.0 );
671 vertices[vertexIndex].normal = Vector3( 0.0, -1.0, 0.0 );
674 void PrimitiveVisual::FormSphereTriangles( Vector<unsigned short>& indices, int slices, int stacks )
678 //Set indices to placeholder "error" values.
679 //This will display nothing, which is the expected behaviour for this edge case.
684 int numTriangles = 2 * slices * ( stacks - 1 );
686 indices.Resize( 3 * numTriangles );
688 int indiceIndex = 0; //Used to keep track of progress through indices.
689 int previousCycleBeginning = 1; //Stores the index of the vertex that started the cycle of the previous stack.
690 int currentCycleBeginning = 1 + slices;
692 //Top stack. Loop from index 1 to index slices, as not counting the very first vertex.
693 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
695 indices[indiceIndex] = 0;
698 //End, so loop around.
699 indices[indiceIndex + 1] = 1;
703 indices[indiceIndex + 1] = i + 1;
705 indices[indiceIndex + 2] = i;
708 //Middle Stacks. Want to form triangles between the top and bottom stacks, so loop up to the number of stacks - 2.
709 for( int i = 0; i < stacks - 2; i++, previousCycleBeginning += slices, currentCycleBeginning += slices )
711 for( int j = 0; j < slices; j++, indiceIndex += 6 )
713 if( j == slices - 1 )
715 //End, so loop around.
716 indices[indiceIndex] = previousCycleBeginning + j;
717 indices[indiceIndex + 1] = previousCycleBeginning;
718 indices[indiceIndex + 2] = currentCycleBeginning + j;
719 indices[indiceIndex + 3] = currentCycleBeginning + j;
720 indices[indiceIndex + 4] = previousCycleBeginning;
721 indices[indiceIndex + 5] = currentCycleBeginning;
725 indices[indiceIndex] = previousCycleBeginning + j;
726 indices[indiceIndex + 1] = previousCycleBeginning + 1 + j;
727 indices[indiceIndex + 2] = currentCycleBeginning + j;
728 indices[indiceIndex + 3] = currentCycleBeginning + j;
729 indices[indiceIndex + 4] = previousCycleBeginning + 1 + j;
730 indices[indiceIndex + 5] = currentCycleBeginning + 1 + j;
735 //Bottom stack. Loop around the last stack from the previous loop, and go up to the penultimate vertex.
736 for( int i = 0; i < slices; i++, indiceIndex += 3 )
738 indices[indiceIndex] = previousCycleBeginning + slices;
739 indices[indiceIndex + 1] = previousCycleBeginning + i;
740 if( i == slices - 1 )
742 //End, so loop around.
743 indices[indiceIndex + 2] = previousCycleBeginning;
747 indices[indiceIndex + 2] = previousCycleBeginning + i + 1;
752 void PrimitiveVisual::ComputeConicVertices( Vector<Vertex>& vertices, float scaleTopRadius,
753 float scaleBottomRadius, float scaleHeight, int slices )
755 int vertexIndex = 0; //Track progress through vertices.
756 Vector<float> sinTable;
757 Vector<float> cosTable;
759 ComputeCircleTables( sinTable, cosTable, slices, false );
761 int numVertices = 2; //Always will have one at the top and one at the bottom.
763 //Add vertices for each circle. Need two per point for different face normals.
764 if( scaleTopRadius > 0.0 )
766 numVertices += 2 * slices;
768 if( scaleBottomRadius > 0.0 )
770 numVertices += 2 * slices;
773 vertices.Resize( numVertices );
776 //Scale to bounding region of -0.5 to 0.5 (i.e range of 1).
777 float biggestObjectDimension = std::max( std::max( scaleTopRadius * 2.0f, scaleBottomRadius * 2.0f ), scaleHeight );
778 scaleTopRadius = scaleTopRadius / biggestObjectDimension;
779 scaleBottomRadius = scaleBottomRadius / biggestObjectDimension;
781 //Dimensions for vertex coordinates. Y is constant, and so can be initialised now.
783 float y = scaleHeight / biggestObjectDimension / 2.0f;
787 vertices[0].position = Vector3( 0, y, 0 );
788 vertices[0].normal = Vector3( 0, 1, 0 );
792 if( scaleTopRadius > 0.0 )
794 //Loop around the circle.
795 for( int i = 0; i < slices; i++, vertexIndex++ )
797 x = sinTable[i] * scaleTopRadius;
798 z = cosTable[i] * scaleTopRadius;
800 //Upward-facing normal.
801 vertices[vertexIndex].position = Vector3( x, y, z );
802 vertices[vertexIndex].normal = Vector3( 0, 1, 0 );
804 //Outward-facing normal.
805 vertices[vertexIndex + slices].position = Vector3( x, y, z );
806 vertices[vertexIndex + slices].normal = Vector3( x, 0, z );
809 vertexIndex += slices;
813 if( scaleBottomRadius > 0.0 )
815 //Loop around the circle.
816 for( int i = 0; i < slices; i++, vertexIndex++ )
818 x = sinTable[i] * scaleBottomRadius;
819 z = cosTable[i] * scaleBottomRadius;
821 //Outward-facing normal.
822 vertices[vertexIndex].position = Vector3( x, -y, z );
823 vertices[vertexIndex].normal = Vector3( x, 0, z );
825 //Downward-facing normal.
826 vertices[vertexIndex + slices].position = Vector3( x, -y, z );
827 vertices[vertexIndex + slices].normal = Vector3( 0, -1, 0 );
830 vertexIndex += slices;
834 vertices[vertexIndex].position = Vector3( 0, -y, 0 );
835 vertices[vertexIndex].normal = Vector3( 0, -1, 0 );
839 void PrimitiveVisual::FormConicTriangles( Vector<unsigned short>& indices, float scaleTopRadius,
840 float scaleBottomRadius, int slices )
842 int indiceIndex = 0; //Track progress through indices.
843 int numTriangles = 0;
844 bool coneTop = scaleTopRadius <= 0.0;
845 bool coneBottom = scaleBottomRadius <= 0.0;
847 if( coneTop && coneBottom )
849 //Set indices to placeholder "error" values.
850 //This will display nothing, which is the expected behaviour for this edge case.
857 numTriangles += 2 * slices;
861 numTriangles += 2 * slices;
864 indices.Resize( 3 * numTriangles );
866 //Switch on the type of conic we have.
867 if( !coneTop && !coneBottom )
869 //Top circle. Start at index of first outer point and go around.
870 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
872 indices[indiceIndex] = 0;
873 indices[indiceIndex + 1] = i;
876 //End, so loop around.
877 indices[indiceIndex + 2] = 1;
881 indices[indiceIndex + 2] = i + 1;
885 int topCycleBeginning = slices + 1;
886 int bottomCycleBeginning = topCycleBeginning + slices;
889 for( int i = 0; i < slices; i++, indiceIndex += 6 )
891 if( i == slices - 1 )
893 //End, so loop around.
894 indices[indiceIndex] = topCycleBeginning + i;
895 indices[indiceIndex + 1] = bottomCycleBeginning + i;
896 indices[indiceIndex + 2] = topCycleBeginning;
897 indices[indiceIndex + 3] = bottomCycleBeginning + i;
898 indices[indiceIndex + 4] = bottomCycleBeginning;
899 indices[indiceIndex + 5] = topCycleBeginning;
903 indices[indiceIndex] = topCycleBeginning + i;
904 indices[indiceIndex + 1] = bottomCycleBeginning + i;
905 indices[indiceIndex + 2] = topCycleBeginning + 1 + i;
906 indices[indiceIndex + 3] = bottomCycleBeginning + i;
907 indices[indiceIndex + 4] = bottomCycleBeginning + 1 + i;
908 indices[indiceIndex + 5] = topCycleBeginning + 1 + i;
912 int bottomFaceCycleBeginning = bottomCycleBeginning + slices;
915 for( int i = 0; i < slices; i++, indiceIndex += 3 )
917 indices[indiceIndex] = bottomFaceCycleBeginning;
918 if( i == slices - 1 )
920 //End, so loop around.
921 indices[indiceIndex + 1] = bottomFaceCycleBeginning;
925 indices[indiceIndex + 1] = bottomFaceCycleBeginning + i + 1;
927 indices[indiceIndex + 2] = bottomFaceCycleBeginning + i;
930 else if( !coneTop || !coneBottom )
932 //Top circle/edges. Start at index of first outer point and go around.
933 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
935 indices[indiceIndex] = 0;
936 indices[indiceIndex + 1] = i;
939 //End, so loop around.
940 indices[indiceIndex + 2] = 1;
944 indices[indiceIndex + 2] = i + 1;
948 //Bottom circle/edges. Start at index of first outer point and go around.
949 for( int i = 1; i <= slices; i++, indiceIndex += 3 )
951 indices[indiceIndex] = 2 * slices + 1;
954 //End, so loop around.
955 indices[indiceIndex + 1] = slices + 1;
959 indices[indiceIndex + 1] = slices + i + 1;
961 indices[indiceIndex + 2] = slices + i;
966 void PrimitiveVisual::ComputeCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions )
968 int numVertices = 4 * 6; //Four per face.
969 int vertexIndex = 0; //Tracks progress through vertices.
970 float scaledX = 0.5 * dimensions.x;
971 float scaledY = 0.5 * dimensions.y;
972 float scaledZ = 0.5 * dimensions.z;
974 vertices.Resize( numVertices );
976 Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
978 Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
981 positions[0] = Vector3( -scaledX, scaledY, -scaledZ );
982 positions[1] = Vector3( scaledX, scaledY, -scaledZ );
983 positions[2] = Vector3( scaledX, scaledY, scaledZ );
984 positions[3] = Vector3( -scaledX, scaledY, scaledZ );
985 positions[4] = Vector3( -scaledX, -scaledY, -scaledZ );
986 positions[5] = Vector3( scaledX, -scaledY, -scaledZ );
987 positions[6] = Vector3( scaledX, -scaledY, scaledZ );
988 positions[7] = Vector3( -scaledX, -scaledY, scaledZ );
990 normals[0] = Vector3( 0, 1, 0 );
991 normals[1] = Vector3( 0, 0, -1 );
992 normals[2] = Vector3( 1, 0, 0 );
993 normals[3] = Vector3( 0, 0, 1 );
994 normals[4] = Vector3( -1, 0, 0 );
995 normals[5] = Vector3( 0, -1, 0 );
997 //Top face, upward normals.
998 for( int i = 0; i < 4; i++, vertexIndex++ )
1000 vertices[vertexIndex].position = positions[i];
1001 vertices[vertexIndex].normal = normals[0];
1004 //Top face, outward normals.
1005 for( int i = 0; i < 4; i++, vertexIndex += 2 )
1007 vertices[vertexIndex].position = positions[i];
1008 vertices[vertexIndex].normal = normals[i + 1];
1012 //End, so loop around.
1013 vertices[vertexIndex + 1].position = positions[0];
1017 vertices[vertexIndex + 1].position = positions[i + 1];
1019 vertices[vertexIndex + 1].normal = normals[i + 1];
1022 //Bottom face, outward normals.
1023 for( int i = 0; i < 4; i++, vertexIndex += 2 )
1025 vertices[vertexIndex].position = positions[i + 4];
1026 vertices[vertexIndex].normal = normals[i + 1];
1030 //End, so loop around.
1031 vertices[vertexIndex + 1].position = positions[4];
1035 vertices[vertexIndex + 1].position = positions[i + 5];
1037 vertices[vertexIndex + 1].normal = normals[i + 1];
1040 //Bottom face, downward normals.
1041 for( int i = 0; i < 4; i++, vertexIndex++ )
1043 vertices[vertexIndex].position = positions[i + 4];
1044 vertices[vertexIndex].normal = normals[5];
1049 void PrimitiveVisual::FormCubeTriangles( Vector<unsigned short>& indices )
1051 int numTriangles = 12;
1052 int triangleIndex = 0; //Track progress through indices.
1054 indices.Resize( 3 * numTriangles );
1057 indices[triangleIndex] = 0;
1058 indices[triangleIndex + 1] = 2;
1059 indices[triangleIndex + 2] = 1;
1060 indices[triangleIndex + 3] = 2;
1061 indices[triangleIndex + 4] = 0;
1062 indices[triangleIndex + 5] = 3;
1065 int topFaceStart = 4;
1066 int bottomFaceStart = 12;
1069 for( int i = 0; i < 8; i += 2, triangleIndex += 6 )
1071 indices[triangleIndex ] = i + topFaceStart;
1072 indices[triangleIndex + 1] = i + topFaceStart + 1;
1073 indices[triangleIndex + 2] = i + bottomFaceStart + 1;
1074 indices[triangleIndex + 3] = i + topFaceStart;
1075 indices[triangleIndex + 4] = i + bottomFaceStart + 1;
1076 indices[triangleIndex + 5] = i + bottomFaceStart;
1080 indices[triangleIndex] = 20;
1081 indices[triangleIndex + 1] = 21;
1082 indices[triangleIndex + 2] = 22;
1083 indices[triangleIndex + 3] = 22;
1084 indices[triangleIndex + 4] = 23;
1085 indices[triangleIndex + 5] = 20;
1088 void PrimitiveVisual::ComputeOctahedronVertices( Vector<Vertex>& vertices, Vector3 dimensions, float smoothness )
1090 int numVertices = 3 * 8; //Three per face
1091 int vertexIndex = 0; //Tracks progress through vertices.
1092 float scaledX = 0.5 * dimensions.x;
1093 float scaledY = 0.5 * dimensions.y;
1094 float scaledZ = 0.5 * dimensions.z;
1096 vertices.Resize( numVertices );
1098 Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
1099 positions.Resize(6);
1100 Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
1102 Vector<Vector3> outerNormals; //Holds normals that point outwards at each vertex.
1103 outerNormals.Resize( 6 );
1105 positions[0] = Vector3( 0.0, scaledY, 0.0 );
1106 positions[1] = Vector3( -scaledX, 0.0, 0.0 );
1107 positions[2] = Vector3( 0.0, 0.0, -scaledZ );
1108 positions[3] = Vector3( scaledX, 0.0, 0.0 );
1109 positions[4] = Vector3( 0.0, 0.0, scaledZ );
1110 positions[5] = Vector3( 0.0, -scaledY, 0.0 );
1112 normals[0] = Vector3( -1, 1, -1 );
1113 normals[1] = Vector3( 1, 1, -1 );
1114 normals[2] = Vector3( 1, 1, 1 );
1115 normals[3] = Vector3( -1, 1, 1 );
1116 normals[4] = Vector3( -1, -1, -1 );
1117 normals[5] = Vector3( 1, -1, -1 );
1118 normals[6] = Vector3( 1, -1, 1 );
1119 normals[7] = Vector3( -1, -1, 1 );
1121 outerNormals[0] = Vector3( 0, 1, 0 );
1122 outerNormals[1] = Vector3( -1, 0, 0 );
1123 outerNormals[2] = Vector3( 0, 0, -1 );
1124 outerNormals[3] = Vector3( 1, 0, 0 );
1125 outerNormals[4] = Vector3( 0, 0, 1 );
1126 outerNormals[5] = Vector3( 0, -1, 0 );
1128 //Loop through top faces.
1129 for( int i = 0; i < 4; i++, vertexIndex += 3 )
1133 //End, so loop around.
1134 vertices[vertexIndex ].position = positions[0];
1135 vertices[vertexIndex ].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1136 vertices[vertexIndex + 1].position = positions[1];
1137 vertices[vertexIndex + 1].normal = outerNormals[1] * smoothness + normals[i] * (1 - smoothness);
1138 vertices[vertexIndex + 2].position = positions[i + 1];
1139 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1143 vertices[vertexIndex ].position = positions[0];
1144 vertices[vertexIndex ].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1145 vertices[vertexIndex + 1].position = positions[i + 2];
1146 vertices[vertexIndex + 1].normal = outerNormals[i + 2] * smoothness + normals[i] * (1 - smoothness);
1147 vertices[vertexIndex + 2].position = positions[i + 1];
1148 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1152 //Loop through bottom faces.
1153 for( int i = 0; i < 4; i++, vertexIndex += 3 )
1157 //End, so loop around.
1158 vertices[vertexIndex ].position = positions[5];
1159 vertices[vertexIndex ].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1160 vertices[vertexIndex + 1].position = positions[i + 1];
1161 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1162 vertices[vertexIndex + 2].position = positions[1];
1163 vertices[vertexIndex + 2].normal = outerNormals[1] * smoothness + normals[i + 4] * (1 - smoothness);
1167 vertices[vertexIndex ].position = positions[5];
1168 vertices[vertexIndex ].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1169 vertices[vertexIndex + 1].position = positions[i + 1];
1170 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1171 vertices[vertexIndex + 2].position = positions[i + 2];
1172 vertices[vertexIndex + 2].normal = outerNormals[i + 2] * smoothness + normals[i + 4] * (1 - smoothness);
1177 void PrimitiveVisual::FormOctahedronTriangles( Vector<unsigned short>& indices )
1179 int numTriangles = 8;
1180 int numIndices = numTriangles * 3;
1182 indices.Resize( numIndices );
1184 for( unsigned short i = 0; i < numIndices; i++ )
1190 void PrimitiveVisual::ComputeBevelledCubeVertices( Vector<Vertex>& vertices, Vector3 dimensions,
1191 float bevelPercentage, float bevelSmoothness )
1193 int numPositions = 24;
1195 int numOuterFaces = 6;
1196 int numVertices = 6 * 4 + 12 * 4 + 8 * 3; //Six outer faces, 12 slanting rectangles, 8 slanting triangles.
1197 int vertexIndex = 0; //Track progress through vertices.
1198 int normalIndex = 0; //Track progress through normals, as vertices are calculated per face.
1200 float minDimension = std::min( std::min( dimensions.x, dimensions.y ), dimensions.z );
1201 float bevelAmount = 0.5 * std::min( bevelPercentage, minDimension ); //Cap bevel amount if necessary.
1203 //Distances from centre to outer edge points.
1204 float outerX = 0.5 * dimensions.x;
1205 float outerY = 0.5 * dimensions.y;
1206 float outerZ = 0.5 * dimensions.z;
1208 //Distances from centre to bevelled points.
1209 float bevelX = outerX - bevelAmount;
1210 float bevelY = outerY - bevelAmount;
1211 float bevelZ = outerZ - bevelAmount;
1213 Vector<Vector3> positions; //Holds object points, to be shared between vertexes.
1214 positions.Resize( numPositions );
1215 Vector<Vector3> normals; //Holds face normals, to be shared between vertexes.
1216 normals.Resize( numFaces );
1217 Vector<Vector3> outerNormals; //Holds normals of the outermost faces specifically.
1218 outerNormals.Resize( numOuterFaces );
1219 vertices.Resize( numVertices );
1221 //Topmost face positions.
1222 positions[0 ] = Vector3( -bevelX, outerY, -bevelZ );
1223 positions[1 ] = Vector3( bevelX, outerY, -bevelZ );
1224 positions[2 ] = Vector3( bevelX, outerY, bevelZ );
1225 positions[3 ] = Vector3( -bevelX, outerY, bevelZ );
1227 //Second layer positions.
1228 positions[4 ] = Vector3( -outerX, bevelY, -bevelZ );
1229 positions[5 ] = Vector3( -bevelX, bevelY, -outerZ );
1230 positions[6 ] = Vector3( bevelX, bevelY, -outerZ );
1231 positions[7 ] = Vector3( outerX, bevelY, -bevelZ );
1232 positions[8 ] = Vector3( outerX, bevelY, bevelZ );
1233 positions[9 ] = Vector3( bevelX, bevelY, outerZ );
1234 positions[10] = Vector3( -bevelX, bevelY, outerZ );
1235 positions[11] = Vector3( -outerX, bevelY, bevelZ );
1237 //Third layer positions.
1238 positions[12] = Vector3( -outerX, -bevelY, -bevelZ );
1239 positions[13] = Vector3( -bevelX, -bevelY, -outerZ );
1240 positions[14] = Vector3( bevelX, -bevelY, -outerZ );
1241 positions[15] = Vector3( outerX, -bevelY, -bevelZ );
1242 positions[16] = Vector3( outerX, -bevelY, bevelZ );
1243 positions[17] = Vector3( bevelX, -bevelY, outerZ );
1244 positions[18] = Vector3( -bevelX, -bevelY, outerZ );
1245 positions[19] = Vector3( -outerX, -bevelY, bevelZ );
1247 //Bottom-most face positions.
1248 positions[20] = Vector3( -bevelX, -outerY, -bevelZ );
1249 positions[21] = Vector3( bevelX, -outerY, -bevelZ );
1250 positions[22] = Vector3( bevelX, -outerY, bevelZ );
1251 positions[23] = Vector3( -bevelX, -outerY, bevelZ );
1254 normals[0 ] = Vector3( 0, 1, 0 );
1256 //Top slope normals.
1257 normals[1 ] = Vector3( -1, 1, -1 );
1258 normals[2 ] = Vector3( 0, 1, -1 );
1259 normals[3 ] = Vector3( 1, 1, -1 );
1260 normals[4 ] = Vector3( 1, 1, 0 );
1261 normals[5 ] = Vector3( 1, 1, 1 );
1262 normals[6 ] = Vector3( 0, 1, 1 );
1263 normals[7 ] = Vector3( -1, 1, 1 );
1264 normals[8 ] = Vector3( -1, 1, 0 );
1267 normals[9 ] = Vector3( -1, 0, -1 );
1268 normals[10] = Vector3( 0, 0, -1 );
1269 normals[11] = Vector3( 1, 0, -1 );
1270 normals[12] = Vector3( 1, 0, 0 );
1271 normals[13] = Vector3( 1, 0, 1 );
1272 normals[14] = Vector3( 0, 0, 1 );
1273 normals[15] = Vector3( -1, 0, 1 );
1274 normals[16] = Vector3( -1, 0, 0 );
1276 //Bottom slope normals.
1277 normals[17] = Vector3( -1, -1, -1 );
1278 normals[18] = Vector3( 0, -1, -1 );
1279 normals[19] = Vector3( 1, -1, -1 );
1280 normals[20] = Vector3( 1, -1, 0 );
1281 normals[21] = Vector3( 1, -1, 1 );
1282 normals[22] = Vector3( 0, -1, 1 );
1283 normals[23] = Vector3( -1, -1, 1 );
1284 normals[24] = Vector3( -1, -1, 0 );
1286 //Bottom face normal.
1287 normals[25] = Vector3( 0, -1, 0 );
1289 //Top, back, right, front, left and bottom faces, respectively.
1290 outerNormals[0] = Vector3( 0, 1, 0 );
1291 outerNormals[1] = Vector3( 0, 0, -1 );
1292 outerNormals[2] = Vector3( 1, 0, 0 );
1293 outerNormals[3] = Vector3( 0, 0, 1 );
1294 outerNormals[4] = Vector3( -1, 0, 0 );
1295 outerNormals[5] = Vector3( 0, -1, 0 );
1297 //Topmost face vertices.
1298 for( int i = 0; i < 4; i++, vertexIndex++ )
1300 vertices[vertexIndex].position = positions[i];
1301 vertices[vertexIndex].normal = normals[normalIndex];
1306 //Top slope vertices.
1307 for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
1310 vertices[vertexIndex ].position = positions[i];
1311 vertices[vertexIndex ].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1312 vertices[vertexIndex + 1].position = positions[2 * i + 4];
1313 vertices[vertexIndex + 1].normal = outerNormals[( i == 0 ) ? 4 : i] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1314 vertices[vertexIndex + 2].position = positions[2 * i + 5];
1315 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1320 //End, so loop around.
1321 vertices[vertexIndex + 3].position = positions[i];
1322 vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1323 vertices[vertexIndex + 4].position = positions[0];
1324 vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1325 vertices[vertexIndex + 5].position = positions[2 * i + 5];
1326 vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1327 vertices[vertexIndex + 6].position = positions[4];
1328 vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1332 vertices[vertexIndex + 3].position = positions[i];
1333 vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1334 vertices[vertexIndex + 4].position = positions[i + 1];
1335 vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1336 vertices[vertexIndex + 5].position = positions[2 * i + 5];
1337 vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1338 vertices[vertexIndex + 6].position = positions[2 * i + 6];
1339 vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1343 int secondCycleBeginning = 4;
1344 int thirdCycleBeginning = secondCycleBeginning + 8;
1345 int bottomCycleBeginning = thirdCycleBeginning + 8;
1348 for( int i = 0; i < 8; i++, vertexIndex += 4, normalIndex++ )
1352 //End, so loop around.
1353 vertices[vertexIndex ].position = positions[secondCycleBeginning + i];
1354 vertices[vertexIndex ].normal = normals[normalIndex];
1355 vertices[vertexIndex + 1].position = positions[secondCycleBeginning];
1356 vertices[vertexIndex + 1].normal = normals[normalIndex];
1357 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1358 vertices[vertexIndex + 2].normal = normals[normalIndex];
1359 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning];
1360 vertices[vertexIndex + 3].normal = normals[normalIndex];
1362 else if( (i % 2) == 0 )
1364 //'even' faces are corner ones, and need smoothing.
1365 vertices[vertexIndex ].position = positions[secondCycleBeginning + i];
1366 vertices[vertexIndex ].normal = outerNormals[( i == 0 ) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1367 vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1368 vertices[vertexIndex + 1].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1369 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1370 vertices[vertexIndex + 2].normal = outerNormals[( i == 0 ) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1371 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1372 vertices[vertexIndex + 3].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1376 //'odd' faces are outer ones, and so don't need smoothing.
1377 vertices[vertexIndex ].position = positions[secondCycleBeginning + i];
1378 vertices[vertexIndex ].normal = normals[normalIndex];
1379 vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1380 vertices[vertexIndex + 1].normal = normals[normalIndex];
1381 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1382 vertices[vertexIndex + 2].normal = normals[normalIndex];
1383 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1384 vertices[vertexIndex + 3].normal = normals[normalIndex];
1388 //Bottom slope vertices.
1389 for( int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2 )
1392 vertices[vertexIndex ].position = positions[thirdCycleBeginning + 2 * i];
1393 vertices[vertexIndex ].normal = outerNormals[( i == 0 ) ? 4 : i] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1394 vertices[vertexIndex + 1].position = positions[thirdCycleBeginning + 2 * i + 1];
1395 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1396 vertices[vertexIndex + 2].position = positions[bottomCycleBeginning + i];
1397 vertices[vertexIndex + 2].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1402 //End, so loop around.
1403 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1404 vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1405 vertices[vertexIndex + 4].position = positions[thirdCycleBeginning];
1406 vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1407 vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1408 vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1409 vertices[vertexIndex + 6].position = positions[bottomCycleBeginning];
1410 vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1414 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1415 vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1416 vertices[vertexIndex + 4].position = positions[thirdCycleBeginning + 2 * i + 2];
1417 vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1418 vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1419 vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1420 vertices[vertexIndex + 6].position = positions[bottomCycleBeginning + i + 1];
1421 vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1425 //Bottom-most face vertices.
1426 for( int i = 0; i < 4; i++, vertexIndex++ )
1428 vertices[vertexIndex].position = positions[ bottomCycleBeginning + i];
1429 vertices[vertexIndex].normal = normals[normalIndex];
1435 void PrimitiveVisual::FormBevelledCubeTriangles( Vector<unsigned short>& indices )
1437 int numTriangles = 44; //(Going from top to bottom, that's 2 + 12 + 16 + 12 + 2)
1438 int indiceIndex = 0; //Track progress through indices.
1439 int vertexIndex = 0; //Track progress through vertices as they're processed.
1441 indices.Resize( 3 * numTriangles );
1444 indices[indiceIndex ] = vertexIndex;
1445 indices[indiceIndex + 1] = vertexIndex + 2;
1446 indices[indiceIndex + 2] = vertexIndex + 1;
1447 indices[indiceIndex + 3] = vertexIndex + 0;
1448 indices[indiceIndex + 4] = vertexIndex + 3;
1449 indices[indiceIndex + 5] = vertexIndex + 2;
1454 for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1457 indices[indiceIndex ] = vertexIndex;
1458 indices[indiceIndex + 1] = vertexIndex + 2;
1459 indices[indiceIndex + 2] = vertexIndex + 1;
1462 indices[indiceIndex + 3] = vertexIndex + 3;
1463 indices[indiceIndex + 4] = vertexIndex + 4;
1464 indices[indiceIndex + 5] = vertexIndex + 5;
1465 indices[indiceIndex + 6] = vertexIndex + 4;
1466 indices[indiceIndex + 7] = vertexIndex + 6;
1467 indices[indiceIndex + 8] = vertexIndex + 5;
1471 for( int i = 0; i < 8; i++, indiceIndex += 6, vertexIndex += 4 )
1473 indices[indiceIndex ] = vertexIndex;
1474 indices[indiceIndex + 1] = vertexIndex + 1;
1475 indices[indiceIndex + 2] = vertexIndex + 2;
1476 indices[indiceIndex + 3] = vertexIndex + 1;
1477 indices[indiceIndex + 4] = vertexIndex + 3;
1478 indices[indiceIndex + 5] = vertexIndex + 2;
1482 for( int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7 )
1485 indices[indiceIndex ] = vertexIndex;
1486 indices[indiceIndex + 1] = vertexIndex + 1;
1487 indices[indiceIndex + 2] = vertexIndex + 2;
1490 indices[indiceIndex + 3] = vertexIndex + 3;
1491 indices[indiceIndex + 4] = vertexIndex + 4;
1492 indices[indiceIndex + 5] = vertexIndex + 5;
1493 indices[indiceIndex + 6] = vertexIndex + 4;
1494 indices[indiceIndex + 7] = vertexIndex + 6;
1495 indices[indiceIndex + 8] = vertexIndex + 5;
1499 indices[indiceIndex ] = vertexIndex;
1500 indices[indiceIndex + 1] = vertexIndex + 1;
1501 indices[indiceIndex + 2] = vertexIndex + 2;
1502 indices[indiceIndex + 3] = vertexIndex + 0;
1503 indices[indiceIndex + 4] = vertexIndex + 2;
1504 indices[indiceIndex + 5] = vertexIndex + 3;
1509 } // namespace Internal
1511 } // namespace Toolkit