Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7732 was 7732, checked in by patrick, 18 years ago

orxonox: removed a memory leak

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