-
Notifications
You must be signed in to change notification settings - Fork 20
/
SolidCone.cpp
108 lines (101 loc) · 3.73 KB
/
SolidCone.cpp
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
#include "SolidCone.h"
#include "SolidQuad.h"
#include "PrimTriangle.h"
namespace rt {
// Constructor
CSolidCone::CSolidCone(ptr_shader_t pShader, const Vec3f& origin, float radius, float height, size_t height_segments, size_t sides, bool smooth) : CSolid(origin)
{
const Vec3f top(0, height, 0); // The top point
const Vec3f slope(0, radius / height, 0);
Vec3f dir0(1, 0, 0); // Initial direction
Vec3f p0 = origin + radius * dir0; // Initial point
Vec3f n0 = normalize(dir0 + slope); // Initial normnal
Vec3f dir1, p1, n1; // Next point and normal
float t0 = 0; // Initial texture coordinate
for (size_t s = 0; s < sides; s++) {
float t1 = static_cast<float>(s + 1) / sides; // Next texture coordinate: [1/sides; 1]
float phi = -2 * Pif * t1;
dir1 = Vec3f(cosf(phi), 0, sinf(phi));
p1 = origin + radius * dir1;
n1 = normalize(dir1 + slope);
// Sides: quads
float h0 = 0; // The initial height
for (int h = 0; h < height_segments - 1; h++) {
float h1 = static_cast<float>(h + 1) / height_segments; // Next height: [1/height_segments; 1]
if (height >= 0) {
if (smooth)
add(CSolidQuad(pShader, origin,
p0 + h0 * (top - radius * dir0),
p1 + h0 * (top - radius * dir1),
p1 + h1 * (top - radius * dir1),
p0 + h1 * (top - radius * dir0),
Vec2f(t0, 1 - h0), Vec2f(t1, 1 - h0), Vec2f(t1, 1 - h1), Vec2f(t0, 1 - h1),
n0, n1, n1, n0));
else
add(CSolidQuad(pShader, origin,
p0 + h0 * (top - radius * dir0),
p1 + h0 * (top - radius * dir1),
p1 + h1 * (top - radius * dir1),
p0 + h1 * (top - radius * dir0),
Vec2f(t0, 1 - h0), Vec2f(t1, 1 - h0), Vec2f(t1, 1 - h1), Vec2f(t0, 1 - h1)));
}
else {
if (smooth)
add(CSolidQuad(pShader, origin,
p0 + h0 * (top - radius * dir0),
p0 + h1 * (top - radius * dir0),
p1 + h1 * (top - radius * dir1),
p1 + h0 * (top - radius * dir1),
Vec2f(t0, 1 - h0), Vec2f(t0, 1 - h1), Vec2f(t1, 1 - h1), Vec2f(t1, 1 - h0),
n0, n0, n1, n1));
else
add(CSolidQuad(pShader, origin,
p0 + h0 * (top - radius * dir0),
p0 + h1 * (top - radius * dir0),
p1 + h1 * (top - radius * dir1),
p1 + h0 * (top - radius * dir1),
Vec2f(t0, 1 - h0), Vec2f(t0, 1 - h1), Vec2f(t1, 1 - h1), Vec2f(t1, 1 - h0)));
}
h0 = h1;
}
// Top Sides: triangles
if (height >= 0) {
if (smooth)
add(std::make_shared<CPrimTriangle>(pShader, origin,
origin + top,
p0 + h0 * (top - radius * dir0),
p1 + h0 * (top - radius * dir1),
Vec2f(0.5f, 0), Vec2f(t0, 1 - h0), Vec2f(t1, 1 - h0),
normalize(n0 + n1), n0, n1));
else
add(std::make_shared<CPrimTriangle>(pShader, origin,
origin + top,
p0 + h0 * (top - radius * dir0),
p1 + h0 * (top - radius * dir1),
Vec2f(0.5f, 0), Vec2f(t0, 1 - h0), Vec2f(t1, 1 - h0)));
}
else {
if (smooth)
add(std::make_shared<CPrimTriangle>(pShader, origin,
origin + top,
p1 + h0 * (top - radius * dir1),
p0 + h0 * (top - radius * dir0),
Vec2f(0.5f, 0), Vec2f(t1, 1 - h0), Vec2f(t0, 1 - h0),
normalize(n0 + n1), n1, n0));
else
add(std::make_shared<CPrimTriangle>(pShader, origin,
origin + top,
p1 + h0 * (top - radius * dir1),
p0 + h0 * (top - radius * dir0),
Vec2f(0.5f, 0), Vec2f(t1, 1 - h0), Vec2f(t0, 1 - h0)));
}
// Cap
if (height >= 0) add(std::make_shared<CPrimTriangle>(pShader, origin, origin, p1, p0, Vec2f(0.5f, 1), Vec2f(t1, 1), Vec2f(t0, 1)));
else add(std::make_shared<CPrimTriangle>(pShader, origin, origin, p0, p1, Vec2f(0.5f, 1), Vec2f(t0, 1), Vec2f(t1, 1)));
dir0 = dir1;
p0 = p1;
n0 = n1;
t0 = t1;
}
}
}