-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOBJObject.cpp
More file actions
334 lines (261 loc) · 7.5 KB
/
OBJObject.cpp
File metadata and controls
334 lines (261 loc) · 7.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "OBJObject.h"
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "Window.h"
#include "math.h"
#include <sstream>
#include <fstream>
#include "Globals.h"
#define deleteVector(__type__, __vect__) do {\
std::vector<__type__>::iterator iter; \
std::vector<__type__>::iterator end; \
iter = __vect__->begin();\
end = __vect__->end();\
while(iter != end) delete (*(iter++));\
delete __vect__;\
} while(false)
OBJObject::OBJObject(std::string filename) : Drawable()
{
this->faces = new std::vector<Face>();
this->vertices = new std::vector<Vector3>();
this->colors = new std::vector<Vector3>();
this->normals = new std::vector<Vector3>();
this->all = new std::vector<Vector3>();
parse(filename);
//Translating the OBJObject to the origin
float xCenter = (maxX + minX) / 2.0;
float yCenter = (maxY + minY) / 2.0;
float zCenter = (maxZ + minZ) / 2.0;
float size = 40.0 / sqrt(3) / (maxX - minX);
Matrix4 translate;
translate.makeTranslate(-xCenter, -yCenter, -zCenter);
toWorld = translate * toWorld;
Matrix4 scale;
if (filename == "bunny.obj") { //screenWidth/objectWidth = 40/sqrt(3)/(maxX-minX)
scale.makeScale(16.37/20.0*size);
}
else if (filename == "bear.obj") {
scale.makeScale(15.25/20.0*size);
}
else if (filename == "dragon.obj") {
scale.makeScale(18.44/20.0*size);
}
toWorld = scale * toWorld;
startMatrix = toWorld;
}
OBJObject::~OBJObject()
{
//No pointers = no deletions?
}
int OBJObject::getSize(){
return (int)all->size();
}
void OBJObject::draw(DrawData& data)
{
glEnable(GL_LIGHTING);
material.apply();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glMultMatrixf(toWorld.ptr());
glBegin(GL_TRIANGLES);
for (int i = 0; i < (int)all->size(); i += 3) {
//Original implementation
//glColor3f((*all)[i][0], (*all)[i][1], (*all)[i][2]);
glNormal3f((*all)[i+1][0], (*all)[i+1][1], (*all)[i+1][2]);
glVertex3f((*all)[i + 2][0], (*all)[i + 2][1], (*all)[i + 2][2]);
}
glEnd();
glPopMatrix();
}
void OBJObject::update(UpdateData& data)
{
//
}
void OBJObject::parse(std::string& filename)
{
int lineNum = 0;
std::cout << "Starting parse..." << std::endl;
FILE* fp; // file pointer
float x, y, z; // vertex coordinates
float n0, n1, n2; // normal coordinates
float r, g, b; // vertex color
int c1, c2; // characters read from file
int xn, yn, zn; // normal indicies
int xv, yv, zv; // vertex indicies
int count;
char ignore[256];
fp = fopen(filename.c_str(), "rb");
if (fp == NULL)
{
std::cerr << "error loading file" << std::endl; exit(-1);
}
while ((c1 = fgetc(fp)) != EOF) {
c2 = fgetc(fp);
//Progress
if (++lineNum % 100000 == 0)
std::cout << "At line " << lineNum << std::endl;
//Vertex case
if ((c1 == 'v') && (c2 == ' ')) {
count = fscanf(fp, "%f %f %f %f %f %f \n", &x, &y, &z, &r, &g, &b);
if (count != 6 && count != 3)
break;
//stuff it into the vector
vertices->push_back(Vector3(x,y,z));
if (count == 3) {
colors->push_back(Vector3(.8,.8,.8));
}
else {
colors->push_back(Vector3(r,g,b));
}
}
//Normal case
else if ((c1 == 'v') && (c2 == 'n')) {
count = fscanf(fp, "%f %f %f \n", &n0, &n1, &n2);
if (count != 3)
break;
float mag = sqrt(n0 * n0 + n1 * n1 + n2 * n2);
normals->push_back((Vector3(n0 / mag, n1 / mag, n2 / mag)));
}
//Face case
else if ((c1 == 'f') && (c2 == ' ')) {
count = fscanf(fp, "%d//%d %d//%d %d//%d \n", &xv, &xn, &yv, &yn, &zv, &zn);
if (count != 6)
break;
Face* face = new Face;
face->vertexIndices[0] = xv - 1;
face->vertexIndices[1] = yv - 1;
face->vertexIndices[2] = zv - 1;
face->normalIndices[0] = xn - 1;
face->normalIndices[1] = yn - 1;
face->normalIndices[2] = zn - 1;
faces->push_back(*face);
}
//Ignores blank and commented lines
else {
fgets(ignore, 256, fp);
}
}
fclose(fp); // make sure you don't forget to close the file when done
//Transferring data to more optimal format
for (int i = 0; i < (int)faces->size(); i++) {
Face face = faces->at(i);
int vIndex0 = face.vertexIndices[0];
int vIndex1 = face.vertexIndices[1];
int vIndex2 = face.vertexIndices[2];
int nIndex0 = face.normalIndices[0];
int nIndex1 = face.normalIndices[1];
int nIndex2 = face.normalIndices[2];
for (int j = 0; j < 3; j++) {
float x = (*vertices)[face.vertexIndices[j]][0];
float y = (*vertices)[face.vertexIndices[j]][1];
float z = (*vertices)[face.vertexIndices[j]][2];
//Updating min and max values of vertices
if (i == 0) {
maxX = x;
minX = x;
maxY = y;
minY = y;
maxZ = z;
minZ = z;
}
else {
if (x > maxX) { maxX = x; }
else if (x < minX) { minX = x; }
if (y > maxY) { maxY = y; }
else if (y < minY) { minY = y; }
if (z > maxZ) { maxZ = z; }
else if (z < minZ) { minZ = z; }
}
}
all->push_back((*colors)[vIndex0]);
all->push_back((*normals)[nIndex0]);
all->push_back((*vertices)[vIndex0]);
all->push_back((*colors)[vIndex1]);
all->push_back((*normals)[nIndex1]);
all->push_back((*vertices)[vIndex1]);
all->push_back((*colors)[vIndex2]);
all->push_back((*normals)[nIndex2]);
all->push_back((*vertices)[vIndex2]);
}
std::cout << "Done parsing." << std::endl;
}
//Split functions from the interwebs
//http://stackoverflow.com/questions/236129/split-a-string-in-c
std::vector<std::string>& OBJObject::split(const std::string &s, char delim, std::vector<std::string> &elems)
{
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
std::vector<std::string> OBJObject::split(const std::string &s, char delim)
{
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
void OBJObject::left(float dist) {
Matrix4 translation;
translation.makeTranslate(dist, 0, 0);
toWorld = translation * toWorld;
}
void OBJObject::right(float dist) {
Matrix4 translation;
translation.makeTranslate(-dist, 0, 0);
toWorld = translation * toWorld;
}
void OBJObject::up(float dist) {
Matrix4 translation;
translation.makeTranslate(0, dist, 0);
toWorld = translation * toWorld;
}
void OBJObject::down(float dist) {
Matrix4 translation;
translation.makeTranslate(0, -dist, 0);
toWorld = translation*toWorld;
}
void OBJObject::in(float dist) {
Matrix4 translation;
translation.makeTranslate(0, 0, -dist);
toWorld = translation*toWorld;
}
void OBJObject::out(float dist) {
Matrix4 translation;
translation.makeTranslate(0, 0, dist);
toWorld = translation*toWorld;
}
void OBJObject::reset() {
//Push a save state onto the matrix stack, and multiply in the toWorld matrix
toWorld = startMatrix;
}
void OBJObject::scaleUp() {
Matrix4 scale;
scale.makeScale(1.1);
toWorld = scale * toWorld;
}
void OBJObject::scaleDown() {
Matrix4 scale;
scale.makeScale(.9);
toWorld = scale * toWorld;
}
void OBJObject::orbit(float angle) {
Matrix4 rotate;
rotate.makeRotateZ(angle);
toWorld = rotate * toWorld;
}
void OBJObject::rotate(Vector3 axis, float angle) {
Matrix4 rotate;
rotate.makeRotateArbitrary(axis, angle);
toWorld = rotate * toWorld;
}
void OBJObject::translate(Vector3 direction) {
Matrix4 translation;
translation.makeTranslate(direction[0], direction[1], direction[2]);
toWorld = translation*toWorld;
}