Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/importer/static_model.cc @ 6423

Last change on this file since 6423 was 6423, checked in by bensch, 18 years ago

trunk: staticModel now uses vector instead of tArray (more portable)

File size: 30.0 KB
Line 
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   2005-07-06: (Patrick) added new function buildTriangleList()
16*/
17
18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER
19
20#include "static_model.h"
21
22#include "stdlibincl.h"
23#include <stdarg.h>
24
25#include "vector.h"
26
27using namespace std;
28
29
30////////////////////
31/// SUB-Elements ///
32////////////////////
33/**
34 * @brief creates a new ModelFaceElement
35 */
36ModelFaceElement::ModelFaceElement()
37{
38  this->vertexNumber = -1;
39  this->normalNumber = -1;
40  this->texCoordNumber = -1;
41
42  this->next = NULL;
43}
44
45/**
46 * @brief destroys a ModelFaceElement
47 */
48ModelFaceElement::~ModelFaceElement()
49{
50  if (this->next)
51    delete this->next;
52}
53
54/**
55 * @brief creates a new ModelFace
56 */
57ModelFace::ModelFace()
58{
59  this->vertexCount = 0;
60
61  this->firstElem = NULL;
62
63  this->material = NULL;
64
65  this->next = NULL;
66}
67
68/**
69 *  deletes a ModelFace
70*/
71ModelFace::~ModelFace()
72{
73  PRINTF(5)("Cleaning up Face\n");
74
75  if (this->firstElem != NULL)
76    delete this->firstElem;
77
78  if (this->next != NULL)
79    delete this->next;
80}
81
82/**
83 * @brief Creates a new ModelGroup
84 */
85ModelGroup::ModelGroup()
86{
87  PRINTF(4)("Adding new Group\n");
88  this->name = NULL;
89  this->faceMode = -1;
90  this->faceCount = 0;
91  this->next = NULL;
92  this->listNumber = 0;
93  this->indices = NULL;
94
95  this->firstFace = new ModelFace;
96  this->currentFace = this->firstFace;
97}
98
99/**
100 * @brief deletes a ModelGroup
101 */
102ModelGroup::~ModelGroup()
103{
104  PRINTF(5)("Cleaning up group\n");
105  if (this->firstFace != NULL)
106    delete this->firstFace;
107
108  // deleting the glList
109  if (this->listNumber != 0)
110    glDeleteLists(this->listNumber, 1);
111
112  if (this->name != NULL)
113    delete[] this->name;
114
115  if (this->next !=NULL)
116    delete this->next;
117
118}
119
120/**
121 * @brief cleans up a ModelGroup
122 *
123 * actually does the same as the delete Operator, but does not delete the predecessing group
124 */
125void ModelGroup::cleanup()
126{
127  PRINTF(5)("Cleaning up group\n");
128  if (this->firstFace)
129    delete this->firstFace;
130  this->firstFace = NULL;
131  if (this->next)
132    this->next->cleanup();
133}
134
135
136/////////////
137/// MODEL ///
138/////////////
139/**
140 * @brief Creates a 3D-Model.
141 *
142 * assigns it a Name and a Type
143 */
144StaticModel::StaticModel(const char* modelName)
145{
146  this->setClassID(CL_STATIC_MODEL, "StaticModel");
147  PRINTF(4)("new 3D-Model is being created\n");
148  this->setName(modelName);
149
150  this->finalized = false;
151
152  // setting the start group;
153  this->currentGroup = this->firstGroup = new ModelGroup;
154  this->groupCount = 0;
155  this->faceCount = 0;
156
157  this->scaleFactor = 1.0f;
158}
159
160/**
161 * @brief deletes an Model.
162 *
163 * Looks if any from model allocated space is still in use, and if so deleted it.
164 */
165StaticModel::~StaticModel()
166{
167  PRINTF(4)("Deleting Model ");
168  if (this->getName())
169  {
170    PRINT(4)("%s\n", this->getName());
171  }
172  else
173  {
174    PRINT(4)("\n");
175  }
176  this->cleanup();
177
178  PRINTF(5)("Deleting display Lists.\n");
179  delete this->firstGroup;
180
181  // deleting the MaterialList
182  PRINTF(5)("Deleting Materials.\n");
183
184  //! @todo do we really have to delete this material??
185  list<ModelMaterial*>::iterator modMat;
186  for(modMat = this->materialList.begin(); modMat != this->materialList.end(); modMat++)
187  {
188    if (!(*modMat)->external)
189      delete (*modMat)->material;
190    delete (*modMat);
191  }
192}
193
194/**
195 * @brief Finalizes an Object. This can be done outside of the Class.
196 */
197void StaticModel::finalize()
198{
199  // this creates the display List.
200  this->importToDisplayList();
201  this->buildTriangleList();
202
203  this->pModelInfo.pVertices = &this->vertices[0];
204  this->pModelInfo.pNormals = &this->normals[0];
205  this->pModelInfo.pTexCoor = &this->vTexture[0];
206
207  this->finalized = true;
208}
209
210/**
211 * @brief rebuild the Model from the Information we got.
212 */
213void StaticModel::rebuild()
214{
215  PRINTF(3)("Rebuilding Model '%s'\n", this->getName());
216  this->finalize();
217}
218
219//////////
220// DRAW //
221//////////
222/**
223 * @brief Draws the Models of all Groups.
224 *
225 * It does this by just calling the Lists that must have been created earlier.
226 */
227void StaticModel::draw () const
228{
229  PRINTF(4)("drawing the 3D-Models\n");
230  ModelGroup* tmpGroup = this->firstGroup;
231  while (tmpGroup != NULL)
232    {
233      PRINTF(5)("Drawing model %s\n", tmpGroup->name);
234      glCallList (tmpGroup->listNumber);
235      tmpGroup = tmpGroup->next;
236    }
237}
238
239
240/**
241 * @brief Draws the Model number groupNumber
242 * @param groupNumber The number of the group that will be displayed.
243 *
244 * It does this by just calling the List that must have been created earlier.
245 */
246void StaticModel::draw (int groupNumber) const
247{
248  if (unlikely(groupNumber >= this->groupCount))
249    {
250      PRINTF(2)("You requested model number %i, but this File only contains of %i Models.\n", groupNumber-1, this->groupCount);
251      return;
252    }
253  PRINTF(4)("drawing the requested 3D-Models if found.\n");
254  ModelGroup* tmpGroup = this->firstGroup;
255  int counter = 0;
256  while (tmpGroup != NULL)
257    {
258      if (counter == groupNumber)
259        {
260          PRINTF(4)("Drawing model number %i named %s\n", counter, tmpGroup->name);
261          glCallList (tmpGroup->listNumber);
262          return;
263        }
264      ++counter;
265      tmpGroup = tmpGroup->next;
266    }
267  PRINTF(2)("Model number %i in %s not Found.\n", groupNumber, this->getName());
268  return;
269}
270
271
272/**
273 * @brief Draws the Model with a specific groupName
274 * @param groupName The name of the group that will be displayed.
275 *
276 * It does this by just calling the List that must have been created earlier.
277 */
278void StaticModel::draw (char* groupName) const
279{
280  if (groupName == NULL)
281     return;
282  PRINTF(4)("drawing the requested 3D-Models if found.\n");
283  ModelGroup* tmpGroup = this->firstGroup;
284  while (tmpGroup != NULL)
285    {
286      if (tmpGroup->name != NULL && !strcmp(tmpGroup->name, groupName))
287        {
288          PRINTF(4)("Drawing model %s\n", tmpGroup->name);
289          glCallList (tmpGroup->listNumber);
290          return;
291        }
292      tmpGroup = tmpGroup->next;
293    }
294  PRINTF(2)("Model Named %s in %s not Found.\n", groupName, this->getName());
295  return;
296}
297
298//////////
299// INIT //
300//////////
301
302/**
303 * @brief finalizes an Model.
304 *
305 * This funcion is needed, to delete all the Lists, and arrays that are no more
306 * needed because they are already imported into openGL.
307 * This will be applied at the end of the importing Process.
308*/
309bool StaticModel::cleanup()
310{
311  PRINTF(4)("cleaning up the 3D-Model to save Memory.\n");
312  this->firstGroup->cleanup();
313  return true;
314}
315
316//////////
317// MESH //
318//////////
319/**
320 * @brief adds a new Material to the Material List
321 * @param material the Material to add
322 * @returns the added material
323 *
324 * this also tells this Model, that all the Materials are handled externally
325 * with this option set the Materials will not be deleted with the Model.
326 */
327Material* StaticModel::addMaterial(Material* material)
328{
329  if (material == NULL)
330    return NULL;
331  ModelMaterial* modMat = new ModelMaterial;
332  modMat->external = true;
333  modMat->material = material;
334  this->materialList.push_back(modMat);
335  return modMat->material;
336}
337
338/**
339 * @brief adds a new Material to the Material List
340 * @param materialName the name of the Material to add
341 * @returns the added material
342 */
343Material* StaticModel::addMaterial(const char* materialName)
344{
345  ModelMaterial* modMat = new ModelMaterial;
346  modMat->external = false;
347  modMat->material = new Material(materialName);
348
349  // adding material to the List of materials
350  this->materialList.push_back(modMat);
351  return modMat->material;
352}
353
354/**
355 * @brief finds a Material by its name and returns it
356 * @param materialName the Name of the material to search for.
357 * @returns the Material if found, NULL otherwise
358 */
359Material* StaticModel::findMaterialByName(const char* materialName)
360{
361  list<ModelMaterial*>::iterator modMat;
362  for  (modMat = this->materialList.begin(); modMat != this->materialList.end(); modMat++)
363    if (!strcmp((*modMat)->material->getName(), materialName))
364      return (*modMat)->material;
365  return NULL;
366}
367
368/**
369 * @brief parses a group String
370 * @param groupString the new Group to create
371 *
372 * This function initializes a new Group.
373 * With it you should be able to create Models with more than one SubModel inside
374 */
375bool StaticModel::addGroup(const char* groupString)
376{
377  PRINTF(5)("Read Group: %s.\n", groupString);
378  if (this->groupCount != 0 && this->currentGroup->faceCount > 0)
379    {
380      // finalizeGroup(currentGroup);
381      this->currentGroup = this->currentGroup->next = new ModelGroup;
382    }
383  // setting the group name if not default.
384  if (strcmp(groupString, "default"))
385    {
386      this->currentGroup->name = new char [strlen(groupString)+1];
387      strcpy(this->currentGroup->name, groupString);
388    }
389  ++this->groupCount;
390}
391
392/**
393 * @brief parses a vertex-String
394 * @param vertexString The String that will be parsed.
395 *
396 *  If a vertex line is found this function will inject it into the vertex-Array
397 */
398bool StaticModel::addVertex (const char* vertexString)
399{
400  float subbuffer1;
401  float subbuffer2;
402  float subbuffer3;
403  sscanf (vertexString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3);
404  this->vertices.push_back(subbuffer1*scaleFactor);
405  this->vertices.push_back(subbuffer2*scaleFactor);
406  this->vertices.push_back(subbuffer3*scaleFactor);
407  this->pModelInfo.numVertices++;
408  return true;
409}
410
411/**
412 * @brief parses a vertex-String
413 * @param x the X-coordinate of the Vertex to add.
414 * @param y the Y-coordinate of the Vertex to add.
415 * @param z the Z-coordinate of the Vertex to add.
416 */
417bool StaticModel::addVertex(float x, float y, float z)
418{
419  PRINTF(5)("reading in a vertex: %f %f %f\n", x, y, z);
420  this->vertices.push_back(x*scaleFactor);
421  this->vertices.push_back(y*scaleFactor);
422  this->vertices.push_back(z*scaleFactor);
423  this->pModelInfo.numVertices++;
424  return true;
425}
426
427/**
428 * @brief parses a vertexNormal-String
429 * @param normalString The String that will be parsed.
430 *
431 * If a vertexNormal line is found this function will inject it into the vertexNormal-Array
432 */
433bool StaticModel::addVertexNormal (const char* normalString)
434{
435  float subbuffer1;
436  float subbuffer2;
437  float subbuffer3;
438  sscanf (normalString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3);
439  this->normals.push_back(subbuffer1);
440  this->normals.push_back(subbuffer2);
441  this->normals.push_back(subbuffer3);
442  this->pModelInfo.numNormals++;
443  return true;
444}
445
446/**
447 * @brief adds a VertexNormal.
448 * @param x The x coordinate of the Normal.
449 * @param y The y coordinate of the Normal.
450 * @param z The z coordinate of the Normal.
451 *
452 * If a vertexNormal line is found this function will inject it into the vertexNormal-Array
453 */
454bool StaticModel::addVertexNormal(float x, float y, float z)
455{
456  PRINTF(5)("found vertex-Normal %f, %f, %f\n", x, y, z);
457  this->normals.push_back(x);
458  this->normals.push_back(y);
459  this->normals.push_back(z);
460  this->pModelInfo.numNormals++;
461  return true;
462}
463
464/**
465 * @brief parses a vertexTextureCoordinate-String
466 * @param vTextureString The String that will be parsed.
467 *
468 * If a vertexTextureCoordinate line is found,
469 * this function will inject it into the vertexTexture-Array
470 *
471 * !! WARNING THIS IS DIFFERNT FROM addVervexTexture(float, float); because it changes the second entry to 1-v !!
472 */
473bool StaticModel::addVertexTexture (const char* vTextureString)
474{
475  float subbuffer1;
476  float subbuffer2;
477  sscanf (vTextureString, "%f %f", &subbuffer1, &subbuffer2);
478  this->vTexture.push_back(subbuffer1);
479  this->vTexture.push_back(1 - subbuffer2);
480  this->pModelInfo.numTexCoor++;
481  return true;
482}
483
484/**
485 * @brief adds a Texture Coordinate
486 * @param u The u coordinate of the TextureCoordinate.
487 * @param v The y coordinate of the TextureCoordinate.
488 *
489 * If a TextureCoordinate line is found this function will
490 *  inject it into the TextureCoordinate-Array
491 */
492bool StaticModel::addVertexTexture(float u, float v)
493{
494  PRINTF(5)("found vertex-Texture %f, %f\n", u, v);
495  this->vTexture.push_back(u);
496  this->vTexture.push_back(v);
497  this->pModelInfo.numTexCoor++;
498  return true;
499}
500
501/**
502 * @brief parses a face-string
503 * @param faceString The String that will be parsed.
504 *
505 * If a face line is found this function will add it to the glList.
506 *
507 * String is different from the argument addFace,
508 * in this, that the first Vertex/Normal/Texcoord is 1 instead of 0
509 */
510bool StaticModel::addFace (const char* faceString)
511{
512  if (this->currentGroup->faceCount >0)
513    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
514
515  ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement;
516  tmpElem->next = NULL;
517  while(strcmp (faceString, "\0"))
518    {
519      if (this->currentGroup->currentFace->vertexCount>0)
520          tmpElem = tmpElem->next = new ModelFaceElement;
521      tmpElem->next = NULL;
522
523      char tmpValue [50];
524      int tmpLen;
525      char* vertex = NULL;
526      char* texture = NULL;
527      char* normal = NULL;
528
529      sscanf (faceString, "%s", tmpValue);
530      tmpLen = strlen(tmpValue);
531      vertex = tmpValue;
532
533      if ((texture = strstr (vertex, "/")) != NULL)
534        {
535          texture[0] = '\0';
536          texture ++;
537
538          if ((normal = strstr (texture, "/")) !=NULL)
539            {
540              normal[0] = '\0';
541              normal ++;
542            }
543        }
544      if (vertex)
545        tmpElem->vertexNumber = atoi(vertex)-1;
546      if (texture)
547        tmpElem->texCoordNumber = atoi(texture)-1;
548      if (normal)
549        tmpElem->normalNumber = atoi(normal)-1;
550
551      faceString += tmpLen;
552      if (strcmp (faceString, "\0"))
553        faceString++;
554      this->currentGroup->currentFace->vertexCount++;
555    }
556
557  this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount -2;
558  this->faceCount += this->currentGroup->currentFace->vertexCount -2;
559}
560
561/**
562 * @brief adds a new Face
563 * @param faceElemCount the number of Vertices to add to the Face.
564 * @param type The information Passed with each Vertex
565*/
566bool StaticModel::addFace(int faceElemCount, VERTEX_FORMAT type, ...)
567{
568  if (this->currentGroup->faceCount > 0)
569    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
570
571  ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement;
572
573  va_list itemlist;
574  va_start (itemlist, type);
575
576  for (int i = 0; i < faceElemCount; i++)
577    {
578      if (this->currentGroup->currentFace->vertexCount > 0)
579        tmpElem = tmpElem->next = new ModelFaceElement;
580
581      tmpElem->vertexNumber = va_arg (itemlist, int);
582      if (type & TEXCOORD)
583        tmpElem->texCoordNumber = va_arg (itemlist, int);
584      if (type & NORMAL)
585        tmpElem->normalNumber = va_arg(itemlist, int);
586      this->currentGroup->currentFace->vertexCount++;
587    }
588  va_end(itemlist);
589
590  this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount - 2;
591  this->faceCount += this->currentGroup->currentFace->vertexCount -2;
592}
593
594/**
595 * Function that selects a material, if changed in the obj file.
596 * @param matString the Material that will be set.
597*/
598bool StaticModel::setMaterial(const char* matString)
599{
600  if (this->currentGroup->faceCount > 0)
601    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
602
603  this->currentGroup->currentFace->material = this->findMaterialByName(matString);
604
605  if (this->currentGroup->faceCount == 0)
606    this->currentGroup->faceCount++;
607}
608
609/**
610 * Function that selects a material, if changed in the obj file.
611 * @param mtl the Material that will be set.
612*/
613bool StaticModel::setMaterial(Material* mtl)
614{
615  if (this->currentGroup->faceCount > 0)
616    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
617
618  this->currentGroup->currentFace->material = mtl;
619
620  if (this->currentGroup->faceCount == 0)
621    this->currentGroup->faceCount++;
622}
623
624/**
625 * @brief A routine that is able to create normals.
626 *
627 * The algorithm does the following:
628 * 1. It calculates creates Vectors for each normale, and sets them to zero.
629 * 2. It then Walks through a) all the Groups b) all the Faces c) all the FaceElements
630 * 3. It searches for a points two neighbours per Face, takes Vecotrs to them calculates FaceNormals and adds it to the Points Normal.
631 * 4. It goes through all the normale-Points and calculates the VertexNormale and includes it in the normals-Array.
632 */
633bool StaticModel::buildVertexNormals ()
634{
635  PRINTF(4)("Normals are being calculated.\n");
636
637  Vector* normArray = new Vector [vertices.size()/3];
638  for (int i=0; i<vertices.size()/3;i++)
639    normArray[i] = Vector(.0,.0,.0);
640
641  int firstTouch;
642  int secondTouch;
643  Vector prevV;
644  Vector nextV;
645  Vector curV;
646
647  ModelGroup* tmpGroup = firstGroup;
648  while (tmpGroup != NULL)
649    {
650      ModelFace* tmpFace = tmpGroup->firstFace;
651      while (tmpFace != NULL)
652        {
653          if (tmpFace->firstElem != NULL)
654            {
655              ModelFaceElement* firstElem = tmpFace->firstElem;
656              ModelFaceElement* prevElem;
657              ModelFaceElement* curElem = firstElem;
658              ModelFaceElement* nextElem;
659              ModelFaceElement* lastElem;
660              // 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.
661              while (curElem != NULL)
662                {
663                  prevElem = curElem;
664                  curElem = curElem->next;
665                }
666              lastElem = prevElem;
667
668              curElem = firstElem;
669              for (int j=0; j<tmpFace->vertexCount; j++)
670                {
671                  if (!(nextElem = curElem->next))
672                    nextElem = firstElem;
673                  curElem->normalNumber = curElem->vertexNumber;
674
675                  curV = Vector (this->vertices[curElem->vertexNumber*3],
676                                 this->vertices[curElem->vertexNumber*3+1],
677                                 this->vertices[curElem->vertexNumber*3+2]);
678
679                  prevV = Vector (this->vertices[prevElem->vertexNumber*3],
680                                  this->vertices[prevElem->vertexNumber*3+1],
681                                  this->vertices[prevElem->vertexNumber*3+2]) - curV;
682
683                  nextV = Vector (this->vertices[nextElem->vertexNumber*3],
684                                  this->vertices[nextElem->vertexNumber*3+1],
685                                  this->vertices[nextElem->vertexNumber*3+2]) - curV;
686                  normArray[curElem->vertexNumber] = normArray[curElem->vertexNumber] + nextV.cross(prevV);
687
688                  prevElem = curElem;
689                  curElem = curElem->next;
690                }
691            }
692          tmpFace = tmpFace->next;
693        }
694      tmpGroup = tmpGroup->next;
695    }
696
697  for (int i=0; i < this->vertices.size()/3;i++)
698    {
699      normArray[i].normalize();
700      PRINTF(5)("Found Normale number %d: (%f; %f, %f).\n", i, normArray[i].x, normArray[i].y, normArray[i].z);
701
702      this->addVertexNormal(normArray[i].x, normArray[i].y, normArray[i].z);
703
704    }
705  delete[] normArray;
706}
707
708////////////
709// openGL //
710////////////
711/**
712 *  reads and includes the Faces/Materials into the openGL state Machine
713*/
714bool StaticModel::importToDisplayList()
715{
716  // finalize the Arrays
717  if (normals.size() == 0) // vertices-Array must be built for this
718    this->buildVertexNormals();
719
720  this->currentGroup = this->firstGroup;
721
722  while (this->currentGroup != NULL)
723    {
724
725      // creating a glList for the Group
726      if ((this->currentGroup->listNumber = glGenLists(1)) == 0)
727        {
728          PRINTF(2)("glList could not be created for this Model\n");
729          return false;
730        }
731      glNewList (this->currentGroup->listNumber, GL_COMPILE);
732
733      // Putting Faces to GL
734      ModelFace* tmpFace = this->currentGroup->firstFace;
735      while (tmpFace != NULL)
736        {
737          if (tmpFace->vertexCount == 0 && tmpFace->material != NULL)
738            {
739              if (this->currentGroup->faceMode != -1)
740                glEnd();
741              this->currentGroup->faceMode = 0;
742              Material* tmpMat;
743              if (tmpFace->material != NULL)
744                {
745                  tmpFace->material->select();
746                  PRINTF(5)("using material %s for coming Faces.\n", tmpFace->material->getName());
747                }
748            }
749
750          else if (tmpFace->vertexCount == 3)
751            {
752              if (this->currentGroup->faceMode != 3)
753                {
754                  if (this->currentGroup->faceMode != -1)
755                    glEnd();
756                  glBegin(GL_TRIANGLES);
757                }
758
759              this->currentGroup->faceMode = 3;
760              PRINTF(5)("found triag.\n");
761            }
762
763          else if (tmpFace->vertexCount == 4)
764            {
765              if (this->currentGroup->faceMode != 4)
766                {
767                  if (this->currentGroup->faceMode != -1)
768                    glEnd();
769                  glBegin(GL_QUADS);
770                }
771              this->currentGroup->faceMode = 4;
772              PRINTF(5)("found quad.\n");
773            }
774
775          else if (tmpFace->vertexCount > 4)
776            {
777              if (this->currentGroup->faceMode != -1)
778                glEnd();
779              glBegin(GL_POLYGON);
780              PRINTF(5)("Polygon with %i faces found.", tmpFace->vertexCount);
781              this->currentGroup->faceMode = tmpFace->vertexCount;
782            }
783
784          ModelFaceElement* tmpElem = tmpFace->firstElem;
785          while (tmpElem != NULL)
786            {
787              //      PRINTF(2)("%s\n", tmpElem->value);
788              this->addGLElement(tmpElem);
789              tmpElem = tmpElem->next;
790            }
791          tmpFace = tmpFace->next;
792        }
793      glEnd();
794      glEndList();
795
796      this->currentGroup = this->currentGroup->next;
797    }
798}
799
800
801/**
802 *  builds an array of triangles, that can later on be used for obb separation and octree separation
803 */
804bool StaticModel::buildTriangleList()
805{
806  if( unlikely(this->pModelInfo.pTriangles != NULL))
807    return true;
808  /* make sure, that all the arrays are finalized */
809  if( normals.size() == 0) // vertices-Array must be built for this
810    this->buildVertexNormals();
811
812  int index = 0;                   //!< the counter for the triangle array
813  ModelFaceElement* tmpElem;       //!< the temporary faceelement reference
814  ModelFace* tmpFace;              //!< the temporary face referece
815
816  /* count the number of triangles */
817  /* now iterate through all groups and build up the triangle list */
818  this->currentGroup = this->firstGroup;
819  while( this->currentGroup != NULL)
820  {
821    tmpFace = this->currentGroup->firstFace;
822    while( tmpFace != NULL)
823    {
824
825      /* if its a triangle just add it to the list */
826      if( tmpFace->vertexCount == 3)
827      {
828        ++this->pModelInfo.numTriangles;
829      } /* if the polygon is a quad */
830      else if( tmpFace->vertexCount == 4)
831      {
832        this->pModelInfo.numTriangles += 2;
833      }
834      else if( tmpFace->vertexCount > 4)
835      {
836        PRINTF(1)("This model (%s) got over 4 vertices per face <=> conflicts in the CD engine!\n", this->getName());
837      //exit(0);
838      }
839      tmpFace = tmpFace->next;
840    }
841    this->currentGroup = this->currentGroup->next;
842  }
843
844  PRINTF(3)("got %i triangles, %i vertices\n", this->pModelInfo.numTriangles, this->pModelInfo.numVertices);
845
846
847  /* write MODELINFO structure */
848
849  /* allocate memory for the new triangle structures */
850  if( (this->pModelInfo.pTriangles = new sTriangleExt[this->pModelInfo.numTriangles]) == NULL)
851  {
852    PRINTF(1)("Could not allocate memory for triangle list\n");
853    return false;
854  }
855
856  /* now iterate through all groups and build up the triangle list */
857  this->currentGroup = this->firstGroup;
858  while( this->currentGroup != NULL)
859  {
860      // Putting Faces to GL
861    tmpFace = this->currentGroup->firstFace;
862    while( tmpFace != NULL)
863    {
864      tmpElem = tmpFace->firstElem;
865
866      /* if its a triangle just add it to the list */
867      if( tmpFace->vertexCount == 3)
868      {
869        for( int j = 0; j < 3; ++j)
870        {
871          this->pModelInfo.pTriangles[index].indexToVertices[j] = (unsigned int)tmpElem->vertexNumber /* * 3 */;
872          this->pModelInfo.pTriangles[index].indexToNormals[j] = (unsigned int)tmpElem->normalNumber /* * 3 */;
873          this->pModelInfo.pTriangles[index].indexToTexCoor[j] = (unsigned int)tmpElem->texCoordNumber /* * 3 */;
874          tmpElem = tmpElem->next;
875        }
876        ++index;
877      } /* if the polygon is a quad */
878      else if( tmpFace->vertexCount == 4)
879      {
880
881        this->pModelInfo.pTriangles[index].indexToVertices[0] = (unsigned int)tmpElem->vertexNumber;
882        this->pModelInfo.pTriangles[index].indexToNormals[0] = (unsigned int)tmpElem->normalNumber;
883        this->pModelInfo.pTriangles[index].indexToTexCoor[0] = (unsigned int)tmpElem->texCoordNumber;
884
885        this->pModelInfo.pTriangles[index + 1].indexToVertices[0] = (unsigned int)tmpElem->vertexNumber;
886        this->pModelInfo.pTriangles[index + 1].indexToNormals[0] = (unsigned int)tmpElem->normalNumber;
887        this->pModelInfo.pTriangles[index + 1].indexToTexCoor[0] = (unsigned int)tmpElem->texCoordNumber;
888        tmpElem = tmpElem->next;
889
890        this->pModelInfo.pTriangles[index].indexToVertices[1] = (unsigned int)tmpElem->vertexNumber;
891        this->pModelInfo.pTriangles[index].indexToNormals[1] = (unsigned int)tmpElem->normalNumber;
892        this->pModelInfo.pTriangles[index].indexToTexCoor[1] = (unsigned int)tmpElem->texCoordNumber;
893        tmpElem = tmpElem->next;
894
895        this->pModelInfo.pTriangles[index].indexToVertices[2] = (unsigned int)tmpElem->vertexNumber;
896        this->pModelInfo.pTriangles[index].indexToNormals[2] = (unsigned int)tmpElem->normalNumber;
897        this->pModelInfo.pTriangles[index].indexToTexCoor[2] = (unsigned int)tmpElem->texCoordNumber;
898
899        this->pModelInfo.pTriangles[index + 1].indexToVertices[2] = (unsigned int)tmpElem->vertexNumber;
900        this->pModelInfo.pTriangles[index + 1].indexToNormals[2] = (unsigned int)tmpElem->normalNumber;
901        this->pModelInfo.pTriangles[index + 1].indexToTexCoor[2] = (unsigned int)tmpElem->texCoordNumber;
902        tmpElem = tmpElem->next;
903
904        this->pModelInfo.pTriangles[index + 1].indexToVertices[1] = (unsigned int)tmpElem->vertexNumber;
905        this->pModelInfo.pTriangles[index + 1].indexToNormals[1] = (unsigned int)tmpElem->normalNumber;
906        this->pModelInfo.pTriangles[index + 1].indexToTexCoor[1] = (unsigned int)tmpElem->texCoordNumber;
907
908        index += 2;
909      }
910      tmpFace = tmpFace->next;
911    }
912    this->currentGroup = this->currentGroup->next;
913  }
914  return true;
915}
916
917
918/**
919 *  Adds a Face-element (one vertex of a face) with all its information.
920 * @param elem The FaceElement to add to the OpenGL-environment.
921
922   It does this by searching:
923   1. The Vertex itself
924   2. The VertexNormale
925   3. The VertexTextureCoordinate
926   merging this information, the face will be drawn.
927*/
928bool StaticModel::addGLElement (ModelFaceElement* elem)
929{
930  PRINTF(5)("importing grafical Element to openGL.\n");
931
932  if (elem->texCoordNumber != -1)
933    {
934      if (likely(elem->texCoordNumber < this->pModelInfo.numTexCoor))
935        glTexCoord2fv(&this->vTexture[0] + elem->texCoordNumber * 2);
936      else
937        PRINTF(2)("TextureCoordinate %d is not in the List (max: %d)\nThe Model might be incomplete\n",
938                  elem->texCoordNumber, this->pModelInfo.numTexCoor);
939    }
940  if (elem->normalNumber != -1)
941    {
942    if (likely(elem->normalNumber < this->pModelInfo.numNormals))
943      glNormal3fv(&this->normals[0] + elem->normalNumber * 3);
944    else
945        PRINTF(2)("Normal %d is not in the List (max: %d)\nThe Model might be incomplete",
946                  elem->normalNumber, this->pModelInfo.numNormals);
947    }
948  if (elem->vertexNumber != -1)
949    {
950      if (likely(elem->vertexNumber < this->pModelInfo.numVertices))
951          glVertex3fv(&this->vertices[0]+ elem->vertexNumber * 3);
952      else
953        PRINTF(2)("Vertex %d is not in the List (max: %d)\nThe Model might be incomplete",
954                  elem->vertexNumber, this->pModelInfo.numVertices);
955    }
956
957}
958
959/**
960 *  Includes a default model
961
962   This will inject a Cube, because this is the most basic model.
963*/
964void StaticModel::cubeModel()
965{
966  this->addVertex (-0.5, -0.5, 0.5);
967  this->addVertex (0.5, -0.5, 0.5);
968  this->addVertex (-0.5, 0.5, 0.5);
969  this->addVertex (0.5, 0.5, 0.5);
970  this->addVertex (-0.5, 0.5, -0.5);
971  this->addVertex (0.5, 0.5, -0.5);
972  this->addVertex (-0.5, -0.5, -0.5);
973  this->addVertex (0.5, -0.5, -0.5);
974
975  this->addVertexTexture (0.0, 0.0);
976  this->addVertexTexture (1.0, 0.0);
977  this->addVertexTexture (0.0, 1.0);
978  this->addVertexTexture (1.0, 1.0);
979  this->addVertexTexture (0.0, 2.0);
980  this->addVertexTexture (1.0, 2.0);
981  this->addVertexTexture (0.0, 3.0);
982  this->addVertexTexture (1.0, 3.0);
983  this->addVertexTexture (0.0, 4.0);
984  this->addVertexTexture (1.0, 4.0);
985  this->addVertexTexture (2.0, 0.0);
986  this->addVertexTexture (2.0, 1.0);
987  this->addVertexTexture (-1.0, 0.0);
988  this->addVertexTexture (-1.0, 1.0);
989
990  this->addVertexNormal (0.0, 0.0, 1.0);
991  this->addVertexNormal (0.0, 0.0, 1.0);
992  this->addVertexNormal (0.0, 0.0, 1.0);
993  this->addVertexNormal (0.0, 0.0, 1.0);
994  this->addVertexNormal (0.0, 1.0, 0.0);
995  this->addVertexNormal (0.0, 1.0, 0.0);
996  this->addVertexNormal (0.0, 1.0, 0.0);
997  this->addVertexNormal (0.0, 1.0, 0.0);
998  this->addVertexNormal (0.0, 0.0, -1.0);
999  this->addVertexNormal (0.0, 0.0, -1.0);
1000  this->addVertexNormal (0.0, 0.0, -1.0);
1001  this->addVertexNormal (0.0, 0.0, -1.0);
1002  this->addVertexNormal (0.0, -1.0, 0.0);
1003  this->addVertexNormal (0.0, -1.0, 0.0);
1004  this->addVertexNormal (0.0, -1.0, 0.0);
1005  this->addVertexNormal (0.0, -1.0, 0.0);
1006  this->addVertexNormal (1.0, 0.0, 0.0);
1007  this->addVertexNormal (1.0, 0.0, 0.0);
1008  this->addVertexNormal (1.0, 0.0, 0.0);
1009  this->addVertexNormal (1.0, 0.0, 0.0);
1010  this->addVertexNormal (-1.0, 0.0, 0.0);
1011  this->addVertexNormal (-1.0, 0.0, 0.0);
1012  this->addVertexNormal (-1.0, 0.0, 0.0);
1013  this->addVertexNormal (-1.0, 0.0, 0.0);
1014
1015  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,1, 3,3,2, 2,2,3);
1016  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,2,4, 3,3,5, 5,5,6, 4,4,7);
1017  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,4,8, 5,5,9, 7,7,10, 6,6,11);
1018  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,6,12, 7,7,13, 1,9,14, 0,8,15);
1019  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,1,16, 7,10,17, 5,11,18, 3,3,19);
1020  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,12,20, 0,0,21, 2,2,22, 4,13,23);
1021}
Note: See TracBrowser for help on using the repository browser.