-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcylinder.cpp
More file actions
166 lines (141 loc) · 4.5 KB
/
cylinder.cpp
File metadata and controls
166 lines (141 loc) · 4.5 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "cylinder.h"
#include <algorithm>
#include <cmath>
#include <vector>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
Cylinder::Cylinder(CylinderDimensions const &dimensions, CylinderParameters const ¶meters)
: Mesh{}, dimensions{dimensions}, parameters{parameters}
{
createVertices();
computeFaces();
createMesh();
}
Cylinder::~Cylinder() = default;
void Cylinder::rebuildGeometry()
{
ClearMesh();
vertices.clear();
normals.clear();
faces.clear();
indices.clear();
createVertices();
computeFaces();
createMesh();
threeBSPDone = false;
}
void Cylinder::createVertices()
{
/** Three.js `CylinderGeometry` (r162): `radiusTop`, `radiusBottom`, height, radialSegments, heightSegments,
* openEnded=false, thetaStart=0, thetaLength=2*pi */
int const radialSegments = std::max(3, parameters.radialSegments);
int const heightSegments = std::max(1, parameters.heightSegments);
float const radiusTop = std::max(1e-4f, dimensions.radiusTop);
float const radiusBottom = std::max(1e-4f, dimensions.radiusBottom);
float const height = std::max(1e-4f, dimensions.height);
float const thetaStart = 0.0f;
float const thetaLength = static_cast<float>(2.0 * M_PI);
float const halfHeight = height * 0.5f;
float const slope = (radiusBottom - radiusTop) / height;
std::vector<std::vector<int>> indexArray;
vertices.clear();
indices.clear();
int index = 0;
auto generateTorso = [&]()
{
for (int y = 0; y <= heightSegments; ++y)
{
std::vector<int> indexRow;
float const v = static_cast<float>(y) / static_cast<float>(heightSegments);
float const radius = v * (radiusBottom - radiusTop) + radiusTop;
for (int x = 0; x <= radialSegments; ++x)
{
float const u = static_cast<float>(x) / static_cast<float>(radialSegments);
float const theta = u * thetaLength + thetaStart;
float const sinTheta = std::sin(theta);
float const cosTheta = std::cos(theta);
glm::vec3 vertex;
vertex.x = radius * sinTheta;
vertex.y = -v * height + halfHeight;
vertex.z = radius * cosTheta;
vertices.push_back(vertex);
glm::vec3 normal(sinTheta, slope, cosTheta);
normal = glm::normalize(normal);
normals.push_back(normal);
indexRow.push_back(index++);
}
indexArray.push_back(std::move(indexRow));
}
for (int x = 0; x < radialSegments; ++x)
{
for (int y = 0; y < heightSegments; ++y)
{
int const a = indexArray[y][x];
int const b = indexArray[y + 1][x];
int const c = indexArray[y + 1][x + 1];
int const d = indexArray[y][x + 1];
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));
}
}
};
auto generateCap = [&](bool top)
{
int const centerIndexStart = index;
float const radius = top ? radiusTop : radiusBottom;
float const sign = top ? 1.0f : -1.0f;
for (int x = 1; x <= radialSegments; ++x)
{
vertices.push_back(glm::vec3(0.0f, halfHeight * sign, 0.0f));
normals.push_back(glm::vec3(0.0f, sign, 0.0f));
++index;
}
int const centerIndexEnd = index;
for (int x = 0; x <= radialSegments; ++x)
{
float const u = static_cast<float>(x) / static_cast<float>(radialSegments);
float const theta = u * thetaLength + thetaStart;
float const cosTheta = std::cos(theta);
float const sinTheta = std::sin(theta);
glm::vec3 vertex;
vertex.x = radius * sinTheta;
vertex.y = halfHeight * sign;
vertex.z = radius * cosTheta;
vertices.push_back(vertex);
normals.push_back(glm::vec3(0.0f, sign, 0.0f));
++index;
}
for (int x = 0; x < radialSegments; ++x)
{
int const c = centerIndexStart + x;
int const i = centerIndexEnd + x;
if (top)
{
indices.push_back(static_cast<unsigned int>(i));
indices.push_back(static_cast<unsigned int>(i + 1));
indices.push_back(static_cast<unsigned int>(c));
}
else
{
indices.push_back(static_cast<unsigned int>(i + 1));
indices.push_back(static_cast<unsigned int>(i));
indices.push_back(static_cast<unsigned int>(c));
}
}
};
generateTorso();
if (radiusTop > 0.0f)
generateCap(true);
if (radiusBottom > 0.0f)
generateCap(false);
/** Bake the old Y-up -> Z-up alignment into geometry so object-local Z is up at identity model. */
for (auto &p : vertices)
p = glm::vec3(p.x, -p.z, p.y);
for (auto &n : normals)
n = glm::normalize(glm::vec3(n.x, -n.z, n.y));
}