2 * Copyright (c) 2021 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.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
370 void PrimitiveVisual::OnInitialize()
382 mImpl->mRenderer = Renderer::New(mGeometry, mShader);
383 mImpl->mRenderer.SetProperty(Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK);
385 // Register transform properties
386 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
388 mImpl->mMixColorIndex = mImpl->mRenderer.RegisterProperty(Toolkit::PrimitiveVisual::Property::MIX_COLOR, MIX_COLOR, Vector3(mImpl->mMixColor));
391 void PrimitiveVisual::UpdateShaderUniforms()
393 Stage stage = Stage::GetCurrent();
394 float width = stage.GetSize().width;
395 float height = stage.GetSize().height;
397 //Flip model to account for DALi starting with (0, 0) at the top left.
399 scaleMatrix.SetIdentityAndScale(Vector3(1.0, -1.0, 1.0));
401 mShader.RegisterProperty(STAGE_OFFSET_UNIFORM_NAME, Vector2(width, height) / 2.0f);
402 mShader.RegisterProperty(LIGHT_POSITION_UNIFORM_NAME, mLightPosition);
403 mShader.RegisterProperty(OBJECT_MATRIX_UNIFORM_NAME, scaleMatrix);
404 mShader.RegisterProperty(OBJECT_DIMENSIONS_UNIFORM_NAME, mObjectDimensions);
407 void PrimitiveVisual::CreateShader()
409 mShader = Shader::New(SHADER_PRIMITIVE_VISUAL_SHADER_VERT, SHADER_PRIMITIVE_VISUAL_SHADER_FRAG);
410 UpdateShaderUniforms();
413 void PrimitiveVisual::CreateGeometry()
415 Dali::Vector<Vertex> vertices;
416 Dali::Vector<unsigned short> indices;
418 switch(mPrimitiveType)
420 case Toolkit::PrimitiveVisual::Shape::SPHERE:
422 CreateSphere(vertices, indices, mSlices, mStacks);
425 case Toolkit::PrimitiveVisual::Shape::CONE:
427 //Create a conic with zero top radius.
428 CreateConic(vertices, indices, 0, mScaleBottomRadius, mScaleHeight, mSlices);
431 case Toolkit::PrimitiveVisual::Shape::CYLINDER:
433 //Create a conic with equal radii on the top and bottom.
434 CreateConic(vertices, indices, mScaleRadius, mScaleRadius, mScaleHeight, mSlices);
437 case Toolkit::PrimitiveVisual::Shape::CUBE:
439 //Create a cube by creating a bevelled cube with minimum bevel.
440 CreateBevelledCube(vertices, indices, mScaleDimensions, 0.0, 0.0);
443 case Toolkit::PrimitiveVisual::Shape::OCTAHEDRON:
445 //Create an octahedron by creating a bevelled cube with maximum bevel.
446 CreateBevelledCube(vertices, indices, mScaleDimensions, 1.0, mBevelSmoothness);
449 case Toolkit::PrimitiveVisual::Shape::BEVELLED_CUBE:
451 CreateBevelledCube(vertices, indices, mScaleDimensions, mBevelPercentage, mBevelSmoothness);
454 case Toolkit::PrimitiveVisual::Shape::CONICAL_FRUSTUM:
456 CreateConic(vertices, indices, mScaleTopRadius, mScaleBottomRadius, mScaleHeight, mSlices);
461 mGeometry = Geometry::New();
464 Property::Map vertexFormat;
465 vertexFormat[POSITION] = Property::VECTOR3;
466 vertexFormat[NORMAL] = Property::VECTOR3;
467 VertexBuffer surfaceVertices = VertexBuffer::New(vertexFormat);
468 surfaceVertices.SetData(&vertices[0], vertices.Size());
470 mGeometry.AddVertexBuffer(surfaceVertices);
472 //Indices for triangle formulation
473 mGeometry.SetIndexBuffer(&indices[0], indices.Size());
476 void PrimitiveVisual::CreateSphere(Vector<Vertex>& vertices, Vector<unsigned short>& indices, int slices, int stacks)
478 ComputeSphereVertices(vertices, slices, stacks);
479 FormSphereTriangles(indices, slices, stacks);
481 mObjectDimensions = Vector3::ONE;
484 void PrimitiveVisual::CreateConic(Vector<Vertex>& vertices, Vector<unsigned short>& indices, float scaleTopRadius, float scaleBottomRadius, float scaleHeight, int slices)
486 ComputeConicVertices(vertices, scaleTopRadius, scaleBottomRadius, scaleHeight, slices);
487 FormConicTriangles(indices, scaleTopRadius, scaleBottomRadius, slices);
489 //Determine object dimensions, and scale them to be between 0.0 and 1.0.
490 float xDimension = std::max(scaleTopRadius, scaleBottomRadius) * 2.0f;
491 float yDimension = scaleHeight;
492 float largestDimension = std::max(xDimension, yDimension);
494 mObjectDimensions = Vector3(xDimension / largestDimension, yDimension / largestDimension, xDimension / largestDimension);
497 void PrimitiveVisual::CreateBevelledCube(Vector<Vertex>& vertices, Vector<unsigned short>& indices, Vector3 dimensions, float bevelPercentage, float bevelSmoothness)
499 float maxDimension = std::max(std::max(dimensions.x, dimensions.y), dimensions.z);
500 dimensions = dimensions / maxDimension;
502 if(bevelPercentage <= MIN_BEVEL_PERCENTAGE) //No bevel, form a cube.
504 ComputeCubeVertices(vertices, dimensions);
505 FormCubeTriangles(indices);
507 else if(bevelPercentage >= MAX_BEVEL_PERCENTAGE) //Max bevel, form an octahedron.
509 ComputeOctahedronVertices(vertices, dimensions, bevelSmoothness);
510 FormOctahedronTriangles(indices);
512 else //In between, form a bevelled cube.
514 ComputeBevelledCubeVertices(vertices, dimensions, bevelPercentage, bevelSmoothness);
515 FormBevelledCubeTriangles(indices);
518 mObjectDimensions = dimensions;
521 void PrimitiveVisual::ComputeCircleTables(Vector<float>& sinTable, Vector<float>& cosTable, int divisions, bool halfCircle)
528 const float angleDivision = (halfCircle ? 1.0f : 2.0f) * Dali::Math::PI / (float)divisions;
530 sinTable.Resize(divisions);
531 cosTable.Resize(divisions);
533 for(int i = 0; i < divisions; i++)
535 sinTable[i] = sin(angleDivision * i);
536 cosTable[i] = cos(angleDivision * i);
540 void PrimitiveVisual::ComputeSphereVertices(Vector<Vertex>& vertices, int slices, int stacks)
542 //Tables for calculating slices angles and stacks angles, respectively.
543 Vector<float> sinTable1;
544 Vector<float> cosTable1;
545 Vector<float> sinTable2;
546 Vector<float> cosTable2;
548 ComputeCircleTables(sinTable1, cosTable1, slices, false);
549 ComputeCircleTables(sinTable2, cosTable2, stacks, true);
551 int numVertices = slices * (stacks - 1) + 2;
552 vertices.Resize(numVertices);
554 int vertexIndex = 0; //Track progress through vertices.
560 vertices[vertexIndex].position = Vector3(0.0, 0.5, 0.0);
561 vertices[vertexIndex].normal = Vector3(0.0, 1.0, 0.0);
565 for(int i = 1; i < stacks; i++)
567 for(int j = 0; j < slices; j++, vertexIndex++)
569 x = cosTable1[j] * sinTable2[i];
571 z = sinTable1[j] * sinTable2[i];
573 vertices[vertexIndex].position = Vector3(x / 2.0f, y / 2.0f, z / 2.0f);
574 vertices[vertexIndex].normal = Vector3(x, y, z);
579 vertices[vertexIndex].position = Vector3(0.0, -0.5, 0.0);
580 vertices[vertexIndex].normal = Vector3(0.0, -1.0, 0.0);
583 void PrimitiveVisual::FormSphereTriangles(Vector<unsigned short>& indices, int slices, int stacks)
587 //Set indices to placeholder "error" values.
588 //This will display nothing, which is the expected behaviour for this edge case.
593 int numTriangles = 2 * slices * (stacks - 1);
595 indices.Resize(3 * numTriangles);
597 int indiceIndex = 0; //Used to keep track of progress through indices.
598 int previousCycleBeginning = 1; //Stores the index of the vertex that started the cycle of the previous stack.
599 int currentCycleBeginning = 1 + slices;
601 //Top stack. Loop from index 1 to index slices, as not counting the very first vertex.
602 for(int i = 1; i <= slices; i++, indiceIndex += 3)
604 indices[indiceIndex] = 0;
607 //End, so loop around.
608 indices[indiceIndex + 1] = 1;
612 indices[indiceIndex + 1] = i + 1;
614 indices[indiceIndex + 2] = i;
617 //Middle Stacks. Want to form triangles between the top and bottom stacks, so loop up to the number of stacks - 2.
618 for(int i = 0; i < stacks - 2; i++, previousCycleBeginning += slices, currentCycleBeginning += slices)
620 for(int j = 0; j < slices; j++, indiceIndex += 6)
624 //End, so loop around.
625 indices[indiceIndex] = previousCycleBeginning + j;
626 indices[indiceIndex + 1] = previousCycleBeginning;
627 indices[indiceIndex + 2] = currentCycleBeginning + j;
628 indices[indiceIndex + 3] = currentCycleBeginning + j;
629 indices[indiceIndex + 4] = previousCycleBeginning;
630 indices[indiceIndex + 5] = currentCycleBeginning;
634 indices[indiceIndex] = previousCycleBeginning + j;
635 indices[indiceIndex + 1] = previousCycleBeginning + 1 + j;
636 indices[indiceIndex + 2] = currentCycleBeginning + j;
637 indices[indiceIndex + 3] = currentCycleBeginning + j;
638 indices[indiceIndex + 4] = previousCycleBeginning + 1 + j;
639 indices[indiceIndex + 5] = currentCycleBeginning + 1 + j;
644 //Bottom stack. Loop around the last stack from the previous loop, and go up to the penultimate vertex.
645 for(int i = 0; i < slices; i++, indiceIndex += 3)
647 indices[indiceIndex] = previousCycleBeginning + slices;
648 indices[indiceIndex + 1] = previousCycleBeginning + i;
651 //End, so loop around.
652 indices[indiceIndex + 2] = previousCycleBeginning;
656 indices[indiceIndex + 2] = previousCycleBeginning + i + 1;
661 void PrimitiveVisual::ComputeConicVertices(Vector<Vertex>& vertices, float scaleTopRadius, float scaleBottomRadius, float scaleHeight, int slices)
663 int vertexIndex = 0; //Track progress through vertices.
664 Vector<float> sinTable;
665 Vector<float> cosTable;
667 ComputeCircleTables(sinTable, cosTable, slices, false);
669 int numVertices = 2; //Always will have one at the top and one at the bottom.
671 //Add vertices for each circle. Need two per point for different face normals.
672 if(scaleTopRadius > 0.0)
674 numVertices += 2 * slices;
676 if(scaleBottomRadius > 0.0)
678 numVertices += 2 * slices;
681 vertices.Resize(numVertices);
683 //Scale to bounding region of -0.5 to 0.5 (i.e range of 1).
684 float biggestObjectDimension = std::max(std::max(scaleTopRadius * 2.0f, scaleBottomRadius * 2.0f), scaleHeight);
685 scaleTopRadius = scaleTopRadius / biggestObjectDimension;
686 scaleBottomRadius = scaleBottomRadius / biggestObjectDimension;
688 //Dimensions for vertex coordinates. Y is constant, and so can be initialised now.
690 float y = scaleHeight / biggestObjectDimension / 2.0f;
694 vertices[0].position = Vector3(0, y, 0);
695 vertices[0].normal = Vector3(0, 1, 0);
699 if(scaleTopRadius > 0.0)
701 //Loop around the circle.
702 for(int i = 0; i < slices; i++, vertexIndex++)
704 x = sinTable[i] * scaleTopRadius;
705 z = cosTable[i] * scaleTopRadius;
707 //Upward-facing normal.
708 vertices[vertexIndex].position = Vector3(x, y, z);
709 vertices[vertexIndex].normal = Vector3(0, 1, 0);
711 //Outward-facing normal.
712 vertices[vertexIndex + slices].position = Vector3(x, y, z);
713 vertices[vertexIndex + slices].normal = Vector3(x, 0, z);
716 vertexIndex += slices;
720 if(scaleBottomRadius > 0.0)
722 //Loop around the circle.
723 for(int i = 0; i < slices; i++, vertexIndex++)
725 x = sinTable[i] * scaleBottomRadius;
726 z = cosTable[i] * scaleBottomRadius;
728 //Outward-facing normal.
729 vertices[vertexIndex].position = Vector3(x, -y, z);
730 vertices[vertexIndex].normal = Vector3(x, 0, z);
732 //Downward-facing normal.
733 vertices[vertexIndex + slices].position = Vector3(x, -y, z);
734 vertices[vertexIndex + slices].normal = Vector3(0, -1, 0);
737 vertexIndex += slices;
741 vertices[vertexIndex].position = Vector3(0, -y, 0);
742 vertices[vertexIndex].normal = Vector3(0, -1, 0);
746 void PrimitiveVisual::FormConicTriangles(Vector<unsigned short>& indices, float scaleTopRadius, float scaleBottomRadius, int slices)
748 int indiceIndex = 0; //Track progress through indices.
749 int numTriangles = 0;
750 bool coneTop = scaleTopRadius <= 0.0;
751 bool coneBottom = scaleBottomRadius <= 0.0;
753 if(coneTop && coneBottom)
755 //Set indices to placeholder "error" values.
756 //This will display nothing, which is the expected behaviour for this edge case.
763 numTriangles += 2 * slices;
767 numTriangles += 2 * slices;
770 indices.Resize(3 * numTriangles);
772 //Switch on the type of conic we have.
773 if(!coneTop && !coneBottom)
775 //Top circle. Start at index of first outer point and go around.
776 for(int i = 1; i <= slices; i++, indiceIndex += 3)
778 indices[indiceIndex] = 0;
779 indices[indiceIndex + 1] = i;
782 //End, so loop around.
783 indices[indiceIndex + 2] = 1;
787 indices[indiceIndex + 2] = i + 1;
791 int topCycleBeginning = slices + 1;
792 int bottomCycleBeginning = topCycleBeginning + slices;
795 for(int i = 0; i < slices; i++, indiceIndex += 6)
799 //End, so loop around.
800 indices[indiceIndex] = topCycleBeginning + i;
801 indices[indiceIndex + 1] = bottomCycleBeginning + i;
802 indices[indiceIndex + 2] = topCycleBeginning;
803 indices[indiceIndex + 3] = bottomCycleBeginning + i;
804 indices[indiceIndex + 4] = bottomCycleBeginning;
805 indices[indiceIndex + 5] = topCycleBeginning;
809 indices[indiceIndex] = topCycleBeginning + i;
810 indices[indiceIndex + 1] = bottomCycleBeginning + i;
811 indices[indiceIndex + 2] = topCycleBeginning + 1 + i;
812 indices[indiceIndex + 3] = bottomCycleBeginning + i;
813 indices[indiceIndex + 4] = bottomCycleBeginning + 1 + i;
814 indices[indiceIndex + 5] = topCycleBeginning + 1 + i;
818 int bottomFaceCycleBeginning = bottomCycleBeginning + slices;
821 for(int i = 0; i < slices; i++, indiceIndex += 3)
823 indices[indiceIndex] = bottomFaceCycleBeginning;
826 //End, so loop around.
827 indices[indiceIndex + 1] = bottomFaceCycleBeginning;
831 indices[indiceIndex + 1] = bottomFaceCycleBeginning + i + 1;
833 indices[indiceIndex + 2] = bottomFaceCycleBeginning + i;
836 else if(!coneTop || !coneBottom)
838 //Top circle/edges. Start at index of first outer point and go around.
839 for(int i = 1; i <= slices; i++, indiceIndex += 3)
841 indices[indiceIndex] = 0;
842 indices[indiceIndex + 1] = i;
845 //End, so loop around.
846 indices[indiceIndex + 2] = 1;
850 indices[indiceIndex + 2] = i + 1;
854 //Bottom circle/edges. Start at index of first outer point and go around.
855 for(int i = 1; i <= slices; i++, indiceIndex += 3)
857 indices[indiceIndex] = 2 * slices + 1;
860 //End, so loop around.
861 indices[indiceIndex + 1] = slices + 1;
865 indices[indiceIndex + 1] = slices + i + 1;
867 indices[indiceIndex + 2] = slices + i;
872 void PrimitiveVisual::ComputeCubeVertices(Vector<Vertex>& vertices, Vector3 dimensions)
874 int numVertices = 4 * 6; //Four per face.
875 int vertexIndex = 0; //Tracks progress through vertices.
876 float scaledX = 0.5 * dimensions.x;
877 float scaledY = 0.5 * dimensions.y;
878 float scaledZ = 0.5 * dimensions.z;
880 vertices.Resize(numVertices);
882 Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
884 Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
887 positions[0] = Vector3(-scaledX, scaledY, -scaledZ);
888 positions[1] = Vector3(scaledX, scaledY, -scaledZ);
889 positions[2] = Vector3(scaledX, scaledY, scaledZ);
890 positions[3] = Vector3(-scaledX, scaledY, scaledZ);
891 positions[4] = Vector3(-scaledX, -scaledY, -scaledZ);
892 positions[5] = Vector3(scaledX, -scaledY, -scaledZ);
893 positions[6] = Vector3(scaledX, -scaledY, scaledZ);
894 positions[7] = Vector3(-scaledX, -scaledY, scaledZ);
896 normals[0] = Vector3(0, 1, 0);
897 normals[1] = Vector3(0, 0, -1);
898 normals[2] = Vector3(1, 0, 0);
899 normals[3] = Vector3(0, 0, 1);
900 normals[4] = Vector3(-1, 0, 0);
901 normals[5] = Vector3(0, -1, 0);
903 //Top face, upward normals.
904 for(int i = 0; i < 4; i++, vertexIndex++)
906 vertices[vertexIndex].position = positions[i];
907 vertices[vertexIndex].normal = normals[0];
910 //Top face, outward normals.
911 for(int i = 0; i < 4; i++, vertexIndex += 2)
913 vertices[vertexIndex].position = positions[i];
914 vertices[vertexIndex].normal = normals[i + 1];
918 //End, so loop around.
919 vertices[vertexIndex + 1].position = positions[0];
923 vertices[vertexIndex + 1].position = positions[i + 1];
925 vertices[vertexIndex + 1].normal = normals[i + 1];
928 //Bottom face, outward normals.
929 for(int i = 0; i < 4; i++, vertexIndex += 2)
931 vertices[vertexIndex].position = positions[i + 4];
932 vertices[vertexIndex].normal = normals[i + 1];
936 //End, so loop around.
937 vertices[vertexIndex + 1].position = positions[4];
941 vertices[vertexIndex + 1].position = positions[i + 5];
943 vertices[vertexIndex + 1].normal = normals[i + 1];
946 //Bottom face, downward normals.
947 for(int i = 0; i < 4; i++, vertexIndex++)
949 vertices[vertexIndex].position = positions[i + 4];
950 vertices[vertexIndex].normal = normals[5];
954 void PrimitiveVisual::FormCubeTriangles(Vector<unsigned short>& indices)
956 int numTriangles = 12;
957 int triangleIndex = 0; //Track progress through indices.
959 indices.Resize(3 * numTriangles);
962 indices[triangleIndex] = 0;
963 indices[triangleIndex + 1] = 2;
964 indices[triangleIndex + 2] = 1;
965 indices[triangleIndex + 3] = 2;
966 indices[triangleIndex + 4] = 0;
967 indices[triangleIndex + 5] = 3;
970 int topFaceStart = 4;
971 int bottomFaceStart = 12;
974 for(int i = 0; i < 8; i += 2, triangleIndex += 6)
976 indices[triangleIndex] = i + topFaceStart;
977 indices[triangleIndex + 1] = i + topFaceStart + 1;
978 indices[triangleIndex + 2] = i + bottomFaceStart + 1;
979 indices[triangleIndex + 3] = i + topFaceStart;
980 indices[triangleIndex + 4] = i + bottomFaceStart + 1;
981 indices[triangleIndex + 5] = i + bottomFaceStart;
985 indices[triangleIndex] = 20;
986 indices[triangleIndex + 1] = 21;
987 indices[triangleIndex + 2] = 22;
988 indices[triangleIndex + 3] = 22;
989 indices[triangleIndex + 4] = 23;
990 indices[triangleIndex + 5] = 20;
993 void PrimitiveVisual::ComputeOctahedronVertices(Vector<Vertex>& vertices, Vector3 dimensions, float smoothness)
995 int numVertices = 3 * 8; //Three per face
996 int vertexIndex = 0; //Tracks progress through vertices.
997 float scaledX = 0.5 * dimensions.x;
998 float scaledY = 0.5 * dimensions.y;
999 float scaledZ = 0.5 * dimensions.z;
1001 vertices.Resize(numVertices);
1003 Vector<Vector3> positions; //Stores vertex positions, which are shared between vertexes at the same position but with a different normal.
1004 positions.Resize(6);
1005 Vector<Vector3> normals; //Stores normals, which are shared between vertexes of the same face.
1007 Vector<Vector3> outerNormals; //Holds normals that point outwards at each vertex.
1008 outerNormals.Resize(6);
1010 positions[0] = Vector3(0.0, scaledY, 0.0);
1011 positions[1] = Vector3(-scaledX, 0.0, 0.0);
1012 positions[2] = Vector3(0.0, 0.0, -scaledZ);
1013 positions[3] = Vector3(scaledX, 0.0, 0.0);
1014 positions[4] = Vector3(0.0, 0.0, scaledZ);
1015 positions[5] = Vector3(0.0, -scaledY, 0.0);
1017 normals[0] = Vector3(-1, 1, -1);
1018 normals[1] = Vector3(1, 1, -1);
1019 normals[2] = Vector3(1, 1, 1);
1020 normals[3] = Vector3(-1, 1, 1);
1021 normals[4] = Vector3(-1, -1, -1);
1022 normals[5] = Vector3(1, -1, -1);
1023 normals[6] = Vector3(1, -1, 1);
1024 normals[7] = Vector3(-1, -1, 1);
1026 outerNormals[0] = Vector3(0, 1, 0);
1027 outerNormals[1] = Vector3(-1, 0, 0);
1028 outerNormals[2] = Vector3(0, 0, -1);
1029 outerNormals[3] = Vector3(1, 0, 0);
1030 outerNormals[4] = Vector3(0, 0, 1);
1031 outerNormals[5] = Vector3(0, -1, 0);
1033 //Loop through top faces.
1034 for(int i = 0; i < 4; i++, vertexIndex += 3)
1038 //End, so loop around.
1039 vertices[vertexIndex].position = positions[0];
1040 vertices[vertexIndex].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1041 vertices[vertexIndex + 1].position = positions[1];
1042 vertices[vertexIndex + 1].normal = outerNormals[1] * smoothness + normals[i] * (1 - smoothness);
1043 vertices[vertexIndex + 2].position = positions[i + 1];
1044 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1048 vertices[vertexIndex].position = positions[0];
1049 vertices[vertexIndex].normal = outerNormals[0] * smoothness + normals[i] * (1 - smoothness);
1050 vertices[vertexIndex + 1].position = positions[i + 2];
1051 vertices[vertexIndex + 1].normal = outerNormals[i + 2] * smoothness + normals[i] * (1 - smoothness);
1052 vertices[vertexIndex + 2].position = positions[i + 1];
1053 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * smoothness + normals[i] * (1 - smoothness);
1057 //Loop through bottom faces.
1058 for(int i = 0; i < 4; i++, vertexIndex += 3)
1062 //End, so loop around.
1063 vertices[vertexIndex].position = positions[5];
1064 vertices[vertexIndex].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1065 vertices[vertexIndex + 1].position = positions[i + 1];
1066 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1067 vertices[vertexIndex + 2].position = positions[1];
1068 vertices[vertexIndex + 2].normal = outerNormals[1] * smoothness + normals[i + 4] * (1 - smoothness);
1072 vertices[vertexIndex].position = positions[5];
1073 vertices[vertexIndex].normal = outerNormals[5] * smoothness + normals[i + 4] * (1 - smoothness);
1074 vertices[vertexIndex + 1].position = positions[i + 1];
1075 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * smoothness + normals[i + 4] * (1 - smoothness);
1076 vertices[vertexIndex + 2].position = positions[i + 2];
1077 vertices[vertexIndex + 2].normal = outerNormals[i + 2] * smoothness + normals[i + 4] * (1 - smoothness);
1082 void PrimitiveVisual::FormOctahedronTriangles(Vector<unsigned short>& indices)
1084 int numTriangles = 8;
1085 int numIndices = numTriangles * 3;
1087 indices.Resize(numIndices);
1089 for(unsigned short i = 0; i < numIndices; i++)
1095 void PrimitiveVisual::ComputeBevelledCubeVertices(Vector<Vertex>& vertices, Vector3 dimensions, float bevelPercentage, float bevelSmoothness)
1097 int numPositions = 24;
1099 int numOuterFaces = 6;
1100 int numVertices = 6 * 4 + 12 * 4 + 8 * 3; //Six outer faces, 12 slanting rectangles, 8 slanting triangles.
1101 int vertexIndex = 0; //Track progress through vertices.
1102 int normalIndex = 0; //Track progress through normals, as vertices are calculated per face.
1104 float minDimension = std::min(std::min(dimensions.x, dimensions.y), dimensions.z);
1105 float bevelAmount = 0.5 * std::min(bevelPercentage, minDimension); //Cap bevel amount if necessary.
1107 //Distances from centre to outer edge points.
1108 float outerX = 0.5 * dimensions.x;
1109 float outerY = 0.5 * dimensions.y;
1110 float outerZ = 0.5 * dimensions.z;
1112 //Distances from centre to bevelled points.
1113 float bevelX = outerX - bevelAmount;
1114 float bevelY = outerY - bevelAmount;
1115 float bevelZ = outerZ - bevelAmount;
1117 Vector<Vector3> positions; //Holds object points, to be shared between vertexes.
1118 positions.Resize(numPositions);
1119 Vector<Vector3> normals; //Holds face normals, to be shared between vertexes.
1120 normals.Resize(numFaces);
1121 Vector<Vector3> outerNormals; //Holds normals of the outermost faces specifically.
1122 outerNormals.Resize(numOuterFaces);
1123 vertices.Resize(numVertices);
1125 //Topmost face positions.
1126 positions[0] = Vector3(-bevelX, outerY, -bevelZ);
1127 positions[1] = Vector3(bevelX, outerY, -bevelZ);
1128 positions[2] = Vector3(bevelX, outerY, bevelZ);
1129 positions[3] = Vector3(-bevelX, outerY, bevelZ);
1131 //Second layer positions.
1132 positions[4] = Vector3(-outerX, bevelY, -bevelZ);
1133 positions[5] = Vector3(-bevelX, bevelY, -outerZ);
1134 positions[6] = Vector3(bevelX, bevelY, -outerZ);
1135 positions[7] = Vector3(outerX, bevelY, -bevelZ);
1136 positions[8] = Vector3(outerX, bevelY, bevelZ);
1137 positions[9] = Vector3(bevelX, bevelY, outerZ);
1138 positions[10] = Vector3(-bevelX, bevelY, outerZ);
1139 positions[11] = Vector3(-outerX, bevelY, bevelZ);
1141 //Third layer positions.
1142 positions[12] = Vector3(-outerX, -bevelY, -bevelZ);
1143 positions[13] = Vector3(-bevelX, -bevelY, -outerZ);
1144 positions[14] = Vector3(bevelX, -bevelY, -outerZ);
1145 positions[15] = Vector3(outerX, -bevelY, -bevelZ);
1146 positions[16] = Vector3(outerX, -bevelY, bevelZ);
1147 positions[17] = Vector3(bevelX, -bevelY, outerZ);
1148 positions[18] = Vector3(-bevelX, -bevelY, outerZ);
1149 positions[19] = Vector3(-outerX, -bevelY, bevelZ);
1151 //Bottom-most face positions.
1152 positions[20] = Vector3(-bevelX, -outerY, -bevelZ);
1153 positions[21] = Vector3(bevelX, -outerY, -bevelZ);
1154 positions[22] = Vector3(bevelX, -outerY, bevelZ);
1155 positions[23] = Vector3(-bevelX, -outerY, bevelZ);
1158 normals[0] = Vector3(0, 1, 0);
1160 //Top slope normals.
1161 normals[1] = Vector3(-1, 1, -1);
1162 normals[2] = Vector3(0, 1, -1);
1163 normals[3] = Vector3(1, 1, -1);
1164 normals[4] = Vector3(1, 1, 0);
1165 normals[5] = Vector3(1, 1, 1);
1166 normals[6] = Vector3(0, 1, 1);
1167 normals[7] = Vector3(-1, 1, 1);
1168 normals[8] = Vector3(-1, 1, 0);
1171 normals[9] = Vector3(-1, 0, -1);
1172 normals[10] = Vector3(0, 0, -1);
1173 normals[11] = Vector3(1, 0, -1);
1174 normals[12] = Vector3(1, 0, 0);
1175 normals[13] = Vector3(1, 0, 1);
1176 normals[14] = Vector3(0, 0, 1);
1177 normals[15] = Vector3(-1, 0, 1);
1178 normals[16] = Vector3(-1, 0, 0);
1180 //Bottom slope normals.
1181 normals[17] = Vector3(-1, -1, -1);
1182 normals[18] = Vector3(0, -1, -1);
1183 normals[19] = Vector3(1, -1, -1);
1184 normals[20] = Vector3(1, -1, 0);
1185 normals[21] = Vector3(1, -1, 1);
1186 normals[22] = Vector3(0, -1, 1);
1187 normals[23] = Vector3(-1, -1, 1);
1188 normals[24] = Vector3(-1, -1, 0);
1190 //Bottom face normal.
1191 normals[25] = Vector3(0, -1, 0);
1193 //Top, back, right, front, left and bottom faces, respectively.
1194 outerNormals[0] = Vector3(0, 1, 0);
1195 outerNormals[1] = Vector3(0, 0, -1);
1196 outerNormals[2] = Vector3(1, 0, 0);
1197 outerNormals[3] = Vector3(0, 0, 1);
1198 outerNormals[4] = Vector3(-1, 0, 0);
1199 outerNormals[5] = Vector3(0, -1, 0);
1201 //Topmost face vertices.
1202 for(int i = 0; i < 4; i++, vertexIndex++)
1204 vertices[vertexIndex].position = positions[i];
1205 vertices[vertexIndex].normal = normals[normalIndex];
1210 //Top slope vertices.
1211 for(int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2)
1214 vertices[vertexIndex].position = positions[i];
1215 vertices[vertexIndex].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1216 vertices[vertexIndex + 1].position = positions[2 * i + 4];
1217 vertices[vertexIndex + 1].normal = outerNormals[(i == 0) ? 4 : i] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1218 vertices[vertexIndex + 2].position = positions[2 * i + 5];
1219 vertices[vertexIndex + 2].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1224 //End, so loop around.
1225 vertices[vertexIndex + 3].position = positions[i];
1226 vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1227 vertices[vertexIndex + 4].position = positions[0];
1228 vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1229 vertices[vertexIndex + 5].position = positions[2 * i + 5];
1230 vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1231 vertices[vertexIndex + 6].position = positions[4];
1232 vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1236 vertices[vertexIndex + 3].position = positions[i];
1237 vertices[vertexIndex + 3].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1238 vertices[vertexIndex + 4].position = positions[i + 1];
1239 vertices[vertexIndex + 4].normal = outerNormals[0] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1240 vertices[vertexIndex + 5].position = positions[2 * i + 5];
1241 vertices[vertexIndex + 5].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1242 vertices[vertexIndex + 6].position = positions[2 * i + 6];
1243 vertices[vertexIndex + 6].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1247 int secondCycleBeginning = 4;
1248 int thirdCycleBeginning = secondCycleBeginning + 8;
1249 int bottomCycleBeginning = thirdCycleBeginning + 8;
1252 for(int i = 0; i < 8; i++, vertexIndex += 4, normalIndex++)
1256 //End, so loop around.
1257 vertices[vertexIndex].position = positions[secondCycleBeginning + i];
1258 vertices[vertexIndex].normal = normals[normalIndex];
1259 vertices[vertexIndex + 1].position = positions[secondCycleBeginning];
1260 vertices[vertexIndex + 1].normal = normals[normalIndex];
1261 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1262 vertices[vertexIndex + 2].normal = normals[normalIndex];
1263 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning];
1264 vertices[vertexIndex + 3].normal = normals[normalIndex];
1266 else if((i % 2) == 0)
1268 //'even' faces are corner ones, and need smoothing.
1269 vertices[vertexIndex].position = positions[secondCycleBeginning + i];
1270 vertices[vertexIndex].normal = outerNormals[(i == 0) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1271 vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1272 vertices[vertexIndex + 1].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1273 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1274 vertices[vertexIndex + 2].normal = outerNormals[(i == 0) ? 4 : i / 2] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1275 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1276 vertices[vertexIndex + 3].normal = outerNormals[i / 2 + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1280 //'odd' faces are outer ones, and so don't need smoothing.
1281 vertices[vertexIndex].position = positions[secondCycleBeginning + i];
1282 vertices[vertexIndex].normal = normals[normalIndex];
1283 vertices[vertexIndex + 1].position = positions[secondCycleBeginning + i + 1];
1284 vertices[vertexIndex + 1].normal = normals[normalIndex];
1285 vertices[vertexIndex + 2].position = positions[thirdCycleBeginning + i];
1286 vertices[vertexIndex + 2].normal = normals[normalIndex];
1287 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + i + 1];
1288 vertices[vertexIndex + 3].normal = normals[normalIndex];
1292 //Bottom slope vertices.
1293 for(int i = 0; i < 4; i++, vertexIndex += 7, normalIndex += 2)
1296 vertices[vertexIndex].position = positions[thirdCycleBeginning + 2 * i];
1297 vertices[vertexIndex].normal = outerNormals[(i == 0) ? 4 : i] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1298 vertices[vertexIndex + 1].position = positions[thirdCycleBeginning + 2 * i + 1];
1299 vertices[vertexIndex + 1].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1300 vertices[vertexIndex + 2].position = positions[bottomCycleBeginning + i];
1301 vertices[vertexIndex + 2].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex] * (1 - bevelSmoothness);
1306 //End, so loop around.
1307 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1308 vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1309 vertices[vertexIndex + 4].position = positions[thirdCycleBeginning];
1310 vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1311 vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1312 vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1313 vertices[vertexIndex + 6].position = positions[bottomCycleBeginning];
1314 vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1318 vertices[vertexIndex + 3].position = positions[thirdCycleBeginning + 2 * i + 1];
1319 vertices[vertexIndex + 3].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1320 vertices[vertexIndex + 4].position = positions[thirdCycleBeginning + 2 * i + 2];
1321 vertices[vertexIndex + 4].normal = outerNormals[i + 1] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1322 vertices[vertexIndex + 5].position = positions[bottomCycleBeginning + i];
1323 vertices[vertexIndex + 5].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1324 vertices[vertexIndex + 6].position = positions[bottomCycleBeginning + i + 1];
1325 vertices[vertexIndex + 6].normal = outerNormals[5] * bevelSmoothness + normals[normalIndex + 1] * (1 - bevelSmoothness);
1329 //Bottom-most face vertices.
1330 for(int i = 0; i < 4; i++, vertexIndex++)
1332 vertices[vertexIndex].position = positions[bottomCycleBeginning + i];
1333 vertices[vertexIndex].normal = normals[normalIndex];
1339 void PrimitiveVisual::FormBevelledCubeTriangles(Vector<unsigned short>& indices)
1341 int numTriangles = 44; //(Going from top to bottom, that's 2 + 12 + 16 + 12 + 2)
1342 int indiceIndex = 0; //Track progress through indices.
1343 int vertexIndex = 0; //Track progress through vertices as they're processed.
1345 indices.Resize(3 * numTriangles);
1348 indices[indiceIndex] = vertexIndex;
1349 indices[indiceIndex + 1] = vertexIndex + 2;
1350 indices[indiceIndex + 2] = vertexIndex + 1;
1351 indices[indiceIndex + 3] = vertexIndex + 0;
1352 indices[indiceIndex + 4] = vertexIndex + 3;
1353 indices[indiceIndex + 5] = vertexIndex + 2;
1358 for(int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7)
1361 indices[indiceIndex] = vertexIndex;
1362 indices[indiceIndex + 1] = vertexIndex + 2;
1363 indices[indiceIndex + 2] = vertexIndex + 1;
1366 indices[indiceIndex + 3] = vertexIndex + 3;
1367 indices[indiceIndex + 4] = vertexIndex + 4;
1368 indices[indiceIndex + 5] = vertexIndex + 5;
1369 indices[indiceIndex + 6] = vertexIndex + 4;
1370 indices[indiceIndex + 7] = vertexIndex + 6;
1371 indices[indiceIndex + 8] = vertexIndex + 5;
1375 for(int i = 0; i < 8; i++, indiceIndex += 6, vertexIndex += 4)
1377 indices[indiceIndex] = vertexIndex;
1378 indices[indiceIndex + 1] = vertexIndex + 1;
1379 indices[indiceIndex + 2] = vertexIndex + 2;
1380 indices[indiceIndex + 3] = vertexIndex + 1;
1381 indices[indiceIndex + 4] = vertexIndex + 3;
1382 indices[indiceIndex + 5] = vertexIndex + 2;
1386 for(int i = 0; i < 4; i++, indiceIndex += 9, vertexIndex += 7)
1389 indices[indiceIndex] = vertexIndex;
1390 indices[indiceIndex + 1] = vertexIndex + 1;
1391 indices[indiceIndex + 2] = vertexIndex + 2;
1394 indices[indiceIndex + 3] = vertexIndex + 3;
1395 indices[indiceIndex + 4] = vertexIndex + 4;
1396 indices[indiceIndex + 5] = vertexIndex + 5;
1397 indices[indiceIndex + 6] = vertexIndex + 4;
1398 indices[indiceIndex + 7] = vertexIndex + 6;
1399 indices[indiceIndex + 8] = vertexIndex + 5;
1403 indices[indiceIndex] = vertexIndex;
1404 indices[indiceIndex + 1] = vertexIndex + 1;
1405 indices[indiceIndex + 2] = vertexIndex + 2;
1406 indices[indiceIndex + 3] = vertexIndex + 0;
1407 indices[indiceIndex + 4] = vertexIndex + 2;
1408 indices[indiceIndex + 5] = vertexIndex + 3;
1413 } // namespace Internal
1415 } // namespace Toolkit