Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10141 in orxonox.OLD for trunk/src/lib


Ignore:
Timestamp:
Dec 23, 2006, 12:05:13 PM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: ported the static_model to std::vectort.
Took me 90 minutes, and i hope everything is still working as it did before :)

Location:
trunk/src/lib/graphics/importer
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/graphics/importer/static_model.h

    r9869 r10141  
    3737  void rebuild() { data->rebuild(); };
    3838
    39   Material* addMaterial(Material* material) { return data->addMaterial(material); };
     39  Material* addMaterial(const Material& material) { return data->addMaterial(material); };
    4040  Material* addMaterial(const std::string& materialName) { return data->addMaterial(materialName); };
    4141
  • trunk/src/lib/graphics/importer/static_model_data.cc

    r9869 r10141  
    2222#include "debug.h"
    2323#include <stdarg.h>
    24 
     24#include <algorithm>
    2525
    2626
     
    3232 * @brief creates a new ModelFaceElement
    3333 */
    34 ModelFaceElement::ModelFaceElement()
     34StaticModelData::FaceElement::FaceElement()
    3535{
    3636  this->vertexNumber = -1;
    3737  this->normalNumber = -1;
    3838  this->texCoordNumber = -1;
    39 
    40   this->next = NULL;
    41 }
    42 
    43 /**
    44  * @brief destroys a ModelFaceElement
    45  */
    46 ModelFaceElement::~ModelFaceElement()
    47 {
    48   if (this->next)
    49     delete this->next;
    50 }
     39}
     40
    5141
    5242/**
    5343 * @brief creates a new ModelFace
    5444 */
    55 ModelFace::ModelFace()
    56 {
    57   this->vertexCount = 0;
    58 
    59   this->firstElem = NULL;
    60 
    61   this->material = NULL;
    62 
    63   this->next = NULL;
    64 }
    65 
    66 /**
    67  *  deletes a ModelFace
    68 */
    69 ModelFace::~ModelFace()
    70 {
    71   PRINTF(5)("Cleaning up Face\n");
    72 
    73   if (this->firstElem != NULL)
    74     delete this->firstElem;
    75 
    76   if (this->next != NULL)
    77     delete this->next;
     45StaticModelData::Face::Face()
     46{
     47  this->_material = NULL;
    7848}
    7949
     
    8151 * @brief Creates a new ModelGroup
    8252 */
    83 ModelGroup::ModelGroup()
     53StaticModelData::Group::Group()
    8454{
    8555  PRINTF(4)("Adding new Group\n");
    8656  this->name = "";
    8757  this->faceMode = -1;
    88   this->faceCount = 0;
    89   this->next = NULL;
    9058  this->listNumber = 0;
    9159  this->indices = NULL;
    92 
    93   this->firstFace = new ModelFace;
    94   this->currentFace = this->firstFace;
    9560}
    9661
     
    9863 * @brief deletes a ModelGroup
    9964 */
    100 ModelGroup::~ModelGroup()
     65StaticModelData::Group::~Group()
    10166{
    10267  PRINTF(5)("Cleaning up group\n");
    103   if (this->firstFace != NULL)
    104     delete this->firstFace;
    105 
    10668  // deleting the glList
    10769  if (this->listNumber != 0)
    10870    glDeleteLists(this->listNumber, 1);
    109 
    110   if (this->next !=NULL)
    111     delete this->next;
    112 
    11371}
    11472
     
    11876 * actually does the same as the delete Operator, but does not delete the predecessing group
    11977 */
    120 void ModelGroup::cleanup()
     78void StaticModelData::Group::cleanup()
    12179{
    12280  PRINTF(5)("Cleaning up group\n");
    123   if (this->firstFace)
    124     delete this->firstFace;
    125   this->firstFace = NULL;
    126   if (this->next)
    127     this->next->cleanup();
     81  this->_faces.clear();
    12882}
    12983
     
    150104
    151105  // setting the start group;
    152   this->currentGroup = this->firstGroup = new ModelGroup;
    153   this->groupCount = 0;
     106  this->_modelGroups.push_back(Group());
    154107  this->faceCount = 0;
    155108
     
    164117StaticModelData::~StaticModelData()
    165118{
    166   PRINTF(4)("Deleting Model ");
    167   if (!this->getName().empty())
    168   {
    169     PRINT(4)("%s\n", this->getCName());
    170   }
    171   else
    172   {
    173     PRINT(4)("\n");
    174   }
     119  PRINTF(4)("Deleting Model %s\n", this->getCName());
    175120  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   std::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   }
    191121}
    192122
     
    223153{
    224154  PRINTF(4)("drawing the 3D-Models\n");
    225   ModelGroup* tmpGroup = this->firstGroup;
    226   while (tmpGroup != NULL)
    227   {
    228     PRINTF(5)("Drawing model %s\n", tmpGroup->name.c_str());
    229     glCallList (tmpGroup->listNumber);
    230     tmpGroup = tmpGroup->next;
     155
     156  for(unsigned int i = 0; i < _modelGroups.size(); ++i )
     157  {
     158    PRINTF(5)("Drawing model %s\n", _modelGroups[i].name.c_str());
     159    glCallList (_modelGroups[i].listNumber);
    231160  }
    232161}
     
    239168 * It does this by just calling the List that must have been created earlier.
    240169 */
    241 void StaticModelData::draw (int groupNumber) const
    242 {
    243   if (unlikely(groupNumber >= this->groupCount))
    244   {
    245     PRINTF(2)("You requested model number %i, but this File only contains of %i Models.\n", groupNumber-1, this->groupCount);
     170void StaticModelData::draw (unsigned int groupNumber) const
     171{
     172  if (unlikely(groupNumber >= _modelGroups.size()))
     173  {
     174    PRINTF(2)("You requested model number %i, but this File only contains of %i Models.\n", groupNumber-1, _modelGroups.size());
    246175    return;
    247176  }
    248   PRINTF(4)("drawing the requested 3D-Models if found.\n");
    249   ModelGroup* tmpGroup = this->firstGroup;
    250   int counter = 0;
    251   while (tmpGroup != NULL)
    252   {
    253     if (counter == groupNumber)
    254     {
    255       PRINTF(4)("Drawing model number %i named %s\n", counter, tmpGroup->name.c_str());
    256       glCallList (tmpGroup->listNumber);
    257       return;
    258     }
    259     ++counter;
    260     tmpGroup = tmpGroup->next;
    261   }
    262   PRINTF(2)("Model number %i in %s not Found.\n", groupNumber, this->getCName());
    263   return;
     177  else
     178  {
     179    PRINTF(4)("Drawing model number %i named %s\n", groupNumber, _modelGroups[groupNumber].name.c_str());
     180    glCallList (_modelGroups[groupNumber].listNumber);
     181  }
    264182}
    265183
     
    274192{
    275193  PRINTF(4)("drawing the requested 3D-Models if found.\n");
    276   ModelGroup* tmpGroup = this->firstGroup;
    277   while (tmpGroup != NULL)
    278   {
    279     if (tmpGroup->name == groupName)
    280     {
    281       PRINTF(4)("Drawing model %s\n", tmpGroup->name.c_str());
    282       glCallList (tmpGroup->listNumber);
    283       return;
    284     }
    285     tmpGroup = tmpGroup->next;
    286   }
    287   PRINTF(2)("Model Named %s in %s not Found.\n", groupName.c_str(), this->getCName());
    288   return;
     194  std::vector<Group>::const_iterator it = std::find(_modelGroups.begin(), _modelGroups.end(), groupName);
     195
     196  if (it != _modelGroups.end())
     197  {
     198    PRINTF(4)("Drawing model %s\n", (*it).name.c_str());
     199    glCallList ((*it).listNumber);
     200  }
     201  else
     202  {
     203    PRINTF(2)("Model Named %s in %s not Found.\n", groupName.c_str(), this->getCName());
     204  }
    289205}
    290206
     
    303219{
    304220  PRINTF(4)("cleaning up the 3D-Model to save Memory.\n");
    305   this->firstGroup->cleanup();
     221  for (unsigned int i = 0; i < _modelGroups.size(); ++i)
     222    _modelGroups[i].cleanup();
    306223  return true;
    307224}
     
    318235 * with this option set the Materials will not be deleted with the Model.
    319236 */
    320 Material* StaticModelData::addMaterial(Material* material)
    321 {
    322   if (material == NULL)
    323     return NULL;
    324   ModelMaterial* modMat = new ModelMaterial;
    325   modMat->external = true;
    326   modMat->material = material;
    327   this->materialList.push_back(modMat);
    328   return modMat->material;
     237Material* StaticModelData::addMaterial(const Material& material)
     238{
     239  this->materialList.push_back(material);
     240  return &materialList.back();
    329241}
    330242
     
    336248Material* StaticModelData::addMaterial(const std::string& materialName)
    337249{
    338   ModelMaterial* modMat = new ModelMaterial;
    339   modMat->external = false;
    340   modMat->material = new Material(materialName);
    341 
    342250  // adding material to the List of materials
    343   this->materialList.push_back(modMat);
    344   return modMat->material;
     251  this->materialList.push_back(Material(materialName));
     252  return &materialList.back();
    345253}
    346254
     
    352260Material* StaticModelData::findMaterialByName(const std::string& materialName)
    353261{
    354   std::list<ModelMaterial*>::iterator modMat;
    355   for  (modMat = this->materialList.begin(); modMat != this->materialList.end(); modMat++)
    356     if (materialName == (*modMat)->material->getName())
    357       return (*modMat)->material;
    358   return NULL;
     262  std::list<Material>::iterator it = std::find(materialList.begin(), materialList.end(), materialName);
     263  if (it != materialList.end())
     264    return &(*it);
     265  else
     266    return NULL;
    359267}
    360268
     
    369277{
    370278  PRINTF(5)("Read Group: %s.\n", groupString.c_str());
    371   if (this->groupCount != 0 && this->currentGroup->faceCount > 0)
    372   {
    373     // finalizeGroup(currentGroup);
    374     this->currentGroup = this->currentGroup->next = new ModelGroup;
    375   }
     279  if (!_modelGroups.empty() && !_modelGroups.back()._faces.empty())
     280    _modelGroups.push_back(Group());
     281
     282  if (groupString == "default")
     283    _modelGroups.back().name = groupString;
    376284  // setting the group name if not default.
    377   if (groupString == "default")
    378   {
    379     this->currentGroup->name = groupString;
    380   }
    381   ++this->groupCount;
    382285  return true;
    383286}
     
    500403{
    501404  const char* faceString = faceStringInput.c_str();
    502   if (this->currentGroup->faceCount >0)
    503     this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
    504 
    505   ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement;
    506   tmpElem->next = NULL;
     405  Face newFace;
     406
    507407  while(strcmp (faceString, "\0"))
    508408  {
    509     if (this->currentGroup->currentFace->vertexCount>0)
    510       tmpElem = tmpElem->next = new ModelFaceElement;
    511     tmpElem->next = NULL;
     409    FaceElement newElem;
    512410
    513411    char tmpValue [50];
     
    533431    }
    534432    if (vertex)
    535       tmpElem->vertexNumber = atoi(vertex)-1;
     433      newElem.vertexNumber = atoi(vertex)-1;
    536434    if (texture)
    537       tmpElem->texCoordNumber = atoi(texture)-1;
     435      newElem.texCoordNumber = atoi(texture)-1;
    538436    if (normal)
    539       tmpElem->normalNumber = atoi(normal)-1;
     437      newElem.normalNumber = atoi(normal)-1;
    540438
    541439    faceString += tmpLen;
    542440    if (strcmp (faceString, "\0"))
    543441      faceString++;
    544     this->currentGroup->currentFace->vertexCount++;
    545   }
    546 
    547   this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount -2;
    548   this->faceCount += this->currentGroup->currentFace->vertexCount -2;
     442
     443    newFace._elements.push_back(newElem);
     444  }
     445
     446  //this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount -2;
     447  _modelGroups.back()._faces.push_back(newFace);
     448  this->faceCount += newFace._elements.size() - 2;
    549449  return true;
    550450}
     
    557457bool StaticModelData::addFace(int faceElemCount, VERTEX_FORMAT type, va_list args)
    558458{
    559   if (this->currentGroup->faceCount > 0)
    560     this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
    561 
    562   ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement;
     459  Face newFace;
    563460
    564461  for (int i = 0; i < faceElemCount; i++)
    565462  {
    566     if (this->currentGroup->currentFace->vertexCount > 0)
    567       tmpElem = tmpElem->next = new ModelFaceElement;
    568 
    569     tmpElem->vertexNumber = va_arg (args, int);
     463    FaceElement newElem;
     464
     465    newElem.vertexNumber = va_arg (args, int);
    570466    if (type & TEXCOORD)
    571       tmpElem->texCoordNumber = va_arg (args, int);
     467      newElem.texCoordNumber = va_arg (args, int);
    572468    if (type & NORMAL)
    573       tmpElem->normalNumber = va_arg(args, int);
    574     this->currentGroup->currentFace->vertexCount++;
    575   }
    576 
    577   this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount - 2;
    578   this->faceCount += this->currentGroup->currentFace->vertexCount -2;
     469      newElem.normalNumber = va_arg(args, int);
     470
     471    newFace._elements.push_back(newElem);
     472  }
     473
     474  //this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount -2;
     475  _modelGroups.back()._faces.push_back(newFace);
     476  this->faceCount += newFace._elements.size() - 2;
    579477  return true;
    580478}
     
    586484bool StaticModelData::setMaterial(const std::string& matString)
    587485{
    588   if (this->currentGroup->faceCount > 0)
    589     this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
    590 
    591   this->currentGroup->currentFace->material = this->findMaterialByName(matString);
    592 
    593   if (this->currentGroup->faceCount == 0)
    594     this->currentGroup->faceCount++;
     486  Face matFace;
     487  matFace._material = this->findMaterialByName(matString);
     488  _modelGroups.back()._faces.push_back(matFace);
     489
    595490  return true;
    596491}
     
    602497bool StaticModelData::setMaterial(Material* mtl)
    603498{
    604   if (this->currentGroup->faceCount > 0)
    605     this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
    606 
    607   this->currentGroup->currentFace->material = mtl;
    608 
    609   if (this->currentGroup->faceCount == 0)
    610     this->currentGroup->faceCount++;
     499  Face matFace;
     500  matFace._material = mtl;
     501  _modelGroups.back()._faces.push_back(matFace);
     502
    611503  return true;
    612504}
     
    633525  Vector curV;
    634526
    635   ModelGroup* tmpGroup = firstGroup;
    636   while (tmpGroup != NULL)
    637   {
    638     ModelFace* tmpFace = tmpGroup->firstFace;
    639     while (tmpFace != NULL)
     527  for (std::vector<Group>::iterator group = _modelGroups.begin();
     528       group != _modelGroups.end();
     529       ++group)
     530  {
     531    for (std::vector<Face>::iterator face = (*group)._faces.begin();
     532         face != (*group)._faces.end();
     533         ++face)
    640534    {
    641       if (tmpFace->firstElem != NULL)
    642       {
    643         ModelFaceElement* firstElem = tmpFace->firstElem;
    644         ModelFaceElement* prevElem;
    645         ModelFaceElement* curElem = firstElem;
    646         ModelFaceElement* nextElem;
    647         ModelFaceElement* lastElem;
    648         // 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.
    649         while (curElem != NULL)
     535      if (!(*face)._elements.empty())
     536      {
     537        std::vector<FaceElement>::iterator firstElem = (*face)._elements.begin();
     538        std::vector<FaceElement>::iterator prevElem;
     539        std::vector<FaceElement>::iterator curElem = firstElem;
     540        std::vector<FaceElement>::iterator nextElem;
     541        std::vector<FaceElement>::iterator lastElem;
     542
     543        // find last Element of the Chain.
     544        while (curElem != (*face)._elements.end())
    650545        {
    651546          prevElem = curElem;
    652           curElem = curElem->next;
     547          ++curElem;
    653548        }
    654549        lastElem = prevElem;
    655550
    656551        curElem = firstElem;
    657         for (unsigned int j = 0; j < tmpFace->vertexCount; j++)
     552        for (unsigned int j = 0; j < (*face)._elements.size(); j++)
    658553        {
    659           if (!(nextElem = curElem->next))
     554          nextElem = curElem;
     555          nextElem++;
     556          if (nextElem == (*face)._elements.end())
    660557            nextElem = firstElem;
    661           curElem->normalNumber = curElem->vertexNumber;
    662 
    663           curV = Vector (this->vertices[curElem->vertexNumber*3],
    664                          this->vertices[curElem->vertexNumber*3+1],
    665                          this->vertices[curElem->vertexNumber*3+2]);
    666 
    667           prevV = Vector (this->vertices[prevElem->vertexNumber*3],
    668                           this->vertices[prevElem->vertexNumber*3+1],
    669                           this->vertices[prevElem->vertexNumber*3+2]) - curV;
    670 
    671           nextV = Vector (this->vertices[nextElem->vertexNumber*3],
    672                           this->vertices[nextElem->vertexNumber*3+1],
    673                           this->vertices[nextElem->vertexNumber*3+2]) - curV;
    674           normArray[curElem->vertexNumber] = normArray[curElem->vertexNumber] + nextV.cross(prevV);
     558          (*curElem).normalNumber = (*curElem).vertexNumber;
     559
     560          curV = Vector (this->vertices[(*curElem).vertexNumber*3],
     561                         this->vertices[(*curElem).vertexNumber*3+1],
     562                         this->vertices[(*curElem).vertexNumber*3+2]);
     563
     564          prevV = Vector (this->vertices[(*prevElem).vertexNumber*3],
     565                          this->vertices[(*prevElem).vertexNumber*3+1],
     566                          this->vertices[(*prevElem).vertexNumber*3+2]) - curV;
     567
     568          nextV = Vector (this->vertices[(*nextElem).vertexNumber*3],
     569                          this->vertices[(*nextElem).vertexNumber*3+1],
     570                          this->vertices[(*nextElem).vertexNumber*3+2]) - curV;
     571          normArray[(*curElem).vertexNumber] = normArray[(*curElem).vertexNumber] + nextV.cross(prevV);
    675572
    676573          prevElem = curElem;
    677           curElem = curElem->next;
     574          ++curElem;
    678575        }
    679576      }
    680       tmpFace = tmpFace->next;
    681577    }
    682     tmpGroup = tmpGroup->next;
    683578  }
    684579
     
    707602    this->buildVertexNormals();
    708603
    709   this->currentGroup = this->firstGroup;
    710 
    711   while (this->currentGroup != NULL)
     604  for (std::vector<Group>::iterator group = _modelGroups.begin();
     605       group != _modelGroups.end();
     606       ++group)
    712607  {
    713608
    714609    // creating a glList for the Group
    715     if ((this->currentGroup->listNumber = glGenLists(1)) == 0)
     610    if (((*group).listNumber = glGenLists(1)) == 0)
    716611    {
    717612      PRINTF(2)("glList could not be created for this Model\n");
    718613      return false;
    719614    }
    720     glNewList (this->currentGroup->listNumber, GL_COMPILE);
     615    glNewList ((*group).listNumber, GL_COMPILE);
    721616
    722617    // Putting Faces to GL
    723     ModelFace* tmpFace = this->currentGroup->firstFace;
    724     while (tmpFace != NULL)
     618    for (std::vector<Face>::const_iterator face = (*group)._faces.begin();
     619         face != (*group)._faces.end();
     620         ++face)
    725621    {
    726       if (tmpFace->vertexCount == 0 && tmpFace->material != NULL)
    727       {
    728         if (this->currentGroup->faceMode != -1)
     622      if ((*face)._elements.empty() && (*face)._material != NULL)
     623      {
     624        if ((*group).faceMode != -1)
    729625          glEnd();
    730         this->currentGroup->faceMode = 0;
    731         if (tmpFace->material != NULL)
     626        (*group).faceMode = 0;
     627        if ((*face)._material != NULL)
    732628        {
    733           tmpFace->material->select();
    734           PRINTF(5)("using material %s for coming Faces.\n", tmpFace->material->getCName());
     629          (*face)._material->select();
     630          PRINTF(5)("using material %s for coming Faces.\n", (*face)._material->getCName());
    735631        }
    736632      }
    737633
    738       else if (tmpFace->vertexCount == 3)
    739       {
    740         if (this->currentGroup->faceMode != 3)
     634      else if ((*face)._elements.size() == 3)
     635      {
     636        if ((*group).faceMode != 3)
    741637        {
    742           if (this->currentGroup->faceMode != -1)
     638          if ((*group).faceMode != -1)
    743639            glEnd();
    744640          glBegin(GL_TRIANGLES);
    745641        }
    746642
    747         this->currentGroup->faceMode = 3;
     643        (*group).faceMode = 3;
    748644        PRINTF(5)("found triag.\n");
    749645      }
    750646
    751       else if (tmpFace->vertexCount == 4)
    752       {
    753         if (this->currentGroup->faceMode != 4)
     647      else if ((*face)._elements.size() == 4)
     648      {
     649        if ((*group).faceMode != 4)
    754650        {
    755           if (this->currentGroup->faceMode != -1)
     651          if ((*group).faceMode != -1)
    756652            glEnd();
    757653          glBegin(GL_QUADS);
    758654        }
    759         this->currentGroup->faceMode = 4;
     655        (*group).faceMode = 4;
    760656        PRINTF(5)("found quad.\n");
    761657      }
    762658
    763       else if (tmpFace->vertexCount > 4)
    764       {
    765         if (this->currentGroup->faceMode != -1)
     659      else if ((*face)._elements.size() > 4)
     660      {
     661        if ((*group).faceMode != -1)
    766662          glEnd();
    767663        glBegin(GL_POLYGON);
    768         PRINTF(5)("Polygon with %i faces found.", tmpFace->vertexCount);
    769         this->currentGroup->faceMode = tmpFace->vertexCount;
    770       }
    771 
    772       ModelFaceElement* tmpElem = tmpFace->firstElem;
    773       while (tmpElem != NULL)
    774       {
    775         //      PRINTF(2)("%s\n", tmpElem->value);
    776         this->addGLElement(tmpElem);
    777         tmpElem = tmpElem->next;
    778       }
    779       tmpFace = tmpFace->next;
     664        PRINTF(5)("Polygon with %i faces found.", (*face)._elements.size());
     665        (*group).faceMode = (*face)._elements.size();
     666      }
     667
     668      for (std::vector<FaceElement>::const_iterator elem = (*face)._elements.begin();
     669           elem != (*face)._elements.end();
     670           ++elem)
     671      {
     672        //      PRINTF(2)("%s\n", (*elem).value);
     673        this->addGLElement(*elem);
     674      }
    780675    }
    781676    glEnd();
    782677    glEndList();
    783678
    784     this->currentGroup = this->currentGroup->next;
    785679  }
    786680  return true;
     
    800694
    801695  int                index = 0;                   //!< the counter for the triangle array
    802   ModelFaceElement*  tmpElem;       //!< the temporary faceelement reference
    803   ModelFace*         tmpFace;              //!< the temporary face referece
    804 
    805696  unsigned int numTriangles = 0;
    806697  bool warned = false;
     
    808699  /* count the number of triangles */
    809700  /* now iterate through all groups and build up the triangle list */
    810   this->currentGroup = this->firstGroup;
    811   while( this->currentGroup != NULL)
    812   {
    813     tmpFace = this->currentGroup->firstFace;
    814     while( tmpFace != NULL)
     701  for (std::vector<Group>::iterator group = _modelGroups.begin();
     702       group != _modelGroups.end();
     703       ++group)
     704  {
     705    for (std::vector<Face>::const_iterator face = (*group)._faces.begin();
     706         face != (*group)._faces.end();
     707         ++face)
    815708    {
    816709      /* if its a triangle just add it to the list */
    817       if( tmpFace->vertexCount == 3)
     710      if( (*face)._elements.size() == 3)
    818711      {
    819712        ++numTriangles;
    820713      } /* if the polygon is a quad */
    821       else if( tmpFace->vertexCount == 4)
     714      else if((*face)._elements.size() == 4)
    822715      {
    823716        numTriangles += 2;
    824717      }
    825       else if( tmpFace->vertexCount > 4)
     718      else if( (*face)._elements.size() > 4)
    826719      {
    827720        if (!warned)
     
    831724        }
    832725      }
    833       tmpFace = tmpFace->next;
    834726    }
    835     this->currentGroup = this->currentGroup->next;
    836727  }
    837728
     
    845736
    846737  /* now iterate through all groups and build up the triangle list */
    847   this->currentGroup = this->firstGroup;
    848   while( this->currentGroup != NULL)
    849   {
    850     tmpFace = this->currentGroup->firstFace;
    851     while( tmpFace != NULL)
     738  for (std::vector<Group>::iterator group = _modelGroups.begin();
     739       group != _modelGroups.end();
     740       ++group)
     741  {
     742    for (std::vector<Face>::const_iterator face = (*group)._faces.begin();
     743         face != (*group)._faces.end();
     744         ++face)
    852745    {
    853       tmpElem = tmpFace->firstElem;
    854 
    855746      /* if its a triangle just add it to the list */
    856       if( tmpFace->vertexCount == 3)
    857       {
    858         for( int j = 0; j < 3; ++j)
     747      if( (*face)._elements.size() == 3)
     748      {
     749        unsigned int j = 0;
     750        for (std::vector<FaceElement>::const_iterator elem = (*face)._elements.begin();
     751             elem != (*face)._elements.end();
     752             ++elem)
    859753        {
    860           this->triangles[index].indexToVertices[j] = (unsigned int)tmpElem->vertexNumber * 3 ;
    861           this->triangles[index].indexToNormals[j] = (unsigned int)tmpElem->normalNumber * 3 ;
    862           this->triangles[index].indexToTexCoor[j] = (unsigned int)tmpElem->texCoordNumber * 3 ;
    863           tmpElem = tmpElem->next;
    864 
     754          this->triangles[index].indexToVertices[j] = (unsigned int)(*elem).vertexNumber * 3;
     755          this->triangles[index].indexToNormals[j] = (unsigned int)(*elem).normalNumber * 3;
     756          this->triangles[index].indexToTexCoor[j] = (unsigned int)(*elem).texCoordNumber * 3;
     757          ++j;
    865758        }
    866759        ++index;
    867760      } /* if the polygon is a quad */
    868       else if( tmpFace->vertexCount == 4)
    869       {
    870 
    871         this->triangles[index].indexToVertices[0] = (unsigned int)tmpElem->vertexNumber * 3;
    872         this->triangles[index].indexToNormals[0] = (unsigned int)tmpElem->normalNumber * 3;
    873         this->triangles[index].indexToTexCoor[0] = (unsigned int)tmpElem->texCoordNumber * 3;
    874 
    875         this->triangles[index + 1].indexToVertices[0] = (unsigned int)tmpElem->vertexNumber * 3;
    876         this->triangles[index + 1].indexToNormals[0] = (unsigned int)tmpElem->normalNumber * 3;
    877         this->triangles[index + 1].indexToTexCoor[0] = (unsigned int)tmpElem->texCoordNumber * 3;
    878         tmpElem = tmpElem->next;
    879 
    880         this->triangles[index].indexToVertices[1] = (unsigned int)tmpElem->vertexNumber * 3;
    881         this->triangles[index].indexToNormals[1] = (unsigned int)tmpElem->normalNumber * 3;
    882         this->triangles[index].indexToTexCoor[1] = (unsigned int)tmpElem->texCoordNumber * 3;
    883         tmpElem = tmpElem->next;
    884 
    885         this->triangles[index].indexToVertices[2] = (unsigned int)tmpElem->vertexNumber * 3;
    886         this->triangles[index].indexToNormals[2] = (unsigned int)tmpElem->normalNumber * 3;
    887         this->triangles[index].indexToTexCoor[2] = (unsigned int)tmpElem->texCoordNumber * 3;
    888 
    889         this->triangles[index + 1].indexToVertices[2] = (unsigned int)tmpElem->vertexNumber * 3;
    890         this->triangles[index + 1].indexToNormals[2] = (unsigned int)tmpElem->normalNumber * 3;
    891         this->triangles[index + 1].indexToTexCoor[2] = (unsigned int)tmpElem->texCoordNumber * 3;
    892         tmpElem = tmpElem->next;
    893 
    894         this->triangles[index + 1].indexToVertices[1] = (unsigned int)tmpElem->vertexNumber * 3;
    895         this->triangles[index + 1].indexToNormals[1] = (unsigned int)tmpElem->normalNumber * 3;
    896         this->triangles[index + 1].indexToTexCoor[1] = (unsigned int)tmpElem->texCoordNumber * 3;
     761
     762      else if( (*face)._elements.size() == 4)
     763      {
     764        std::vector<FaceElement>::const_iterator elem = (*face)._elements.begin();
     765
     766        this->triangles[index].indexToVertices[0] = (unsigned int)(*elem).vertexNumber * 3;
     767        this->triangles[index].indexToNormals[0] = (unsigned int)(*elem).normalNumber * 3;
     768        this->triangles[index].indexToTexCoor[0] = (unsigned int)(*elem).texCoordNumber * 3;
     769
     770        this->triangles[index + 1].indexToVertices[0] = (unsigned int)(*elem).vertexNumber * 3;
     771        this->triangles[index + 1].indexToNormals[0] = (unsigned int)(*elem).normalNumber * 3;
     772        this->triangles[index + 1].indexToTexCoor[0] = (unsigned int)(*elem).texCoordNumber * 3;
     773        ++elem;
     774
     775        this->triangles[index].indexToVertices[1] = (unsigned int)(*elem).vertexNumber * 3;
     776        this->triangles[index].indexToNormals[1] = (unsigned int)(*elem).normalNumber * 3;
     777        this->triangles[index].indexToTexCoor[1] = (unsigned int)(*elem).texCoordNumber * 3;
     778        ++elem;
     779
     780        this->triangles[index].indexToVertices[2] = (unsigned int)(*elem).vertexNumber * 3;
     781        this->triangles[index].indexToNormals[2] = (unsigned int)(*elem).normalNumber * 3;
     782        this->triangles[index].indexToTexCoor[2] = (unsigned int)(*elem).texCoordNumber * 3;
     783
     784        this->triangles[index + 1].indexToVertices[2] = (unsigned int)(*elem).vertexNumber * 3;
     785        this->triangles[index + 1].indexToNormals[2] = (unsigned int)(*elem).normalNumber * 3;
     786        this->triangles[index + 1].indexToTexCoor[2] = (unsigned int)(*elem).texCoordNumber * 3;
     787        ++elem;
     788
     789        this->triangles[index + 1].indexToVertices[1] = (unsigned int)(*elem).vertexNumber * 3;
     790        this->triangles[index + 1].indexToNormals[1] = (unsigned int)(*elem).normalNumber * 3;
     791        this->triangles[index + 1].indexToTexCoor[1] = (unsigned int)(*elem).texCoordNumber * 3;
    897792
    898793        index += 2;
    899794      }
    900       tmpFace = tmpFace->next;
    901795    }
    902     this->currentGroup = this->currentGroup->next;
    903796  }
    904797  return true;
     
    916809   merging this information, the face will be drawn.
    917810*/
    918 bool StaticModelData::addGLElement (ModelFaceElement* elem)
     811bool StaticModelData::addGLElement (const FaceElement& elem)
    919812{
    920813  PRINTF(5)("importing grafical Element to openGL.\n");
    921814
    922   if (elem->texCoordNumber > -1)
    923   {
    924     if (likely((unsigned int)elem->texCoordNumber < this->vTexture.size()))
    925       glTexCoord2fv(&this->vTexture[0] + elem->texCoordNumber * 2);
     815  if (elem.texCoordNumber > -1)
     816  {
     817    if (likely((unsigned int)elem.texCoordNumber < this->vTexture.size()))
     818      glTexCoord2fv(&this->vTexture[0] + elem.texCoordNumber * 2);
    926819    else
    927820      PRINTF(2)("TextureCoordinate %d is not in the List (max: %d)\nThe Model might be incomplete\n",
    928                 elem->texCoordNumber, this->vTexture.size());
    929   }
    930   if (elem->normalNumber > -1)
    931   {
    932     if (likely((unsigned int)elem->normalNumber < this->normals.size()))
    933       glNormal3fv(&this->normals[0] + elem->normalNumber * 3);
     821                elem.texCoordNumber, this->vTexture.size());
     822  }
     823  if (elem.normalNumber > -1)
     824  {
     825    if (likely((unsigned int)elem.normalNumber < this->normals.size()))
     826      glNormal3fv(&this->normals[0] + elem.normalNumber * 3);
    934827    else
    935828      PRINTF(2)("Normal %d is not in the List (max: %d)\nThe Model might be incomplete",
    936                 elem->normalNumber, this->normals.size());
    937   }
    938   if (elem->vertexNumber > -1)
    939   {
    940     if (likely((unsigned int)elem->vertexNumber < this->vertices.size()))
    941       glVertex3fv(&this->vertices[0]+ elem->vertexNumber * 3);
     829                elem.normalNumber, this->normals.size());
     830  }
     831  if (elem.vertexNumber > -1)
     832  {
     833    if (likely((unsigned int)elem.vertexNumber < this->vertices.size()))
     834      glVertex3fv(&this->vertices[0]+ elem.vertexNumber * 3);
    942835    else
    943836      PRINTF(2)("Vertex %d is not in the List (max: %d)\nThe Model might be incomplete",
    944                 elem->vertexNumber, this->vertices.size());
    945   }
    946 
    947   return true;
    948 }
     837                elem.vertexNumber, this->vertices.size());
     838  }
     839
     840  return true;
     841}
  • trunk/src/lib/graphics/importer/static_model_data.h

    r9869 r10141  
    2727};
    2828
    29 ////////////////////
    30 /// SUB-ELEMENTS ///
    31 ////////////////////
    32 //! This is the placeholder of one Vertex beloning to a Face.
    33 class ModelFaceElement
    34 {
    35 public:
    36   ModelFaceElement();
    37   ~ModelFaceElement();
    3829
    39   int                 vertexNumber;         //!< The number of the Vertex out of the Array* vertices, this vertex points to.
    40   int                 normalNumber;         //!< The number of the Normal out of the Array* normals, this vertex points to.
    41   int                 texCoordNumber;       //!< The number of the textureCoordinate out of the Array* vTexture, this vertex points to.
    42 
    43   ModelFaceElement*   next;                 //!< Point to the next FaceElement in this List.
    44 };
    45 
    46 //! This is the placeholder of a Face belonging to a Group of Faces.
    47 class ModelFace
    48 {
    49 public:
    50   ModelFace();
    51   ~ModelFace();
    52 
    53   unsigned int        vertexCount;     //!< The Count of vertices this Face has.
    54   ModelFaceElement*   firstElem;       //!< Points to the first Vertex (FaceElement) of this Face.
    55   Material*           material;        //!< The Material to use.
    56 
    57   ModelFace*          next;            //!< Pointer to the next Face.
    58 };
    59 
    60 //! Group to handle multiple Models per obj-file.
    61 class ModelGroup
    62 {
    63 public:
    64   ModelGroup();
    65   ~ModelGroup();
    66 
    67   void cleanup();
    68 
    69   std::string  name;           //!< the Name of the Group. this is an identifier, that can be accessed via the draw (std::string name) function.
    70   GLubyte*     indices;        //!< The indices of the Groups. Needed for vertex-arrays
    71   GLuint       listNumber;     //!< The number of the GL-List this Group gets.
    72   ModelFace*   firstFace;      //!< The first Face in this group.
    73   ModelFace*   currentFace;    //!< The current Face in this Group (the one we are currently working with.)
    74   int          faceMode;       //!< The Mode the Face is in: initially -1, 0 for FaceList opened, 1 for Material,  3 for triangle, 4 for Quad, 5+ for Poly @todo ENUM...
    75   int          faceCount;      //!< The Number of Faces this Group holds.
    76 
    77   ModelGroup*  next;           //!< Pointer to the next Group.
    78 };
    79 
    80 struct ModelMaterial
    81 {
    82   Material* material;
    83   bool external;
    84 };
    8530
    8631/////////////
     
    9540{
    9641  ObjectListDeclaration(StaticModelData);
     42
     43private:
     44  ////////////////////
     45  /// SUB-ELEMENTS ///
     46  ////////////////////
     47  //! This is the placeholder of one Vertex beloning to a Face.
     48  class FaceElement
     49  {
    9750  public:
    98     typedef CountPointer<StaticModelData> Pointer;
     51    FaceElement();
     52
     53    int                 vertexNumber;         //!< The number of the Vertex out of the Array* vertices, this vertex points to.
     54    int                 normalNumber;         //!< The number of the Normal out of the Array* normals, this vertex points to.
     55    int                 texCoordNumber;       //!< The number of the textureCoordinate out of the Array* vTexture, this vertex points to.
     56  };
     57
     58  //! This is the placeholder of a Face belonging to a Group of Faces.
     59  class Face
     60  {
     61  public:
     62    Face();
     63
     64    std::vector<FaceElement>  _elements; //!< Elements of the Face.
     65    Material*                 _material;        //!< The Material to use.
     66  };
     67
     68  //! Group to handle multiple Models per obj-file.
     69  class Group
     70  {
     71  public:
     72    Group();
     73    ~Group();
     74
     75    //! Compares the name with the groups name.
     76    bool operator==(const std::string& name) const { return this->name == name; };
     77    void cleanup();
     78
     79    std::string  name;           //!< the Name of the Group. this is an identifier, that can be accessed via the draw (std::string name) function.
     80    GLubyte*     indices;        //!< The indices of the Groups. Needed for vertex-arrays
     81    GLuint       listNumber;     //!< The number of the GL-List this Group gets.
     82    int          faceMode;       //!< The Mode the Face is in: initially -1, 0 for FaceList opened, 1 for Material,  3 for triangle, 4 for Quad, 5+ for Poly @todo ENUM...
     83
     84    std::vector<Face> _faces;    //!< Faces.
     85  };
    9986
    10087  public:
     88  typedef CountPointer<StaticModelData> Pointer;
     89
     90public:
    10191  StaticModelData(const std::string& modelName = "");
    10292  virtual ~StaticModelData();
    10393
    10494  void draw() const;
    105   void draw(int groupNumber) const;
     95  void draw(unsigned int groupNumber) const;
    10696  void draw(const std::string& groupName) const;
    10797
    10898  void rebuild();
    10999
    110   Material* addMaterial(Material* material);
     100  Material* addMaterial(const Material& material);
    111101  Material* addMaterial(const std::string& materialName);
    112102
     
    151141  bool buildTriangleList();
    152142
    153   bool addGLElement(ModelFaceElement* elem);
     143  bool addGLElement(const StaticModelData::FaceElement& elem);
    154144
    155145  bool cleanup();
     
    168158  std::vector<sTriangleExt>  triangles;       //!< The Triangles if built.
    169159
    170   ModelGroup*                firstGroup;      //!< The first of all groups.
    171   ModelGroup*                currentGroup;    //!< The currentGroup. this is the one we will work with.
    172   int                        groupCount;      //!< The Count of Groups.
    173160
    174   std::list<ModelMaterial*>  materialList;    //!< A list for all the Materials in this Model
     161  std::vector<Group>         _modelGroups;
     162
     163  std::list<Material>        materialList;    //!< A list for all the Materials in this Model
    175164};
    176165
Note: See TracChangeset for help on using the changeset viewer.