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