Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 18, 2005, 11:27:40 AM (19 years ago)
Author:
bensch
Message:

orxonox/branches/movie_player: merged the trunk back into the movie_player
merged with command:
svn merge -r 4014:HEAD ../trunk/ movie_player/
no conflicts

File:
1 edited

Legend:

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

    r3917 r4217  
    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
     
    514523
    515524   If a face line is found this function will add it to the glList.
     525
     526   String is different from the argument addFace, in this that the first Vertex/Normal/Texcoord is 1 instead of 0
    516527*/
    517528bool Model::addFace (const char* faceString)
    518529{
    519530  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;
     531    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
     532
     533  ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement;
    524534  tmpElem->next = NULL;
    525535  while(strcmp (faceString, "\0"))
    526536    {
    527537      if (this->currentGroup->currentFace->vertexCount>0)
    528           tmpElem = tmpElem->next = new FaceElement;
     538          tmpElem = tmpElem->next = new ModelFaceElement;
    529539      tmpElem->next = NULL;
    530540
     
    552562      if (vertex)
    553563        tmpElem->vertexNumber = atoi(vertex)-1;
    554       else
    555         tmpElem->vertexNumber = -1;
    556564      if (texture)
    557565        tmpElem->texCoordNumber = atoi(texture)-1;
    558       else
    559         tmpElem->texCoordNumber = -1;
    560566      if (normal)
    561567        tmpElem->normalNumber = atoi(normal)-1;
    562       else
    563         tmpElem->normalNumber = -1;
    564568
    565569      faceString += tmpLen;
     
    575579   \brief adds a new Face
    576580   \param faceElemCount the number of Vertices to add to the Face.
    577    \param type 0: vertex only, 1: vertex and normal, 2: vertex and Texture, 3 vertex, normal and texture
     581   \param type The information Passed with each Vertex
    578582*/
    579583bool Model::addFace(int faceElemCount, VERTEX_FORMAT type, ...)
    580584{
    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;
     585  if (this->currentGroup->faceCount > 0)
     586    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
     587 
     588  ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement;
    587589 
    588590  va_list itemlist;
     
    591593  for (int i = 0; i < faceElemCount; i++)
    592594    {
    593       if (this->currentGroup->currentFace->vertexCount>0)
    594           tmpElem = tmpElem->next = new FaceElement;
    595       tmpElem->next = NULL;
    596 
    597       tmpElem->vertexNumber = va_arg (itemlist, int) -1;
     595      if (this->currentGroup->currentFace->vertexCount > 0)
     596        tmpElem = tmpElem->next = new ModelFaceElement;
     597
     598      tmpElem->vertexNumber = va_arg (itemlist, int);
    598599      if (type & TEXCOORD)
    599         tmpElem->texCoordNumber = va_arg (itemlist, int) -1;
     600        tmpElem->texCoordNumber = va_arg (itemlist, int);
    600601      if (type & NORMAL)
    601         tmpElem->normalNumber = va_arg(itemlist, int) -1;
     602        tmpElem->normalNumber = va_arg(itemlist, int);
    602603      this->currentGroup->currentFace->vertexCount++;
    603604    }
     
    614615{
    615616  if (this->currentGroup->faceCount > 0)
    616     this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
    617   this->initFace (this->currentGroup->currentFace);
     617    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
    618618 
    619619  this->currentGroup->currentFace->material = this->findMaterialByName(matString);
     
    630630{
    631631  if (this->currentGroup->faceCount > 0)
    632     this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
    633   this->initFace (this->currentGroup->currentFace);
     632    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
    634633 
    635634  this->currentGroup->currentFace->material = mtl;
     
    662661  Vector curV;
    663662
    664   Group* tmpGroup = firstGroup;
     663  ModelGroup* tmpGroup = firstGroup;
    665664  while (tmpGroup)
    666665    {
    667       Face* tmpFace = tmpGroup->firstFace;
     666      ModelFace* tmpFace = tmpGroup->firstFace;
    668667      while (tmpFace)
    669668        {
    670669          if (tmpFace->firstElem)
    671670            {
    672               FaceElement* firstElem = tmpFace->firstElem;
    673               FaceElement* prevElem;
    674               FaceElement* curElem = firstElem;
    675               FaceElement* nextElem;
    676               FaceElement* lastElem;
     671              ModelFaceElement* firstElem = tmpFace->firstElem;
     672              ModelFaceElement* prevElem;
     673              ModelFaceElement* curElem = firstElem;
     674              ModelFaceElement* nextElem;
     675              ModelFaceElement* lastElem;
    677676              // 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.
    678677              while (curElem)
     
    704703    }
    705704
    706   for (int i=0; i<vertices->getCount()/3;i++)
     705  for (int i=0; i < vertices->getCount()/3;i++)
    707706    {
    708707      normArray[i].normalize();
    709708      PRINTF(5)("Found Normale number %d: (%f; %f, %f).\n", i, normArray[i].x, normArray[i].y, normArray[i].z);
    710709     
    711       this->normals->addEntry(normArray[i].x, normArray[i].y, normArray[i].z);
     710      this->addVertexNormal(normArray[i].x, normArray[i].y, normArray[i].z);
    712711
    713712    }
     
    744743
    745744      // Putting Faces to GL
    746       Face* tmpFace = this->currentGroup->firstFace;
     745      ModelFace* tmpFace = this->currentGroup->firstFace;
    747746      while (tmpFace != NULL)
    748747        {
     
    794793            }
    795794         
    796           FaceElement* tmpElem = tmpFace->firstElem;
     795          ModelFaceElement* tmpElem = tmpFace->firstElem;
    797796          while (tmpElem != NULL)
    798797            {
     
    826825  glNormalPointer(3, 0, this->normals->getArray());
    827826  glTexCoordPointer(2, GL_FLOAT, 0, this->vTexture->getArray());
    828 
    829827}
    830828
     
    841839   merging this information, the face will be drawn.
    842840*/
    843 bool Model::addGLElement (FaceElement* elem)
     841bool Model::addGLElement (ModelFaceElement* elem)
    844842{
    845843  PRINTF(5)("importing grafical Element to openGL.\n");
    846844
    847845  if (elem->texCoordNumber != -1)
    848     glTexCoord2fv(this->vTexture->getArray() + elem->texCoordNumber * 2);
     846    {
     847      if (likely(elem->texCoordNumber < this->texCoordCount))
     848        glTexCoord2fv(this->vTexture->getArray() + elem->texCoordNumber * 2);
     849      else
     850        PRINTF(2)("TextureCoordinate %d is not in the List (max: %d)\nThe Model might be incomplete\n",
     851                  elem->texCoordNumber, this->texCoordCount);
     852    }
    849853  if (elem->normalNumber != -1)
    850     glNormal3fv(this->normals->getArray() + elem->normalNumber * 3);
     854    {
     855    if (likely(elem->normalNumber < this->normalCount))
     856      glNormal3fv(this->normals->getArray() + elem->normalNumber * 3);
     857    else
     858        PRINTF(2)("Normal %d is not in the List (max: %d)\nThe Model might be incomplete",
     859                  elem->normalNumber, this->normalCount);     
     860    }
    851861  if (elem->vertexNumber != -1)
    852     glVertex3fv(this->vertices->getArray() + elem->vertexNumber * 3);
     862    {
     863      if (likely(elem->vertexNumber < this->vertexCount))
     864          glVertex3fv(this->vertices->getArray() + elem->vertexNumber * 3);
     865      else
     866        PRINTF(2)("Vertex %d is not in the List (max: %d)\nThe Model might be incomplete",
     867                  elem->vertexNumber, this->vertexCount);     
     868    }   
    853869
    854870}
     
    910926  this->addVertexNormal (-1.0, 0.0, 0.0);
    911927
    912   /* normaleLess-testingMode
    913   this->addFace ("1 2 4 3");
    914   this->addFace ("3 4 6 5");
    915   this->addFace ("5 6 8 7");
    916   this->addFace ("7 8 2 1");
    917   this->addFace ("2 8 6 4");
    918   this->addFace ("7 1 3 5");
    919   */
    920 
    921   this->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,1,1, 2,2,2, 4,4,3, 3,3,4);
    922   this->addFace (4, VERTEX_TEXCOORD_NORMAL, 3,3,5, 4,4,6, 6,6,7, 5,5,8);
    923   this->addFace (4, VERTEX_TEXCOORD_NORMAL, 5,5,9, 6,6,10, 8,8,11, 7,7,12);
    924   this->addFace (4, VERTEX_TEXCOORD_NORMAL, 7,7,13, 8,8,14, 2,10,15, 1,9,16);
    925   this->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,2,17, 8,11,18, 6,12,19, 4,4,20);
    926   this->addFace (4, VERTEX_TEXCOORD_NORMAL, 7,13,21, 1,1,22, 3,3,23, 5,14,24);
    927 
    928 }
     928  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,1, 3,3,2, 2,2,3);
     929  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,2,4, 3,3,5, 5,5,6, 4,4,7);
     930  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,4,8, 5,5,9, 7,7,10, 6,6,11);
     931  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,6,12, 7,7,13, 1,9,14, 0,8,15);
     932  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,1,16, 7,10,17, 5,11,18, 3,3,19);
     933  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,12,20, 0,0,21, 2,2,22, 4,13,23);
     934
     935}
Note: See TracChangeset for help on using the changeset viewer.