-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.h
More file actions
305 lines (234 loc) · 8.53 KB
/
Model.h
File metadata and controls
305 lines (234 loc) · 8.53 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#ifndef MODEL_H_
#define MODEL_H_
#include <vector> //vector
#include <utility> //pair
#include <algorithm> //remove and swap
#include <fstream> //ifstream
#include <cassert> //assert
#include <iostream> //istream and ostream
#include <stdexcept> //runtime_error
#include <string> //string
#include "Vector4.h"
#include "Matrix44.h"
using namespace std;
/**
* @brief This class defines a model to be drawn on the screen.
* Vertices (specified as triangles) can be supplied via container or from file.
* The class also draws shadow volumes for use with the stenciled shadow volume algorithm.
*/
class Model
{
private:
typedef pair<Vector4, Vector4> Edge;
enum { x, y, z, w };
vector<Vector4> vertices;
Matrix44 globalOrientation;
Vector4 color;
//Used in computing and caching the shadow volume
mutable bool hasMoved;
mutable Vector4 previousLightPosition;
mutable vector<Vector4> normals;
mutable vector<Vector4> shadowVolumeVertices;
mutable vector<Vector4> transformedVertices;
mutable vector<Edge> edges;
public:
Model(const vector<Vector4>& vertices = vector<Vector4>(), const Matrix44& globalOrientation = Matrix44(), Vector4 color = Vector4(1.0, 1.0, 1.0, 1.0)) : vertices(vertices), globalOrientation(globalOrientation), color(color), hasMoved(true), previousLightPosition() { vertices >> *this; }
Model(const char* filePath, const Matrix44& globalOrientation = Matrix44(), Vector4 color = Vector4(1.0, 1.0, 1.0, 1.0)) : globalOrientation(globalOrientation), color(color), hasMoved(true), previousLightPosition() { filePath >> *this; }
void draw() const
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glMultMatrixf(globalOrientation);
glColor4fv(color.data());
glBegin(GL_TRIANGLES);
for(vector<Vector4>::const_iterator i = vertices.begin(), j = normals.begin(); i != vertices.end() && j != normals.end(); i += 3, j++)
{
glNormal3fv(j -> data());
glVertex4fv(i -> data());
glVertex4fv((i + 1) -> data());
glVertex4fv((i + 2) -> data());
}
glEnd();
glPopMatrix();
}
void drawShadowVolume(const Vector4& lightPosition) const
{
assert(lightPosition[w] == 1.0);
if(hasMoved || previousLightPosition != lightPosition)
{
//Color for debugging purposes
glColor4f(0.0, 1.0, 0.0, 0.5);
computeShadowVolume(lightPosition);
}
else
glColor4f(1.0, 0.0, 0.0, 0.5);
//Draw the cached shadow volume
vector<Edge>::iterator startVertexPosition = edges.begin();
vector<Vector4>::iterator startShadowVolumeVertexPosition = shadowVolumeVertices.begin();
glMatrixMode(GL_MODELVIEW);
glBegin(GL_QUAD_STRIP);
vector<Edge>::iterator i;
vector<Vector4>::iterator j;
for(i = edges.begin(), j = shadowVolumeVertices.begin(); i != edges.end() && j != shadowVolumeVertices.end(); i++, j++)
{
glVertex4fv(i -> first.data());
glVertex4fv(j -> data());
if(i != edges.end() - 1 && i -> second != (i + 1) -> first)
{
glVertex4fv(startVertexPosition -> first.data());
glVertex4fv(startShadowVolumeVertexPosition -> data());
startVertexPosition = i + 1;
startShadowVolumeVertexPosition = j + 1;
glEnd();
glBegin(GL_QUAD_STRIP);
}
}
glVertex4fv(startVertexPosition -> first.data());
glVertex4fv(startShadowVolumeVertexPosition -> data());
glEnd();
}
vector<GLfloat>::size_type getVertexCount() const { return vertices.size(); }
const Vector4& getColor() const { return color; }
const Matrix44& getOrientation() const { return globalOrientation; }
void setColor(const Vector4& color) { this -> color = color; }
friend void operator >> (const char* filePath, Model& model)
{
vector<GLfloat>::size_type vertexCount;
ifstream in(filePath);
if(in.fail())
throw runtime_error("The specified file '" + string(filePath) + "' does not exist.");
in >> vertexCount;
assert(vertexCount % 3 == 0);
model.vertices.clear();
model.normals.clear();
model.shadowVolumeVertices.clear();
model.vertices.reserve(vertexCount);
model.normals.reserve(vertexCount / 3);
model.shadowVolumeVertices.reserve(vertexCount);
for(vector<Vector4>::size_type i = 0; i < vertexCount; i++)
{
//NOTE: Can we shorten this?
Vector4 temp;
in >> temp[x];
in >> temp[y];
in >> temp[z];
in >> temp[w];
model.vertices.push_back(temp);
}
model.computeNormals();
}
friend void operator >> (const vector<Vector4>& vertices, Model& model)
{
assert(vertices.size() % 3 == 0);
model.vertices.clear();
model.normals.clear();
model.shadowVolumeVertices.clear();
model.vertices.reserve(vertices.size());
model.normals.reserve(vertices.size() / 3);
model.shadowVolumeVertices.reserve(vertices.size());
model.vertices = vertices;
model.computeNormals();
}
friend ostream& operator << (ostream& out, const Model& model)
{
out << "Vertices:\n" << endl;
for(vector<Vector4>::const_iterator i = model.vertices.begin(); i != model.vertices.end(); i++)
out << *i << "\n";
out << "\nNormals:\n" << endl;
for(vector<Vector4>::const_iterator i = model.normals.begin(); i != model.normals.end(); i++)
{
out << *i;
if(i != model.normals.end() - 1)
out << "\n";
}
return out << flush;
}
private:
void computeShadowVolume(const Vector4& lightPosition) const
{
assert(lightPosition[w] == 1.0);
if(hasMoved)
{
transformedVertices.clear();
transformedVertices = vertices;
for(vector<Vector4>::iterator i = transformedVertices.begin(); i != transformedVertices.end(); i++)
*i = globalOrientation.getTranspose() * *i;
//*i = globalOrientation * *i;
}
hasMoved = false;
previousLightPosition = lightPosition;
shadowVolumeVertices.clear();
shadowVolumeVertices.reserve(vertices.size());
edges.clear();
edges.reserve(vertices.size());
//For all triangles...
for(vector<Vector4>::iterator i = transformedVertices.begin(); i != transformedVertices.end(); i += 3)
{
//If the triangle is facing the light...
if(Vector4::dotProduct(computeNormal(*i, *(i + 1), *(i + 2)), lightPosition) > 0.0)
{
vector<Edge> triangle;
triangle.push_back(Edge(*i , *(i + 1)));
triangle.push_back(Edge(*(i + 1), *(i + 2)));
triangle.push_back(Edge(*(i + 2), *i));
for(vector<Edge>::iterator i = triangle.begin(); i != triangle.end(); i++)
{
bool found = false;
vector<Edge>::iterator removeLocation;
for(vector<Edge>::iterator j = edges.begin(); j != edges.end() && !found; j++)
if(*i == *j || *i == Edge(j -> second, j -> first))
{
found = true;
removeLocation = remove(edges.begin(), edges.end(), *j);
}
//If a duplicate edge exists, we remove all instances of the edge from our container.
if(found)
edges.erase(removeLocation, edges.end());
//Otherwise, it is a candidate for a silhouette edge.
else
edges.push_back(*i);
}
}
}
//We must sort the edges to use a quad strip.
sortEdges(edges);
for(vector<Edge>::iterator i = edges.begin(); i != edges.end(); i++)
{
//Extrude the vertex to infinity
shadowVolumeVertices.push_back((i -> first - lightPosition).normalize());
//Vertex is at infinity
assert(shadowVolumeVertices.back()[w] == 0.0);
}
}
void computeNormals() const
{
for(vector<Vector4>::const_iterator i = vertices.begin(); i != vertices.end(); i += 3)
normals.push_back(computeNormal(*i, *(i + 1), *(i + 2)));
}
static Vector4 computeNormal(const Vector4& vertex1, const Vector4& vertex2, const Vector4& vertex3)
{
assert(vertex1[w] == 1.0 && vertex2[w] == 1.0 && vertex3[w] == 1.0);
Vector4 vector4(vertex2[x] - vertex1[x], vertex2[y] - vertex1[y], vertex2[z] - vertex1[z], 0.0);
Vector4 vector5(vertex3[x] - vertex1[x], vertex3[y] - vertex1[y], vertex3[z] - vertex1[z], 0.0);
Vector4 normal = Vector4::crossProduct(vector4, vector5);
normal.normalize();
return normal;
}
static void sortEdges(vector<Edge>& edges)
{
bool swapped;
for(vector<Edge>::iterator i = edges.begin(); i != edges.end() - 1; i++)
{
swapped = false;
for(vector<Edge>::iterator j = i + 1; j != edges.end() && !swapped; j++)
if(i -> second == j -> first || i -> second == j -> second)
{
if(i -> second == j -> second)
swap(j -> first, j -> second);
swap(*(i + 1), *j);
swapped = true;
}
}
}
};
#endif /*MODEL_H_*/