Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/lib/graphics/importer/static_model.cc @ 6046

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

orxonox/branches/controll: Should compile on windows again

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