Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 7, 2005, 5:22:44 PM (20 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the Trunk/importer back here.
merged with command:
svn merge -r 3918:HEAD ../../trunk/src/lib/graphics/importer/ src/lib/graphics/importer/

this fixed the issue with the segfault. It really was an error in the Model-class
@nico:

  1. sorry
  2. thanks for pointing this out to me
File:
1 edited

Legend:

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

    r3917 r4111  
    2727using namespace std;
    2828
    29 //////////////////
    30 // DE-CONSTRUCT //
    31 //////////////////
    32 /**
    33    \brief Creates a 3D-Model. and assigns it a Name.
     29
     30////////////////////
     31/// SUB-Elements ///
     32////////////////////
     33/**
     34   \brief creates a new ModelFaceElement
     35*/
     36ModelFaceElement::ModelFaceElement()
     37{
     38  this->vertexNumber = -1;
     39  this->normalNumber = -1;
     40  this->texCoordNumber = -1;   
     41
     42  this->next = NULL;
     43}
     44
     45/**
     46   \brief destroys a ModelFaceElement
     47*/
     48ModelFaceElement::~ModelFaceElement()
     49{
     50  if (this->next)
     51    delete this->next;
     52}
     53
     54/**
     55   \brief creates a new ModelFace
     56*/
     57ModelFace::ModelFace()
     58{
     59  this->vertexCount = 0;
     60
     61  this->firstElem = NULL;
     62 
     63  this->material = NULL;
     64 
     65  this->next = NULL;
     66
     67}
     68
     69/**
     70   \brief deletes a ModelFace
     71*/
     72ModelFace::~ModelFace()
     73{
     74  PRINTF(5)("Cleaning up Face\n");
     75
     76  if (this->firstElem != NULL)
     77    delete this->firstElem;
     78 
     79  if (this->next != NULL)
     80    delete this->next;
     81}
     82
     83/**
     84   \brief Creates a new ModelGroup
     85*/
     86ModelGroup::ModelGroup()
     87{
     88  PRINTF(4)("Adding new Group\n");
     89  this->name = "";
     90  this->faceMode = -1;
     91  this->faceCount = 0; 
     92  this->next = NULL;
     93 
     94  this->firstFace = new ModelFace;
     95  this->currentFace = this->firstFace;
     96}
     97
     98/**
     99   \brief deletes a ModelGroup
     100*/
     101ModelGroup::~ModelGroup()
     102{
     103  PRINTF(5)("Cleaning up group\n");
     104  if (this->firstFace != NULL)
     105    delete this->firstFace;
     106
     107  if (this->next !=NULL)
     108    delete this->next;
     109}
     110
     111/**
     112   \brief cleans up a ModelGroup
     113
     114   actually does the same as the delete Operator, but does not delete the predecessing group
     115*/
     116void ModelGroup::cleanup(void)
     117{
     118  PRINTF(5)("Cleaning up group\n");
     119  if (this->firstFace)
     120    delete this->firstFace;
     121  this->firstFace = NULL;
     122  if (this->next)
     123    this->next->cleanup();
     124}
     125
     126
     127/////////////
     128/// MODEL ///
     129/////////////
     130/**
     131   \brief Creates a 3D-Model.
     132
     133   assigns it a Name and a Type
    34134*/
    35135Model::Model(const char* modelName, MODEL_TYPE type)
     
    42142  this->finalized = false;
    43143  // setting the start group;
    44   this->firstGroup = new Group;
    45   this->currentGroup = this->firstGroup;
     144  this->currentGroup = this->firstGroup = new ModelGroup;
    46145  this->groupCount = 0;
    47  
    48   this->initGroup (this->currentGroup);
     146  this->vertexCount = 0;
     147  this->normalCount = 0;
     148  this->texCoordCount = 0;
     149 
    49150  this->scaleFactor = 1;
    50151
     
    76177
    77178  PRINTF(5)("Deleting display Lists.\n");
    78   Group* walker = this->firstGroup;
    79   while (walker != NULL)
    80     {
    81       glDeleteLists (walker->listNumber, 1);
    82       Group* delWalker = walker;
    83       walker = walker->next;
    84       delete delWalker;
    85     }
     179  delete this->firstGroup;
    86180
    87181  // deleting Arrays
     
    92186  tIterator<Material>* tmpIt = this->materialList->getIterator();
    93187  Material* material = tmpIt->nextElement();
     188
     189  //! \todo do we really have to delete this material??
    94190  while(material)
    95191    {
     
    128224{
    129225  PRINTF(4)("drawing the 3D-Models\n");
    130   Group* walker = this->firstGroup;
    131   while (walker != NULL)
    132     {
    133       PRINTF(5)("Drawing model %s\n", walker->name);
    134       glCallList (walker->listNumber);
    135       walker = walker->next;
     226  ModelGroup* tmpGroup = this->firstGroup;
     227  while (tmpGroup != NULL)
     228    {
     229      PRINTF(5)("Drawing model %s\n", tmpGroup->name);
     230      glCallList (tmpGroup->listNumber);
     231      tmpGroup = tmpGroup->next;
    136232    }
    137233}
     
    151247    }
    152248  PRINTF(4)("drawing the requested 3D-Models if found.\n");
    153   Group* walker = this->firstGroup;
     249  ModelGroup* tmpGroup = this->firstGroup;
    154250  int counter = 0;
    155   while (walker != NULL)
     251  while (tmpGroup != NULL)
    156252    {
    157253      if (counter == groupNumber)
    158254        {
    159           PRINTF(4)("Drawing model number %i named %s\n", counter, walker->name);
    160           glCallList (walker->listNumber);
     255          PRINTF(4)("Drawing model number %i named %s\n", counter, tmpGroup->name);
     256          glCallList (tmpGroup->listNumber);
    161257          return;
    162258        }
    163259      ++counter;
    164       walker = walker->next;
     260      tmpGroup = tmpGroup->next;
    165261    }
    166262  PRINTF(2)("Model number %i in %s not Found.\n", groupNumber, this->name);
     
    178274{
    179275  PRINTF(4)("drawing the requested 3D-Models if found.\n");
    180   Group* walker = this->firstGroup;
    181   while (walker != NULL)
    182     {
    183       if (!strcmp(walker->name, groupName))
     276  ModelGroup* tmpGroup = this->firstGroup;
     277  while (tmpGroup != NULL)
     278    {
     279      if (!strcmp(tmpGroup->name, groupName))
    184280        {
    185           PRINTF(4)("Drawing model %s\n", walker->name);
    186           glCallList (walker->listNumber);
     281          PRINTF(4)("Drawing model %s\n", tmpGroup->name);
     282          glCallList (tmpGroup->listNumber);
    187283          return;
    188284        }
    189       walker = walker->next;
     285      tmpGroup = tmpGroup->next;
    190286    }
    191287  PRINTF(2)("Model Named %s in %s not Found.\n", groupName, this->name);
    192288  return;
    193 }
    194 
    195 /**
    196    \returns Count of the Models in this File
    197 */
    198 int Model::getGroupCount (void) const
    199 {
    200   return this->groupCount;
    201289}
    202290
     
    210298void Model::setName(const char* name)
    211299{
    212   if (this->name) 
    213     delete this->name;
     300  if (this->name)
     301    delete []this->name;
    214302  if (name)
    215303    {
     
    219307  else
    220308    this->name = NULL;
    221 }
    222 
    223 /**
    224    \brief initializes a new Group model
    225    \param group the group that should be initialized.
    226    \todo Maybe Group should be a Class, because it does a lot of stuff
    227    
    228 */
    229 bool Model::initGroup(Group* group)
    230 {
    231   PRINTF(4)("Adding new Group\n");
    232   group->name = "";
    233   group->faceMode = -1;
    234   group->faceCount = 0; 
    235   group->next = NULL;
    236 
    237   group->firstFace = new Face;
    238   this->initFace (group->firstFace);
    239   group->currentFace = group->firstFace;
    240 }
    241 
    242 /**
    243    \brief initializes a new Face. (sets default Values)
    244    \param face The face to initialize
    245 */
    246 bool Model::initFace (Face* face)
    247 {
    248   face->vertexCount = 0;
    249 
    250   face->firstElem = NULL;
    251  
    252   face->material = NULL;
    253  
    254   face->next = NULL;
    255 
    256   return true;
    257309}
    258310
     
    268320  if (this->normals)
    269321    delete this->normals;
     322
    270323  this->vertices = NULL;
    271324  this->vTexture = NULL;
     
    280333{
    281334  PRINTF(4)("cleaning up the 3D-Model to save Memory.\n");
    282   this->cleanupGroup(this->firstGroup);
     335  this->firstGroup->cleanup();
    283336  return true;
    284337}
    285338
    286 /**
    287    \brief Cleans up all groups starting from group.
    288    \param group the first Group to clean
    289 */
    290 bool Model::cleanupGroup (Group* group)
    291 {
    292   PRINTF(5)("Cleaning up group\n");
    293   if (group->firstFace != NULL)
    294     {
    295       cleanupFace (group->firstFace);
    296       delete group->firstFace;
    297     }
    298 
    299   if (group->next !=NULL)
    300     cleanupGroup (group->next);
    301   return true;
    302 }
    303 
    304 /**
    305    \brief Cleans up all Faces starting from face until NULL is reached.
    306    \param face the first face to clean.
    307 */
    308 bool Model::cleanupFace (Face* face)
    309 {
    310   PRINTF(5)("Cleaning up Face\n");
    311 
    312   if (face->firstElem != NULL)
    313     {
    314       this->cleanupFaceElement(face->firstElem);
    315       delete face->firstElem;
    316     }
    317      
    318   if (face->next != NULL)
    319     {
    320       this->cleanupFace (face->next);
    321       delete face->next;
    322     }
    323 }
    324 
    325 /**
    326    \brief Cleans up all FaceElements starting from faceElem.
    327    \param faceElem the first FaceElement to clean.
    328 */
    329 bool Model::cleanupFaceElement(FaceElement* faceElem)
    330 {
    331   if (faceElem->next != NULL)
    332     {
    333       this->cleanupFaceElement (faceElem->next);
    334       delete faceElem->next;
    335     }
    336 }
     339
    337340
    338341//////////
     
    354357/**
    355358   \brief adds a new Material to the Material List
    356    \param material the name of the Material to add
     359   \param materialName the name of the Material to add
    357360   \returns the added material
    358361*/
    359362Material* Model::addMaterial(const char* materialName)
    360363{
    361  
    362364  Material* newMat = new Material();
    363365  newMat->setName(materialName);
     
    395397
    396398   This function initializes a new Group.
    397    With it you should be able to import .obj-files with more than one Models inside.
    398 */
    399 bool Model::addGroup (const char* groupString)
     399   With it you should be able to create Models with more than one SubModel inside
     400*/
     401bool Model::addGroup(const char* groupString)
    400402{
    401403  PRINTF(5)("Read Group: %s.\n", groupString);
    402   if (this->groupCount != 0 && this->currentGroup->faceCount>0)
     404  if (this->groupCount != 0 && this->currentGroup->faceCount > 0)
    403405    {
    404406      //      finalizeGroup(currentGroup);
    405       this->currentGroup = this->currentGroup->next = new Group;
    406       this->initGroup(this->currentGroup);
     407      this->currentGroup = this->currentGroup->next = new ModelGroup;
    407408    }
    408409  // setting the group name if not default.
     
    429430  PRINTF(5)("reading in a vertex: %f %f %f\n", &subbuffer1, &subbuffer2, &subbuffer3);
    430431  this->vertices->addEntry(subbuffer1*scaleFactor, subbuffer2*scaleFactor, subbuffer3*scaleFactor);
     432  this->vertexCount++;
    431433  return true;
    432434}
     
    443445  PRINTF(5)("reading in a vertex: %f %f %f\n", x, y, z);
    444446  this->vertices->addEntry(x*scaleFactor, y*scaleFactor, z*scaleFactor);
     447  this->vertexCount++;
    445448  return true;
    446449}
     
    460463  PRINTF(5)("found vertex-Normal %f, %f, %f\n", &subbuffer1,&subbuffer2,&subbuffer3);
    461464  this->normals->addEntry(subbuffer1, subbuffer2, subbuffer3);
     465  this->normalCount++;
    462466  return true;
    463467}
     
    475479  PRINTF(5)("found vertex-Normal %f, %f, %f\n", x, y, z);
    476480  this->normals->addEntry(x, y, z);
     481  this->normalCount++;
     482  return true;
    477483}
    478484
     
    492498  this->vTexture->addEntry(subbuffer1);
    493499  this->vTexture->addEntry(subbuffer2);
     500  this->texCoordCount++;
    494501  return true;
    495502}
     
    507514  this->vTexture->addEntry(u);
    508515  this->vTexture->addEntry(v);
     516  this->texCoordCount++;
     517  return true;
    509518}
    510519
     
    518527{
    519528  if (this->currentGroup->faceCount >0)
    520     this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
    521   this->initFace (this->currentGroup->currentFace);
    522 
    523   FaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new FaceElement;
     529    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
     530
     531  ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement;
    524532  tmpElem->next = NULL;
    525533  while(strcmp (faceString, "\0"))
    526534    {
    527535      if (this->currentGroup->currentFace->vertexCount>0)
    528           tmpElem = tmpElem->next = new FaceElement;
     536          tmpElem = tmpElem->next = new ModelFaceElement;
    529537      tmpElem->next = NULL;
    530538
     
    552560      if (vertex)
    553561        tmpElem->vertexNumber = atoi(vertex)-1;
    554       else
    555         tmpElem->vertexNumber = -1;
    556562      if (texture)
    557563        tmpElem->texCoordNumber = atoi(texture)-1;
    558       else
    559         tmpElem->texCoordNumber = -1;
    560564      if (normal)
    561565        tmpElem->normalNumber = atoi(normal)-1;
    562       else
    563         tmpElem->normalNumber = -1;
    564566
    565567      faceString += tmpLen;
     
    579581bool Model::addFace(int faceElemCount, VERTEX_FORMAT type, ...)
    580582{
    581    if (this->currentGroup->faceCount > 0)
    582     this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
    583   this->initFace (this->currentGroup->currentFace);
    584 
    585   FaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new FaceElement;
    586   tmpElem->next = NULL;
     583  if (this->currentGroup->faceCount > 0)
     584    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
     585 
     586  ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement;
    587587 
    588588  va_list itemlist;
     
    591591  for (int i = 0; i < faceElemCount; i++)
    592592    {
    593       if (this->currentGroup->currentFace->vertexCount>0)
    594           tmpElem = tmpElem->next = new FaceElement;
    595       tmpElem->next = NULL;
     593      if (this->currentGroup->currentFace->vertexCount > 0)
     594        tmpElem = tmpElem->next = new ModelFaceElement;
    596595
    597596      tmpElem->vertexNumber = va_arg (itemlist, int) -1;
     
    614613{
    615614  if (this->currentGroup->faceCount > 0)
    616     this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
    617   this->initFace (this->currentGroup->currentFace);
     615    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
    618616 
    619617  this->currentGroup->currentFace->material = this->findMaterialByName(matString);
     
    630628{
    631629  if (this->currentGroup->faceCount > 0)
    632     this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
    633   this->initFace (this->currentGroup->currentFace);
     630    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
    634631 
    635632  this->currentGroup->currentFace->material = mtl;
     
    662659  Vector curV;
    663660
    664   Group* tmpGroup = firstGroup;
     661  ModelGroup* tmpGroup = firstGroup;
    665662  while (tmpGroup)
    666663    {
    667       Face* tmpFace = tmpGroup->firstFace;
     664      ModelFace* tmpFace = tmpGroup->firstFace;
    668665      while (tmpFace)
    669666        {
    670667          if (tmpFace->firstElem)
    671668            {
    672               FaceElement* firstElem = tmpFace->firstElem;
    673               FaceElement* prevElem;
    674               FaceElement* curElem = firstElem;
    675               FaceElement* nextElem;
    676               FaceElement* lastElem;
     669              ModelFaceElement* firstElem = tmpFace->firstElem;
     670              ModelFaceElement* prevElem;
     671              ModelFaceElement* curElem = firstElem;
     672              ModelFaceElement* nextElem;
     673              ModelFaceElement* lastElem;
    677674              // 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.
    678675              while (curElem)
     
    704701    }
    705702
    706   for (int i=0; i<vertices->getCount()/3;i++)
     703  for (int i=0; i < vertices->getCount()/3;i++)
    707704    {
    708705      normArray[i].normalize();
    709706      PRINTF(5)("Found Normale number %d: (%f; %f, %f).\n", i, normArray[i].x, normArray[i].y, normArray[i].z);
    710707     
    711       this->normals->addEntry(normArray[i].x, normArray[i].y, normArray[i].z);
     708      this->addVertexNormal(normArray[i].x, normArray[i].y, normArray[i].z);
    712709
    713710    }
     
    744741
    745742      // Putting Faces to GL
    746       Face* tmpFace = this->currentGroup->firstFace;
     743      ModelFace* tmpFace = this->currentGroup->firstFace;
    747744      while (tmpFace != NULL)
    748745        {
     
    794791            }
    795792         
    796           FaceElement* tmpElem = tmpFace->firstElem;
     793          ModelFaceElement* tmpElem = tmpFace->firstElem;
    797794          while (tmpElem != NULL)
    798795            {
     
    826823  glNormalPointer(3, 0, this->normals->getArray());
    827824  glTexCoordPointer(2, GL_FLOAT, 0, this->vTexture->getArray());
    828 
    829825}
    830826
     
    841837   merging this information, the face will be drawn.
    842838*/
    843 bool Model::addGLElement (FaceElement* elem)
     839bool Model::addGLElement (ModelFaceElement* elem)
    844840{
    845841  PRINTF(5)("importing grafical Element to openGL.\n");
    846842
    847843  if (elem->texCoordNumber != -1)
    848     glTexCoord2fv(this->vTexture->getArray() + elem->texCoordNumber * 2);
     844    {
     845      if (likely(elem->texCoordNumber < this->texCoordCount))
     846        glTexCoord2fv(this->vTexture->getArray() + elem->texCoordNumber * 2);
     847      else
     848        PRINTF(2)("TextureCoordinate %d is not in the List (max: %d)\nThe Model might be incomplete\n",
     849                  elem->texCoordNumber, this->texCoordCount);
     850    }
    849851  if (elem->normalNumber != -1)
    850     glNormal3fv(this->normals->getArray() + elem->normalNumber * 3);
     852    {
     853    if (likely(elem->normalNumber < this->normalCount))
     854      glNormal3fv(this->normals->getArray() + elem->normalNumber * 3);
     855    else
     856        PRINTF(2)("Normal %d is not in the List (max: %d)\nThe Model might be incomplete",
     857                  elem->normalNumber, this->normalCount);     
     858    }
    851859  if (elem->vertexNumber != -1)
    852     glVertex3fv(this->vertices->getArray() + elem->vertexNumber * 3);
     860    {
     861      if (likely(elem->vertexNumber < this->vertexCount))
     862          glVertex3fv(this->vertices->getArray() + elem->vertexNumber * 3);
     863      else
     864        PRINTF(2)("Vertex %d is not in the List (max: %d)\nThe Model might be incomplete",
     865                  elem->vertexNumber, this->vertexCount);     
     866    }   
    853867
    854868}
Note: See TracChangeset for help on using the changeset viewer.