Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3066 in orxonox.OLD for orxonox/trunk/importer


Ignore:
Timestamp:
Dec 3, 2004, 3:19:55 PM (20 years ago)
Author:
bensch
Message:

orxonox/trunk/importer: re-arranged the Methods to a more logical order.

Location:
orxonox/trunk/importer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/importer/object.cc

    r3065 r3066  
    2929  importToGL ();
    3030
    31   finalize();
     31  cleanup();
    3232}
    3333
     
    4444  importToGL ();
    4545
    46   finalize();
     46  cleanup();
    4747}
    4848
     
    6161  importToGL ();
    6262
    63   finalize();
     63  cleanup();
    6464}
    6565
     
    7070{
    7171  if (verbose >= 2)
    72     printf ("Deleting display List.\n");
     72    printf ("Deleting display Lists.\n");
    7373  Group* walker = firstGroup;
    7474  while (walker != NULL)
    7575    {
    7676      glDeleteLists (walker->listNumber, 1);
    77       Group* lastWalker = walker;
     77      Group* delWalker = walker;
    7878      walker = walker->next;
    79       delete lastWalker;
     79      delete delWalker;
    8080    }
    8181}
    8282
    83 /**
     83
     84/**
     85   \brief Draws the Objects of all Groups.
     86   It does this by just calling the Lists that must have been created earlier.
     87*/
     88void Object::draw (void) const
     89{
     90  if (verbose >=2)
     91    printf("drawing the 3D-Objects\n");
     92  Group* walker = firstGroup;
     93  while (walker != NULL)
     94    {
     95      if (verbose >= 3)
     96        printf ("Drawing object %s\n", walker->name);
     97      glCallList (walker->listNumber);
     98      walker = walker->next;
     99    }
     100}
     101
     102/**
     103   \brief Draws the Object number groupNumber
     104   It does this by just calling the List that must have been created earlier.
     105   \param groupNumber The number of the group that will be displayed.
     106*/
     107void Object::draw (int groupNumber) const
     108{
     109  if (groupNumber >= groupCount)
     110    {
     111      if (verbose>=1)
     112        printf ("You requested object number %i, but this File only contains of %i Objects.\n", groupNumber-1, groupCount);
     113      return;
     114    }
     115  if (verbose >=2)
     116    printf("drawing the requested 3D-Objects if found.\n");
     117  Group* walker = firstGroup;
     118  int counter = 0;
     119  while (walker != NULL)
     120    {
     121      if (counter == groupNumber)
     122        {
     123          if (verbose >= 2)
     124            printf ("Drawing object number %i named %s\n", counter, walker->name);
     125          glCallList (walker->listNumber);
     126          return;
     127        }
     128      ++counter;
     129      walker = walker->next;
     130    }
     131  if (verbose >= 1)
     132    printf("Object number %i in %s not Found.\n", groupNumber, objFileName);
     133  return;
     134
     135}
     136
     137/**
     138   \brief Draws the Object with a specific groupName
     139   It does this by just calling the List that must have been created earlier.
     140   \param groupName The name of the group that will be displayed.
     141*/
     142void Object::draw (char* groupName) const
     143{
     144  if (verbose >=2)
     145    printf("drawing the requested 3D-Objects if found.\n");
     146  Group* walker = firstGroup;
     147  while (walker != NULL)
     148    {
     149      if (!strcmp(walker->name, groupName))
     150        {
     151          if (verbose >= 2)
     152            printf ("Drawing object %s\n", walker->name);
     153          glCallList (walker->listNumber);
     154          return;
     155        }
     156      walker = walker->next;
     157    }
     158  if (verbose >= 2)
     159    printf("Object Named %s in %s not Found.\n", groupName, objFileName);
     160  return;
     161}
     162
     163/**
     164   \returns Count of the Objects in this File
     165*/
     166int Object::getGroupCount (void) const
     167{
     168  return groupCount;
     169}
     170
     171/**
    84172    \brief initializes the Object
    85173    This Function initializes all the needed arrays, Lists and clientStates
     
    108196
    109197/**
    110    \brief Imports a obj file and handles the the relative location
    111    \param fileName The file to import
    112 */
    113 bool Object::importFile (char* fileName)
    114 {
    115   if (verbose >=3)
    116     printf("preparing to read in file: %s\n", fileName);   
    117   objFileName = fileName;
    118   this->readFromObjFile (objFileName);
     198   \brief initializes a new Group object
     199*/
     200bool Object::initGroup(Group* group)
     201{
     202  if (verbose >= 2)
     203    printf("Adding new Group\n");
     204  group->name = "";
     205  group->faceMode = -1;
     206  group->faceCount = 0; 
     207  group->next = NULL;
     208
     209  group->firstFace = new Face;
     210  group->currentFace = group->firstFace;
     211}
     212
     213/**
     214   \brief initializes a new Face. (sets default Values)
     215*/
     216bool Object::initFace (Face* face)
     217{
     218  face->vertexCount = 0;
     219
     220  face->firstElem = NULL;
     221 
     222  face->materialString = NULL;
     223 
     224  face->next = NULL;
     225
    119226  return true;
    120227}
    121228
    122229/**
    123   \brief finalizes an Object.
    124    This funcion is needed, to close the glList and all the other lists. This will be applied at the end of the importing Process.
    125 */
    126 bool Object::finalize(void)
     230   \brief finalizes an Object.
     231   This funcion is needed, to delete all the Lists, and arrays that are no more needed because they are already imported into openGL. This will be applied at the end of the importing Process.
     232*/
     233bool Object::cleanup(void)
    127234{
    128235  //  if (verbose >=3)
    129236    printf("finalizing the 3D-Object\n");
    130   finalizeGroup (currentGroup);
    131237
    132238  if (vertices != NULL)
     
    143249
    144250/**
    145    \brief Draws the Objects of all Groups.
    146    It does this by just calling the Lists that must have been created earlier.
    147 */
    148 void Object::draw (void) const
    149 {
    150   if (verbose >=2)
    151     printf("drawing the 3D-Objects\n");
    152   Group* walker = firstGroup;
    153   while (walker != NULL)
    154     {
    155       if (verbose >= 3)
    156         printf ("Drawing object %s\n", walker->name);
    157       glCallList (walker->listNumber);
    158       walker = walker->next;
    159     }
    160 }
    161 
    162 /**
    163    \brief Draws the Object number groupNumber
    164    It does this by just calling the List that must have been created earlier.
    165    \param groupNumber The number of the group that will be displayed.
    166 */
    167 void Object::draw (int groupNumber) const
    168 {
    169   if (groupNumber >= groupCount)
    170     {
    171       if (verbose>=1)
    172         printf ("You requested object number %i, but this File only contains of %i Objects.\n", groupNumber-1, groupCount);
    173       return;
    174     }
    175   if (verbose >=2)
    176     printf("drawing the requested 3D-Objects if found.\n");
    177   Group* walker = firstGroup;
    178   int counter = 0;
    179   while (walker != NULL)
    180     {
    181       if (counter == groupNumber)
    182         {
    183           if (verbose >= 2)
    184             printf ("Drawing object number %i named %s\n", counter, walker->name);
    185           glCallList (walker->listNumber);
    186           return;
    187         }
    188       ++counter;
    189       walker = walker->next;
    190     }
    191   if (verbose >= 1)
    192     printf("Object number %i in %s not Found.\n", groupNumber, objFileName);
    193   return;
    194 
    195 }
    196 
    197 /**
    198    \brief Draws the Object with a specific groupName
    199    It does this by just calling the List that must have been created earlier.
    200    \param groupName The name of the group that will be displayed.
    201 */
    202 void Object::draw (char* groupName) const
    203 {
    204   if (verbose >=2)
    205     printf("drawing the requested 3D-Objects if found.\n");
    206   Group* walker = firstGroup;
    207   while (walker != NULL)
    208     {
    209       if (!strcmp(walker->name, groupName))
    210         {
    211           if (verbose >= 2)
    212             printf ("Drawing object %s\n", walker->name);
    213           glCallList (walker->listNumber);
    214           return;
    215         }
    216       walker = walker->next;
    217     }
    218   if (verbose >= 2)
    219     printf("Object Named %s in %s not Found.\n", groupName, objFileName);
    220   return;
    221 }
    222 
    223 /**
    224    \returns Count of the Objects in this File
    225 */
    226 int Object::getGroupCount (void) const
    227 {
    228   return groupCount;
    229 }
    230 
    231 /**
    232    \brief initializes a new Group object
    233 */
    234 bool Object::initGroup(Group* group)
    235 {
    236   if (verbose >= 2)
    237     printf("Adding new Group\n");
    238   group->name = "";
    239   group->faceMode = -1;
    240   group->faceCount = 0; 
    241   group->next = NULL;
    242 
    243   group->firstFace = new Face;
    244   group->currentFace = group->firstFace;
    245 }
    246 
    247 /**
    248    \brief finalizes a Group.
    249    \param group the group to finalize.
    250 */
    251 bool Object::finalizeGroup(Group* group) // think it is not needed anymore
    252 {
    253   //  if (verbose >=2)
    254   //    printf ("Finalize group %s.\n", group->name);
    255 
     251   \brief Imports a obj file and handles the the relative location
     252   \param fileName The file to import
     253*/
     254bool Object::importFile (char* fileName)
     255{
     256  if (verbose >=3)
     257    printf("preparing to read in file: %s\n", fileName);   
     258  objFileName = fileName;
     259  this->readFromObjFile (objFileName);
     260  return true;
    256261}
    257262
     
    326331  OBJ_FILE->close();
    327332  return true;
     333
     334}
     335
     336/**
     337   \brief parses a group String
     338   This function initializes a new Group.
     339   With it you should be able to import .obj-files with more than one Objects inside.
     340   \param groupString the new Group to create
     341*/
     342bool Object::readGroup (char* groupString)
     343{
     344  // setting the group name if not default.
     345  if (strcmp(currentGroup->name, "default"))
     346    {
     347      currentGroup->name = new char [strlen(groupString)];
     348      strcpy(currentGroup->name, groupString);
     349    }
     350  if (groupCount != 0 && currentGroup->faceCount>0)
     351    {
     352      Group* newGroup = new Group;
     353      //      finalizeGroup(currentGroup);
     354      currentGroup->next = newGroup;
     355      initGroup(newGroup);
     356      currentGroup = newGroup; // must be after init see initGroup for more info
     357    }
     358
     359  ++groupCount;
    328360
    329361}
     
    381413
    382414/**
    383    \brief Adds a Face-element (one vertex of a face) with all its information.
    384    It does this by searching:
    385    1. The Vertex itself
    386    2. The VertexNormale
    387    3. The VertexTextureCoordinate
    388    merging this information, the face will be drawn.
    389 
    390 */
    391 bool Object::addGLElement (char* elementString)
    392 {
    393   if (verbose >=3)
    394     printf ("importing grafical Element to openGL: %s\n", elementString);
    395   char* vertex = elementString;
    396 
    397   char* texture;
    398   if ((texture = strstr (vertex, "/")) != NULL)
    399     {
    400       texture[0] = '\0';
    401       texture ++;
    402       glTexCoord2fv(vTexture->getArray()+(atoi(texture)-1)*2);
    403 
    404       char* normal;
    405       if ((normal = strstr (texture, "/")) !=NULL)
    406         {
    407           normal[0] = '\0';
    408           normal ++;
    409           //glArrayElement(atoi(vertex)-1);
    410           glNormal3fv(normals->getArray() +(atoi(normal)-1)*3);
    411         }
    412     }
    413   glVertex3fv(vertices->getArray() +(atoi(vertex)-1)*3);
    414 
    415 }
    416 
    417 /**
    418415   \brief parses a vertexNormal-String
    419416   If a vertexNormal line is found this function will inject it into the vertexNormal-Array
     
    449446}
    450447
    451 /**
    452    \brief parses a group String
    453    This function initializes a new Group.
    454    With it you should be able to import .obj-files with more than one Objects inside.
    455    \param groupString the new Group to create
    456 */
    457 bool Object::readGroup (char* groupString)
    458 {
    459   // setting the group name if not default.
    460   if (strcmp(currentGroup->name, "default"))
    461     {
    462       currentGroup->name = new char [strlen(groupString)];
    463       strcpy(currentGroup->name, groupString);
    464     }
    465   if (groupCount != 0 && currentGroup->faceCount>0)
    466     {
    467       Group* newGroup = new Group;
    468       //      finalizeGroup(currentGroup);
    469       currentGroup->next = newGroup;
    470       initGroup(newGroup);
    471       currentGroup = newGroup; // must be after init see initGroup for more info
    472     }
    473 
    474   ++groupCount;
    475 
    476 }
    477 
    478 
    479 bool Object::initFace (Face* face)
    480 {
    481   face->vertexCount = 0;
    482 
    483   face->firstElem = NULL;
     448/**
     449    \brief Function to read in a mtl File.
     450    this Function parses all Lines of an mtl File
     451    \param mtlFile The .mtl file to read
     452*/
     453bool Object::readMtlLib (char* mtlFile)
     454{
     455  MTL_FILE = new ifstream (mtlFile);
     456  if (!MTL_FILE->is_open())
     457    {
     458      if (verbose >= 1)
     459        printf ("unable to open file: %s\n", mtlFile);
     460      return false;
     461    }
     462  mtlFileName = mtlFile;
     463  if (verbose >=2)
     464    printf ("Opening mtlFile: %s\n", mtlFileName);
     465  char Buffer[500];
     466  Material* tmpMat = material;
     467  while(!MTL_FILE->eof())
     468    {
     469      MTL_FILE->getline(Buffer, 500);
     470      if (verbose >= 4)
     471        printf("found line in mtlFile: %s\n", Buffer);
     472     
     473
     474      // create new Material
     475      if (!strncmp(Buffer, "newmtl ", 7))
     476        {
     477          tmpMat = tmpMat->addMaterial(Buffer+7);
     478          //      printf ("%s, %p\n", tmpMat->getName(), tmpMat);
     479        }
     480      // setting a illumMode
     481      else if (!strncmp(Buffer, "illum ", 6))
     482        {
     483          tmpMat->setIllum(Buffer+6);
     484
     485        }
     486      // setting Diffuse Color
     487      else if (!strncmp(Buffer, "Kd ", 3))
     488        {
     489          tmpMat->setDiffuse(Buffer+3);
     490        }
     491      // setting Ambient Color
     492      else if (!strncmp(Buffer, "Ka ", 3))
     493        {
     494          tmpMat->setAmbient(Buffer+3);
     495        }
     496      // setting Specular Color
     497      else if (!strncmp(Buffer, "Ks ", 3))
     498        {
     499          tmpMat->setSpecular(Buffer+3);
     500        }
     501      // setting The Specular Shininess
     502      else if (!strncmp(Buffer, "Ns ", 3))
     503        {
     504          tmpMat->setShininess(Buffer+3);
     505        }
     506      // setting up transparency
     507      else if (!strncmp(Buffer, "d ", 2))
     508        {
     509          tmpMat->setTransparency(Buffer+2);
     510        }
     511      else if (!strncpy(Buffer, "Tf ", 3))
     512        {
     513          tmpMat->setTransparency(Buffer+3);
     514        }
     515
     516    }
     517  return true;
     518}
     519
     520/**
     521   \brief Function that selects a material, if changed in the obj file.
     522   \param matString the Material that will be set.
     523*/
     524bool Object::readUseMtl (char* matString)
     525{
     526  if (!strcmp (mtlFileName, ""))
     527    {
     528      if (verbose >= 1)
     529        printf ("Not using new defined material, because no mtlFile found yet\n");
     530      return false;
     531    }
     532     
     533  if (currentGroup->faceCount >0)
     534    currentGroup->currentFace = currentGroup->currentFace->next = new Face;
     535  initFace (currentGroup->currentFace);
    484536 
    485   face->materialString = NULL;
     537  currentGroup->currentFace->materialString = new char[strlen(matString)];
     538  strcpy (currentGroup->currentFace->materialString, matString);
    486539 
    487   face->next = NULL;
    488 
    489   return true;
    490 }
    491 
     540  if (currentGroup->faceCount == 0)
     541    currentGroup->faceCount ++;
     542
     543}
     544
     545/**
     546   \brief reads and includes the Faces/Materials into the openGL state Machine
     547*/
    492548bool Object::importToGL (void)
    493549{
     
    564620         
    565621          FaceElement* tmpElem = tmpFace->firstElem;
    566           FaceElement* delElem;
    567622          while (tmpElem != NULL)
    568623            {
    569624              //      printf ("%s\n", tmpElem->value);
    570625              addGLElement(tmpElem->value);
    571               delElem = tmpElem;
    572626              tmpElem = tmpElem->next;
    573               //              delete delElem;
    574627            }
    575628          tmpFace = tmpFace->next;
     
    581634}
    582635
    583 /**
    584     \brief Function to read in a mtl File.
    585     this Function parses all Lines of an mtl File
    586     \param mtlFile The .mtl file to read
    587 */
    588 bool Object::readMtlLib (char* mtlFile)
    589 {
    590   MTL_FILE = new ifstream (mtlFile);
    591   if (!MTL_FILE->is_open())
    592     {
    593       if (verbose >= 1)
    594         printf ("unable to open file: %s\n", mtlFile);
    595       return false;
    596     }
    597   mtlFileName = mtlFile;
    598   if (verbose >=2)
    599     printf ("Opening mtlFile: %s\n", mtlFileName);
    600   char Buffer[500];
    601   Material* tmpMat = material;
    602   while(!MTL_FILE->eof())
    603     {
    604       MTL_FILE->getline(Buffer, 500);
    605       if (verbose >= 4)
    606         printf("found line in mtlFile: %s\n", Buffer);
    607      
    608 
    609       // create new Material
    610       if (!strncmp(Buffer, "newmtl ", 7))
    611         {
    612           tmpMat = tmpMat->addMaterial(Buffer+7);
    613           //      printf ("%s, %p\n", tmpMat->getName(), tmpMat);
    614         }
    615       // setting a illumMode
    616       else if (!strncmp(Buffer, "illum ", 6))
    617         {
    618           tmpMat->setIllum(Buffer+6);
    619 
    620         }
    621       // setting Diffuse Color
    622       else if (!strncmp(Buffer, "Kd ", 3))
    623         {
    624           tmpMat->setDiffuse(Buffer+3);
    625         }
    626       // setting Ambient Color
    627       else if (!strncmp(Buffer, "Ka ", 3))
    628         {
    629           tmpMat->setAmbient(Buffer+3);
    630         }
    631       // setting Specular Color
    632       else if (!strncmp(Buffer, "Ks ", 3))
    633         {
    634           tmpMat->setSpecular(Buffer+3);
    635         }
    636       // setting The Specular Shininess
    637       else if (!strncmp(Buffer, "Ns ", 3))
    638         {
    639           tmpMat->setShininess(Buffer+3);
    640         }
    641       // setting up transparency
    642       else if (!strncmp(Buffer, "d ", 2))
    643         {
    644           tmpMat->setTransparency(Buffer+2);
    645         }
    646       else if (!strncpy(Buffer, "Tf ", 3))
    647         {
    648           tmpMat->setTransparency(Buffer+3);
    649         }
    650 
    651     }
    652   return true;
    653 }
    654 
    655 /**
    656    \brief Function that selects a material, if changed in the obj file.
    657    \param matString the Material that will be set.
    658 */
    659 bool Object::readUseMtl (char* matString)
    660 {
    661   if (!strcmp (mtlFileName, ""))
    662     {
    663       if (verbose >= 1)
    664         printf ("Not using new defined material, because no mtlFile found yet\n");
    665       return false;
    666     }
    667      
    668   if (currentGroup->faceCount >0)
    669     currentGroup->currentFace = currentGroup->currentFace->next = new Face;
    670   initFace (currentGroup->currentFace);
    671  
    672   currentGroup->currentFace->materialString = new char[strlen(matString)];
    673   strcpy (currentGroup->currentFace->materialString, matString);
    674  
    675   if (currentGroup->faceCount == 0)
    676     currentGroup->faceCount ++;
    677 
    678 }
     636/**
     637   \brief Adds a Face-element (one vertex of a face) with all its information.
     638   It does this by searching:
     639   1. The Vertex itself
     640   2. The VertexNormale
     641   3. The VertexTextureCoordinate
     642   merging this information, the face will be drawn.
     643
     644*/
     645bool Object::addGLElement (char* elementString)
     646{
     647  if (verbose >=3)
     648    printf ("importing grafical Element to openGL: %s\n", elementString);
     649  char* vertex = elementString;
     650
     651  char* texture;
     652  if ((texture = strstr (vertex, "/")) != NULL)
     653    {
     654      texture[0] = '\0';
     655      texture ++;
     656      glTexCoord2fv(vTexture->getArray()+(atoi(texture)-1)*2);
     657
     658      char* normal;
     659      if ((normal = strstr (texture, "/")) !=NULL)
     660        {
     661          normal[0] = '\0';
     662          normal ++;
     663          //glArrayElement(atoi(vertex)-1);
     664          glNormal3fv(normals->getArray() +(atoi(normal)-1)*3);
     665        }
     666    }
     667  glVertex3fv(vertices->getArray() +(atoi(vertex)-1)*3);
     668
     669}
     670
    679671
    680672/**
  • orxonox/trunk/importer/object.h

    r3065 r3066  
    2929  ~Object ();
    3030 
    31   bool importFile (char* fileName);
    32   bool initialize (void);
    33   bool finalize(void);
    3431  void draw (void) const;
    3532  void draw (int groupNumber) const;
     
    4845  {
    4946    int vertexCount;
    50 
    5147    FaceElement* firstElem;
    5248
     
    8278  int groupCount;
    8379
    84   char* objFileName;
    85   char* mtlFileName;
    86 
    8780  Material* material;
    8881  float scaleFactor;
    8982
    9083  char* objPath;
     84  char* objFileName;
     85  char* mtlFileName;
    9186  ifstream* OBJ_FILE;
    9287  ifstream* MTL_FILE;
    9388
     89  bool initialize (void);
    9490  bool initGroup(Group* group);
    95   bool finalizeGroup (Group* group);
    96 
     91  bool initFace (Face* face);
     92  bool cleanup(void);
     93  bool cleanupGroup(Group* group);
     94  bool cleanupFace(Face* face);
    9795
    9896  ///// readin ///
     97  bool importFile (char* fileName);
    9998  bool readFromObjFile (char* fileName);
    10099 
     100  bool readGroup (char* groupString);
    101101  bool readVertex (char* vertexString);
    102102  bool readFace (char* faceString);
    103   bool readVT (char* vtString);
    104103  bool readVertexNormal (char* normalString);
    105104  bool readVertexTexture (char* vTextureString);
    106   bool readGroup (char* groupString);
    107105  bool readMtlLib (char* matFile);
    108106  bool readUseMtl (char* mtlString);
    109107
    110   bool initFace (Face* face);
    111108  bool importToGL (void);
    112109  bool addGLElement (char* elementString);
Note: See TracChangeset for help on using the changeset viewer.