Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 4022 in orxonox.OLD


Ignore:
Timestamp:
May 3, 2005, 1:35:22 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: Model-Class is now more dynamic (all subelements are now classes too, i hope, that atilla was right about classes being as fast as structs

Location:
orxonox/trunk/src/lib/graphics/importer
Files:
2 edited

Legend:

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

    r4020 r4022  
    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////////////////////
     33ModelFaceElement::ModelFaceElement()
     34{
     35}
     36
     37ModelFaceElement::~ModelFaceElement()
     38{
     39  if (this->next != NULL)
     40    delete this->next;
     41}
     42
     43ModelFace::ModelFace()
     44{
     45  this->vertexCount = 0;
     46
     47  this->firstElem = NULL;
     48 
     49  this->material = NULL;
     50 
     51  this->next = NULL;
     52
     53}
     54
     55ModelFace::~ModelFace()
     56{
     57  PRINTF(5)("Cleaning up Face\n");
     58
     59  if (this->firstElem != NULL)
     60    {
     61      delete this->firstElem;
     62    }
     63     
     64  if (this->next != NULL)
     65    {
     66      delete this->next;
     67    }
     68
     69}
     70
     71ModelGroup::ModelGroup()
     72{
     73  PRINTF(4)("Adding new Group\n");
     74  this->name = "";
     75  this->faceMode = -1;
     76  this->faceCount = 0; 
     77  this->next = NULL;
     78 
     79  this->firstFace = new ModelFace;
     80  this->currentFace = this->firstFace;
     81}
     82
     83ModelGroup::~ModelGroup()
     84{
     85  PRINTF(5)("Cleaning up group\n");
     86  if (this->firstFace != NULL)
     87    {
     88      delete this->firstFace;
     89    }
     90
     91  if (this->next !=NULL)
     92    delete this->next;
     93}
     94
     95void ModelGroup::cleanup(void)
     96{
     97  if (this->firstFace)
     98    delete this->firstFace;
     99  this->firstFace = NULL;
     100  if (this->next)
     101    this->next->cleanup();
     102}
     103
     104
     105/////////////
     106/// MODEL ///
     107/////////////
     108/**
     109   \brief Creates a 3D-Model.
     110
     111   assigns it a Name and a Type
    34112*/
    35113Model::Model(const char* modelName, MODEL_TYPE type)
     
    42120  this->finalized = false;
    43121  // setting the start group;
    44   this->firstGroup = new Group;
    45   this->currentGroup = this->firstGroup;
     122  this->currentGroup = this->firstGroup = new ModelGroup;
    46123  this->groupCount = 0;
    47124 
    48   this->initGroup (this->currentGroup);
    49125  this->scaleFactor = 1;
    50126
     
    76152
    77153  PRINTF(5)("Deleting display Lists.\n");
    78   Group* walker = this->firstGroup;
     154  ModelGroup* walker = this->firstGroup;
    79155  while (walker != NULL)
    80156    {
    81157      glDeleteLists (walker->listNumber, 1);
    82       Group* delWalker = walker;
     158      ModelGroup* delWalker = walker;
    83159      walker = walker->next;
    84160      delete delWalker;
     
    128204{
    129205  PRINTF(4)("drawing the 3D-Models\n");
    130   Group* walker = this->firstGroup;
     206  ModelGroup* walker = this->firstGroup;
    131207  while (walker != NULL)
    132208    {
     
    151227    }
    152228  PRINTF(4)("drawing the requested 3D-Models if found.\n");
    153   Group* walker = this->firstGroup;
     229  ModelGroup* walker = this->firstGroup;
    154230  int counter = 0;
    155231  while (walker != NULL)
     
    178254{
    179255  PRINTF(4)("drawing the requested 3D-Models if found.\n");
    180   Group* walker = this->firstGroup;
     256  ModelGroup* walker = this->firstGroup;
    181257  while (walker != NULL)
    182258    {
     
    210286void Model::setName(const char* name)
    211287{
    212   if (this->name) 
     288  if (this->name)
    213289    delete []this->name;
    214290  if (name)
     
    219295  else
    220296    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;
    257297}
    258298
     
    280320{
    281321  PRINTF(4)("cleaning up the 3D-Model to save Memory.\n");
    282   this->cleanupGroup(this->firstGroup);
     322  this->firstGroup->cleanup();
    283323  return true;
    284324}
    285325
    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 }
     326
    337327
    338328//////////
     
    395385
    396386   This function initializes a new Group.
    397    With it you should be able to import .obj-files with more than one Models inside.
     387   With it you should be able to create Models with more than one SubModel inside
    398388*/
    399389bool Model::addGroup (const char* groupString)
    400390{
    401391  PRINTF(5)("Read Group: %s.\n", groupString);
    402   if (this->groupCount != 0 && this->currentGroup->faceCount>0)
     392  if (this->groupCount != 0 && this->currentGroup->faceCount > 0)
    403393    {
    404394      //      finalizeGroup(currentGroup);
    405       this->currentGroup = this->currentGroup->next = new Group;
    406       this->initGroup(this->currentGroup);
     395      this->currentGroup = this->currentGroup->next = new ModelGroup;
    407396    }
    408397  // setting the group name if not default.
     
    518507{
    519508  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;
     509    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
     510
     511  ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement;
    524512  tmpElem->next = NULL;
    525513  while(strcmp (faceString, "\0"))
    526514    {
    527515      if (this->currentGroup->currentFace->vertexCount>0)
    528           tmpElem = tmpElem->next = new FaceElement;
     516          tmpElem = tmpElem->next = new ModelFaceElement;
    529517      tmpElem->next = NULL;
    530518
     
    579567bool Model::addFace(int faceElemCount, VERTEX_FORMAT type, ...)
    580568{
    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;
     569  if (this->currentGroup->faceCount > 0)
     570    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
     571 
     572  ModelFaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new ModelFaceElement;
    586573  tmpElem->next = NULL;
    587574 
     
    592579    {
    593580      if (this->currentGroup->currentFace->vertexCount>0)
    594           tmpElem = tmpElem->next = new FaceElement;
     581        tmpElem = tmpElem->next = new ModelFaceElement;
    595582      tmpElem->next = NULL;
    596583
     
    614601{
    615602  if (this->currentGroup->faceCount > 0)
    616     this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
    617   this->initFace (this->currentGroup->currentFace);
     603    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
    618604 
    619605  this->currentGroup->currentFace->material = this->findMaterialByName(matString);
     
    630616{
    631617  if (this->currentGroup->faceCount > 0)
    632     this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
    633   this->initFace (this->currentGroup->currentFace);
     618    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new ModelFace;
    634619 
    635620  this->currentGroup->currentFace->material = mtl;
     
    662647  Vector curV;
    663648
    664   Group* tmpGroup = firstGroup;
     649  ModelGroup* tmpGroup = firstGroup;
    665650  while (tmpGroup)
    666651    {
    667       Face* tmpFace = tmpGroup->firstFace;
     652      ModelFace* tmpFace = tmpGroup->firstFace;
    668653      while (tmpFace)
    669654        {
    670655          if (tmpFace->firstElem)
    671656            {
    672               FaceElement* firstElem = tmpFace->firstElem;
    673               FaceElement* prevElem;
    674               FaceElement* curElem = firstElem;
    675               FaceElement* nextElem;
    676               FaceElement* lastElem;
     657              ModelFaceElement* firstElem = tmpFace->firstElem;
     658              ModelFaceElement* prevElem;
     659              ModelFaceElement* curElem = firstElem;
     660              ModelFaceElement* nextElem;
     661              ModelFaceElement* lastElem;
    677662              // 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.
    678663              while (curElem)
     
    744729
    745730      // Putting Faces to GL
    746       Face* tmpFace = this->currentGroup->firstFace;
     731      ModelFace* tmpFace = this->currentGroup->firstFace;
    747732      while (tmpFace != NULL)
    748733        {
     
    794779            }
    795780         
    796           FaceElement* tmpElem = tmpFace->firstElem;
     781          ModelFaceElement* tmpElem = tmpFace->firstElem;
    797782          while (tmpElem != NULL)
    798783            {
     
    841826   merging this information, the face will be drawn.
    842827*/
    843 bool Model::addGLElement (FaceElement* elem)
     828bool Model::addGLElement (ModelFaceElement* elem)
    844829{
    845830  PRINTF(5)("importing grafical Element to openGL.\n");
  • orxonox/trunk/src/lib/graphics/importer/model.h

    r3917 r4022  
    3434                    VERTEX_TEXCOORD_NORMAL = NORMAL | TEXCOORD};
    3535
     36////////////////////
     37/// SUB-ELEMENTS ///
     38////////////////////
     39//! This is the placeholder of one Vertex beloning to a Face.
     40class ModelFaceElement
     41{
     42 public:
     43  ModelFaceElement();
     44  ~ModelFaceElement();
     45
     46  int vertexNumber;         //!< The number of the Vertex out of the Array* vertices, this vertex points to.
     47  int normalNumber;         //!< The number of the Normal out of the Array* normals, this vertex points to.
     48  int texCoordNumber;       //!< The number of the textureCoordinate out of the Array* vTexture, this vertex points to.
     49  ModelFaceElement* next;   //!< Point to the next FaceElement in this List.
     50};
     51
     52//! This is the placeholder of a Face belonging to a Group of Faces.
     53class ModelFace
     54{
     55 public:
     56  ModelFace();
     57  ~ModelFace();
     58
     59  int vertexCount;                //!< The Count of vertices this Face has.
     60  ModelFaceElement* firstElem;    //!< Points to the first Vertex (FaceElement) of this Face.
     61 
     62  Material* material;             //!< The Material to use.
     63 
     64  ModelFace* next;                //!< Pointer to the next Face.
     65};
     66
     67//! Group to handle multiple Models per obj-file.
     68class ModelGroup
     69{
     70 public:
     71  ModelGroup();
     72  ~ModelGroup();
     73
     74  void cleanup();
     75
     76  char* name;                 //!< the Name of the Group. this is an identifier, that can be accessed via the draw (char* name) function.
     77 
     78  GLubyte* indices;           //!< The indices of the Groups. Needed for vertex-arrays
     79  GLuint listNumber;          //!< The number of the GL-List this Group gets.
     80  ModelFace* firstFace;       //!< The first Face in this group.
     81  ModelFace* currentFace;     //!< The current Face in this Group (the one we are currently working with.)
     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  int faceCount;              //!< The Number of Faces this Group holds.
     84 
     85  ModelGroup* next;           //!< Pointer to the next Group.
     86};
     87
     88/////////////
     89/// MODEL ///
     90/////////////
     91
    3692//! Class that handles 3D-Models. it can also read them in and display them.
    3793class Model
    3894{
    3995 private:
    40   /////////////
    41   // structs //
    42   /////////////
    43   //! This is the placeholder of one Vertex beloning to a Face.
    44   struct FaceElement
    45   {
    46     int vertexNumber;    //!< The number of the Vertex out of the Array* vertices, this vertex points to.
    47     int normalNumber;    //!< The number of the Normal out of the Array* normals, this vertex points to.
    48     int texCoordNumber;  //!< The number of the textureCoordinate out of the Array* vTexture, this vertex points to.
    49     FaceElement* next;   //!< Point to the next FaceElement in this List.
    50   };
    5196
    52   //! This is the placeholder of a Face belonging to a Group of Faces.
    53   struct Face
    54   {
    55     int vertexCount;        //!< The Count of vertices this Face has.
    56     FaceElement* firstElem; //!< Points to the first Vertex (FaceElement) of this Face.
     97  char* name;                 //!< This is the name of the Model.
     98  MODEL_TYPE type;
     99  bool finalized;             //!< Sets the Object to be finalized.
    57100
    58     Material* material;     //!< The Material to use.
     101  Array* vertices;            //!< The Array that handles the Vertices.
     102  int verticesCount;          //!< A global Counter for vertices.
     103  Array* normals;             //!< The Array that handles the Normals.
     104  Array* vTexture;            //!< The Array that handles the VertexTextureCoordinates.
    59105
    60     Face* next;             //!< Pointer to the next Face.
    61   };
     106  ModelGroup* firstGroup;     //!< The first of all groups.
     107  ModelGroup* currentGroup;   //!< The currentGroup. this is the one we will work with.
     108  int groupCount;             //!< The Count of Groups.
    62109
    63   //! Group to handle multiple Models per obj-file.
    64   struct Group
    65   {
    66     char* name;         //!< the Name of the Group. this is an identifier, that can be accessed via the draw (char* name) function.
    67 
    68     GLubyte* indices;   //!< The indices of the Groups. Needed for vertex-arrays
    69     GLuint listNumber;  //!< The number of the GL-List this Group gets.
    70     Face* firstFace;    //!< The first Face in this group.
    71     Face* currentFace;  //!< The current Face in this Group (the one we are currently working with.)
    72     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...
    73     int faceCount;      //!< The Number of Faces this Group holds.
    74 
    75     Group* next;        //!< Pointer to the next Group.
    76   };
    77 
    78   char* name;            //!< This is the name of the Model.
    79   MODEL_TYPE type;
    80   bool finalized;        //!< Sets the Object to be finalized.
    81 
    82   Array* vertices;      //!< The Array that handles the Vertices.
    83   int verticesCount;    //!< A global Counter for vertices.
    84   Array* normals;       //!< The Array that handles the Normals.
    85   Array* vTexture;      //!< The Array that handles the VertexTextureCoordinates.
    86 
    87   Group* firstGroup;    //!< The first of all groups.
    88   Group* currentGroup;  //!< The currentGroup. this is the one we will work with.
    89   int groupCount;       //!< The Count of Groups.
    90 
    91   tList<Material>* materialList;
     110  tList<Material>* materialList;//!< A list for all the Materials in this Model
    92111 
    93 
    94   bool initGroup(Group* group);
    95   bool initFace (Face* face);
    96 
    97112  bool buildVertexNormals(void);
    98113
    99114  bool importToDisplayList(void);
    100   bool addGLElement(FaceElement* elem);
     115  bool addGLElement(ModelFaceElement* elem);
    101116
    102117  bool importToVertexArray(void);
     
    104119  bool deleteArrays(void);
    105120  bool cleanup(void);
    106   bool cleanupGroup(Group* group);
    107   bool cleanupFace(Face* face);
    108   bool cleanupFaceElement(FaceElement* faceElem);
    109121
    110122
Note: See TracChangeset for help on using the changeset viewer.