Skip to main content

WireframeSphere.ixx File

Simple wireframe sphere mesh asset with positions, normals, UVs, and line indices. More...

Included Headers

#include <array> #include <span> #include <cmath> #include <numbers> #include <cstddef> #include <helios.math> #include <helios.engine.rendering.mesh.types.PrimitiveType> #include <helios.engine.rendering.mesh.types.MeshData> #include <helios.engine.rendering.common.types.Vertex>

Namespaces Index

namespacehelios
namespaceengine
namespacerendering
namespacemesh
namespaceassets

Classes Index

structWireframeSphere

Static wireframe sphere mesh definition. More...

Description

Simple wireframe sphere mesh asset with positions, normals, UVs, and line indices.

File Listing

The file content with the documentation metadata removed is:

1
5module;
6
7#include <array>
8#include <span>
9#include <cmath>
10#include <numbers>
11#include <cstddef>
12
13export module helios.engine.rendering.mesh.assets:WireframeSphere;
14
15import helios.engine.rendering.common.types.Vertex;
16import helios.engine.rendering.mesh.types.MeshData;
17import helios.engine.rendering.mesh.types.PrimitiveType;
18import helios.math;
19
22
24
32
33 static constexpr std::size_t Stacks = 12;
34 static constexpr std::size_t Slices = 24;
35
36 static constexpr float Radius = 0.5f;
37
38 static constexpr std::size_t VertexCount =
39 (Stacks + 1) * Slices;
40
41 static constexpr std::size_t HorizontalEdgeCount =
42 (Stacks - 1) * Slices;
43
44 static constexpr std::size_t VerticalEdgeCount =
46
47 static constexpr std::size_t IndexCount =
49
50 private:
51
52 static std::size_t vertexIndex(
53 const std::size_t stack,
54 const std::size_t slice
55 ) noexcept {
56 return stack * Slices + slice;
57 }
58
59 static std::array<Vertex, VertexCount> makeVertices() {
60 std::array<Vertex, VertexCount> result{};
61
62 constexpr float pi = std::numbers::pi_v<float>;
63
64 for (std::size_t stack = 0; stack <= Stacks; ++stack) {
65 const float v = static_cast<float>(stack) / static_cast<float>(Stacks);
66 const float theta = v * pi;
67
68 const float y = std::cos(theta) * Radius;
69 const float ringRadius = std::sin(theta) * Radius;
70
71 for (std::size_t slice = 0; slice < Slices; ++slice) {
72 const float u = static_cast<float>(slice) / static_cast<float>(Slices);
73 const float phi = u * 2.0f * pi;
74
75 const float x = std::cos(phi) * ringRadius;
76 const float z = std::sin(phi) * ringRadius;
77
78 const float nx = x / Radius;
79 const float ny = y / Radius;
80 const float nz = z / Radius;
81
82 result[vertexIndex(stack, slice)] = Vertex{
83 {x, y, z},
84 {nx, ny, nz},
85 {u, v}
86 };
87 }
88 }
89
90 return result;
91 }
92
93 static std::array<unsigned int, IndexCount> makeIndices() {
94 std::array<unsigned int, IndexCount> result{};
95 std::size_t cursor = 0;
96
97 // Latitude rings, excluding top and bottom poles.
98 for (std::size_t stack = 1; stack < Stacks; ++stack) {
99 for (std::size_t slice = 0; slice < Slices; ++slice) {
100 const auto current = vertexIndex(stack, slice);
101 const auto next = vertexIndex(stack, (slice + 1) % Slices);
102
103 result[cursor++] = static_cast<unsigned int>(current);
104 result[cursor++] = static_cast<unsigned int>(next);
105 }
106 }
107
108 // Longitude arcs.
109 for (std::size_t slice = 0; slice < Slices; ++slice) {
110 for (std::size_t stack = 0; stack < Stacks; ++stack) {
111 const auto current = vertexIndex(stack, slice);
112 const auto next = vertexIndex(stack + 1, slice);
113
114 result[cursor++] = static_cast<unsigned int>(current);
115 result[cursor++] = static_cast<unsigned int>(next);
116 }
117 }
118
119 return result;
120 }
121
122 public:
123
127 inline static const std::array<Vertex, VertexCount> vertices = makeVertices();
128
132 inline static const std::array<unsigned int, IndexCount> indices = makeIndices();
133
137 static constexpr PrimitiveType primitiveType = PrimitiveType::Lines;
138
143 static MeshData meshData() {
144 return MeshData{
145 std::span{vertices},
146 std::span{indices},
148 };
149 }
150
155 static helios::math::aabbf boundsData() {
156 return helios::math::aabbf{
157 -Radius, -Radius, -Radius,
159 };
160 }
161
162 };
163
164}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.