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 | /** |
---|
19 | \brief Creates a 3D-Object, but does not load any 3D-models |
---|
20 | pretty useless |
---|
21 | */ |
---|
22 | Object::Object () |
---|
23 | { |
---|
24 | |
---|
25 | initialize(); |
---|
26 | |
---|
27 | BoxObject(); |
---|
28 | |
---|
29 | importToGL (); |
---|
30 | |
---|
31 | cleanup(); |
---|
32 | } |
---|
33 | |
---|
34 | /** |
---|
35 | \brief Crates a 3D-Object and loads in a File |
---|
36 | \param fileName file to parse and load (must be a .obj file) |
---|
37 | */ |
---|
38 | Object::Object(char* fileName) |
---|
39 | { |
---|
40 | initialize(); |
---|
41 | |
---|
42 | importFile (fileName); |
---|
43 | |
---|
44 | importToGL (); |
---|
45 | |
---|
46 | cleanup(); |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | \brief Crates a 3D-Object, loads in a File and scales it. |
---|
51 | \param fileName file to parse and load (must be a .obj file) |
---|
52 | \param scaling The factor that the object will be scaled with. |
---|
53 | */ |
---|
54 | Object::Object(char* fileName, float scaling) |
---|
55 | { |
---|
56 | initialize(); |
---|
57 | scaleFactor = scaling; |
---|
58 | |
---|
59 | importFile (fileName); |
---|
60 | |
---|
61 | importToGL (); |
---|
62 | |
---|
63 | cleanup(); |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | \brief deletes an Object |
---|
68 | */ |
---|
69 | Object::~Object() |
---|
70 | { |
---|
71 | if (verbose >= 2) |
---|
72 | printf ("Deleting display Lists.\n"); |
---|
73 | Group* walker = firstGroup; |
---|
74 | while (walker != NULL) |
---|
75 | { |
---|
76 | glDeleteLists (walker->listNumber, 1); |
---|
77 | Group* delWalker = walker; |
---|
78 | walker = walker->next; |
---|
79 | delete delWalker; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | /** |
---|
85 | \brief Draws the Objects of all Groups. |
---|
86 | It does this by just calling the Lists that must have been created earlier. |
---|
87 | */ |
---|
88 | void Object::draw (void) const |
---|
89 | { |
---|
90 | if (verbose >=2) |
---|
91 | printf("drawing the 3D-Objects\n"); |
---|
92 | Group* walker = firstGroup; |
---|
93 | while (walker != NULL) |
---|
94 | { |
---|
95 | if (verbose >= 3) |
---|
96 | printf ("Drawing object %s\n", walker->name); |
---|
97 | glCallList (walker->listNumber); |
---|
98 | walker = walker->next; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | \brief Draws the Object number groupNumber |
---|
104 | It does this by just calling the List that must have been created earlier. |
---|
105 | \param groupNumber The number of the group that will be displayed. |
---|
106 | */ |
---|
107 | void Object::draw (int groupNumber) const |
---|
108 | { |
---|
109 | if (groupNumber >= groupCount) |
---|
110 | { |
---|
111 | if (verbose>=1) |
---|
112 | printf ("You requested object number %i, but this File only contains of %i Objects.\n", groupNumber-1, groupCount); |
---|
113 | return; |
---|
114 | } |
---|
115 | if (verbose >=2) |
---|
116 | printf("drawing the requested 3D-Objects if found.\n"); |
---|
117 | Group* walker = firstGroup; |
---|
118 | int counter = 0; |
---|
119 | while (walker != NULL) |
---|
120 | { |
---|
121 | if (counter == groupNumber) |
---|
122 | { |
---|
123 | if (verbose >= 2) |
---|
124 | printf ("Drawing object number %i named %s\n", counter, walker->name); |
---|
125 | glCallList (walker->listNumber); |
---|
126 | return; |
---|
127 | } |
---|
128 | ++counter; |
---|
129 | walker = walker->next; |
---|
130 | } |
---|
131 | if (verbose >= 1) |
---|
132 | printf("Object number %i in %s not Found.\n", groupNumber, objFileName); |
---|
133 | return; |
---|
134 | |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | \brief Draws the Object with a specific groupName |
---|
139 | It does this by just calling the List that must have been created earlier. |
---|
140 | \param groupName The name of the group that will be displayed. |
---|
141 | */ |
---|
142 | void Object::draw (char* groupName) const |
---|
143 | { |
---|
144 | if (verbose >=2) |
---|
145 | printf("drawing the requested 3D-Objects if found.\n"); |
---|
146 | Group* walker = firstGroup; |
---|
147 | while (walker != NULL) |
---|
148 | { |
---|
149 | if (!strcmp(walker->name, groupName)) |
---|
150 | { |
---|
151 | if (verbose >= 2) |
---|
152 | printf ("Drawing object %s\n", walker->name); |
---|
153 | glCallList (walker->listNumber); |
---|
154 | return; |
---|
155 | } |
---|
156 | walker = walker->next; |
---|
157 | } |
---|
158 | if (verbose >= 2) |
---|
159 | printf("Object Named %s in %s not Found.\n", groupName, objFileName); |
---|
160 | return; |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | \returns Count of the Objects in this File |
---|
165 | */ |
---|
166 | int Object::getGroupCount (void) const |
---|
167 | { |
---|
168 | return groupCount; |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | \brief initializes the Object |
---|
173 | This Function initializes all the needed arrays, Lists and clientStates |
---|
174 | */ |
---|
175 | bool Object::initialize (void) |
---|
176 | { |
---|
177 | if (verbose >=3) |
---|
178 | printf("new 3D-Object is being created\n"); |
---|
179 | |
---|
180 | // setting the start group; |
---|
181 | firstGroup = new Group; |
---|
182 | currentGroup = firstGroup; |
---|
183 | groupCount = 0; |
---|
184 | |
---|
185 | initGroup (firstGroup); |
---|
186 | mtlFileName = ""; |
---|
187 | scaleFactor = 1; |
---|
188 | material = new Material(); |
---|
189 | |
---|
190 | vertices = new Array(); |
---|
191 | vTexture = new Array(); |
---|
192 | normals = new Array(); |
---|
193 | |
---|
194 | return true; |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | \brief initializes a new Group object |
---|
199 | */ |
---|
200 | bool Object::initGroup(Group* group) |
---|
201 | { |
---|
202 | if (verbose >= 2) |
---|
203 | printf("Adding new Group\n"); |
---|
204 | group->name = ""; |
---|
205 | group->faceMode = -1; |
---|
206 | group->faceCount = 0; |
---|
207 | group->next = NULL; |
---|
208 | |
---|
209 | group->firstFace = new Face; |
---|
210 | initFace (group->firstFace); |
---|
211 | group->currentFace = group->firstFace; |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | \brief initializes a new Face. (sets default Values) |
---|
216 | */ |
---|
217 | bool Object::initFace (Face* face) |
---|
218 | { |
---|
219 | face->vertexCount = 0; |
---|
220 | |
---|
221 | face->firstElem = NULL; |
---|
222 | |
---|
223 | face->materialString = NULL; |
---|
224 | |
---|
225 | face->next = NULL; |
---|
226 | |
---|
227 | return true; |
---|
228 | } |
---|
229 | |
---|
230 | /** |
---|
231 | \brief finalizes an Object. |
---|
232 | This funcion is needed, to delete all the Lists, and arrays that are no more needed because they are already imported into openGL. This will be applied at the end of the importing Process. |
---|
233 | */ |
---|
234 | bool Object::cleanup(void) |
---|
235 | { |
---|
236 | if (verbose >=2) |
---|
237 | printf("cleaning up the 3D-Object to save Memory.\n"); |
---|
238 | |
---|
239 | if (vertices != NULL) |
---|
240 | delete vertices; |
---|
241 | if (vTexture != NULL) |
---|
242 | delete vTexture; |
---|
243 | if (normals != NULL) |
---|
244 | delete normals; |
---|
245 | |
---|
246 | if (material != NULL) |
---|
247 | delete material; |
---|
248 | |
---|
249 | cleanupGroup(firstGroup); |
---|
250 | return true; |
---|
251 | } |
---|
252 | |
---|
253 | /** |
---|
254 | \brief Cleans up all groups starting from group. |
---|
255 | \param group the first Group to clean |
---|
256 | */ |
---|
257 | bool Object::cleanupGroup (Group* group) |
---|
258 | { |
---|
259 | if (verbose>=4) |
---|
260 | printf ("Cleaning up group\n"); |
---|
261 | if (group->firstFace != NULL) |
---|
262 | { |
---|
263 | cleanupFace (group->firstFace); |
---|
264 | delete group->firstFace; |
---|
265 | } |
---|
266 | |
---|
267 | if (group->next !=NULL) |
---|
268 | cleanupGroup (group->next); |
---|
269 | return true; |
---|
270 | } |
---|
271 | |
---|
272 | /** |
---|
273 | \brief Cleans up all Faces starting from face. |
---|
274 | \param face the first face to clean |
---|
275 | */ |
---|
276 | bool Object::cleanupFace (Face* face) |
---|
277 | { |
---|
278 | if (verbose>=4) |
---|
279 | printf ("Cleaning up Face\n"); |
---|
280 | |
---|
281 | if (face->materialString != NULL) |
---|
282 | delete []face->materialString; |
---|
283 | if (face->firstElem != NULL) |
---|
284 | { |
---|
285 | cleanupFaceElement(face->firstElem); |
---|
286 | delete face->firstElem; |
---|
287 | } |
---|
288 | |
---|
289 | if (face->next != NULL) |
---|
290 | { |
---|
291 | cleanupFace (face->next); |
---|
292 | delete face->next; |
---|
293 | } |
---|
294 | |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | /** |
---|
299 | \brief Cleans up all FaceElements starting from faceElem. |
---|
300 | \param faceElem the first FaceElement to clean. |
---|
301 | */ |
---|
302 | bool Object::cleanupFaceElement(FaceElement* faceElem) |
---|
303 | { |
---|
304 | if (faceElem->next != NULL) |
---|
305 | { |
---|
306 | cleanupFaceElement (faceElem->next); |
---|
307 | delete faceElem->next; |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | /** |
---|
312 | \brief Imports a obj file and handles the the relative location |
---|
313 | \param fileName The file to import |
---|
314 | */ |
---|
315 | bool Object::importFile (char* fileName) |
---|
316 | { |
---|
317 | if (verbose >=3) |
---|
318 | printf("preparing to read in file: %s\n", fileName); |
---|
319 | objFileName = fileName; |
---|
320 | this->readFromObjFile (fileName); |
---|
321 | return true; |
---|
322 | } |
---|
323 | |
---|
324 | /** |
---|
325 | \brief Reads in the .obj File and sets all the Values. |
---|
326 | This function does read the file, parses it for the occurence of things like vertices, faces and so on, and executes the specific tasks |
---|
327 | \param fileName the File that will be parsed (.obj-file) |
---|
328 | */ |
---|
329 | bool Object::readFromObjFile (char* fileName) |
---|
330 | { |
---|
331 | OBJ_FILE = new ifstream(fileName); |
---|
332 | if (!OBJ_FILE->is_open()) |
---|
333 | { |
---|
334 | if (verbose >=1) |
---|
335 | printf ("unable to open .OBJ file: %s\n Loading Box Object instead.\n", fileName); |
---|
336 | BoxObject(); |
---|
337 | return false; |
---|
338 | } |
---|
339 | objFileName = fileName; |
---|
340 | char Buffer[10000]; |
---|
341 | while(!OBJ_FILE->eof()) |
---|
342 | { |
---|
343 | OBJ_FILE->getline(Buffer, 10000); |
---|
344 | if (verbose >=4) |
---|
345 | printf ("Read input line: %s\n",Buffer); |
---|
346 | |
---|
347 | |
---|
348 | // case vertice |
---|
349 | if (!strncmp(Buffer, "v ", 2)) |
---|
350 | { |
---|
351 | readVertex(Buffer+2); |
---|
352 | } |
---|
353 | |
---|
354 | // case face |
---|
355 | else if (!strncmp(Buffer, "f ", 2)) |
---|
356 | { |
---|
357 | readFace (Buffer+2); |
---|
358 | } |
---|
359 | |
---|
360 | else if (!strncmp(Buffer, "mtllib ", 7)) |
---|
361 | { |
---|
362 | readMtlLib (Buffer+7); |
---|
363 | } |
---|
364 | |
---|
365 | else if (!strncmp(Buffer, "usemtl ", 7)) |
---|
366 | { |
---|
367 | readUseMtl (Buffer+7); |
---|
368 | } |
---|
369 | |
---|
370 | // case VertexNormal |
---|
371 | else if (!strncmp(Buffer, "vn ", 3)) |
---|
372 | { |
---|
373 | readVertexNormal(Buffer+3); |
---|
374 | } |
---|
375 | |
---|
376 | // case VertexTextureCoordinate |
---|
377 | else if (!strncmp(Buffer, "vt ", 3)) |
---|
378 | { |
---|
379 | readVertexTexture(Buffer+3); |
---|
380 | } |
---|
381 | // case group |
---|
382 | else if (!strncmp(Buffer, "g ", 2)) |
---|
383 | { |
---|
384 | readGroup (Buffer+2); |
---|
385 | } |
---|
386 | else if (!strncmp(Buffer, "s ", 2)) |
---|
387 | { |
---|
388 | if (verbose >= 2) |
---|
389 | printf("smoothing groups not supportet yet. line: %s\n", Buffer); |
---|
390 | } |
---|
391 | } |
---|
392 | OBJ_FILE->close(); |
---|
393 | return true; |
---|
394 | |
---|
395 | } |
---|
396 | |
---|
397 | /** |
---|
398 | \brief parses a group String |
---|
399 | This function initializes a new Group. |
---|
400 | With it you should be able to import .obj-files with more than one Objects inside. |
---|
401 | \param groupString the new Group to create |
---|
402 | */ |
---|
403 | bool Object::readGroup (char* groupString) |
---|
404 | { |
---|
405 | // setting the group name if not default. |
---|
406 | if (strcmp(currentGroup->name, "default")) |
---|
407 | { |
---|
408 | currentGroup->name = new char [strlen(groupString)]; |
---|
409 | strcpy(currentGroup->name, groupString); |
---|
410 | } |
---|
411 | if (groupCount != 0 && currentGroup->faceCount>0) |
---|
412 | { |
---|
413 | // finalizeGroup(currentGroup); |
---|
414 | currentGroup = currentGroup->next = new Group; |
---|
415 | initGroup(currentGroup); |
---|
416 | } |
---|
417 | |
---|
418 | ++groupCount; |
---|
419 | |
---|
420 | } |
---|
421 | |
---|
422 | /** |
---|
423 | \brief parses a vertex-String |
---|
424 | If a vertex line is found this function will inject it into the vertex-Array |
---|
425 | \param vertexString The String that will be parsed. |
---|
426 | */ |
---|
427 | bool Object::readVertex (char* vertexString) |
---|
428 | { |
---|
429 | float subbuffer1; |
---|
430 | float subbuffer2; |
---|
431 | float subbuffer3; |
---|
432 | sscanf (vertexString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3); |
---|
433 | if (verbose >= 3) |
---|
434 | printf ("reading in a vertex: %f %f %f\n", &subbuffer1, &subbuffer2, &subbuffer3); |
---|
435 | vertices->addEntry(subbuffer1*scaleFactor, subbuffer2*scaleFactor, subbuffer3*scaleFactor); |
---|
436 | return true; |
---|
437 | } |
---|
438 | |
---|
439 | /** |
---|
440 | \brief parses a face-string |
---|
441 | If a face line is found this function will add it to the glList. |
---|
442 | The function makes a difference between QUADS and TRIANGLES, and will if changed re-open, set and re-close the gl-processe. |
---|
443 | \param faceString The String that will be parsed. |
---|
444 | */ |
---|
445 | bool Object::readFace (char* faceString) |
---|
446 | { |
---|
447 | if (currentGroup->faceCount >0) |
---|
448 | currentGroup->currentFace = currentGroup->currentFace->next = new Face; |
---|
449 | initFace (currentGroup->currentFace); |
---|
450 | |
---|
451 | FaceElement* tmpElem = currentGroup->currentFace->firstElem = new FaceElement; |
---|
452 | tmpElem->next = NULL; |
---|
453 | while(strcmp (faceString, "\0")) |
---|
454 | { |
---|
455 | if (currentGroup->currentFace->vertexCount>0) |
---|
456 | tmpElem = tmpElem->next = new FaceElement; |
---|
457 | tmpElem->next = NULL; |
---|
458 | |
---|
459 | char tmpValue [50]; |
---|
460 | int tmpLen; |
---|
461 | char* vertex = NULL; |
---|
462 | char* texture = NULL; |
---|
463 | char* normal = NULL; |
---|
464 | |
---|
465 | sscanf (faceString, "%s", tmpValue); |
---|
466 | tmpLen = strlen(tmpValue); |
---|
467 | vertex = tmpValue; |
---|
468 | |
---|
469 | if ((texture = strstr (vertex, "/")) != NULL) |
---|
470 | { |
---|
471 | texture[0] = '\0'; |
---|
472 | texture ++; |
---|
473 | |
---|
474 | if ((normal = strstr (texture, "/")) !=NULL) |
---|
475 | { |
---|
476 | normal[0] = '\0'; |
---|
477 | normal ++; |
---|
478 | } |
---|
479 | } |
---|
480 | if (vertex) |
---|
481 | tmpElem->vertexNumber = atoi(vertex)-1; |
---|
482 | else |
---|
483 | tmpElem->vertexNumber = -1; |
---|
484 | if (texture) |
---|
485 | tmpElem->texCoordNumber = atoi(texture)-1; |
---|
486 | else |
---|
487 | tmpElem->texCoordNumber = -1; |
---|
488 | if (normal) |
---|
489 | tmpElem->normalNumber = atoi(normal)-1; |
---|
490 | else |
---|
491 | tmpElem->normalNumber = -1; |
---|
492 | |
---|
493 | faceString += tmpLen; |
---|
494 | if (strcmp (faceString, "\0")) |
---|
495 | faceString++; |
---|
496 | currentGroup->currentFace->vertexCount++; |
---|
497 | } |
---|
498 | |
---|
499 | currentGroup->faceCount += currentGroup->currentFace->vertexCount -2; |
---|
500 | } |
---|
501 | |
---|
502 | /** |
---|
503 | \brief parses a vertexNormal-String |
---|
504 | If a vertexNormal line is found this function will inject it into the vertexNormal-Array |
---|
505 | \param normalString The String that will be parsed. |
---|
506 | */ |
---|
507 | bool Object::readVertexNormal (char* normalString) |
---|
508 | { |
---|
509 | float subbuffer1; |
---|
510 | float subbuffer2; |
---|
511 | float subbuffer3; |
---|
512 | sscanf (normalString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3); |
---|
513 | if (verbose >=3 ) |
---|
514 | printf("found vertex-Normal %f, %f, %f\n", &subbuffer1,&subbuffer2,&subbuffer3); |
---|
515 | normals->addEntry(subbuffer1, subbuffer2, subbuffer3); |
---|
516 | return true; |
---|
517 | } |
---|
518 | |
---|
519 | /** |
---|
520 | \brief parses a vertexTextureCoordinate-String |
---|
521 | If a vertexTextureCoordinate line is found this function will inject it into the vertexTexture-Array |
---|
522 | \param vTextureString The String that will be parsed. |
---|
523 | */ |
---|
524 | bool Object::readVertexTexture (char* vTextureString) |
---|
525 | { |
---|
526 | float subbuffer1; |
---|
527 | float subbuffer2; |
---|
528 | sscanf (vTextureString, "%f %f", &subbuffer1, &subbuffer2); |
---|
529 | if (verbose >=3 ) |
---|
530 | printf("found vertex-Texture %f, %f\n", &subbuffer1, &subbuffer2); |
---|
531 | vTexture->addEntry(subbuffer1); |
---|
532 | vTexture->addEntry(subbuffer2); |
---|
533 | return true; |
---|
534 | } |
---|
535 | |
---|
536 | /** |
---|
537 | \brief Function to read in a mtl File. |
---|
538 | this Function parses all Lines of an mtl File |
---|
539 | \param mtlFile The .mtl file to read |
---|
540 | */ |
---|
541 | bool Object::readMtlLib (char* mtlFile) |
---|
542 | { |
---|
543 | MTL_FILE = new ifstream (mtlFile); |
---|
544 | if (!MTL_FILE->is_open()) |
---|
545 | { |
---|
546 | if (verbose >= 1) |
---|
547 | printf ("unable to open file: %s\n", mtlFile); |
---|
548 | return false; |
---|
549 | } |
---|
550 | mtlFileName = mtlFile; |
---|
551 | if (verbose >=2) |
---|
552 | printf ("Opening mtlFile: %s\n", mtlFileName); |
---|
553 | char Buffer[500]; |
---|
554 | Material* tmpMat = material; |
---|
555 | while(!MTL_FILE->eof()) |
---|
556 | { |
---|
557 | MTL_FILE->getline(Buffer, 500); |
---|
558 | if (verbose >= 4) |
---|
559 | printf("found line in mtlFile: %s\n", Buffer); |
---|
560 | |
---|
561 | |
---|
562 | // create new Material |
---|
563 | if (!strncmp(Buffer, "newmtl ", 7)) |
---|
564 | { |
---|
565 | tmpMat = tmpMat->addMaterial(Buffer+7); |
---|
566 | // printf ("%s, %p\n", tmpMat->getName(), tmpMat); |
---|
567 | } |
---|
568 | // setting a illumMode |
---|
569 | else if (!strncmp(Buffer, "illum ", 6)) |
---|
570 | { |
---|
571 | tmpMat->setIllum(Buffer+6); |
---|
572 | |
---|
573 | } |
---|
574 | // setting Diffuse Color |
---|
575 | else if (!strncmp(Buffer, "Kd ", 3)) |
---|
576 | { |
---|
577 | tmpMat->setDiffuse(Buffer+3); |
---|
578 | } |
---|
579 | // setting Ambient Color |
---|
580 | else if (!strncmp(Buffer, "Ka ", 3)) |
---|
581 | { |
---|
582 | tmpMat->setAmbient(Buffer+3); |
---|
583 | } |
---|
584 | // setting Specular Color |
---|
585 | else if (!strncmp(Buffer, "Ks ", 3)) |
---|
586 | { |
---|
587 | tmpMat->setSpecular(Buffer+3); |
---|
588 | } |
---|
589 | // setting The Specular Shininess |
---|
590 | else if (!strncmp(Buffer, "Ns ", 3)) |
---|
591 | { |
---|
592 | tmpMat->setShininess(Buffer+3); |
---|
593 | } |
---|
594 | // setting up transparency |
---|
595 | else if (!strncmp(Buffer, "d ", 2)) |
---|
596 | { |
---|
597 | tmpMat->setTransparency(Buffer+2); |
---|
598 | } |
---|
599 | else if (!strncmp(Buffer, "Tf ", 3)) |
---|
600 | { |
---|
601 | tmpMat->setTransparency(Buffer+3); |
---|
602 | } |
---|
603 | |
---|
604 | else if (!strncmp(Buffer, "map_Kd ", 7)) |
---|
605 | { |
---|
606 | tmpMat->setDiffuseMap(Buffer+7); |
---|
607 | } |
---|
608 | else if (!strncmp(Buffer, "map_Ka ", 7)) |
---|
609 | { |
---|
610 | tmpMat->setAmbientMap(Buffer+7); |
---|
611 | } |
---|
612 | else if (!strncmp(Buffer, "map_Ks ", 7)) |
---|
613 | { |
---|
614 | tmpMat->setSpecularMap(Buffer+7); |
---|
615 | } |
---|
616 | else if (!strncmp(Buffer, "bump ", 5)) |
---|
617 | { |
---|
618 | tmpMat->setBump(Buffer+7); |
---|
619 | } |
---|
620 | |
---|
621 | |
---|
622 | } |
---|
623 | return true; |
---|
624 | } |
---|
625 | |
---|
626 | /** |
---|
627 | \brief Function that selects a material, if changed in the obj file. |
---|
628 | \param matString the Material that will be set. |
---|
629 | */ |
---|
630 | bool Object::readUseMtl (char* matString) |
---|
631 | { |
---|
632 | if (!strcmp (mtlFileName, "")) |
---|
633 | { |
---|
634 | if (verbose >= 1) |
---|
635 | printf ("Not using new defined material, because no mtlFile found yet\n"); |
---|
636 | return false; |
---|
637 | } |
---|
638 | |
---|
639 | if (currentGroup->faceCount >0) |
---|
640 | currentGroup->currentFace = currentGroup->currentFace->next = new Face; |
---|
641 | initFace (currentGroup->currentFace); |
---|
642 | |
---|
643 | currentGroup->currentFace->materialString = new char[strlen(matString)]; |
---|
644 | strcpy (currentGroup->currentFace->materialString, matString); |
---|
645 | |
---|
646 | if (currentGroup->faceCount == 0) |
---|
647 | currentGroup->faceCount ++; |
---|
648 | |
---|
649 | } |
---|
650 | |
---|
651 | /** |
---|
652 | \brief reads and includes the Faces/Materials into the openGL state Machine |
---|
653 | */ |
---|
654 | bool Object::importToGL (void) |
---|
655 | { |
---|
656 | |
---|
657 | // finalize the Arrays |
---|
658 | vertices->finalizeArray(); |
---|
659 | vTexture->finalizeArray(); |
---|
660 | if (normals->getCount() == 0) // vertices-Array must be uilt for this |
---|
661 | buildVertexNormals(); |
---|
662 | normals->finalizeArray(); |
---|
663 | |
---|
664 | currentGroup = firstGroup; |
---|
665 | |
---|
666 | while (currentGroup != NULL) |
---|
667 | { |
---|
668 | |
---|
669 | // creating a glList for the Group |
---|
670 | if ((currentGroup->listNumber = glGenLists(1)) == 0) |
---|
671 | { |
---|
672 | printf ("list could not be created for this Object\n"); |
---|
673 | return false; |
---|
674 | } |
---|
675 | glNewList (currentGroup->listNumber, GL_COMPILE); |
---|
676 | |
---|
677 | // Putting Faces to GL |
---|
678 | Face* tmpFace = currentGroup->firstFace; |
---|
679 | while (tmpFace != NULL) |
---|
680 | { |
---|
681 | if (tmpFace->vertexCount == 0 && tmpFace->materialString != NULL) |
---|
682 | { |
---|
683 | if (currentGroup->faceMode != -1) |
---|
684 | glEnd(); |
---|
685 | currentGroup->faceMode = 0; |
---|
686 | if (verbose >= 2) |
---|
687 | printf ("using material %s for coming Faces.\n", tmpFace->materialString); |
---|
688 | Material* tmpMat; |
---|
689 | if ((tmpMat = material->search(tmpFace->materialString)) != NULL) |
---|
690 | tmpMat->select(); |
---|
691 | |
---|
692 | } |
---|
693 | |
---|
694 | else if (tmpFace->vertexCount == 3) |
---|
695 | { |
---|
696 | if (currentGroup->faceMode != 3) |
---|
697 | { |
---|
698 | if (currentGroup->faceMode != -1) |
---|
699 | glEnd(); |
---|
700 | glBegin(GL_TRIANGLES); |
---|
701 | } |
---|
702 | |
---|
703 | currentGroup->faceMode = 3; |
---|
704 | if (verbose >=3) |
---|
705 | printf ("found triag.\n"); |
---|
706 | } |
---|
707 | |
---|
708 | else if (tmpFace->vertexCount == 4) |
---|
709 | { |
---|
710 | if (currentGroup->faceMode != 4) |
---|
711 | { |
---|
712 | if (currentGroup->faceMode != -1) |
---|
713 | glEnd(); |
---|
714 | glBegin(GL_QUADS); |
---|
715 | } |
---|
716 | currentGroup->faceMode = 4; |
---|
717 | if (verbose >=3 ) |
---|
718 | printf ("found quad.\n"); |
---|
719 | } |
---|
720 | |
---|
721 | else if (tmpFace->vertexCount > 4) |
---|
722 | { |
---|
723 | if (currentGroup->faceMode != -1) |
---|
724 | glEnd(); |
---|
725 | glBegin(GL_POLYGON); |
---|
726 | if (verbose >=3) |
---|
727 | printf ("Polygon with %i faces found.", tmpFace->vertexCount); |
---|
728 | currentGroup->faceMode = tmpFace->vertexCount; |
---|
729 | } |
---|
730 | |
---|
731 | FaceElement* tmpElem = tmpFace->firstElem; |
---|
732 | while (tmpElem != NULL) |
---|
733 | { |
---|
734 | // printf ("%s\n", tmpElem->value); |
---|
735 | addGLElement(tmpElem); |
---|
736 | tmpElem = tmpElem->next; |
---|
737 | } |
---|
738 | tmpFace = tmpFace->next; |
---|
739 | } |
---|
740 | glEnd(); |
---|
741 | glEndList(); |
---|
742 | currentGroup = currentGroup->next; |
---|
743 | } |
---|
744 | } |
---|
745 | |
---|
746 | /** |
---|
747 | \brief Adds a Face-element (one vertex of a face) with all its information. |
---|
748 | It does this by searching: |
---|
749 | 1. The Vertex itself |
---|
750 | 2. The VertexNormale |
---|
751 | 3. The VertexTextureCoordinate |
---|
752 | merging this information, the face will be drawn. |
---|
753 | |
---|
754 | */ |
---|
755 | bool Object::addGLElement (FaceElement* elem) |
---|
756 | { |
---|
757 | if (verbose >=3) |
---|
758 | printf ("importing grafical Element to openGL.\n"); |
---|
759 | |
---|
760 | if (elem->texCoordNumber != -1) |
---|
761 | glTexCoord2fv(vTexture->getArray() + elem->texCoordNumber * 2); |
---|
762 | if (elem->normalNumber != -1) |
---|
763 | glNormal3fv(normals->getArray() + elem->normalNumber * 3); |
---|
764 | if (elem->vertexNumber != -1) |
---|
765 | glVertex3fv(vertices->getArray() + elem->vertexNumber * 3); |
---|
766 | |
---|
767 | } |
---|
768 | |
---|
769 | /** |
---|
770 | \brief A routine that is able to create normals. |
---|
771 | The algorithm does the following: |
---|
772 | 1. It calculates creates Vectors for each normale, and sets them to zero. |
---|
773 | 2. It then Walks through a) all the Groups b) all the Faces c) all the FaceElements |
---|
774 | 3. It searches for a points two neighbours per Face, takes Vecotrs to them calculates FaceNormals and adds it to the Points Normal. |
---|
775 | 4. It goes through all the normale-Points and calculates the VertexNormale and includes it in the normals-Array. |
---|
776 | */ |
---|
777 | bool Object::buildVertexNormals () |
---|
778 | { |
---|
779 | |
---|
780 | if (verbose >=2) |
---|
781 | printf("Normals are being calculated.\n"); |
---|
782 | |
---|
783 | Vector* normArray = new Vector [vertices->getCount()/3]; |
---|
784 | for (int i=0; i<vertices->getCount()/3;i++) |
---|
785 | normArray[i] = Vector(.0,.0,.0); |
---|
786 | |
---|
787 | int firstTouch; |
---|
788 | int secondTouch; |
---|
789 | Vector prevV; |
---|
790 | Vector nextV; |
---|
791 | Vector curV; |
---|
792 | |
---|
793 | Group* tmpGroup = firstGroup; |
---|
794 | while (tmpGroup) |
---|
795 | { |
---|
796 | Face* tmpFace = tmpGroup->firstFace; |
---|
797 | while (tmpFace) |
---|
798 | { |
---|
799 | if (tmpFace->firstElem) |
---|
800 | { |
---|
801 | FaceElement* firstElem = tmpFace->firstElem; |
---|
802 | FaceElement* prevElem; |
---|
803 | FaceElement* curElem = firstElem; |
---|
804 | FaceElement* nextElem; |
---|
805 | FaceElement* lastElem; |
---|
806 | // find last Element of the Chain. !! IMPORTANT:the last Element of the Chain must point to NULL, or it will resolv into an infinity-loop. |
---|
807 | while (curElem) |
---|
808 | { |
---|
809 | prevElem = curElem; |
---|
810 | curElem = curElem->next; |
---|
811 | } |
---|
812 | lastElem = prevElem; |
---|
813 | |
---|
814 | curElem = firstElem; |
---|
815 | for (int j=0; j<tmpFace->vertexCount; j++) |
---|
816 | { |
---|
817 | if (!(nextElem = curElem->next)) |
---|
818 | nextElem = firstElem; |
---|
819 | curElem->normalNumber = curElem->vertexNumber; |
---|
820 | |
---|
821 | curV = Vector (vertices->getArray()[curElem->vertexNumber*3], vertices->getArray()[curElem->vertexNumber*3+1], vertices->getArray()[curElem->vertexNumber*3+2]); |
---|
822 | prevV = Vector (vertices->getArray()[prevElem->vertexNumber*3], vertices->getArray()[prevElem->vertexNumber*3+1], vertices->getArray()[prevElem->vertexNumber*3+2]) - curV; |
---|
823 | nextV = Vector (vertices->getArray()[nextElem->vertexNumber*3], vertices->getArray()[nextElem->vertexNumber*3+1], vertices->getArray()[nextElem->vertexNumber*3+2]) - curV; |
---|
824 | normArray[curElem->vertexNumber] = normArray[curElem->vertexNumber] + nextV.cross(prevV); |
---|
825 | |
---|
826 | prevElem = curElem; |
---|
827 | curElem = curElem->next; |
---|
828 | } |
---|
829 | } |
---|
830 | tmpFace = tmpFace->next; |
---|
831 | } |
---|
832 | tmpGroup = tmpGroup->next; |
---|
833 | } |
---|
834 | |
---|
835 | for (int i=0; i<vertices->getCount()/3;i++) |
---|
836 | { |
---|
837 | normArray[i].normalize(); |
---|
838 | if (verbose >=3) |
---|
839 | printf ("Found Normale number %d: (%f; %f, %f).\n", i, normArray[i].x, normArray[i].y, normArray[i].z); |
---|
840 | |
---|
841 | normals->addEntry(normArray[i].x, normArray[i].y, normArray[i].z); |
---|
842 | |
---|
843 | } |
---|
844 | delete []normArray; |
---|
845 | |
---|
846 | } |
---|
847 | |
---|
848 | |
---|
849 | /** |
---|
850 | \brief Includes a default object |
---|
851 | This will inject a Cube, because this is the most basic object. |
---|
852 | */ |
---|
853 | void Object::BoxObject(void) |
---|
854 | { |
---|
855 | readVertex ("-0.500000 -0.500000 0.500000"); |
---|
856 | readVertex ("0.500000 -0.500000 0.500000"); |
---|
857 | readVertex ("-0.500000 0.500000 0.500000"); |
---|
858 | readVertex ("0.500000 0.500000 0.500000"); |
---|
859 | readVertex ("-0.500000 0.500000 -0.500000"); |
---|
860 | readVertex ("0.500000 0.500000 -0.500000"); |
---|
861 | readVertex ("-0.500000 -0.500000 -0.500000"); |
---|
862 | readVertex ("0.500000 -0.500000 -0.500000"); |
---|
863 | |
---|
864 | readVertexTexture ("0.000000 0.000000"); |
---|
865 | readVertexTexture ("1.000000 0.000000"); |
---|
866 | readVertexTexture ("0.000000 1.000000"); |
---|
867 | readVertexTexture ("1.000000 1.000000"); |
---|
868 | readVertexTexture ("0.000000 2.000000"); |
---|
869 | readVertexTexture ("1.000000 2.000000"); |
---|
870 | readVertexTexture ("0.000000 3.000000"); |
---|
871 | readVertexTexture ("1.000000 3.000000"); |
---|
872 | readVertexTexture ("0.000000 4.000000"); |
---|
873 | readVertexTexture ("1.000000 4.000000"); |
---|
874 | readVertexTexture ("2.000000 0.000000"); |
---|
875 | readVertexTexture ("2.000000 1.000000"); |
---|
876 | readVertexTexture ("-1.000000 0.000000"); |
---|
877 | readVertexTexture ("-1.000000 1.000000"); |
---|
878 | |
---|
879 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
880 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
881 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
882 | readVertexNormal ("0.000000 0.000000 1.000000"); |
---|
883 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
884 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
885 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
886 | readVertexNormal ("0.000000 1.000000 0.000000"); |
---|
887 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
888 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
889 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
890 | readVertexNormal ("0.000000 0.000000 -1.000000"); |
---|
891 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
892 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
893 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
894 | readVertexNormal ("0.000000 -1.000000 0.000000"); |
---|
895 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
896 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
897 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
898 | readVertexNormal ("1.000000 0.000000 0.000000"); |
---|
899 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
900 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
901 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
902 | readVertexNormal ("-1.000000 0.000000 0.000000"); |
---|
903 | |
---|
904 | /* normaleLess-testingMode |
---|
905 | readFace ("1 2 4 3"); |
---|
906 | readFace ("3 4 6 5"); |
---|
907 | readFace ("5 6 8 7"); |
---|
908 | readFace ("7 8 2 1"); |
---|
909 | readFace ("2 8 6 4"); |
---|
910 | readFace ("7 1 3 5"); |
---|
911 | */ |
---|
912 | |
---|
913 | readFace ("1/1/1 2/2/2 4/4/3 3/3/4"); |
---|
914 | readFace ("3/3/5 4/4/6 6/6/7 5/5/8"); |
---|
915 | readFace ("5/5/9 6/6/10 8/8/11 7/7/12"); |
---|
916 | readFace ("7/7/13 8/8/14 2/10/15 1/9/16"); |
---|
917 | readFace ("2/2/17 8/11/18 6/12/19 4/4/20"); |
---|
918 | readFace ("7/13/21 1/1/22 3/3/23 5/14/24"); |
---|
919 | |
---|
920 | } |
---|