2 * Copyright (c) 2022 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/devel-api/common/stage.h>
23 #include <dali/devel-api/scripting/enum-helper.h>
24 #include <dali/devel-api/scripting/scripting.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/public-api/common/constants.h>
29 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
30 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
31 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
32 #include <dali-toolkit/public-api/visuals/visual-properties.h>
43 DALI_ENUM_TO_STRING_TABLE_BEGIN(SHAPE_TYPE)
44 DALI_ENUM_TO_STRING_WITH_SCOPE(Toolkit::PrimitiveVisual::Shape, SPHERE)
45 DALI_ENUM_TO_STRING_WITH_SCOPE(Toolkit::PrimitiveVisual::Shape, CONE)
46 DALI_ENUM_TO_STRING_WITH_SCOPE(Toolkit::PrimitiveVisual::Shape, CYLINDER)
47 DALI_ENUM_TO_STRING_WITH_SCOPE(Toolkit::PrimitiveVisual::Shape, CUBE)
48 DALI_ENUM_TO_STRING_WITH_SCOPE(Toolkit::PrimitiveVisual::Shape, OCTAHEDRON)
49 DALI_ENUM_TO_STRING_WITH_SCOPE(Toolkit::PrimitiveVisual::Shape, BEVELLED_CUBE)
50 DALI_ENUM_TO_STRING_WITH_SCOPE(Toolkit::PrimitiveVisual::Shape, CONICAL_FRUSTUM)
51 DALI_ENUM_TO_STRING_TABLE_END(SHAPE_TYPE)
53 //Primitive property defaults
54 const int DEFAULT_SLICES = 128; ///< For spheres and conics
55 const int DEFAULT_STACKS = 128; ///< For spheres and conics
56 const float DEFAULT_SCALE_TOP_RADIUS = 1.0; ///< For conical frustums
57 const float DEFAULT_SCALE_BOTTOM_RADIUS = 1.5; ///< For cones and conical frustums
58 const float DEFAULT_SCALE_HEIGHT = 3.0; ///< For all conics
59 const float DEFAULT_SCALE_RADIUS = 1.0; ///< For cylinders
60 const float DEFAULT_BEVEL_PERCENTAGE = 0.0; ///< For bevelled cubes
61 const float DEFAULT_BEVEL_SMOOTHNESS = 0.0; ///< For bevelled cubes
62 const Vector4 DEFAULT_COLOR = Vector4(0.5, 0.5, 0.5, 1.0); ///< Grey, for all.
65 const int MIN_SLICES = 3; ///< Minimum number of slices for spheres and conics
66 const int MIN_STACKS = 2; ///< Minimum number of stacks for spheres and conics
67 const int MAX_PARTITIONS = 255; ///< Maximum number of slices or stacks for spheres and conics
68 const float MIN_BEVEL_PERCENTAGE = 0.0; ///< Minimum bevel percentage for bevelled cubes
69 const float MAX_BEVEL_PERCENTAGE = 1.0; ///< Maximum bevel percentage for bevelled cubes
70 const float MIN_SMOOTHNESS = 0.0; ///< Minimum bevel smoothness for bevelled cubes
71 const float MAX_SMOOTHNESS = 1.0; ///< Maximum bevel smoothness for bevelled cubes
73 //Specific shape labels.
74 const char* const SPHERE_LABEL("SPHERE");
75 const char* const CONE_LABEL("CONE");
76 const char* const CYLINDER_LABEL("CYLINDER");
77 const char* const CUBE_LABEL("CUBE");
78 const char* const OCTAHEDRON_LABEL("OCTAHEDRON");
79 const char* const BEVELLED_CUBE_LABEL("BEVELLED_CUBE");
80 const char* const CONICAL_FRUSTUM_LABEL("CONICAL_FRUSTUM");
83 const char* const OBJECT_MATRIX_UNIFORM_NAME("uObjectMatrix");
84 const char* const OBJECT_DIMENSIONS_UNIFORM_NAME("uObjectDimensions");
85 const char* const STAGE_OFFSET_UNIFORM_NAME("uStageOffset");
88 const char* const POSITION("aPosition");
89 const char* const NORMAL("aNormal");
90 const char* const INDICES("aIndices");
92 } // unnamed namespace
94 PrimitiveVisualPtr PrimitiveVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
96 PrimitiveVisualPtr primitiveVisualPtr(new PrimitiveVisual(factoryCache));
97 primitiveVisualPtr->SetProperties(properties);
98 primitiveVisualPtr->Initialize();
99 return primitiveVisualPtr;
102 PrimitiveVisual::PrimitiveVisual(VisualFactoryCache& factoryCache)
103 : Visual::Base(factoryCache, Visual::FittingMode::FIT_KEEP_ASPECT_RATIO, Toolkit::Visual::PRIMITIVE),
104 mScaleDimensions(Vector3::ONE),
105 mScaleTopRadius(DEFAULT_SCALE_TOP_RADIUS),
106 mScaleBottomRadius(DEFAULT_SCALE_BOTTOM_RADIUS),
107 mScaleHeight(DEFAULT_SCALE_HEIGHT),
108 mScaleRadius(DEFAULT_SCALE_RADIUS),
109 mBevelPercentage(DEFAULT_BEVEL_PERCENTAGE),
110 mBevelSmoothness(DEFAULT_BEVEL_SMOOTHNESS),
111 mSlices(DEFAULT_SLICES),
112 mStacks(DEFAULT_STACKS),
113 mPrimitiveType(Toolkit::PrimitiveVisual::Shape::SPHERE)
115 mImpl->mMixColor = DEFAULT_COLOR;
118 PrimitiveVisual::~PrimitiveVisual()
122 void PrimitiveVisual::DoSetProperties(const Property::Map& propertyMap)
124 //Find out which shape to renderer.
125 Property::Value* primitiveTypeValue = propertyMap.Find(Toolkit::PrimitiveVisual::Property::SHAPE, PRIMITIVE_SHAPE);
126 if(primitiveTypeValue)
128 Scripting::GetEnumerationProperty(*primitiveTypeValue, SHAPE_TYPE_TABLE, SHAPE_TYPE_TABLE_COUNT, mPrimitiveType);
132 DALI_LOG_ERROR("Fail to provide shape to the PrimitiveVisual object.\n");
135 // By virtue of DoSetProperties being called last, this will override
136 // anything set by Toolkit::Visual::Property::MIX_COLOR
137 Property::Value* colorValue = propertyMap.Find(Toolkit::PrimitiveVisual::Property::MIX_COLOR, MIX_COLOR);
141 if(colorValue->Get(color))
143 Property::Type type = colorValue->GetType();
144 if(type == Property::VECTOR4)
148 else if(type == Property::VECTOR3)
150 Vector3 color3(color);
156 Property::Value* slices = propertyMap.Find(Toolkit::PrimitiveVisual::Property::SLICES, SLICES);
159 if(slices->Get(mSlices))
162 if(mSlices > MAX_PARTITIONS)
164 mSlices = MAX_PARTITIONS;
165 DALI_LOG_WARNING("Value for slices clamped.\n");
167 else if(mSlices < MIN_SLICES)
169 mSlices = MIN_SLICES;
170 DALI_LOG_WARNING("Value for slices clamped.\n");
175 DALI_LOG_ERROR("Invalid type for slices in PrimitiveVisual.\n");
179 Property::Value* stacks = propertyMap.Find(Toolkit::PrimitiveVisual::Property::STACKS, STACKS);
182 if(stacks->Get(mStacks))
185 if(mStacks > MAX_PARTITIONS)
187 mStacks = MAX_PARTITIONS;
188 DALI_LOG_WARNING("Value for stacks clamped.\n");
190 else if(mStacks < MIN_STACKS)
192 mStacks = MIN_STACKS;
193 DALI_LOG_WARNING("Value for stacks clamped.\n");
198 DALI_LOG_ERROR("Invalid type for stacks in PrimitiveVisual.\n");
202 Property::Value* scaleTop = propertyMap.Find(Toolkit::PrimitiveVisual::Property::SCALE_TOP_RADIUS, SCALE_TOP_RADIUS);
203 if(scaleTop && !scaleTop->Get(mScaleTopRadius))
205 DALI_LOG_ERROR("Invalid type for scale top radius in PrimitiveVisual.\n");
208 Property::Value* scaleBottom = propertyMap.Find(Toolkit::PrimitiveVisual::Property::SCALE_BOTTOM_RADIUS, SCALE_BOTTOM_RADIUS);
209 if(scaleBottom && !scaleBottom->Get(mScaleBottomRadius))
211 DALI_LOG_ERROR("Invalid type for scale bottom radius in PrimitiveVisual.\n");
214 Property::Value* scaleHeight = propertyMap.Find(Toolkit::PrimitiveVisual::Property::SCALE_HEIGHT, SCALE_HEIGHT);
215 if(scaleHeight && !scaleHeight->Get(mScaleHeight))
217 DALI_LOG_ERROR("Invalid type for scale height in PrimitiveVisual.\n");
220 Property::Value* scaleRadius = propertyMap.Find(Toolkit::PrimitiveVisual::Property::SCALE_RADIUS, SCALE_RADIUS);
221 if(scaleRadius && !scaleRadius->Get(mScaleRadius))
223 DALI_LOG_ERROR("Invalid type for scale radius in PrimitiveVisual.\n");
226 Property::Value* dimensions = propertyMap.Find(Toolkit::PrimitiveVisual::Property::SCALE_DIMENSIONS, SCALE_DIMENSIONS);
229 if(dimensions->Get(mScaleDimensions))
231 //If any dimension is invalid, set it to a sensible default.
232 if(mScaleDimensions.x <= 0.0)
234 mScaleDimensions.x = 1.0;
235 DALI_LOG_WARNING("Value for scale dimensions clamped. Must be greater than zero.\n");
237 if(mScaleDimensions.y <= 0.0)
239 mScaleDimensions.y = 1.0;
240 DALI_LOG_WARNING("Value for scale dimensions clamped. Must be greater than zero.\n");
242 if(mScaleDimensions.z <= 0.0)
244 mScaleDimensions.z = 1.0;
245 DALI_LOG_WARNING("Value for scale dimensions clamped. Must be greater than zero.\n");
250 DALI_LOG_ERROR("Invalid type for scale dimensions in PrimitiveVisual.\n");
254 Property::Value* bevel = propertyMap.Find(Toolkit::PrimitiveVisual::Property::BEVEL_PERCENTAGE, BEVEL_PERCENTAGE);
257 if(bevel->Get(mBevelPercentage))
260 if(mBevelPercentage < MIN_BEVEL_PERCENTAGE)
262 mBevelPercentage = MIN_BEVEL_PERCENTAGE;
263 DALI_LOG_WARNING("Value for bevel percentage clamped.\n");
265 else if(mBevelPercentage > MAX_BEVEL_PERCENTAGE)
267 mBevelPercentage = MAX_BEVEL_PERCENTAGE;
268 DALI_LOG_WARNING("Value for bevel percentage clamped.\n");
273 DALI_LOG_ERROR("Invalid type for bevel percentage in PrimitiveVisual.\n");
277 Property::Value* smoothness = propertyMap.Find(Toolkit::PrimitiveVisual::Property::BEVEL_SMOOTHNESS, BEVEL_SMOOTHNESS);
280 if(smoothness->Get(mBevelSmoothness))
283 if(mBevelSmoothness < MIN_SMOOTHNESS)
285 mBevelSmoothness = MIN_SMOOTHNESS;
286 DALI_LOG_WARNING("Value for bevel smoothness clamped.\n");
288 else if(mBevelSmoothness > MAX_SMOOTHNESS)
290 mBevelSmoothness = MAX_SMOOTHNESS;
291 DALI_LOG_WARNING("Value for bevel smoothness clamped.\n");
296 DALI_LOG_ERROR("Invalid type for bevel smoothness in PrimitiveVisual.\n");
300 //Read in light position.
301 Property::Value* lightPosition = propertyMap.Find(Toolkit::PrimitiveVisual::Property::LIGHT_POSITION, LIGHT_POSITION_UNIFORM_NAME);
304 if(!lightPosition->Get(mLightPosition))
306 DALI_LOG_ERROR("Invalid value passed for light position in MeshVisual object.\n");
307 mLightPosition = Vector3::ZERO;
312 //Default behaviour is to place the light directly in front of the object,
313 // at a reasonable distance to light everything on screen.
314 Stage stage = Stage::GetCurrent();
316 mLightPosition = Vector3(stage.GetSize().width / 2, stage.GetSize().height / 2, stage.GetSize().width * 5);
320 void PrimitiveVisual::GetNaturalSize(Vector2& naturalSize)
327 naturalSize.x = mObjectDimensions.x;
328 naturalSize.y = mObjectDimensions.y;
331 void PrimitiveVisual::DoSetOnScene(Actor& actor)
333 actor.AddRenderer(mImpl->mRenderer);
335 // Primitive generated and ready to display
336 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
339 void PrimitiveVisual::DoCreatePropertyMap(Property::Map& map) const
342 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::PRIMITIVE);
343 map.Insert(Toolkit::PrimitiveVisual::Property::MIX_COLOR, mImpl->mMixColor);
344 map.Insert(Toolkit::PrimitiveVisual::Property::SHAPE, mPrimitiveType);
345 map.Insert(Toolkit::PrimitiveVisual::Property::SLICES, mSlices);
346 map.Insert(Toolkit::PrimitiveVisual::Property::STACKS, mStacks);
347 map.Insert(Toolkit::PrimitiveVisual::Property::SCALE_TOP_RADIUS, mScaleTopRadius);
348 map.Insert(Toolkit::PrimitiveVisual::Property::SCALE_BOTTOM_RADIUS, mScaleBottomRadius);
349 map.Insert(Toolkit::PrimitiveVisual::Property::SCALE_HEIGHT, mScaleHeight);
350 map.Insert(Toolkit::PrimitiveVisual::Property::SCALE_RADIUS, mScaleRadius);
351 map.Insert(Toolkit::PrimitiveVisual::Property::SCALE_DIMENSIONS, mScaleDimensions);
352 map.Insert(Toolkit::PrimitiveVisual::Property::BEVEL_PERCENTAGE, mBevelPercentage);
353 map.Insert(Toolkit::PrimitiveVisual::Property::BEVEL_SMOOTHNESS, mBevelSmoothness);
354 map.Insert(Toolkit::PrimitiveVisual::Property::LIGHT_POSITION, mLightPosition);
357 void PrimitiveVisual::DoCreateInstancePropertyMap(Property::Map& map) const
362 void PrimitiveVisual::OnSetTransform()
366 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
370 void PrimitiveVisual::OnInitialize()
382 mImpl->mRenderer = VisualRenderer::New(mGeometry, mShader);
383 mImpl->mRenderer.SetProperty(Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK);
384 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(mImpl->mMixColor));
385 mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
388 void PrimitiveVisual::UpdateShaderUniforms()
390 Stage stage = Stage::GetCurrent();
391 float width = stage.GetSize().width;
392 float height = stage.GetSize().height;
394 //Flip model to account for DALi starting with (0, 0) at the top left.
396 scaleMatrix.SetIdentityAndScale(Vector3(1.0, -1.0, 1.0));
398 mShader.RegisterProperty(STAGE_OFFSET_UNIFORM_NAME, Vector2(width, height) / 2.0f);
399 mShader.RegisterProperty(LIGHT_POSITION_UNIFORM_NAME, mLightPosition);
400 mShader.RegisterProperty(OBJECT_MATRIX_UNIFORM_NAME, scaleMatrix);
401 mShader.RegisterProperty(OBJECT_DIMENSIONS_UNIFORM_NAME, mObjectDimensions);
404 void PrimitiveVisual::CreateShader()
406 mShader = Shader::New(SHADER_PRIMITIVE_VISUAL_SHADER_VERT, SHADER_PRIMITIVE_VISUAL_SHADER_FRAG);
407 UpdateShaderUniforms();
410 void PrimitiveVisual::CreateGeometry()
412 Dali::Vector<Vertex> vertices;
413 Dali::Vector<unsigned short> indices;
415 switch(mPrimitiveType)
417 case Toolkit::PrimitiveVisual::Shape::SPHERE:
419 CreateSphere(vertices, indices, mSlices, mStacks);
422 case Toolkit::PrimitiveVisual::Shape::CONE:
424 //Create a conic with zero top radius.
425 CreateConic(vertices, indices, 0, mScaleBottomRadius, mScaleHeight, mSlices);
428 case Toolkit::PrimitiveVisual::Shape::CYLINDER:
430 //Create a conic with equal radii on the top and bottom.
431 CreateConic(vertices, indices, mScaleRadius, mScaleRadius, mScaleHeight, mSlices);
434 case Toolkit::PrimitiveVisual::Shape::CUBE:
436 //Create a cube by creating a bevelled cube with minimum bevel.
437 CreateBevelledCube(vertices, indices, mScaleDimensions, 0.0, 0.0);
440 case Toolkit::PrimitiveVisual::Shape::OCTAHEDRON:
442 //Create an octahedron by creating a bevelled cube with maximum bevel.
443 CreateBevelledCube(vertices, indices, mScaleDimensions, 1.0, mBevelSmoothness);
446 case Toolkit::PrimitiveVisual::Shape::BEVELLED_CUBE:
448 CreateBevelledCube(vertices, indices, mScaleDimensions, mBevelPercentage, mBevelSmoothness);
451 case Toolkit::PrimitiveVisual::Shape::CONICAL_FRUSTUM:
453 CreateConic(vertices, indices, mScaleTopRadius, mScaleBottomRadius, mScaleHeight, mSlices);
458 mGeometry = Geometry::New();
461 Property::Map vertexFormat;
462 vertexFormat[POSITION] = Property::VECTOR3;
463 vertexFormat[NORMAL] = Property::VECTOR3;
464 VertexBuffer surfaceVertices = VertexBuffer::New(vertexFormat);
465 surfaceVertices.SetData(&vertices[0], vertices.Size());
467 mGeometry.AddVertexBuffer(surfaceVertices);
469 //Indices for triangle formulation
470 mGeometry.SetIndexBuffer(&indices[0], indices.Size());
473 void PrimitiveVisual::CreateSphere(Vector<Vertex>& vertices, Vector<unsigned short>& indices, int slices, int stacks)
475 ComputeSphereVertices(vertices, slices, stacks);
476 FormSphereTriangles(indices, slices, stacks);
478 mObjectDimensions = Vector3::ONE;
481 void PrimitiveVisual::CreateConic(Vector<Vertex>& vertices, Vector<unsigned short>& indices, float scaleTopRadius, float scaleBottomRadius, float scaleHeight, int slices)
483 ComputeConicVertices(vertices, scaleTopRadius, scaleBottomRadius, scaleHeight, slices);
484 FormConicTriangles(indices, scaleTopRadius, scaleBottomRadius, slices);
486 //Determine object dimensions, and scale them to be between 0.0 and 1.0.
487 float xDimension = std::max(scaleTopRadius, scaleBottomRadius) * 2.0f;
488 float yDimension = scaleHeight;
489 float largestDimension = std::max(xDimension, yDimension);
491 mObjectDimensions = Vector3(xDimension / largestDimension, yDimension / largestDimension, xDimension / largestDimension);
494 void PrimitiveVisual::CreateBevelledCube(Vector<Vertex>& vertices, Vector<unsigned short>& indices, Vector3 dimensions, float bevelPercentage, float bevelSmoothness)
496 float maxDimension = std::max(std::max(dimensions.x, dimensions.y), dimensions.z);
497 dimensions = dimensions / maxDimension;
499 if(bevelPercentage <= MIN_BEVEL_PERCENTAGE) //No bevel, form a cube.
501 ComputeCubeVertices(vertices, dimensions);
502 FormCubeTriangles(indices);
504 else if(bevelPercentage >= MAX_BEVEL_PERCENTAGE) //Max bevel, form an octahedron.
506 ComputeOctahedronVertices(vertices, dimensions, bevelSmoothness);
507 FormOctahedronTriangles(indices);
509 else //In between, form a bevelled cube.
511 ComputeBevelledCubeVertices(vertices, dimensions, bevelPercentage, bevelSmoothness);
512 FormBevelledCubeTriangles(indices);
515 mObjectDimensions = dimensions;
518 void PrimitiveVisual::ComputeCircleTables(Vector<float>& sinTable, Vector<float>& cosTable, int divisions, bool halfCircle)
525 const float angleDivision = (halfCircle ? 1.0f : 2.0f) * Dali::Math::PI / (float)divisions;
527 sinTable.Resize(divisions);
528 cosTable.Resize(divisions);
530 for(int i = 0; i < divisions; i++)
532 sinTable[i] = sin(angleDivision * i);
533 cosTable[i] = cos(angleDivision * i);
537 void PrimitiveVisual::ComputeSphereVertices(Vector<Vertex>& vertices, int slices, int stacks)
539 //Tables for calculating slices angles and stacks angles, respectively.
540 Vector<float> sinTable1;
541 Vector<float> cosTable1;
542 Vector<float> sinTable2;
543 Vector<float> cosTable2;
545 ComputeCircleTables(sinTable1, cosTable1, slices, false);
546 ComputeCircleTables(sinTable2, cosTable2, stacks, true);
548 int numVertices = slices * (stacks - 1) + 2;
549 vertices.Resize(numVertices);
551 int vertexIndex = 0; //Track progress through vertices.
557 vertices[vertexIndex].position = Vector3(0.0, 0.5, 0.0);
558 vertices[vertexIndex].normal = Vector3(0.0, 1.0, 0.0);
562 for(int i = 1; i < stacks; i++)
564 for(int j = 0; j < slices; j++, vertexIndex++)
566 x = cosTable1[j] * sinTable2[i];
568 z = sinTable1[j] * sinTable2[i];
570 vertices[vertexIndex].position = Vector3(x / 2.0f, y / 2.0f, z / 2.0f);
571 vertices[vertexIndex].normal = Vector3(x, y, z);
576 vertices[vertexIndex].position = Vector3(0.0, -0.5, 0.0);
577 vertices[vertexIndex].normal = Vector3(0.0, -1.0, 0.0);
580 void PrimitiveVisual::FormSphereTriangles(Vector<unsigned short>& indices, int slices, int stacks)
584 //Set indices to placeholder "error" values.
585 //This will display nothing, which is the expected behaviour for this edge case.
590 int numTriangles = 2 * slices * (stacks - 1);
592 indices.Resize(3 * numTriangles);
594 int indiceIndex = 0; //Used to keep track of progress through indices.
595 int previousCycleBeginning = 1; //Stores the index of the vertex that started the cycle of the previous stack.
596 int currentCycleBeginning = 1 + slices;
598 //Top stack. Loop from index 1 to index slices, as not counting the very first vertex.
599 for(int i = 1; i <= slices; i++, indiceIndex += 3)
601 indices[indiceIndex] = 0;
604 //End, so loop around.
605 indices[indiceIndex + 1] = 1;
609 indices[indiceIndex + 1] = i + 1;
611 indices[indiceIndex + 2] = i;
614 //Middle Stacks. Want to form triangles between the top and bottom stacks, so loop up to the number of stacks - 2.
615 for(int i = 0; i < stacks - 2; i++, previousCycleBeginning += slices, currentCycleBeginning += slices)
617 for(int j = 0; j < slices; j++, indiceIndex += 6)
621 //End, so loop around.
622 indices[indiceIndex] = previousCycleBeginning + j;
623 indices[indiceIndex + 1] = previousCycleBeginning;
624 indices[indiceIndex + 2] = currentCycleBeginning + j;
625 indices[indiceIndex + 3] = currentCycleBeginning + j;
626 indices[indiceIndex + 4] = previousCycleBeginning;
627 indices[indiceIndex + 5] = currentCycleBeginning;
631 indices[indiceIndex] = previousCycleBeginning + j;
632 indices[indiceIndex + 1] = previousCycleBeginning + 1 + j;
633 indices[indiceIndex + 2] = currentCycleBeginning + j;
634 indices[indiceIndex + 3] = currentCycleBeginning + j;
635 indices[indiceIndex + 4] = previousCycleBeginning + 1 + j;
636 indices[indiceIndex + 5] = currentCycleBeginning + 1 + j;
641 //Bottom stack. Loop around the last stack from the previous loop, and go up to the penultimate vertex.
642 for(int i = 0; i < slices; i++, indiceIndex += 3)
644 indices[indiceIndex] = previousCycleBeginning + slices;
645 indices[indiceIndex + 1] = previousCycleBeginning + i;
648 //End, so loop around.
649 indices[indiceIndex + 2] = previousCycleBeginning;
653 indices[indiceIndex + 2] = previousCycleBeginning + i + 1;
658 void PrimitiveVisual::ComputeConicVertices(Vector<Vertex>& vertices, float scaleTopRadius, float scaleBottomRadius, float scaleHeight, int slices)
660 int vertexIndex = 0; //Track progress through vertices.
661 Vector<float> sinTable;
662 Vector<float> cosTable;
664 ComputeCircleTables(sinTable, cosTable, slices, false);
666 int numVertices = 2; //Always will have one at the top and one at the bottom.
668 //Add vertices for each circle. Need two per point for different face normals.
669 if(scaleTopRadius > 0.0)
671 numVertices += 2 * slices;
673 if(scaleBottomRadius > 0.0)
675 numVertices += 2 * slices;
678 vertices.Resize(numVertices);
680 //Scale to bounding region of -0.5 to 0.5 (i.e range of 1).
681 float biggestObjectDimension = std::max(std::max(scaleTopRadius * 2.0f, scaleBottomRadius * 2.0f), scaleHeight);
682 scaleTopRadius = scaleTopRadius / biggestObjectDimension;
683 scaleBottomRadius = scaleBottomRadius / biggestObjectDimension;
685 //Dimensions for vertex coordinates. Y is constant, and so can be initialised now.
687 float y = scaleHeight / biggestObjectDimension / 2.0f;
691 vertices[0].position = Vector3(0, y, 0);
692 vertices[0].normal = Vector3(0, 1, 0);
696 if(scaleTopRadius > 0.0)
698 //Loop around the circle.
699 for(int i = 0; i < slices; i++, vertexIndex++)
701 x = sinTable[i] * scaleTopRadius;
702 z = cosTable[i] * scaleTopRadius;
704 //Upward-facing normal.
705 vertices[vertexIndex].position = Vector3(x, y, z);
706 vertices[vertexIndex].normal = Vector3(0, 1, 0);
708 //Outward-facing normal.
709 vertices[vertexIndex + slices].position = Vector3(x, y, z);
710 vertices[vertexIndex + slices].normal = Vector3(x, 0, z);
713 vertexIndex += slices;
717 if(scaleBottomRadius > 0.0)
719 //Loop around the circle.
720 for(int i = 0; i < slices; i++, vertexIndex++)
722 x = sinTable[i] * scaleBottomRadius;
723 z = cosTable[i] * scaleBottomRadius;
725 //Outward-facing normal.
726 vertices[vertexIndex].position = Vector3(x, -y, z);
727 vertices[vertexIndex].normal = Vector3(x, 0, z);
729 //Downward-facing normal.
730 vertices[vertexIndex + slices].position = Vector3(x, -y, z);
731 vertices[vertexIndex + slices].normal = Vector3(0, -1, 0);
734 vertexIndex += slices;
738 vertices[vertexIndex].position = Vector3(0, -y, 0);
739 vertices[vertexIndex].normal = Vector3(0, -1, 0);
743 void PrimitiveVisual::FormConicTriangles(Vector<unsigned short>& indices, float scaleTopRadius, float scaleBottomRadius, int slices)
745 int indiceIndex = 0; //Track progress through indices.
746 int numTriangles = 0;
747 bool coneTop = scaleTopRadius <= 0.0;
748 bool coneBottom = scaleBottomRadius <= 0.0;
750 if(coneTop && coneBottom)
752 //Set indices to placeholder "error" values.
753 //This will display nothing, which is the expected behaviour for this edge case.
760 numTriangles += 2 * slices;
764 numTriangles += 2 * slices;
767 indices.Resize(3 * numTriangles);
769 //Switch on the type of conic we have.
770 if(!coneTop && !coneBottom)
772 //Top circle. Start at index of first outer point and go around.
773 for(int i = 1; i <= slices; i++, indiceIndex += 3)
775 indices[indiceIndex] = 0;
776 indices[indiceIndex + 1] = i;
779 //End, so loop around.
780 indices[indiceIndex + 2] = 1;
784 indices[indiceIndex + 2] = i + 1;
788 int topCycleBeginning = slices + 1;
789 int bottomCycleBeginning = topCycleBeginning + slices;
792 for(int i = 0; i < slices; i++, indiceIndex += 6)
796 //End, so loop around.
797 indices[indiceIndex] = topCycleBeginning + i;
798 indices[indiceIndex + 1] = bottomCycleBeginning + i;
799 indices[indiceIndex + 2] = topCycleBeginning;
800 indices[indiceIndex + 3] = bottomCycleBeginning + i;
801 indices[indiceIndex + 4] = bottomCycleBeginning;
802 indices[indiceIndex + 5] = topCycleBeginning;
806 indices[indiceIndex] = topCycleBeginning + i;
807 indices[indiceIndex + 1] = bottomCycleBeginning + i;
808 indices[indiceIndex + 2] = topCycleBeginning + 1 + i;
809 indices[indiceIndex + 3] = bottomCycleBeginning + i;
810 indices[indiceIndex + 4] = bottomCycleBeginning + 1 + i;
811 indices[indiceIndex + 5] = topCycleBeginning + 1 + i;
815 int bottomFaceCycleBeginning = bottomCycleBeginning + slices;
818 for(int i = 0; i < slices; i++, indiceIndex += 3)
820 indices[indiceIndex] = bottomFaceCycleBeginning;
823 //End, so loop around.
824 indices[indiceIndex + 1] = bottomFaceCycleBeginning;
828 indices[indiceIndex + 1] = bottomFaceCycleBeginning + i + 1;
830 indices[indiceIndex + 2] = bottomFaceCycleBeginning + i;
833 else if(!coneTop || !coneBottom)
835 //Top circle/edges. Start at index of first outer point and go around.
836 for(int i = 1; i <= slices; i++, indiceIndex += 3)
838 indices[indiceIndex] = 0;
839 indices[indiceIndex + 1] = i;
842 //End, so loop around.
843 indices[indiceIndex + 2] = 1;
847 indices[indiceIndex + 2] = i + 1;
851 //Bottom circle/edges. Start at index of first outer point and go around.
852 for(int i = 1; i <= slices; i++, indiceIndex += 3)
854 indices[indiceIndex] = 2 * slices + 1;
857 //End, so loop around.
858 indices[indiceIndex + 1] = slices + 1;
862 indices[indiceIndex + 1] = slices + i + 1;
864 indices[indiceIndex + 2] = slices + i;
869 void PrimitiveVisual::ComputeCubeVertices(Vector<Vertex>& vertices, Vector3 dimensions)
871 int numVertices = 4 * 6; //Four per face.
872 int vertexIndex = 0; //Tracks progress through vertices.
873 float scaledX = 0.5 * dimensions.x;
874 float scaledY = 0.5 * dimensions.y;
875 float scaledZ = 0.5 * dimensions.z;
877 vertices.Resize(numVertices);
879 Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
881 Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
884 positions[0] = Vector3(-scaledX, scaledY, -scaledZ);
885 positions[1] = Vector3(scaledX, scaledY, -scaledZ);
886 positions[2] = Vector3(scaledX, scaledY, scaledZ);
887 positions[3] = Vector3(-scaledX, scaledY, scaledZ);
888 positions[4] = Vector3(-scaledX, -scaledY, -scaledZ);
889 positions[5] = Vector3(scaledX, -scaledY, -scaledZ);
890 positions[6] = Vector3(scaledX, -scaledY, scaledZ);
891 positions[7] = Vector3(-scaledX, -scaledY, scaledZ);
893 normals[0] = Vector3(0, 1, 0);
894 normals[1] = Vector3(0, 0, -1);
895 normals[2] = Vector3(1, 0, 0);
896 normals[3] = Vector3(0, 0, 1);
897 normals[4] = Vector3(-1, 0, 0);
898 normals[5] = Vector3(0, -1, 0);
900 //Top face, upward normals.
901 for(int i = 0; i < 4; i++, vertexIndex++)
903 vertices[vertexIndex].position = positions[i];
904 vertices[vertexIndex].normal = normals[0];
907 //Top face, outward normals.
908 for(int i = 0; i < 4; i++, vertexIndex += 2)
910 vertices[vertexIndex].position = positions[i];
911 vertices[vertexIndex].normal = normals[i + 1];
915 //End, so loop around.
916 vertices[vertexIndex + 1].position = positions[0];
920 vertices[vertexIndex + 1].position = positions[i + 1];
922 vertices[vertexIndex + 1].normal = normals[i + 1];
925 //Bottom face, outward normals.
926 for(int i = 0; i < 4; i++, vertexIndex += 2)
928 vertices[vertexIndex].position = positions[i + 4];
929 vertices[vertexIndex].normal = normals[i + 1];
933 //End, so loop around.
934 vertices[vertexIndex + 1].position = positions[4];
938 vertices[vertexIndex + 1].position = positions[i + 5];
940 vertices[vertexIndex + 1].normal = normals[i + 1];
943 //Bottom face, downward normals.
944 for(int i = 0; i < 4; i++, vertexIndex++)
946 vertices[vertexIndex].position = positions[i + 4];
947 vertices[vertexIndex].normal = normals[5];
951 void PrimitiveVisual::FormCubeTriangles(Vector<unsigned short>& indices)
953 int numTriangles = 12;
954 int triangleIndex = 0; //Track progress through indices.
956 indices.Resize(3 * numTriangles);
959 indices[triangleIndex] = 0;
960 indices[triangleIndex + 1] = 2;
961 indices[triangleIndex + 2] = 1;
962 indices[triangleIndex + 3] = 2;
963 indices[triangleIndex + 4] = 0;
964 indices[triangleIndex + 5] = 3;
967 int topFaceStart = 4;
968 int bottomFaceStart = 12;
971 for(int i = 0; i < 8; i += 2, triangleIndex += 6)
973 indices[triangleIndex] = i + topFaceStart;
974 indices[triangleIndex + 1] = i + topFaceStart + 1;
975 indices[triangleIndex + 2] = i + bottomFaceStart + 1;
976 indices[triangleIndex + 3] = i + topFaceStart;
977 indices[triangleIndex + 4] = i + bottomFaceStart + 1;
978 indices[triangleIndex + 5] = i + bottomFaceStart;
982 indices[triangleIndex] = 20;
983 indices[triangleIndex + 1] = 21;
984 indices[triangleIndex + 2] = 22;
985 indices[triangleIndex + 3] = 22;
986 indices[triangleIndex + 4] = 23;
987 indices[triangleIndex + 5] = 20;
990 void PrimitiveVisual::ComputeOctahedronVertices(Vector<Vertex>& vertices, Vector3 dimensions, float smoothness)
992 int numVertices = 3 * 8; //Three per face
993 int vertexIndex = 0; //Tracks progress through vertices.
994 float scaledX = 0.5 * dimensions.x;
995 float scaledY = 0.5 * dimensions.y;
996 float scaledZ = 0.5 * dimensions.z;
998 vertices.Resize(numVertices);
1000 Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
1001 positions.Resize(6);
1002 Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
1004 Vector<Vector3> outerNormals; //Holds normals that point outwards at each vertex.
1005 outerNormals.Resize(6);
1007 positions[0] = Vector3(0.0, scaledY, 0.0);
1008 positions[1] = Vector3(-scaledX, 0.0, 0.0);
1009 positions[2] = Vector3(0.0, 0.0, -scaledZ);
1010 positions[3] = Vector3(scaledX, 0.0, 0.0);
1011 positions[4] = Vector3(0.0, 0.0, scaledZ);
1012 positions[5] = Vector3(0.0, -scaledY, 0.0);
1014 normals[0] = Vector3(-1, 1, -1);
1015 normals[1] = Vector3(1, 1, -1);
1016 normals[2] = Vector3(1, 1, 1);
1017 normals[3] = Vector3(-1, 1, 1);
1018 normals[4] = Vector3(-1, -1, -1);
1019 normals[5] = Vector3(1, -1, -1);
1020 normals[6] = Vector3(1, -1, 1);
1021 normals[7] = Vector3(-1, -1, 1);
1023 outerNormals[0] = Vector3(0, 1, 0);
1024 outerNormals[1] = Vector3(-1, 0, 0);
1025 outerNormals[2] = Vector3(0, 0, -1);
1026 outerNormals[3] = Vector3(1, 0, 0);
1027 outerNormals[4] = Vector3(0, 0, 1);
1028 outerNormals[5] = Vector3(0, -1, 0);
1030 //Loop through top faces.
1031 for(int i = 0; i < 4; i++, vertexIndex += 3)
1035 //End, so loop around.
1036 vertices[vertexIndex].position = positions[0];
1037 vertices[vertexIndex].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1038 vertices[vertexIndex + 1].position = positions[1];
1039 vertices[vertexIndex + 1].normal = outerNormals[1] * smoothness + normals[i] * (1 - smoothness);
1040 vertices[vertexIndex + 2].position = positions[i + 1];
1041 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1045 vertices[vertexIndex].position = positions[0];
1046 vertices[vertexIndex].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1047 vertices[vertexIndex + 1].position = positions[i + 2];
1048 vertices[vertexIndex + 1].normal = outerNormals[i + 2] * smoothness + normals[i] * (1 - smoothness);
1049 vertices[vertexIndex + 2].position = positions[i + 1];
1050 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1054 //Loop through bottom faces.
1055 for(int i = 0; i < 4; i++, vertexIndex += 3)
1059 //End, so loop around.
1060 vertices[vertexIndex].position = positions[5];
1061 vertices[vertexIndex].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1062 vertices[vertexIndex + 1].position = positions[i + 1];
1063 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1064 vertices[vertexIndex + 2].position = positions[1];
1065 vertices[vertexIndex + 2].normal = outerNormals[1] * smoothness + normals[i + 4] * (1 - smoothness);
1069 vertices[vertexIndex].position = positions[5];
1070 vertices[vertexIndex].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1071 vertices[vertexIndex + 1].position = positions[i + 1];
1072 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1073 vertices[vertexIndex + 2].position = positions[i + 2];
1074 vertices[vertexIndex + 2].normal = outerNormals[i + 2] * smoothness + normals[i + 4] * (1 - smoothness);
1079 void PrimitiveVisual::FormOctahedronTriangles(Vector<unsigned short>& indices)
1081 int numTriangles = 8;
1082 int numIndices = numTriangles * 3;
1084 indices.Resize(numIndices);
1086 for(unsigned short i = 0; i < numIndices; i++)
1092 void PrimitiveVisual::ComputeBevelledCubeVertices(Vector<Vertex>& vertices, Vector3 dimensions, float bevelPercentage, float bevelSmoothness)
1094 int numPositions = 24;
1096 int numOuterFaces = 6;
1097 int numVertices = 6 * 4 + 12 * 4 + 8 * 3; //Six outer faces, 12 slanting rectangles, 8 slanting triangles.
1098 int vertexIndex = 0; //Track progress through vertices.
1099 int normalIndex = 0; //Track progress through normals, as vertices are calculated per face.
1101 float minDimension = std::min(std::min(dimensions.x, dimensions.y), dimensions.z);
1102 float bevelAmount = 0.5 * std::min(bevelPercentage, minDimension); //Cap bevel amount if necessary.
1104 //Distances from centre to outer edge points.
1105 float outerX = 0.5 * dimensions.x;
1106 float outerY = 0.5 * dimensions.y;
1107 float outerZ = 0.5 * dimensions.z;
1109 //Distances from centre to bevelled points.
1110 float bevelX = outerX - bevelAmount;
1111 float bevelY = outerY - bevelAmount;
1112 float bevelZ = outerZ - bevelAmount;
1114 Vector<Vector3> positions; //Holds object points, to be shared between vertexes.
1115 positions.Resize(numPositions);
1116 Vector<Vector3> normals; //Holds face normals, to be shared between vertexes.
1117 normals.Resize(numFaces);
1118 Vector<Vector3> outerNormals; //Holds normals of the outermost faces specifically.
1119 outerNormals.Resize(numOuterFaces);
1120 vertices.Resize(numVertices);
1122 //Topmost face positions.
1123 positions[0] = Vector3(-bevelX, outerY, -bevelZ);
1124 positions[1] = Vector3(bevelX, outerY, -bevelZ);
1125 positions[2] = Vector3(bevelX, outerY, bevelZ);
1126 positions[3] = Vector3(-bevelX, outerY, bevelZ);
1128 //Second layer positions.
1129 positions[4] = Vector3(-outerX, bevelY, -bevelZ);
1130 positions[5] = Vector3(-bevelX, bevelY, -outerZ);
1131 positions[6] = Vector3(bevelX, bevelY, -outerZ);
1132 positions[7] = Vector3(outerX, bevelY, -bevelZ);
1133 positions[8] = Vector3(outerX, bevelY, bevelZ);
1134 positions[9] = Vector3(bevelX, bevelY, outerZ);
1135 positions[10] = Vector3(-bevelX, bevelY, outerZ);
1136 positions[11] = Vector3(-outerX, bevelY, bevelZ);
1138 //Third layer positions.
1139 positions[12] = Vector3(-outerX, -bevelY, -bevelZ);
1140 positions[13] = Vector3(-bevelX, -bevelY, -outerZ);
1141 positions[14] = Vector3(bevelX, -bevelY, -outerZ);
1142 positions[15] = Vector3(outerX, -bevelY, -bevelZ);
1143 positions[16] = Vector3(outerX, -bevelY, bevelZ);
1144 positions[17] = Vector3(bevelX, -bevelY, outerZ);
1145 positions[18] = Vector3(-bevelX, -bevelY, outerZ);
1146 positions[19] = Vector3(-outerX, -bevelY, bevelZ);
1148 //Bottom-most face positions.
1149 positions[20] = Vector3(-bevelX, -outerY, -bevelZ);
1150 positions[21] = Vector3(bevelX, -outerY, -bevelZ);
1151 positions[22] = Vector3(bevelX, -outerY, bevelZ);
1152 positions[23] = Vector3(-bevelX, -outerY, bevelZ);
1155 normals[0] = Vector3(0, 1, 0);
1157 //Top slope normals.
1158 normals[1] = Vector3(-1, 1, -1);
1159 normals[2] = Vector3(0, 1, -1);
1160 normals[3] = Vector3(1, 1, -1);
1161 normals[4] = Vector3(1, 1, 0);
1162 normals[5] = Vector3(1, 1, 1);
1163 normals[6] = Vector3(0, 1, 1);
1164 normals[7] = Vector3(-1, 1, 1);
1165 normals[8] = Vector3(-1, 1, 0);
1168 normals[9] = Vector3(-1, 0, -1);
1169 normals[10] = Vector3(0, 0, -1);
1170 normals[11] = Vector3(1, 0, -1);
1171 normals[12] = Vector3(1, 0, 0);
1172 normals[13] = Vector3(1, 0, 1);
1173 normals[14] = Vector3(0, 0, 1);
1174 normals[15] = Vector3(-1, 0, 1);
1175 normals[16] = Vector3(-1, 0, 0);
1177 //Bottom slope normals.
1178 normals[17] = Vector3(-1, -1, -1);
1179 normals[18] = Vector3(0, -1, -1);
1180 normals[19] = Vector3(1, -1, -1);
1181 normals[20] = Vector3(1, -1, 0);
1182 normals[21] = Vector3(1, -1, 1);
1183 normals[22] = Vector3(0, -1, 1);
1184 normals[23] = Vector3(-1, -1, 1);
1185 normals[24] = Vector3(-1, -1, 0);
1187 //Bottom face normal.
1188 normals[25] = Vector3(0, -1, 0);
1190 //Top, back, right, front, left and bottom faces, respectively.
1191 outerNormals[0] = Vector3(0, 1, 0);
1192 outerNormals[1] = Vector3(0, 0, -1);
1193 outerNormals[2] = Vector3(1, 0, 0);
1194 outerNormals[3] = Vector3(0, 0, 1);
1195 outerNormals[4] = Vector3(-1, 0, 0);
1196 outerNormals[5] = Vector3(0, -1, 0);
1198 //Topmost face vertices.
1199 for(int i = 0; i < 4; i++, vertexIndex++)
1201 vertices[vertexIndex].position = positions[i];
1202 vertices[vertexIndex].normal = normals[normalIndex];
1207 //Top slope vertices.
1208 for(int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2)
1211 vertices[vertexIndex].position = positions[i];
1212 vertices[vertexIndex].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1213 vertices[vertexIndex + 1].position = positions[2 * i + 4];
1214 vertices[vertexIndex + 1].normal = outerNormals[(i == 0) ? 4 : i] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1215 vertices[vertexIndex + 2].position = positions[2 * i + 5];
1216 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1221 //End, so loop around.
1222 vertices[vertexIndex + 3].position = positions[i];
1223 vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1224 vertices[vertexIndex + 4].position = positions[0];
1225 vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1226 vertices[vertexIndex + 5].position = positions[2 * i + 5];
1227 vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1228 vertices[vertexIndex + 6].position = positions[4];
1229 vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1233 vertices[vertexIndex + 3].position = positions[i];
1234 vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1235 vertices[vertexIndex + 4].position = positions[i + 1];
1236 vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1237 vertices[vertexIndex + 5].position = positions[2 * i + 5];
1238 vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1239 vertices[vertexIndex + 6].position = positions[2 * i + 6];
1240 vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1244 int secondCycleBeginning = 4;
1245 int thirdCycleBeginning = secondCycleBeginning + 8;
1246 int bottomCycleBeginning = thirdCycleBeginning + 8;
1249 for(int i = 0; i < 8; i++, vertexIndex += 4, normalIndex++)
1253 //End, so loop around.
1254 vertices[vertexIndex].position = positions[secondCycleBeginning + i];
1255 vertices[vertexIndex].normal = normals[normalIndex];
1256 vertices[vertexIndex + 1].position = positions[secondCycleBeginning];
1257 vertices[vertexIndex + 1].normal = normals[normalIndex];
1258 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1259 vertices[vertexIndex + 2].normal = normals[normalIndex];
1260 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning];
1261 vertices[vertexIndex + 3].normal = normals[normalIndex];
1263 else if((i % 2) == 0)
1265 //'even' faces are corner ones, and need smoothing.
1266 vertices[vertexIndex].position = positions[secondCycleBeginning + i];
1267 vertices[vertexIndex].normal = outerNormals[(i == 0) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1268 vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1269 vertices[vertexIndex + 1].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1270 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1271 vertices[vertexIndex + 2].normal = outerNormals[(i == 0) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1272 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1273 vertices[vertexIndex + 3].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1277 //'odd' faces are outer ones, and so don't need smoothing.
1278 vertices[vertexIndex].position = positions[secondCycleBeginning + i];
1279 vertices[vertexIndex].normal = normals[normalIndex];
1280 vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1281 vertices[vertexIndex + 1].normal = normals[normalIndex];
1282 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1283 vertices[vertexIndex + 2].normal = normals[normalIndex];
1284 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1285 vertices[vertexIndex + 3].normal = normals[normalIndex];
1289 //Bottom slope vertices.
1290 for(int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2)
1293 vertices[vertexIndex].position = positions[thirdCycleBeginning + 2 * i];
1294 vertices[vertexIndex].normal = outerNormals[(i == 0) ? 4 : i] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1295 vertices[vertexIndex + 1].position = positions[thirdCycleBeginning + 2 * i + 1];
1296 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1297 vertices[vertexIndex + 2].position = positions[bottomCycleBeginning + i];
1298 vertices[vertexIndex + 2].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1303 //End, so loop around.
1304 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1305 vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1306 vertices[vertexIndex + 4].position = positions[thirdCycleBeginning];
1307 vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1308 vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1309 vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1310 vertices[vertexIndex + 6].position = positions[bottomCycleBeginning];
1311 vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1315 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1316 vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1317 vertices[vertexIndex + 4].position = positions[thirdCycleBeginning + 2 * i + 2];
1318 vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1319 vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1320 vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1321 vertices[vertexIndex + 6].position = positions[bottomCycleBeginning + i + 1];
1322 vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1326 //Bottom-most face vertices.
1327 for(int i = 0; i < 4; i++, vertexIndex++)
1329 vertices[vertexIndex].position = positions[bottomCycleBeginning + i];
1330 vertices[vertexIndex].normal = normals[normalIndex];
1336 void PrimitiveVisual::FormBevelledCubeTriangles(Vector<unsigned short>& indices)
1338 int numTriangles = 44; //(Going from top to bottom, that's 2 + 12 + 16 + 12 + 2)
1339 int indiceIndex = 0; //Track progress through indices.
1340 int vertexIndex = 0; //Track progress through vertices as they're processed.
1342 indices.Resize(3 * numTriangles);
1345 indices[indiceIndex] = vertexIndex;
1346 indices[indiceIndex + 1] = vertexIndex + 2;
1347 indices[indiceIndex + 2] = vertexIndex + 1;
1348 indices[indiceIndex + 3] = vertexIndex + 0;
1349 indices[indiceIndex + 4] = vertexIndex + 3;
1350 indices[indiceIndex + 5] = vertexIndex + 2;
1355 for(int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7)
1358 indices[indiceIndex] = vertexIndex;
1359 indices[indiceIndex + 1] = vertexIndex + 2;
1360 indices[indiceIndex + 2] = vertexIndex + 1;
1363 indices[indiceIndex + 3] = vertexIndex + 3;
1364 indices[indiceIndex + 4] = vertexIndex + 4;
1365 indices[indiceIndex + 5] = vertexIndex + 5;
1366 indices[indiceIndex + 6] = vertexIndex + 4;
1367 indices[indiceIndex + 7] = vertexIndex + 6;
1368 indices[indiceIndex + 8] = vertexIndex + 5;
1372 for(int i = 0; i < 8; i++, indiceIndex += 6, vertexIndex += 4)
1374 indices[indiceIndex] = vertexIndex;
1375 indices[indiceIndex + 1] = vertexIndex + 1;
1376 indices[indiceIndex + 2] = vertexIndex + 2;
1377 indices[indiceIndex + 3] = vertexIndex + 1;
1378 indices[indiceIndex + 4] = vertexIndex + 3;
1379 indices[indiceIndex + 5] = vertexIndex + 2;
1383 for(int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7)
1386 indices[indiceIndex] = vertexIndex;
1387 indices[indiceIndex + 1] = vertexIndex + 1;
1388 indices[indiceIndex + 2] = vertexIndex + 2;
1391 indices[indiceIndex + 3] = vertexIndex + 3;
1392 indices[indiceIndex + 4] = vertexIndex + 4;
1393 indices[indiceIndex + 5] = vertexIndex + 5;
1394 indices[indiceIndex + 6] = vertexIndex + 4;
1395 indices[indiceIndex + 7] = vertexIndex + 6;
1396 indices[indiceIndex + 8] = vertexIndex + 5;
1400 indices[indiceIndex] = vertexIndex;
1401 indices[indiceIndex + 1] = vertexIndex + 1;
1402 indices[indiceIndex + 2] = vertexIndex + 2;
1403 indices[indiceIndex + 3] = vertexIndex + 0;
1404 indices[indiceIndex + 4] = vertexIndex + 2;
1405 indices[indiceIndex + 5] = vertexIndex + 3;
1410 } // namespace Internal
1412 } // namespace Toolkit