-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathPolyhedron.cpp
More file actions
156 lines (121 loc) · 3.74 KB
/
Copy pathPolyhedron.cpp
File metadata and controls
156 lines (121 loc) · 3.74 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
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <cpp3ds/Graphics/Polyhedron.hpp>
#include <cpp3ds/Graphics/RenderTarget.hpp>
#include <cpp3ds/Graphics/Texture.hpp>
#include <cpp3ds/System/Err.hpp>
#include <cmath>
namespace
{
// Compute the normal of a face given 2 of its edges
cpp3ds::Vector3f computeNormal(const cpp3ds::Vector3f& v1, const cpp3ds::Vector3f& v2)
{
cpp3ds::Vector3f normal(v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x);
float length = std::sqrt(normal.x * normal.x +
normal.y * normal.y +
normal.z * normal.z);
if (length != 0.f)
normal /= length;
return normal;
}
}
namespace cpp3ds
{
////////////////////////////////////////////////////////////
Polyhedron::~Polyhedron()
{
}
////////////////////////////////////////////////////////////
void Polyhedron::setColor(const Color& color)
{
m_color = color;
updateColors();
}
////////////////////////////////////////////////////////////
const Color& Polyhedron::getColor() const
{
return m_color;
}
////////////////////////////////////////////////////////////
void Polyhedron::setTexture(const Texture* texture)
{
m_texture = texture;
}
////////////////////////////////////////////////////////////
const Texture* Polyhedron::getTexture() const
{
return m_texture;
}
////////////////////////////////////////////////////////////
FloatBox Polyhedron::getLocalBounds() const
{
return m_insideBounds;
}
////////////////////////////////////////////////////////////
FloatBox Polyhedron::getGlobalBounds() const
{
return getTransform().transformBox(getLocalBounds());
}
////////////////////////////////////////////////////////////
Polyhedron::Polyhedron() :
m_texture (NULL),
m_color (Color::White),
m_vertices (Triangles),
m_insideBounds()
{
}
////////////////////////////////////////////////////////////
void Polyhedron::update() const
{
// Get the total number of faces of the polyhedron
unsigned int count = getFaceCount();
if (!count)
{
m_vertices.resize(0);
return;
}
m_vertices.resize(count * 3);
// Vertices
for (unsigned int i = 0; i < count; ++i)
{
Face face = getFace(i);
m_vertices[i * 3 + 0] = face.v0;
m_vertices[i * 3 + 1] = face.v1;
m_vertices[i * 3 + 2] = face.v2;
}
// Update the bounding rectangle
m_insideBounds = m_vertices.getBounds();
}
////////////////////////////////////////////////////////////
void Polyhedron::draw(RenderTarget& target, RenderStates states) const
{
states.transform *= getTransform();
// Render the inside
states.texture = m_texture;
target.draw(m_vertices, states);
}
////////////////////////////////////////////////////////////
void Polyhedron::generateNormals()
{
// Get the total number of faces of the polyhedron
unsigned int count = m_vertices.getVertexCount() / 3;
// Vertices
for (unsigned int i = 0; i < count; ++i)
{
Vector3f normal = computeNormal(m_vertices[i * 3 + 2].position - m_vertices[i * 3 + 1].position,
m_vertices[i * 3 + 0].position - m_vertices[i * 3 + 1].position);
m_vertices[i * 3 + 0].normal = normal;
m_vertices[i * 3 + 1].normal = normal;
m_vertices[i * 3 + 2].normal = normal;
}
}
////////////////////////////////////////////////////////////
void Polyhedron::updateColors()
{
for (unsigned int i = 0; i < m_vertices.getVertexCount(); ++i)
m_vertices[i].color = m_color;
}
} // namespace cpp3ds