Skip to main content

Circle.ixx File

Basic circle shape for rendering. More...

Included Headers

#include <vector> #include <cmath> #include <numbers> #include <helios.rendering.mesh.PrimitiveType> #include <helios.math.types> #include <helios.rendering.Vertex> #include <helios.rendering.asset.shape.Shape>

Namespaces Index

namespacehelios
namespacerendering

Graphics rendering infrastructure. More...

namespaceasset

Rendering asset management. More...

namespaceshape

Geometric shape definitions. More...

namespacebasic

Basic primitive shapes. More...

Classes Index

structCircle

Basic Circle Shape (2D, in XY plane). More...

Description

Basic circle shape for rendering.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file Circle.ixx
3 * @brief Basic circle shape for rendering.
4 */
5module;
6
7#include <vector>
8#include <cmath>
9#include <numbers>
10
11export module helios.rendering.asset.shape.basic.Circle;
12
13import helios.rendering.asset.shape.Shape;
14import helios.rendering.Vertex;
15import helios.math.types;
16import helios.rendering.mesh.PrimitiveType;
17
18using namespace helios::rendering;
19
21
22 /**
23 * @brief Basic Circle Shape (2D, in XY plane).
24 *
25 * Generates a 2D circle mesh, centered at the origin on the XY plane (Z=0).
26 * The normals point in the positive Z direction.
27 * The indices are arranged as a Triangle Fan (for GL_TRIANGLES).
28 */
29 struct Circle : public Shape {
30
31 private:
32 /**
33 * @brief Dynamically creates the vertex list for a circle.
34 * Generates one center vertex (Index 0) and 'segments' vertices on the circumference.
35 */
36 static std::vector<Vertex> generateVertices(float radius, unsigned int segments) {
37 std::vector<Vertex> vertices;
38 // 1 for the center vertex
39 vertices.reserve(segments + 1);
40
41 // center vertex (index 0)
42 vertices.push_back({
43 {0.0f, 0.0f, 0.0f}, // Position
44 {0.0f, 0.0f, 1.0f}, // Normal
45 {0.5f, 0.5f} // TexCoord
46 });
47
48 // circumference vertices (Indices 1 to segments)
49 for (unsigned int i = 0; i < segments; ++i) {
50 // calculate the angle for this segment
51 const float angle = (static_cast<float>(i) / static_cast<float>(segments)) * 2.0f * std::numbers::pi_v<float>;
52
53 float x = radius * std::cos(angle);
54 float y = radius * std::sin(angle);
55
56 // texture coordinates: map [-radius, +radius] to [0, 1]
57 float u = (x / radius) * 0.5f + 0.5f;
58 float v = (y / radius) * 0.5f + 0.5f;
59
60 vertices.push_back({
61 {x, y, 0.0f},
62 {0.0f, 0.0f, 1.0f},
63 {u, v}
64 });
65 }
66 return vertices;
67 }
68
69 /**
70 * @brief Creates the index list for a `Triangle Fan` (for GL_TRIANGLES).
71 * Each triangle consists of (center, point_i, point_i+1).
72 */
73 static std::vector<unsigned int> generateIndices(unsigned int segments) {
74 std::vector<unsigned int> indices;
75 indices.reserve(segments * 3); // 'segments' triangles, 3 indices per triangle
76
77 for (unsigned int i = 1; i <= segments; ++i) {
78 indices.push_back(0); // center vertex
79 indices.push_back(i); // current circumference vertex
80
81 // next circumference vertex
82 // for the last triangle (i == segments),
83 // connect back to Vertex 1
84 unsigned int next_i = (i == segments) ? 1 : (i + 1);
85 indices.push_back(next_i);
86 }
87 return indices;
88 }
89
90 public:
91 /**
92 * @brief Creates a default circle (radius 1.0, 64 segments).
93 */
94 Circle() : Circle(1.0f, 64) {}
95
96 /**
97 * @brief Creates a circle with a specific radius and segment count.
98 *
99 * @param radius The radius of the circle (from center to circumference).
100 * @param segments The number of triangle segments that make up the circle.
101 * (More segments = smoother circle).
102 */
103 explicit Circle(float radius, unsigned int segments) :
104 Shape(
105 generateVertices(radius, segments),
106 generateIndices(segments)
107 ) {}
108
109 [[nodiscard]] helios::rendering::mesh::PrimitiveType primitiveType() const noexcept override {
111 }
112 };
113
114}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.