-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.cpp
More file actions
406 lines (324 loc) · 9 KB
/
Copy pathmodel.cpp
File metadata and controls
406 lines (324 loc) · 9 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
#include "model.h"
#include <cmath>
#include <string>
#include <iostream>
#include <fstream>
#include <QtOpenGL/qgl.h>
#include "scene.h"
using namespace std;
Model::Model() {}
Model::Model(std::string n):name(n)
{}
Box Model::boundingBox() const
{
return _boundingBox;
}
void Model::updateBoundingBox()
{
if (vertices.size())
{
Point p = vertices[0].coord;
_boundingBox=Box(p, p);
for (unsigned int i=1; i<vertices.size(); i++)
_boundingBox.update(vertices[i].coord);
}
}
void Model::Render() {
//MaterialLib matlib;
int i,j;
for (i=0;i<(int)this->faces.size();i++) {
glBegin(GL_POLYGON);
const Material& mat = Scene::matlib.material(this->faces[i].material);
GLfloat rgba[] = { mat.kd.r, mat.kd.g, mat.kd.b, 1.0 };
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, rgba);
glNormal3f(faces[i].normal.x, faces[i].normal.y, faces[i].normal.z);
// Per a pintar del color adequat, heu de fer servir mat.kd amb els seus canals corresponents
// en comptes d'utilitzar el color per defecte que hem posat...
for(j=0;j<(int)this->faces[i].vertices.size();j++) {
glVertex3f(this->vertices[this->faces[i].vertices[j]].coord.x, this->vertices[this->faces[i].vertices[j]].coord.y, this->vertices[this->faces[i].vertices[j]].coord.z);
}
glEnd();
}
// Cal recorrer l'estructura de l'objecte per a pintar les seves cares
}
/*
Lectura d'un fitxer OBJ
Conté fragments de codi de obj_2_ply.c (C) Greg Turk
-----------------------------------------------------------------------
Copyright (c) 1998 Georgia Institute of Technology. All rights reserved.
Permission to use, copy, modify and distribute this software and its
documentation for any purpose is hereby granted without fee, provided
that the above copyright notice and this permission notice appear in
all copies of this software and that you do not sell the software.
*/
/* for file reading */
static char **words;
static int max_words = 0;
static int num_words = 0;
#define BIG_STRING 4096
static char str[BIG_STRING];
static char str_orig[BIG_STRING];
void get_indices ( char *word, int *vindex, int *tindex, int *nindex )
/******************************************************************************
GET_INDICES breaks up a word that may have slash-separated numbers into one or more
numbers.
Entry:
word - word to break up
Exit:
vindex - first number (vertex index)
tindex - second number (texture index)
nindex - third number (normal vector index)
******************************************************************************/
{
char *null = " ";
char *ptr;
char *tp;
char *np;
/* by default, the texture and normal pointers are set to the null string */
tp = null;
np = null;
/* replace slashes with null characters and cause tp and np to point */
/* to character immediately following the first or second slash */
for (ptr = word; *ptr != '\0'; ptr++) {
if (*ptr == '/') {
if (tp == null)
tp = ptr + 1;
else
np = ptr + 1;
*ptr = '\0';
}
}
*vindex = atoi (word);
*tindex = atoi (tp);
*nindex = atoi (np);
}
void Model::make_face ( char **words, int nwords, int currentMaterial, vector<Vertex> &v )
{
Face face;
for (int i = 0; i < nwords; i++)
{
int vindex;
int nindex;
int tindex;
if ((words[i][0]>='0')&&(words[i][0]<='9'))
{
get_indices (words[i], &vindex, &tindex, &nindex);
#if 0
printf ("vtn: %d %d %d\n", vindex, tindex, nindex);
#endif
/* store the vertex index */
if (vindex > 0) /* indices are from one, not zero */
face.vertices.push_back(vindex - 1);
else if (vindex < 0) /* negative indices mean count backwards */
face.vertices.push_back(vertices.size() + vindex);
else
{
fprintf (stderr, "Zero indices not allowed: '%s'\n", str_orig);
exit (-1);
}
/*
if ((tindex != 0 || nindex != 0) && warning == 0) {
fprintf (stderr, "\n");
fprintf (stderr, "Warning: textures and normals currently ignored.\n");
fprintf (stderr, "\n");
warning = 1;
}
*/
}
}
face.material = currentMaterial;
//added by pespin:
face.computeNormal(v);
faces.push_back(face);
}
char *fetch_line ( FILE *fp )
{
//int i,j;
char *ptr;
char *ptr2;
char *result;
//char *comment_ptr;
/* read in a line */
result = fgets (str, BIG_STRING, fp);
/* return NULL if we're at the end-of-file */
if (result == NULL)
return ((char *) -1);
/* convert line-feed and tabs into spaces */
/* (this guarentees that there will be a space before the */
/* null character at the end of the string) */
str[BIG_STRING-2] = ' ';
str[BIG_STRING-1] = '\0';
for (ptr = str; *ptr != '\0'; ptr++) {
if (*ptr == '\t') {
*ptr = ' ';
}
else if (*ptr == '\n') {
*ptr = ' ';
break;
}
}
/* copy the line */
for (ptr = str, ptr2 = str_orig; *ptr != '\0'; ptr++, ptr2++)
*ptr2 = *ptr;
*ptr2 = '\0';
/* look to see if this is a comment line (first non-space is '#') */
for (ptr = str; *ptr != '\0'; ptr++) {
if (*ptr == '#') {
ptr++;
while (*ptr == ' ')
ptr++;
return (ptr);
}
else if (*ptr != ' ') {
break;
}
}
/* if we get here, we've got a non-comment line */
/* strip off trailing comments */
while (*ptr != '\0') {
if (*ptr == '#') {
*ptr++ = ' ';
*ptr = '\0';
break;
}
ptr++;
}
return (NULL);
}
int fetch_words ( void )
{
char *ptr;
/* allocate room for words if necessary */
if (max_words == 0) {
max_words = 20;
words = (char **) malloc (sizeof (char *) * max_words);
}
/* find the words in the line */
ptr = str;
num_words = 0;
while (*ptr != '\0') {
/* jump over leading spaces */
while (*ptr == ' ')
ptr++;
/* break if we reach the end */
if (*ptr == '\0')
break;
/* allocate more room for words if necessary */
if (num_words >= max_words) {
max_words += 10;
words = (char **) realloc (words, sizeof (char *) * max_words);
}
/* save pointer to beginning of word */
words[num_words++] = ptr;
/* jump over non-spaces */
while (*ptr != ' ')
ptr++;
/* place a null character here to mark the end of the word */
*ptr++ = '\0';
}
/* return the number of words */
return (num_words);
}
string getPath(const string& filename)
{
int i=filename.length()-1;
bool found = false;
while (i>=0 && !found)
{
if (filename[i]=='/' || filename[i]=='\\') found=true;
else --i;
}
return filename.substr(0, i+1);
}
// llegeix tots els fitxers .mtl i els afegeix a matlib
void read_mtllib( char **words, int nwords, MaterialLib& matlib,
const string& filename )
{
string path = getPath(filename);
for (int i = 0; i < nwords; i++)
{
int size = strlen(words[i])-1;
while (size && (words[i][size]=='\n' || words[i][size]=='\r') ) words[i][size--]=0;
matlib.readMtl((path+words[i]).c_str());
}
}
// Llegeix un fitxer .obj
// Si el fitxer referencia fitxers de materials (.mtl), tambe es llegeixen.
// Tots els elements del fitxer es llegeixen com a un unic objecte.
//
void Model::readObj(const char* filename, MaterialLib& matlib)
{
int currentMaterial = -1;
FILE *fp = fopen(filename,"rb");
if (!fp)
{
cout << "No puc obrir el fitxer " << filename << endl;
}
else {
netejaDades();
while (true)
{
char *comment_ptr = fetch_line (fp);
if (comment_ptr == (char *) -1) /* end-of-file */
break;
/* did we get a comment? */
if (comment_ptr) {
//make_comment (comment_ptr);
continue;
}
/* if we get here, the line was not a comment */
int nwords = fetch_words();
/* skip empty lines */
if (nwords == 0)
continue;
char *first_word = words[0];
if (!strcmp (first_word, "v"))
{
if (nwords < 4)
{
fprintf (stderr, "Too few coordinates: '%s'", str_orig);
exit (-1);
}
float x = atof (words[1]);
float y = atof (words[2]);
float z = atof (words[3]);
if (nwords == 5)
{
float w = atof (words[4]);
x/=w;
y/=w;
z/=w;
}
//addVertex(Vertex(Point(x,y,z)));
vertices.push_back(Vertex(Point(x,y,z)));
}
else if (!strcmp (first_word, "vn")) {
}
else if (!strcmp (first_word, "vt")) {
}
else if (!strcmp (first_word, "f")) {
make_face (&words[1], nwords-1, currentMaterial, vertices);
}
// added
else if (!strcmp (first_word, "mtllib")) {
read_mtllib (&words[1], nwords-1, matlib, filename);
}
else if (!strcmp (first_word, "usemtl")) {
int size = strlen(words[1])-1;
while (size && (words[1][size]=='\n' || words[1][size]=='\r') ) words[1][size--]=0;
currentMaterial = matlib.index(words[1]);
}
// fadded
else {
//fprintf (stderr, "Do not recognize: '%s'\n", str_orig);
}
}
}
}
// Netela les dades de l'objecte per a poder carregar un nou model
void Model::netejaDades ()
{
vertices.clear();
faces.clear();
_boundingBox.init(Point());
}