[go: up one dir, main page]

Menu

[r9]: / src / obj.cc  Maximize  Restore  History

Download this file

328 lines (257 with data), 9.2 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
#include "nitrito.h"
#include "stdio.h"
/*-----------------------------------------------------------------//
// ObjModel Class //
// */
ObjModel::ObjModel() {
//Set everything to zero and empty...
ObjModel::NumNormal = 0;
ObjModel::NumTexCoord = 0;
ObjModel::NumTriangle = 0;
ObjModel::NumVertex = 0;
ObjModel::NormalArray = NULL;
ObjModel::TexCoordArray = NULL;
ObjModel::TriangleArray = NULL;
ObjModel::VertexArray = NULL;
}
ObjModel::~ObjModel() {
//free up our data
delete[] NormalArray;
delete[] TexCoordArray;
delete[] TriangleArray;
delete[] VertexArray;
}
ObjModel::ObjModel(const ObjModel &copy) {
int i;
//make room for the new data
NormalArray = new ObjNormal[copy.NumNormal];
TexCoordArray = new ObjTexCoord[copy.NumTexCoord];
TriangleArray = new ObjTriangle[copy.NumTriangle];
VertexArray = new ObjVertex[copy.NumVertex];
NumNormal = copy.NumNormal;
NumTexCoord = copy.NumTexCoord;
NumTriangle = copy.NumTriangle;
NumVertex = copy.NumVertex;
//Grab data from the copy to the current class
for(i = 0; i < NumNormal; i++)
NormalArray[i] = copy.NormalArray[i];
for(i = 0; i < NumTexCoord; i++)
TexCoordArray[i] = copy.TexCoordArray[i];
for(i = 0; i < NumTriangle; i++)
TriangleArray[i] = copy.TriangleArray[i];
for(i = 0; i < NumVertex; i++)
VertexArray[i] = copy.VertexArray[i];
//good to go...
}
ObjModel& ObjModel::operator=(const ObjModel &right) {
int i;
//free current data if we have it
delete [] NormalArray;
delete [] TexCoordArray;
delete [] TriangleArray;
delete [] VertexArray;
//make room for the new data
NormalArray = new ObjNormal[right.NumNormal];
TexCoordArray = new ObjTexCoord[right.NumTexCoord];
TriangleArray = new ObjTriangle[right.NumTriangle];
VertexArray = new ObjVertex[right.NumVertex];
NumNormal = right.NumNormal;
NumTexCoord = right.NumTexCoord;
NumTriangle = right.NumTriangle;
NumVertex = right.NumVertex;
//Grab data from the copy to the current class
for(i = 0; i < NumNormal; i++)
NormalArray[i] = right.NormalArray[i];
for(i = 0; i < NumTexCoord; i++)
TexCoordArray[i] = right.TexCoordArray[i];
for(i = 0; i < NumTriangle; i++)
TriangleArray[i] = right.TriangleArray[i];
for(i = 0; i < NumVertex; i++)
VertexArray[i] = right.VertexArray[i];
//good to go...
return *this;
}
/* //
//-----------------------------------------------------------------*/
/*-----------------------------------------------------------------//
// ObjLoader Class //
// */
ObjLoader::ObjLoader() {
fileName = NULL;
theObj = NULL;
}
ObjLoader::~ObjLoader() {
FreeObj();
}
void ObjLoader::FreeObj(void) {
if(fileName != NULL) delete fileName;
if(theObj != NULL) delete theObj;
}
ObjModel ObjLoader::ReturnObj(void) {
ObjModel ret(*theObj);
return ret;
}
ObjLoader::ObjLoader(string file) {
fileName = new string(file);
theObj = new ObjModel();
ReadData();
}
void ObjLoader::LoadObj(string file) {
FreeObj();
fileName = new string(file);
theObj = new ObjModel();
ReadData();
}
void ObjLoader::ReadData(void) {
int i;
string buffer;
int texture=0;
ZZIP_FILE *file;
char buff[256000];
int filesize;
file = zzip_open(fileName->c_str(), 0);
if(file == NULL) {
console.printf("Error al cargar %s.",fileName->c_str());
exit(1);
}
filesize = zzip_read(file, buff, 256000);
zzip_close(file);
string objdata(buff, filesize);
istringstream input(objdata);
istringstream input2(objdata);
//ifstream input(fileName->c_str());
//make sure we got the file opened up ok...
//if( !input.is_open() )
// return;
//Setup our ObjModel arrays...
//read one line at a time of the file...
while( !input2.eof() ) {
getline(input2, buffer);
if(buffer.substr(0,2) == "vn")
theObj->NumNormal++;
else if(buffer.substr(0,2) == "vt")
theObj->NumTexCoord++;
else if(buffer.substr(0,1) == "v")
theObj->NumVertex++;
else if(buffer.substr(0,1) == "f")
theObj->NumTriangle++;
}
//Make the arrays the right size...
theObj->NormalArray = new ObjNormal[theObj->NumNormal];
theObj->TexCoordArray = new ObjTexCoord[theObj->NumTexCoord];
theObj->TriangleArray = new ObjTriangle[theObj->NumTriangle];
theObj->VertexArray = new ObjVertex[theObj->NumVertex];
//close the file...
//input.close();
//reopen it...
//input.open(fileName->c_str());
//input.seekg(0);
//input.clear();
//make sure we got the file opened up ok...
//if( !input.is_open() )
// return;
//hear are the counters...
int nC, vC, tC, fC;
nC = vC = tC = fC = 0;
//get the hard data, and load up our arrays...
//read one line at a time of the file...
while( !input.eof() ) {
getline(input, buffer);
istringstream line(buffer);
string temp;
string f1, f2, f3;
if(buffer.substr(0,2) == "vn") {
line >> temp >> f1 >> f2 >> f3;
theObj->NormalArray[nC].X = atof(f1.c_str());
theObj->NormalArray[nC].Y = atof(f2.c_str());
theObj->NormalArray[nC].Z = atof(f3.c_str());
//sscanf(buffer.c_str(), "vn %f %f %f", theObj->NormalArray[nC].X,
// theObj->NormalArray[nC].Y, theObj->NormalArray[nC].Z);
nC++;
}
else if(buffer.substr(0,2) == "vt") {
line >> temp >> f1 >> f2;
theObj->TexCoordArray[tC].U = atof(f1.c_str());
theObj->TexCoordArray[tC].V = atof(f2.c_str());
//sscanf(buffer.c_str(), "vt %f %f", theObj->TexCoordArray[tC].U,
// theObj->TexCoordArray[tC].V);
tC++;
}
else if(buffer.substr(0,1) == "v") {
line >> temp >> f1 >> f2 >> f3;
theObj->VertexArray[vC].X = atof(f1.c_str());
theObj->VertexArray[vC].Y = atof(f2.c_str());
theObj->VertexArray[vC].Z = atof(f3.c_str());
//sscanf(buffer.c_str(), "v %f %f %f", theObj->VertexArray[vC].X,
// theObj->VertexArray[vC].Y, theObj->VertexArray[vC].Z);
vC++;
}
else if(buffer.substr(0,1) == "f") {
line >> temp >> f1 >> f2 >> f3;
unsigned int sPos = 0;
unsigned int ePos = sPos;
string temp;
ePos = f1.find_first_of("/");
//we have a line with the format of "f %d/%d/%d %d/%d/%d %d/%d/%d"
if(ePos != string::npos) {
temp = f1.substr(sPos, ePos - sPos);
theObj->TriangleArray[fC].Vertex[0] = atoi(temp.c_str()) - 1;
sPos = ePos+1;
ePos = f1.find("/", sPos);
temp = f1.substr(sPos, ePos - sPos);
theObj->TriangleArray[fC].TexCoord[0] = atoi(temp.c_str()) - 1;
sPos = ePos+1;
ePos = f1.length();
temp = f1.substr(sPos, ePos - sPos);
theObj->TriangleArray[fC].Normal[0] = atoi(temp.c_str()) - 1;
}
sPos = 0;
ePos = f2.find_first_of("/");
//we have a line with the format of "f %d/%d/%d %d/%d/%d %d/%d/%d"
if(ePos != string::npos) {
temp = f2.substr(sPos, ePos - sPos);
theObj->TriangleArray[fC].Vertex[1] = atoi(temp.c_str()) - 1;
sPos = ePos + 1;
ePos = f2.find("/", sPos+1);
temp = f2.substr(sPos, ePos - sPos);
theObj->TriangleArray[fC].TexCoord[1] = atoi(temp.c_str()) - 1;
sPos = ePos + 1;
ePos = f2.length();
temp = f2.substr(sPos, ePos - sPos);
theObj->TriangleArray[fC].Normal[1] = atoi(temp.c_str()) - 1;
}
sPos = 0;
ePos = f3.find_first_of("/");
//we have a line with the format of "f %d/%d/%d %d/%d/%d %d/%d/%d"
if(ePos != string::npos) {
temp = f3.substr(sPos, ePos - sPos);
theObj->TriangleArray[fC].Vertex[2] = atoi(temp.c_str()) - 1;
sPos = ePos + 1;
ePos = f3.find("/", sPos+1);
temp = f3.substr(sPos, ePos - sPos);
theObj->TriangleArray[fC].TexCoord[2] = atoi(temp.c_str()) - 1;
sPos = ePos + 1;
ePos = f3.length();
temp = f3.substr(sPos, ePos - sPos);
theObj->TriangleArray[fC].Normal[2] = atoi(temp.c_str()) - 1;
}
//sscanf(buffer.c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d",
// theObj->TriangleArray[fC].Vertex[0], theObj->TriangleArray[fC].TexCoord[0], theObj->TriangleArray[fC].Normal[0],
// theObj->TriangleArray[fC].Vertex[1], theObj->TriangleArray[fC].TexCoord[1], theObj->TriangleArray[fC].Normal[1],
// theObj->TriangleArray[fC].Vertex[2], theObj->TriangleArray[fC].TexCoord[2], theObj->TriangleArray[fC].Normal[2]);
theObj->TriangleArray[fC].Texture=texture;
fC++;
}
else if(buffer.substr(0,6) == "usemat" || buffer.substr(0,6) == "usemtl")
{
for(i = 0; i<num_textures ; i++)
{
if(buffer.substr(7,12) == tex_name[i]) texture = i;
if(buffer.substr(12,17) == tex_name[i]) texture = i;
}
}
}
//all should be good
}
/* //
//-----------------------------------------------------------------*/