1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #include "object.h" |
---|
17 | |
---|
18 | Object::Object () |
---|
19 | { |
---|
20 | |
---|
21 | initialize(); |
---|
22 | |
---|
23 | importFile ("reaphigh.obj"); |
---|
24 | |
---|
25 | finalize(); |
---|
26 | } |
---|
27 | |
---|
28 | Object::Object(char* fileName) |
---|
29 | { |
---|
30 | initialize(); |
---|
31 | |
---|
32 | importFile (fileName); |
---|
33 | |
---|
34 | finalize(); |
---|
35 | } |
---|
36 | |
---|
37 | Object::Object(char* fileName, float scaling) |
---|
38 | { |
---|
39 | initialize(); |
---|
40 | scaleFactor = scaling; |
---|
41 | |
---|
42 | importFile (fileName); |
---|
43 | |
---|
44 | finalize(); |
---|
45 | } |
---|
46 | |
---|
47 | bool Object::initialize (void) |
---|
48 | { |
---|
49 | if (verbose >=3) |
---|
50 | printf("new 3D-Object is being created\n"); |
---|
51 | faceMode = -1; |
---|
52 | if ( (listNumber = glGenLists(1)) == 0 ) |
---|
53 | { |
---|
54 | printf ("list could not be created for this Object\n"); |
---|
55 | return false; |
---|
56 | } |
---|
57 | |
---|
58 | mtlFileName = ""; |
---|
59 | scaleFactor = 1; |
---|
60 | vertices = new Array(); |
---|
61 | normals = new Array(); |
---|
62 | vTexture = new Array(); |
---|
63 | |
---|
64 | glNewList (listNumber, GL_COMPILE); |
---|
65 | glEnableClientState (GL_VERTEX_ARRAY); |
---|
66 | glEnableClientState (GL_NORMAL_ARRAY); |
---|
67 | // glEnableClientState (GL_TEXTURE_COORD_ARRAY); |
---|
68 | |
---|
69 | return true; |
---|
70 | } |
---|
71 | |
---|
72 | bool Object::importFile (char* fileName) |
---|
73 | { |
---|
74 | if (verbose >=3) |
---|
75 | printf("preparing to read in file: %s\n", fileName); |
---|
76 | objFileName = fileName; |
---|
77 | this->readFromObjFile (objFileName); |
---|
78 | return true; |
---|
79 | } |
---|
80 | |
---|
81 | bool Object::finalize(void) |
---|
82 | { |
---|
83 | if (verbose >=3) |
---|
84 | printf("finalizing the 3D-Object\n"); |
---|
85 | OBJ_FILE->close(); |
---|
86 | glEnd(); |
---|
87 | glEndList(); |
---|
88 | return true; |
---|
89 | } |
---|
90 | |
---|
91 | void Object::draw (void) |
---|
92 | { |
---|
93 | if (verbose >=3) |
---|
94 | printf("drawing the 3D-Object\n"); |
---|
95 | glCallList (listNumber); |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | bool Object::readFromObjFile (char* fileName) |
---|
100 | { |
---|
101 | OBJ_FILE = new ifstream(fileName); |
---|
102 | if (!OBJ_FILE->is_open()) |
---|
103 | { |
---|
104 | if (verbose >=1) |
---|
105 | printf ("unable to open .OBJ file: %s\n Loading Box Object instead.\n", fileName); |
---|
106 | BoxObject(); |
---|
107 | return false; |
---|
108 | } |
---|
109 | objFileName = fileName; |
---|
110 | char Buffer[500]; |
---|
111 | while(!OBJ_FILE->eof()) |
---|
112 | { |
---|
113 | OBJ_FILE->getline(Buffer, 500); |
---|
114 | if (verbose >=4) |
---|
115 | printf ("Read input line: %s\n",Buffer); |
---|
116 | |
---|
117 | |
---|
118 | // case vertice |
---|
119 | if (!strncmp(Buffer, "v ", 2)) |
---|
120 | { |
---|
121 | readVertex(Buffer+2); |
---|
122 | } |
---|
123 | |
---|
124 | // case face |
---|
125 | else if (!strncmp(Buffer, "f ", 2)) |
---|
126 | { |
---|
127 | readFace (Buffer+2); |
---|
128 | } |
---|
129 | |
---|
130 | else if (!strncmp(Buffer, "mtllib", 6)) |
---|
131 | { |
---|
132 | readMtlLib (Buffer+7); |
---|
133 | } |
---|
134 | |
---|
135 | else if (!strncmp(Buffer, "usemtl", 6)) |
---|
136 | { |
---|
137 | readUseMtl (Buffer+7); |
---|
138 | } |
---|
139 | |
---|
140 | // case VertexNormal |
---|
141 | else if (!strncmp(Buffer, "vn ", 2)) |
---|
142 | { |
---|
143 | readVertexNormal(Buffer+3); |
---|
144 | } |
---|
145 | |
---|
146 | // case vt |
---|
147 | else if (!strncmp(Buffer, "vt ", 2)) |
---|
148 | { |
---|
149 | readVertexTexture(Buffer+3); |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | |
---|
157 | |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | bool Object::readVertex (char* vertexString) |
---|
162 | { |
---|
163 | readVertices = true; |
---|
164 | char subbuffer1[20]; |
---|
165 | char subbuffer2[20]; |
---|
166 | char subbuffer3[20]; |
---|
167 | sscanf (vertexString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3); |
---|
168 | if (verbose >= 3) |
---|
169 | printf ("reading in a vertex: %s %s %s\n", subbuffer1, subbuffer2, subbuffer3); |
---|
170 | vertices->addEntry(atof(subbuffer1)*scaleFactor, atof(subbuffer2)*scaleFactor, atof(subbuffer3)*scaleFactor); |
---|
171 | return true; |
---|
172 | } |
---|
173 | |
---|
174 | bool Object::readFace (char* faceString) |
---|
175 | { |
---|
176 | if (readVertices == true) |
---|
177 | { |
---|
178 | vertices->finalizeArray(); |
---|
179 | glVertexPointer(3, GL_FLOAT, 0, vertices->getArray()); |
---|
180 | normals->finalizeArray(); |
---|
181 | glNormalPointer(GL_FLOAT, 0, normals->getArray()); |
---|
182 | vTexture->finalizeArray(); |
---|
183 | } |
---|
184 | |
---|
185 | readVertices = false; |
---|
186 | char subbuffer1[20]; |
---|
187 | char subbuffer2[20]; |
---|
188 | char subbuffer3[20]; |
---|
189 | char subbuffer4[20] =""; |
---|
190 | sscanf (faceString, "%s %s %s %s", subbuffer1, subbuffer2, subbuffer3, subbuffer4); |
---|
191 | if (!strcmp(subbuffer4, "")) |
---|
192 | { |
---|
193 | if (faceMode != 3) |
---|
194 | { |
---|
195 | if (faceMode != -1) |
---|
196 | glEnd(); |
---|
197 | glBegin(GL_TRIANGLES); |
---|
198 | } |
---|
199 | |
---|
200 | faceMode = 3; |
---|
201 | if (verbose >=3) |
---|
202 | printf ("found triag: %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3); |
---|
203 | addGLElement(subbuffer1); |
---|
204 | addGLElement(subbuffer2); |
---|
205 | addGLElement(subbuffer3); |
---|
206 | return true; |
---|
207 | } |
---|
208 | else |
---|
209 | { |
---|
210 | if (faceMode != 4) |
---|
211 | { |
---|
212 | if (faceMode != -1) |
---|
213 | glEnd(); |
---|
214 | glBegin(GL_QUADS); |
---|
215 | } |
---|
216 | faceMode = 4; |
---|
217 | if (verbose >=3 ) |
---|
218 | printf ("found quad: %s, %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3, subbuffer4); |
---|
219 | addGLElement(subbuffer1); |
---|
220 | addGLElement(subbuffer2); |
---|
221 | addGLElement(subbuffer3); |
---|
222 | addGLElement(subbuffer4); |
---|
223 | return true; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | bool Object::addGLElement (char* elementString) |
---|
228 | { |
---|
229 | if (verbose >=3) |
---|
230 | printf ("importing grafical Element.... including to openGL\n"); |
---|
231 | char* vertex = elementString; |
---|
232 | |
---|
233 | char* texture; |
---|
234 | texture = strstr (vertex, "/"); |
---|
235 | texture[0] = '\0'; |
---|
236 | texture ++; |
---|
237 | glTexCoord2fv(vTexture->getArray()+(atoi(texture)-1)*2); |
---|
238 | |
---|
239 | char* normal; |
---|
240 | if ((normal = strstr (texture, "/")) !=NULL) |
---|
241 | { |
---|
242 | normal[0] = '\0'; |
---|
243 | normal ++; |
---|
244 | //glArrayElement(atoi(vertex)-1); |
---|
245 | glNormal3fv(normals->getArray() +(atoi(normal)-1)*3); |
---|
246 | } |
---|
247 | glVertex3fv(vertices->getArray() +(atoi(vertex)-1)*3); |
---|
248 | |
---|
249 | } |
---|
250 | |
---|
251 | bool Object::readVertexNormal (char* normalString) |
---|
252 | { |
---|
253 | readVertices = true; |
---|
254 | char subbuffer1[20]; |
---|
255 | char subbuffer2[20]; |
---|
256 | char subbuffer3[20]; |
---|
257 | sscanf (normalString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3); |
---|
258 | if (verbose >=3 ) |
---|
259 | printf("found vertex-Normal %s, %s, %s\n", subbuffer1,subbuffer2,subbuffer3); |
---|
260 | normals->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3)); |
---|
261 | return true; |
---|
262 | } |
---|
263 | |
---|
264 | bool Object::readVertexTexture (char* vTextureString) |
---|
265 | { |
---|
266 | readVertices = true; |
---|
267 | char subbuffer1[20]; |
---|
268 | char subbuffer2[20]; |
---|
269 | sscanf (vTextureString, "%s %s", subbuffer1, subbuffer2); |
---|
270 | if (verbose >=3 ) |
---|
271 | printf("found vertex-Texture %s, %s\n", subbuffer1,subbuffer2); |
---|
272 | vTexture->addEntry(atof(subbuffer1)); |
---|
273 | vTexture->addEntry(atof(subbuffer2)); |
---|
274 | return true; |
---|
275 | } |
---|
276 | |
---|
277 | |
---|
278 | bool Object::readMtlLib (char* mtlFile) |
---|
279 | { |
---|
280 | MTL_FILE = new ifstream (mtlFile); |
---|
281 | if (!MTL_FILE->is_open()) |
---|
282 | { |
---|
283 | if (verbose >= 1) |
---|
284 | printf ("unable to open file: %s\n", mtlFile); |
---|
285 | return false; |
---|
286 | } |
---|
287 | mtlFileName = mtlFile; |
---|
288 | if (verbose >=2) |
---|
289 | printf ("Opening mtlFile: %s\n", mtlFileName); |
---|
290 | char Buffer[500]; |
---|
291 | vertices = new Array(); |
---|
292 | material = new Material(); |
---|
293 | Material* tmpMat = material; |
---|
294 | while(!MTL_FILE->eof()) |
---|
295 | { |
---|
296 | MTL_FILE->getline(Buffer, 500); |
---|
297 | if (verbose >= 4) |
---|
298 | printf("found line in mtlFile: %s\n", Buffer); |
---|
299 | |
---|
300 | |
---|
301 | // create new Material |
---|
302 | if (!strncmp(Buffer, "newmtl ", 2)) |
---|
303 | { |
---|
304 | tmpMat = tmpMat->addMaterial(Buffer+7); |
---|
305 | // printf ("%s, %p\n", tmpMat->getName(), tmpMat); |
---|
306 | } |
---|
307 | // setting a illumMode |
---|
308 | else if (!strncmp(Buffer, "illum", 5)) |
---|
309 | { |
---|
310 | tmpMat->setIllum(Buffer+6); |
---|
311 | |
---|
312 | } |
---|
313 | // setting Diffuse Color |
---|
314 | else if (!strncmp(Buffer, "Kd", 2)) |
---|
315 | { |
---|
316 | tmpMat->setDiffuse(Buffer+3); |
---|
317 | } |
---|
318 | // setting Ambient Color |
---|
319 | else if (!strncmp(Buffer, "Ka", 2)) |
---|
320 | { |
---|
321 | tmpMat->setAmbient(Buffer+3); |
---|
322 | } |
---|
323 | // setting Specular Color |
---|
324 | else if (!strncmp(Buffer, "Ks", 2)) |
---|
325 | { |
---|
326 | tmpMat->setSpecular(Buffer+3); |
---|
327 | } |
---|
328 | // setting The Specular Shininess |
---|
329 | else if (!strncmp(Buffer, "Ns", 2)) |
---|
330 | { |
---|
331 | tmpMat->setShininess(Buffer+3); |
---|
332 | } |
---|
333 | } |
---|
334 | return true; |
---|
335 | } |
---|
336 | |
---|
337 | bool Object::readUseMtl (char* matString) |
---|
338 | { |
---|
339 | if (!strcmp (mtlFileName, "")) |
---|
340 | { |
---|
341 | if (verbose >= 1) |
---|
342 | printf ("Not using new defined material, because no mtlFile found yet\n"); |
---|
343 | return false; |
---|
344 | } |
---|
345 | |
---|
346 | if (faceMode != -1) |
---|
347 | glEnd(); |
---|
348 | faceMode = 0; |
---|
349 | if (verbose >= 2) |
---|
350 | printf ("using material %s for coming Faces.\n", matString); |
---|
351 | material->search(matString)->select(); |
---|
352 | } |
---|
353 | |
---|
354 | |
---|
355 | void Object::BoxObject(void) |
---|
356 | { |
---|
357 | readVertex ("-0.500000 -0.500000 0.500000"); |
---|
358 | readVertex ("0.500000 -0.500000 0.500000"); |
---|
359 | readVertex ("-0.500000 0.500000 0.500000"); |
---|
360 | readVertex ("0.500000 0.500000 0.500000"); |
---|
361 | readVertex ("-0.500000 0.500000 -0.500000"); |
---|
362 | readVertex ("0.500000 0.500000 -0.500000"); |
---|
363 | readVertex ("-0.500000 -0.500000 -0.500000"); |
---|
364 | readVertex ("0.500000 -0.500000 -0.500000"); |
---|
365 | readVertexTexture ("0.000000 0.000000"); |
---|
366 | readVertexTexture ("1.000000 0.000000"); |
---|
367 | readVertexTexture ("0.000000 1.000000"); |
---|
368 | readVertexTexture ("1.000000 1.000000"); |
---|
369 | readVertexTexture ("0.000000 2.000000"); |
---|
370 | readVertexTexture ("1.000000 2.000000"); |
---|
371 | readVertexTexture ("0.000000 3.000000"); |
---|
372 | readVertexTexture ("1.000000 3.000000"); |
---|
373 | readVertexTexture ("0.000000 4.000000"); |
---|
374 | readVertexTexture ("1.000000 4.000000"); |
---|
375 | readVertexTexture ("2.000000 0.000000"); |
---|
376 | readVertexTexture ("2.000000 1.000000"); |
---|
377 | readVertexTexture ("-1.000000 0.000000"); |
---|
378 | readVertexTexture ("-1.000000 1.000000"); |
---|
379 | |
---|
380 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
381 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
382 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
383 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
384 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
385 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
386 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
387 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
388 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
389 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
390 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
391 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
392 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
393 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
394 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
395 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
396 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
397 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
398 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
399 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
400 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
401 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
402 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
403 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
404 | |
---|
405 | readFace ("1/1/1 2/2/2 4/4/3 3/3/4"); |
---|
406 | readFace ("3/3/5 4/4/6 6/6/7 5/5/8"); |
---|
407 | readFace ("5/5/9 6/6/10 8/8/11 7/7/12"); |
---|
408 | readFace ("7/7/13 8/8/14 2/10/15 1/9/16"); |
---|
409 | readFace ("2/2/17 8/11/18 6/12/19 4/4/20"); |
---|
410 | readFace ("7/13/21 1/1/22 3/3/23 5/14/24"); |
---|
411 | } |
---|