-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdobj.cpp
More file actions
489 lines (399 loc) · 8.38 KB
/
dobj.cpp
File metadata and controls
489 lines (399 loc) · 8.38 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
* dobj.cpp
* proj3
*
* Created by Michael Atwood on 6/1/08.
* Copyright 2008. All rights reserved.
*
*/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "dobj.h"
#include "matrix.h"
dobj::dobj(): radius(0.0)
{
isLoaded = false;
//put a 'placeholder' vert in the list b/c the d files start with 1
vert3* v = new vert3(0.0,0.0,0.0);
verts.push_back(v);
centroid.x=0.0;
centroid.y=0.0;
centroid.z=0.0;
//bronze
material_Ka[0] = 0.19125f;
material_Ka[1] = 0.0735f;
material_Ka[2] = 0.0225f;
material_Ka[3] = 1.0f;
material_Kd[0] = 0.7038f;
material_Kd[1] = 0.27048f;
material_Kd[2] = 0.0828f;
material_Kd[3] = 1.0f;
material_Ks[0] = 0.256777f;
material_Ks[1] = 0.137622f;
material_Ks[2] = 0.086014f;
material_Ks[3] = 1.0f;
material_Se = 12.8f;
//chrome
/*material_Ka[0] = 0.25f;
material_Ka[1] = 0.25f;
material_Ka[2] = 0.25f;
material_Ka[3] = 1.0f;
material_Kd[0] = 0.4f;
material_Kd[1] = 0.4f;
material_Kd[2] = 0.4f;
material_Kd[3] = 1.0f;
material_Ks[0] = 0.774597f;
material_Ks[1] = 0.774597f;
material_Ks[2] = 0.774597f;
material_Ks[3] = 1.0f;
material_Se = 76.8f;*/
//polished silver
/*material_Ka[0] = 0.23125f;
material_Ka[1] = 0.23125f;
material_Ka[2] = 0.23125f;
material_Ka[3] = 1.0f;
material_Kd[0] = 0.2775f;
material_Kd[1] = 0.2775f;
material_Kd[2] = 0.2775f;
material_Kd[3] = 1.0f;
material_Ks[0] = 0.773911f;
material_Ks[1] = 0.773911f;
material_Ks[2] = 0.773911f;
material_Ks[3] = 1.0f;
material_Se = 89.6f;*/
}
dobj::~dobj()
{
//clean up the memory used by the verts
while(verts.size() > 0)
{
vert3* v = verts.back();
verts.pop_back();
delete(v);
}
}
/**
* Function that draws this .d obj
*/
void dobj::draw(bool fSmooth)
{
if(isLoaded)
{
//use the same material for all the faces
glMaterialfv(GL_FRONT, GL_AMBIENT, material_Ka);
glMaterialfv(GL_FRONT, GL_DIFFUSE, material_Kd);
glMaterialfv(GL_FRONT, GL_SPECULAR, material_Ks);
glMaterialf(GL_FRONT, GL_SHININESS, material_Se);
if(fSmooth)
{
for(int i = 0; i < faces.size(); i++)
{
glBegin(GL_POLYGON);
list<int> face = faces[i];
list<int>::iterator iter = face.begin();
while(iter != face.end())
{
vert3* v = verts[*iter];
vec3 vnorm = vert_norms[*iter];
glNormal3d(vnorm.x, vnorm.y, vnorm.z);
glVertex3d(v->x, v->y, v->z);
iter++;
}
glEnd();
}
}
else
{
for(int i = 0; i < faces.size(); i++)
{
glBegin(GL_POLYGON);
//get the face normal
list<int> face = faces[i];
vec3 norm = face_norm[i];
glNormal3d(norm.x, norm.y, norm.z);
list<int>::iterator iter = face.begin();
while(iter != face.end())
{
vert3* v = verts[*iter];
glVertex3d(v->x, v->y, v->z);
iter++;
}
glEnd();
}
}
}
}
/**
* Function that draws this .d obj
*/
void dobj::drawVerts()
{
if(isLoaded)
{
glBegin(GL_POINTS);
glMaterialfv(GL_FRONT, GL_AMBIENT, material_Ka);
glMaterialfv(GL_FRONT, GL_DIFFUSE, material_Kd);
glMaterialfv(GL_FRONT, GL_SPECULAR, material_Ks);
for(int i = 1; i < verts.size(); i++)
{
vert3* v = verts[i];
glVertex3d(v->x, v->y, v->z);
}
glEnd();
}
}
/**
* Loads an object from file
* probably need to move this to a 'builder' class
*/
void dobj::loadObj(const string& path)
{
string line;
//open the file for reading
ifstream dfile (path.c_str());
if(dfile.is_open())
{
if(verifyObjFile(dfile))
{
loadVerts(dfile);
loadFaces(dfile);
dfile.close();
loadMetadata();
isLoaded = true;
}
else
{
cout << "File does not appear to be a .d obj file." << endl;
}
}
else
{
//there was some error opening the file
cout << "Error opening the .d file: " << path.c_str() << endl;
}
}
/**
* Function loads the verts from the .d file into internal memory structures
* It assumes that the file point is sitting on the line of the first verts
*/
void dobj::loadVerts(ifstream& objfile)
{
string line;
for(int i = 0; i < num_verts; i++)
{
getline(objfile, line, '\n');
if(!line.empty())
{
istringstream buf(line);
GLdouble x, y, z;
buf >> x;
buf >> y;
buf >> z;
vert3* v = new vert3(x, y, z);
verts.push_back(v);
}
}
}
/**
* Adds a vertex to the list
*/
void dobj::addVertex(GLdouble x, GLdouble y, GLdouble z)
{
vert3* v = new vert3(x, y, z);
verts.push_back(v);
}
/**
* Function loads the faces from the .d file. It assumes
* that the file point is sitting at the first face line
*/
void dobj::loadFaces(ifstream& objfile)
{
string line;
vector<list<int> > vert_face (verts.size());
for(int i = 0; i < num_faces; i++)
{
getline(objfile, line, '\n');
if(!line.empty())
{
list<int> face;
istringstream buf(line);
int numvals;
buf >> numvals;
for(int j = 0; j < numvals; j++)
{
int temp;
buf >> temp;
face.push_back(temp);
//add the face to the vert list
vert_face[temp].push_back(i);
}
//just grab the first 3 verts to gen the face normal
list<int>::iterator iter = face.begin();
vert3* v1 = verts[*iter]; iter++;
vert3* v2 = verts[*iter]; iter++;
vert3* v3 = verts[*iter];
vec3 norm = calc_face_norm(*v3, *v2, *v1);
norm.normalize();
face_norm.push_back(norm);
faces.push_back(face);
}
}
//at this point we should have a list of verts and the faces they belong to
//lets avg the normals
for(int i = 0; i < vert_face.size(); i++)
{
GLdouble x, y, z;
x = 0.0;
y = 0.0;
z = 0.0;
//cout << "Vert: " << i << " Faces:";
list<int> face_list = vert_face[i];
list<int>::iterator iter = face_list.begin();
while(iter != face_list.end())
{
vec3 fvec = face_norm[*iter];
x += fvec.x;
y += fvec.y;
z += fvec.z;
//cout << " " << *iter;
iter++;
}
cout << endl;
vec3 v(x, y, z);
//v.toString();
v.normalize();
vert_norms.push_back(v);
}
}
/**
* Adds a face to the list
*/
void dobj::addFace(list<int> face)
{
faces.push_back(face);
}
/**
* Calculates/loads additional object metadata
*/
void dobj::loadMetadata()
{
calcCentroid();
for(int i = 1; i < verts.size(); i++)
{
vert3* v = verts[i];
vec3 vec = centroid - *v;
GLdouble mag = vec.magnitude();
if(radius < mag)
{
radius = mag;
}
}
}
/**
* Calculates the centroid of the object
*/
void dobj::calcCentroid()
{
GLdouble sumx, sumy, sumz;
sumx=0.0;
sumy=0.0;
sumz=0.0;
for(int i = 1; i < verts.size(); i++)
{
vert3* v = verts[i];
sumx+=v->x;
sumy+=v->y;
sumz+=v->z;
}
centroid.x = sumx/num_verts;
centroid.y = sumy/num_verts;
centroid.z = sumz/num_verts;
}
/**
* Verifies that the file is a .d obj file and loads the num_verts and num_faces vars
*/
bool dobj::verifyObjFile(ifstream& objfile)
{
//read the header information
string hline;
getline(objfile, hline);
if(hline.empty()) {return false;}
istringstream buf;
buf.str(hline);
string head;
buf >> head >> num_verts >> num_faces;
//buf >> num_verts;
//buf >> num_faces;
return true;
}
/**
* Saves the object in memory to a .d file format
*/
void dobj::saveObj(const string& path)
{
//open the file for reading
ofstream dfile;
dfile.open(path.c_str());
dfile << "data " << verts.size() - 1 << " " << faces.size() << endl;
for(int i = 1; i < verts.size(); i++)
{
vert3* v = verts[i];
dfile << v->x << " " << v->y << " " << v->z << endl;
}
for(int i = 0; i < faces.size(); i++)
{
list<int> face = faces[i];
dfile << face.size();
list<int>::iterator iter = face.begin();
while(iter != face.end())
{
dfile << " " << *iter;
iter++;
}
dfile << endl;
}
dfile.close();
}
/**
* Debugging function outputs the loaded verts
*/
void dobj::printVerts()
{
for(int i = 0; i < verts.size(); i++)
{
vert3* v = verts[i];
cout << "X:" << v->x
<< " Y:" << v->y
<< " Z:" << v->z << endl;
}
}
/**
* Debugging function outputs the loaded faces
*/
void dobj::printFaces()
{
for(int i = 0; i < faces.size(); i++)
{
list<int> l = faces[i];
cout << "Face size:" << l.size() << " Verts: ";
list<int>::iterator iter = l.begin();
while(iter != l.end())
{
cout << *iter << " ";
iter++;
}
cout << endl;
}
}
/**
* Debugging function prints out obj metadata
*/
void dobj::printMetadata()
{
cout << "Centroid: x=" << centroid.x
<< " y=" << centroid.y
<< " z=" << centroid.z
<< " Radius:" << radius << endl;
}