-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtorus.cpp
More file actions
87 lines (75 loc) · 2.41 KB
/
torus.cpp
File metadata and controls
87 lines (75 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "torus.h"
#include <algorithm>
#include <cmath>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/** Three.js `TorusGeometry` (r162): radius, tube, radialSegments, tubularSegments, arc=2*pi */
Torus::Torus(TorusDimensions const &dimensions, TorusParameters const ¶meters)
: Mesh{}, dimensions{dimensions}, parameters{parameters}
{
createVertices();
computeFaces();
createMesh();
}
Torus::~Torus() = default;
void Torus::rebuildGeometry()
{
ClearMesh();
vertices.clear();
normals.clear();
faces.clear();
indices.clear();
createVertices();
computeFaces();
createMesh();
threeBSPDone = false;
}
void Torus::createVertices()
{
int const radialSegments = std::max(3, parameters.radialSegments);
int const tubularSegments = std::max(3, parameters.tubularSegments);
float const radius = std::max(1e-4f, dimensions.majorRadius);
float const tube = std::max(1e-4f, dimensions.minorRadius);
float const arc = static_cast<float>(2.0 * M_PI);
vertices.clear();
normals.clear();
indices.clear();
glm::vec3 center(0.0f);
for (int j = 0; j <= radialSegments; ++j)
{
for (int i = 0; i <= tubularSegments; ++i)
{
float const u = static_cast<float>(i) / static_cast<float>(tubularSegments) * arc;
float const v = static_cast<float>(j) / static_cast<float>(radialSegments) * static_cast<float>(2.0 * M_PI);
glm::vec3 vertex;
vertex.x = (radius + tube * std::cos(v)) * std::cos(u);
vertex.y = (radius + tube * std::cos(v)) * std::sin(u);
vertex.z = tube * std::sin(v);
vertices.push_back(vertex);
center.x = radius * std::cos(u);
center.y = radius * std::sin(u);
center.z = 0.0f;
glm::vec3 normal = vertex - center;
float const nl = glm::length(normal);
normal = nl > 1e-20f ? normal / nl : glm::vec3(0.0f, 0.0f, 1.0f);
normals.push_back(normal);
}
}
for (int j = 1; j <= radialSegments; ++j)
{
for (int i = 1; i <= tubularSegments; ++i)
{
int const a = (tubularSegments + 1) * j + i - 1;
int const b = (tubularSegments + 1) * (j - 1) + i - 1;
int const c = (tubularSegments + 1) * (j - 1) + i;
int const d = (tubularSegments + 1) * j + i;
indices.push_back(static_cast<unsigned int>(a));
indices.push_back(static_cast<unsigned int>(b));
indices.push_back(static_cast<unsigned int>(d));
indices.push_back(static_cast<unsigned int>(b));
indices.push_back(static_cast<unsigned int>(c));
indices.push_back(static_cast<unsigned int>(d));
}
}
}