Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/resources/src/lib/graphics/importer/static_model.cc @ 8626

Last change on this file since 8626 was 7221, checked in by bensch, 19 years ago

orxonox/trunk: merged the std-branche back, it runs on windows and Linux

svn merge https://svn.orxonox.net/orxonox/branches/std . -r7202:HEAD

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