Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 10, 2005, 2:32:13 AM (20 years ago)
Author:
bensch
Message:

orxonox/trunk: model is now able to return the values of its vertices

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/lib/graphics/importer/model.cc

    r4357 r4577  
    1 /* 
     1/*
    22   orxonox - the future of 3D-vertical-scrollers
    33
     
    2121#include <stdarg.h>
    2222
    23 #include "array.h"
    2423#include "vector.h"
    2524#include "list.h"
     
    3837  this->vertexNumber = -1;
    3938  this->normalNumber = -1;
    40   this->texCoordNumber = -1;   
     39  this->texCoordNumber = -1;
    4140
    4241  this->next = NULL;
     
    6059
    6160  this->firstElem = NULL;
    62  
     61
    6362  this->material = NULL;
    64  
     63
    6564  this->next = NULL;
    6665
     
    7675  if (this->firstElem != NULL)
    7776    delete this->firstElem;
    78  
     77
    7978  if (this->next != NULL)
    8079    delete this->next;
     
    8988  this->name = "";
    9089  this->faceMode = -1;
    91   this->faceCount = 0; 
     90  this->faceCount = 0;
    9291  this->next = NULL;
    93  
     92
    9493  this->firstFace = new ModelFace;
    9594  this->currentFace = this->firstFace;
     
    135134Model::Model(const char* modelName, MODEL_TYPE type)
    136135{
    137   PRINTF(4)("new 3D-Model is being created\n");
    138   this->name = NULL;
     136  PRINTF(4)("new 3D-Model is being created\n");
    139137  this->setName(modelName);
    140138  this->type = type;
     
    147145  this->normalCount = 0;
    148146  this->texCoordCount = 0;
    149  
     147
    150148  this->scaleFactor = 1;
    151149
     
    168166{
    169167  PRINTF(4)("Deleting Model ");
    170   if (this->name)
    171     {
    172       PRINT(4)("%s\n", this->name);
    173       delete []this->name;
     168  if (this->getName())
     169    {
     170      PRINT(4)("%s\n", this->getName());
    174171    }
    175172  else
     
    204201  // this creates the display List.
    205202  this->importToDisplayList();
    206  
     203
    207204
    208205  // deletes everything we allocated.
     
    223220void Model::draw (void) const
    224221{
    225   PRINTF(4)("drawing the 3D-Models\n"); 
     222  PRINTF(4)("drawing the 3D-Models\n");
    226223  ModelGroup* tmpGroup = this->firstGroup;
    227224  while (tmpGroup != NULL)
     
    239236   It does this by just calling the List that must have been created earlier.
    240237*/
    241 void Model::draw (int groupNumber) const 
     238void Model::draw (int groupNumber) const
    242239{
    243240  if (groupNumber >= this->groupCount)
     
    246243      return;
    247244    }
    248   PRINTF(4)("drawing the requested 3D-Models if found.\n"); 
     245  PRINTF(4)("drawing the requested 3D-Models if found.\n");
    249246  ModelGroup* tmpGroup = this->firstGroup;
    250247  int counter = 0;
     
    252249    {
    253250      if (counter == groupNumber)
    254         {
    255           PRINTF(4)("Drawing model number %i named %s\n", counter, tmpGroup->name);
    256           glCallList (tmpGroup->listNumber);
    257           return;
    258         }
     251        {
     252          PRINTF(4)("Drawing model number %i named %s\n", counter, tmpGroup->name);
     253          glCallList (tmpGroup->listNumber);
     254          return;
     255        }
    259256      ++counter;
    260257      tmpGroup = tmpGroup->next;
    261258    }
    262   PRINTF(2)("Model number %i in %s not Found.\n", groupNumber, this->name);
     259  PRINTF(2)("Model number %i in %s not Found.\n", groupNumber, this->getName());
    263260  return;
    264261
     
    273270void Model::draw (char* groupName) const
    274271{
    275   PRINTF(4)("drawing the requested 3D-Models if found.\n"); 
     272  PRINTF(4)("drawing the requested 3D-Models if found.\n");
    276273  ModelGroup* tmpGroup = this->firstGroup;
    277274  while (tmpGroup != NULL)
    278275    {
    279276      if (!strcmp(tmpGroup->name, groupName))
    280         {
    281           PRINTF(4)("Drawing model %s\n", tmpGroup->name);
    282           glCallList (tmpGroup->listNumber);
    283           return;
    284         }
     277        {
     278          PRINTF(4)("Drawing model %s\n", tmpGroup->name);
     279          glCallList (tmpGroup->listNumber);
     280          return;
     281        }
    285282      tmpGroup = tmpGroup->next;
    286283    }
    287   PRINTF(2)("Model Named %s in %s not Found.\n", groupName, this->name);
     284  PRINTF(2)("Model Named %s in %s not Found.\n", groupName, this->getName());
    288285  return;
    289286}
     
    292289// INIT //
    293290//////////
    294 /**
    295    \brief sets a name to the Model
    296    \param name The name to set to this Model
    297 */
    298 void Model::setName(const char* name)
    299 {
    300   if (this->name)
    301     delete []this->name;
    302   if (name)
    303     {
    304       this->name = new char[strlen(name)+1];
    305       strcpy(this->name, name);
    306     }
    307   else
    308     this->name = NULL;
    309 }
    310 
    311291/**
    312292   \brief deletes all the arrays
     
    334314  PRINTF(4)("cleaning up the 3D-Model to save Memory.\n");
    335315  this->firstGroup->cleanup();
    336   return true;
    337 }
    338 
    339 
     316  return true;
     317}
    340318
    341319//////////
     
    366344
    367345  // adding material to the List of materials
    368   this->materialList->add(newMat); 
     346  this->materialList->add(newMat);
    369347  return newMat;
    370348}
     
    382360    {
    383361      if (!strcmp(material->getName(), materialName))
    384         {
    385           delete tmpIt;
    386           return material;
    387         }
     362        {
     363          delete tmpIt;
     364          return material;
     365        }
    388366      material = tmpIt->nextElement();
    389367    }
     
    396374   \param groupString the new Group to create
    397375
    398    This function initializes a new Group. 
     376   This function initializes a new Group.
    399377   With it you should be able to create Models with more than one SubModel inside
    400378*/
     
    439417   \param y the Y-coordinate of the Vertex to add.
    440418   \param z the Z-coordinate of the Vertex to add.
    441    
     419
    442420*/
    443421bool Model::addVertex(float x, float y, float z)
     
    538516    {
    539517      if (this->currentGroup->currentFace->vertexCount>0)
    540           tmpElem = tmpElem->next = new ModelFaceElement;
     518          tmpElem = tmpElem->next = new ModelFaceElement;
    541519      tmpElem->next = NULL;
    542520
     
    552530
    553531      if ((texture = strstr (vertex, "/")) != NULL)
    554         {
    555           texture[0] = '\0';
    556           texture ++;
    557          
    558           if ((normal = strstr (texture, "/")) !=NULL)
    559             {
    560               normal[0] = '\0';
    561               normal ++;
    562             }     
    563         }
     532        {
     533          texture[0] = '\0';
     534          texture ++;
     535
     536          if ((normal = strstr (texture, "/")) !=NULL)
     537            {
     538              normal[0] = '\0';
     539              normal ++;
     540            }
     541        }
    564542      if (vertex)
    565         tmpElem->vertexNumber = atoi(vertex)-1;
     543        tmpElem->vertexNumber = atoi(vertex)-1;
    566544      if (texture)
    567         tmpElem->texCoordNumber = atoi(texture)-1;
     545        tmpElem->texCoordNumber = atoi(texture)-1;
    568546      if (normal)
    569         tmpElem->normalNumber = atoi(normal)-1;
     547        tmpElem->normalNumber = atoi(normal)-1;
    570548
    571549      faceString += tmpLen;
    572550      if (strcmp (faceString, "\0"))
    573         faceString++;
     551        faceString++;
    574552      this->currentGroup->currentFace->vertexCount++;
    575553    }
     
    587565  if (this->currentGroup->faceCount > 0)
    588566    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
    589  
     567
    590568  ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement;
    591  
     569
    592570  va_list itemlist;
    593571  va_start (itemlist, type);
     
    596574    {
    597575      if (this->currentGroup->currentFace->vertexCount > 0)
    598         tmpElem = tmpElem->next = new ModelFaceElement;
     576        tmpElem = tmpElem->next = new ModelFaceElement;
    599577
    600578      tmpElem->vertexNumber = va_arg (itemlist, int);
    601579      if (type & TEXCOORD)
    602         tmpElem->texCoordNumber = va_arg (itemlist, int);
     580        tmpElem->texCoordNumber = va_arg (itemlist, int);
    603581      if (type & NORMAL)
    604         tmpElem->normalNumber = va_arg(itemlist, int);
     582        tmpElem->normalNumber = va_arg(itemlist, int);
    605583      this->currentGroup->currentFace->vertexCount++;
    606584    }
     
    618596  if (this->currentGroup->faceCount > 0)
    619597    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
    620  
     598
    621599  this->currentGroup->currentFace->material = this->findMaterialByName(matString);
    622600
     
    633611  if (this->currentGroup->faceCount > 0)
    634612    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
    635  
     613
    636614  this->currentGroup->currentFace->material = mtl;
    637615
     
    650628*/
    651629bool Model::buildVertexNormals ()
    652 { 
     630{
    653631  PRINTF(4)("Normals are being calculated.\n");
    654632
     
    656634  for (int i=0; i<vertices->getCount()/3;i++)
    657635    normArray[i] = Vector(.0,.0,.0);
    658  
     636
    659637  int firstTouch;
    660638  int secondTouch;
     
    668646      ModelFace* tmpFace = tmpGroup->firstFace;
    669647      while (tmpFace)
    670         {
    671           if (tmpFace->firstElem)
    672             {
    673               ModelFaceElement* firstElem = tmpFace->firstElem;
    674               ModelFaceElement* prevElem;
    675               ModelFaceElement* curElem = firstElem;
    676               ModelFaceElement* nextElem;
    677               ModelFaceElement* lastElem;
    678               // 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.
    679               while (curElem)
    680                 {
    681                   prevElem = curElem;
    682                   curElem = curElem->next;
    683                 }
    684               lastElem = prevElem;
    685              
    686               curElem = firstElem;
    687               for (int j=0; j<tmpFace->vertexCount; j++)
    688                 {
    689                   if (!(nextElem = curElem->next))
    690                     nextElem = firstElem;
    691                   curElem->normalNumber = curElem->vertexNumber;
    692                  
    693                   curV = Vector (vertices->getArray()[curElem->vertexNumber*3], vertices->getArray()[curElem->vertexNumber*3+1], vertices->getArray()[curElem->vertexNumber*3+2]);
    694                   prevV = Vector (vertices->getArray()[prevElem->vertexNumber*3], vertices->getArray()[prevElem->vertexNumber*3+1], vertices->getArray()[prevElem->vertexNumber*3+2]) - curV;
    695                   nextV = Vector (vertices->getArray()[nextElem->vertexNumber*3], vertices->getArray()[nextElem->vertexNumber*3+1], vertices->getArray()[nextElem->vertexNumber*3+2]) - curV;
    696                   normArray[curElem->vertexNumber] = normArray[curElem->vertexNumber] + nextV.cross(prevV);
    697 
    698                   prevElem = curElem;
    699                   curElem = curElem->next;
    700                 }
    701             }
    702           tmpFace = tmpFace->next;
    703         }
     648        {
     649          if (tmpFace->firstElem)
     650            {
     651              ModelFaceElement* firstElem = tmpFace->firstElem;
     652              ModelFaceElement* prevElem;
     653              ModelFaceElement* curElem = firstElem;
     654              ModelFaceElement* nextElem;
     655              ModelFaceElement* lastElem;
     656              // 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.
     657              while (curElem)
     658                {
     659                  prevElem = curElem;
     660                  curElem = curElem->next;
     661                }
     662              lastElem = prevElem;
     663
     664              curElem = firstElem;
     665              for (int j=0; j<tmpFace->vertexCount; j++)
     666                {
     667                  if (!(nextElem = curElem->next))
     668                    nextElem = firstElem;
     669                  curElem->normalNumber = curElem->vertexNumber;
     670
     671                  curV = Vector (vertices->getArray()[curElem->vertexNumber*3], vertices->getArray()[curElem->vertexNumber*3+1], vertices->getArray()[curElem->vertexNumber*3+2]);
     672                  prevV = Vector (vertices->getArray()[prevElem->vertexNumber*3], vertices->getArray()[prevElem->vertexNumber*3+1], vertices->getArray()[prevElem->vertexNumber*3+2]) - curV;
     673                  nextV = Vector (vertices->getArray()[nextElem->vertexNumber*3], vertices->getArray()[nextElem->vertexNumber*3+1], vertices->getArray()[nextElem->vertexNumber*3+2]) - curV;
     674                  normArray[curElem->vertexNumber] = normArray[curElem->vertexNumber] + nextV.cross(prevV);
     675
     676                  prevElem = curElem;
     677                  curElem = curElem->next;
     678                }
     679            }
     680          tmpFace = tmpFace->next;
     681        }
    704682      tmpGroup = tmpGroup->next;
    705683    }
     
    709687      normArray[i].normalize();
    710688      PRINTF(5)("Found Normale number %d: (%f; %f, %f).\n", i, normArray[i].x, normArray[i].y, normArray[i].z);
    711      
     689
    712690      this->addVertexNormal(normArray[i].x, normArray[i].y, normArray[i].z);
    713691
    714692    }
    715   delete []normArray; 
     693  delete []normArray;
    716694}
    717695
     
    738716      // creating a glList for the Group
    739717      if ((this->currentGroup->listNumber = glGenLists(1)) == 0)
    740         {
    741           PRINTF(2)("glList could not be created for this Model\n");
    742           return false;
    743         }
     718        {
     719          PRINTF(2)("glList could not be created for this Model\n");
     720          return false;
     721        }
    744722      glNewList (this->currentGroup->listNumber, GL_COMPILE);
    745723
     
    747725      ModelFace* tmpFace = this->currentGroup->firstFace;
    748726      while (tmpFace != NULL)
    749         {
    750           if (tmpFace->vertexCount == 0 && tmpFace->material != NULL)
    751             {
    752               if (this->currentGroup->faceMode != -1)
    753                 glEnd();
    754               this->currentGroup->faceMode = 0;
    755               Material* tmpMat;
    756               if (tmpFace->material != NULL)
    757                 {
    758                   tmpFace->material->select();
    759                   PRINTF(5)("using material %s for coming Faces.\n", tmpFace->material->getName());
    760                 }
    761             }
    762 
    763           else if (tmpFace->vertexCount == 3)
    764             {
    765               if (this->currentGroup->faceMode != 3)
    766                 {
    767                   if (this->currentGroup->faceMode != -1)
    768                     glEnd();
    769                   glBegin(GL_TRIANGLES);
    770                 }
    771              
    772               this->currentGroup->faceMode = 3;
    773               PRINTF(5)("found triag.\n");
    774             }
    775          
    776           else if (tmpFace->vertexCount == 4)
    777             {
    778               if (this->currentGroup->faceMode != 4)
    779                 {
    780                   if (this->currentGroup->faceMode != -1)
    781                     glEnd();
    782                   glBegin(GL_QUADS);
    783                 }
    784               this->currentGroup->faceMode = 4;
    785               PRINTF(5)("found quad.\n");
    786             }
    787          
    788           else if (tmpFace->vertexCount > 4)
    789             {
    790               if (this->currentGroup->faceMode != -1)
    791                 glEnd();
    792               glBegin(GL_POLYGON);
    793               PRINTF(5)("Polygon with %i faces found.", tmpFace->vertexCount);
    794               this->currentGroup->faceMode = tmpFace->vertexCount;
    795             }
    796          
    797           ModelFaceElement* tmpElem = tmpFace->firstElem;
    798           while (tmpElem != NULL)
    799             {
    800               //      PRINTF(2)("%s\n", tmpElem->value);
    801               this->addGLElement(tmpElem);
    802               tmpElem = tmpElem->next;
    803             }
    804           tmpFace = tmpFace->next;
    805         }
     727        {
     728          if (tmpFace->vertexCount == 0 && tmpFace->material != NULL)
     729            {
     730              if (this->currentGroup->faceMode != -1)
     731                glEnd();
     732              this->currentGroup->faceMode = 0;
     733              Material* tmpMat;
     734              if (tmpFace->material != NULL)
     735                {
     736                  tmpFace->material->select();
     737                  PRINTF(5)("using material %s for coming Faces.\n", tmpFace->material->getName());
     738                }
     739            }
     740
     741          else if (tmpFace->vertexCount == 3)
     742            {
     743              if (this->currentGroup->faceMode != 3)
     744                {
     745                  if (this->currentGroup->faceMode != -1)
     746                    glEnd();
     747                  glBegin(GL_TRIANGLES);
     748                }
     749
     750              this->currentGroup->faceMode = 3;
     751              PRINTF(5)("found triag.\n");
     752            }
     753
     754          else if (tmpFace->vertexCount == 4)
     755            {
     756              if (this->currentGroup->faceMode != 4)
     757                {
     758                  if (this->currentGroup->faceMode != -1)
     759                    glEnd();
     760                  glBegin(GL_QUADS);
     761                }
     762              this->currentGroup->faceMode = 4;
     763              PRINTF(5)("found quad.\n");
     764            }
     765
     766          else if (tmpFace->vertexCount > 4)
     767            {
     768              if (this->currentGroup->faceMode != -1)
     769                glEnd();
     770              glBegin(GL_POLYGON);
     771              PRINTF(5)("Polygon with %i faces found.", tmpFace->vertexCount);
     772              this->currentGroup->faceMode = tmpFace->vertexCount;
     773            }
     774
     775          ModelFaceElement* tmpElem = tmpFace->firstElem;
     776          while (tmpElem != NULL)
     777            {
     778              //      PRINTF(2)("%s\n", tmpElem->value);
     779              this->addGLElement(tmpElem);
     780              tmpElem = tmpElem->next;
     781            }
     782          tmpFace = tmpFace->next;
     783        }
    806784      glEnd();
    807785      glEndList();
    808786
    809787      this->currentGroup = this->currentGroup->next;
    810     } 
     788    }
    811789}
    812790
     
    832810
    833811/**
    834    \brief Adds a Face-element (one vertex of a face) with all its information. 
     812   \brief Adds a Face-element (one vertex of a face) with all its information.
    835813   \param elem The FaceElement to add to the OpenGL-environment.
    836814
     
    848826    {
    849827      if (likely(elem->texCoordNumber < this->texCoordCount))
    850         glTexCoord2fv(this->vTexture->getArray() + elem->texCoordNumber * 2);
     828        glTexCoord2fv(this->vTexture->getArray() + elem->texCoordNumber * 2);
    851829      else
    852         PRINTF(2)("TextureCoordinate %d is not in the List (max: %d)\nThe Model might be incomplete\n",
    853                   elem->texCoordNumber, this->texCoordCount);
     830        PRINTF(2)("TextureCoordinate %d is not in the List (max: %d)\nThe Model might be incomplete\n",
     831                  elem->texCoordNumber, this->texCoordCount);
    854832    }
    855833  if (elem->normalNumber != -1)
     
    858836      glNormal3fv(this->normals->getArray() + elem->normalNumber * 3);
    859837    else
    860         PRINTF(2)("Normal %d is not in the List (max: %d)\nThe Model might be incomplete",
    861                   elem->normalNumber, this->normalCount);     
     838        PRINTF(2)("Normal %d is not in the List (max: %d)\nThe Model might be incomplete",
     839                  elem->normalNumber, this->normalCount);
    862840    }
    863841  if (elem->vertexNumber != -1)
    864842    {
    865843      if (likely(elem->vertexNumber < this->vertexCount))
    866           glVertex3fv(this->vertices->getArray() + elem->vertexNumber * 3);
     844          glVertex3fv(this->vertices->getArray() + elem->vertexNumber * 3);
    867845      else
    868         PRINTF(2)("Vertex %d is not in the List (max: %d)\nThe Model might be incomplete",
    869                   elem->vertexNumber, this->vertexCount);     
    870     }   
     846        PRINTF(2)("Vertex %d is not in the List (max: %d)\nThe Model might be incomplete",
     847                  elem->vertexNumber, this->vertexCount);
     848    }
    871849
    872850}
Note: See TracChangeset for help on using the changeset viewer.