Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 2853 in orxonox.OLD for orxonox/trunk/src/object.cc


Ignore:
Timestamp:
Nov 14, 2004, 6:41:02 AM (21 years ago)
Author:
bensch
Message:

orxonox/trunk/src: merged importer to trunk again.

File:
1 edited

Legend:

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

    r2835 r2853  
    1818#include "object.h"
    1919
     20/**
     21   \brief Creates a 3D-Object, but does not load any 3D-models
     22   pretty useless
     23*/
    2024Object::Object ()
    2125{
     
    2327  initialize();
    2428
    25   importFile ("reaphigh.obj");
     29  BoxObject();
    2630
    2731  finalize();
    2832}
    2933
     34/**
     35   \brief Crates a 3D-Object and loads in a File
     36   \param fileName file to parse and load (must be a .obj file)
     37*/
    3038Object::Object(char* fileName)
    3139{
     
    3644  finalize();
    3745}
     46
     47/**
     48   \brief Crates a 3D-Object, loads in a File and scales it.
     49   \param fileName file to parse and load (must be a .obj file)
     50   \param scaling The factor that the object will be scaled with.
     51*/
    3852
    3953Object::Object(char* fileName, float scaling)
     
    4761}
    4862
     63/**
     64   \brief deletes an Object
     65*/
     66Object::~Object()
     67{
     68  if (verbose >= 2)
     69    printf ("Deleting display List.\n");
     70  Group* walker = firstGroup;
     71  while (walker != NULL)
     72    {
     73      glDeleteLists (walker->listNumber, 1);
     74      Group* lastWalker = walker;
     75      walker = walker->nextGroup;
     76      delete lastWalker;
     77    }
     78}
     79
     80/**
     81    \brief initializes the Object
     82    This Function initializes all the needed arrays, Lists and clientStates
     83*/
    4984bool Object::initialize (void)
    5085{
    5186  if (verbose >=3)
    5287    printf("new 3D-Object is being created\n");
    53   faceMode = -1;
    54   if ( (listNumber = glGenLists(1)) == 0 )
    55     {
    56       printf ("list could not be created for this Object\n");
    57       return false;
    58     }
    59 
     88
     89  // setting the start group;
     90  firstGroup = new Group;
     91  currentGroup = firstGroup;
     92  groupCount = 0;
     93 
     94  initGroup (currentGroup);
    6095  mtlFileName = "";
    6196  scaleFactor = 1;
    62   vertices = new Array();
    63   normals = new Array();
    64   vTexture = new Array();
    65 
    66   glNewList (listNumber, GL_COMPILE);
     97  material = new Material();
     98
    6799  glEnableClientState (GL_VERTEX_ARRAY);
    68   glEnableClientState (GL_NORMAL_ARRAY);
     100  //  glEnableClientState (GL_NORMAL_ARRAY);
    69101  //  glEnableClientState (GL_TEXTURE_COORD_ARRAY);
    70102
     103
    71104  return true;
    72105}
    73106
     107/**
     108   \brief Imports a obj file and handles the the relative location
     109   \param fileName The file to import
     110*/
    74111bool Object::importFile (char* fileName)
    75112{
     
    81118}
    82119
     120/**
     121   \brief finalizes an Object.
     122   This funcion is needed, to close the glList and all the other lists.
     123*/
    83124bool Object::finalize(void)
    84125{
    85   if (verbose >=3)
     126  //  if (verbose >=3)
    86127    printf("finalizing the 3D-Object\n");
    87   OBJ_FILE->close();
     128  finalizeGroup (currentGroup);
     129  if (material != NULL)
     130    delete material;
     131  return true;
     132}
     133
     134/**
     135   \brief Draws the Objects of all Groups.
     136   It does this by just calling the Lists that must have been created earlier.
     137*/
     138void Object::draw (void)
     139{
     140  if (verbose >=2)
     141    printf("drawing the 3D-Objects\n");
     142  Group* walker = firstGroup;
     143  while (walker != NULL)
     144    {
     145      if (verbose >= 3)
     146        printf ("Drawing object %s\n", walker->name);
     147      glCallList (walker->listNumber);
     148      walker = walker->nextGroup;
     149    }
     150}
     151
     152/**
     153   \brief Draws the Object number groupNumber
     154   It does this by just calling the List that must have been created earlier.
     155   \param groupNumber The number of the group that will be displayed.
     156*/
     157void Object::draw (int groupNumber)
     158{
     159  if (groupNumber >= groupCount)
     160    {
     161      if (verbose>=2)
     162        printf ("You requested object number %i, but this File only contains of %i Objects.\n", groupNumber-1, groupCount);
     163      return;
     164    }
     165  if (verbose >=2)
     166    printf("drawing the requested 3D-Objects if found.\n");
     167  Group* walker = firstGroup;
     168  int counter = 0;
     169  while (walker != NULL)
     170    {
     171      if (counter == groupNumber)
     172        {
     173          if (verbose >= 2)
     174            printf ("Drawing object number %s named %s\n", counter, walker->name);
     175          glCallList (walker->listNumber);
     176          return;
     177        }
     178      ++counter;
     179      walker = walker->nextGroup;
     180    }
     181  if (verbose >= 2)
     182    printf("Object number %i in %s not Found.\n", groupNumber, objFileName);
     183  return;
     184
     185}
     186
     187/**
     188   \brief Draws the Object with a specific groupname
     189   It does this by just calling the List that must have been created earlier.
     190   \param groupName The name of the group that will be displayed.
     191*/
     192void Object::draw (char* groupName)
     193{
     194  if (verbose >=2)
     195    printf("drawing the requested 3D-Objects if found.\n");
     196  Group* walker = firstGroup;
     197  while (walker != NULL)
     198    {
     199      if (!strcmp(walker->name, groupName))
     200        {
     201          if (verbose >= 2)
     202            printf ("Drawing object %s\n", walker->name);
     203          glCallList (walker->listNumber);
     204          return;
     205        }
     206      walker = walker->nextGroup;
     207    }
     208  if (verbose >= 2)
     209    printf("Object Named %s in %s not Found.\n", groupName, objFileName);
     210  return;
     211}
     212
     213/**
     214   \returns Count of the Objects in this File
     215*/
     216int Object::getGroupCount (void)
     217{
     218  return groupCount;
     219}
     220
     221/**
     222   \brief initializes a new Group object
     223*/
     224bool Object::initGroup(Group* group)
     225{
     226  if (verbose >= 2)
     227    printf("Adding new Group\n");
     228  group->faceMode = -1;
     229  group->name = "";
     230  if ((group->listNumber = glGenLists(1)) == 0 )
     231    {
     232      printf ("list could not be created for this Object\n");
     233      return false;
     234    }
     235 
     236  if (groupCount == 0)
     237    {
     238      group->firstVertex = 0;
     239      group->firstNormal = 0;
     240      group->firstNormal = 0;
     241    }
     242  else
     243    {
     244      group->firstVertex = currentGroup->firstVertex + currentGroup->vertices->getCount()/3;
     245      group->firstNormal = currentGroup->firstNormal + currentGroup->normals->getCount()/3;
     246      group->firstVertexTexture = currentGroup->firstVertexTexture + currentGroup->vTexture->getCount()/2;
     247    }
     248
     249  group->vertices = new Array();
     250  group->normals = new Array();
     251  group->vTexture = new Array();
     252
     253  glNewList (group->listNumber, GL_COMPILE);
     254}
     255
     256/**
     257   \brief finalizes a Group.
     258*/
     259bool Object::finalizeGroup(Group* group)
     260{
    88261  glEnd();
    89262  glEndList();
    90   return true;
    91 }
    92 
    93 void Object::draw (void)
    94 {
    95   if (verbose >=3)
    96     printf("drawing the 3D-Object\n");
    97   glCallList (listNumber);
    98 }
    99 
    100 
     263 
     264  delete group->vertices;
     265  delete group->normals;
     266  delete group->vTexture;
     267}
     268/**
     269   \brief Reads in the .obj File and sets all the Values.
     270   This function does read the file, parses it for the occurence of things like vertices, faces and so on, and executes the specific tasks
     271   \param fileName the File that will be parsed (.obj-file)
     272*/
    101273bool Object::readFromObjFile (char* fileName)
    102274{
     
    146318      }
    147319
    148       // case vt
     320      // case VertexTextureCoordinate
    149321      else if (!strncmp(Buffer, "vt ", 2))
    150322      {
    151323        readVertexTexture(Buffer+3);
    152324      }
    153          
    154 
    155     }
    156  
    157 
    158  
    159  
    160 }
    161 
    162 
     325      // case group
     326      else if (!strncmp(Buffer, "g", 1))
     327        {
     328          readGroup (Buffer+2);
     329        }
     330    }
     331  OBJ_FILE->close();
     332
     333}
     334
     335/**
     336   \brief parses a vertex-String
     337   If a vertex line is found this function will inject it into the vertex-Array
     338   \param vertexString The String that will be parsed.
     339*/
    163340bool Object::readVertex (char* vertexString)
    164341{
    165   readVertices = true;
     342  readingVertices = true;
    166343  char subbuffer1[20];
    167344  char subbuffer2[20];
     
    170347  if (verbose >= 3)
    171348    printf ("reading in a vertex: %s %s %s\n", subbuffer1, subbuffer2, subbuffer3);
    172   vertices->addEntry(atof(subbuffer1)*scaleFactor, atof(subbuffer2)*scaleFactor, atof(subbuffer3)*scaleFactor);
     349  currentGroup->vertices->addEntry(atof(subbuffer1)*scaleFactor, atof(subbuffer2)*scaleFactor, atof(subbuffer3)*scaleFactor);
    173350  return true;
    174351}
    175352
     353/**
     354   \brief parses a face-string
     355   If a face line is found this function will add it to the glList.
     356   The function makes a difference between QUADS and TRIANGLES, and will if changed re-open, set and re-close the gl-processe.
     357   \param faceString The String that will be parsed.
     358*/
    176359bool Object::readFace (char* faceString)
    177360{
    178   if (readVertices == true)
    179     {
    180       vertices->finalizeArray();
    181       glVertexPointer(3, GL_FLOAT, 0, vertices->getArray());
    182       normals->finalizeArray();
    183       glNormalPointer(GL_FLOAT, 0, normals->getArray());
    184       vTexture->finalizeArray();
    185     }
    186 
    187   readVertices = false;
     361  // finalize the Arrays;
     362  if (readingVertices == true)
     363    {
     364      currentGroup->vertices->finalizeArray();
     365      glVertexPointer(3, GL_FLOAT, 0, currentGroup->vertices->getArray());
     366      currentGroup->normals->finalizeArray();
     367      glNormalPointer(GL_FLOAT, 0, currentGroup->normals->getArray());
     368      currentGroup->vTexture->finalizeArray();
     369    }
     370
     371  readingVertices = false;
    188372  char subbuffer1[20];
    189373  char subbuffer2[20];
     
    193377  if (!strcmp(subbuffer4, ""))
    194378    {
    195       if (faceMode != 3)
    196         {
    197           if (faceMode != -1)
     379      if (currentGroup->faceMode != 3)
     380        {
     381          if (currentGroup->faceMode != -1)
    198382            glEnd();
    199383          glBegin(GL_TRIANGLES);
    200384        }
    201385     
    202       faceMode = 3;
     386      currentGroup->faceMode = 3;
    203387      if (verbose >=3)
    204388        printf ("found triag: %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3);
     
    210394  else
    211395    {
    212       if (faceMode != 4)
    213         {
    214           if (faceMode != -1)
     396      if (currentGroup->faceMode != 4)
     397        {
     398          if (currentGroup->faceMode != -1)
    215399            glEnd();
    216400          glBegin(GL_QUADS);
    217401        }
    218       faceMode = 4;
     402      currentGroup->faceMode = 4;
    219403      if (verbose >=3 )
    220404        printf ("found quad: %s, %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3, subbuffer4);
     
    227411}
    228412
     413/**
     414   \brief Adds a Face-element (one vertex of a face) with all its information.
     415   It does this by searching:
     416   1. The Vertex itself
     417   2. The VertexNormale
     418   3. The VertexTextureCoordinate
     419   merging this information, the face will be drawn.
     420
     421*/
    229422bool Object::addGLElement (char* elementString)
    230423{
     
    237430  texture[0] = '\0';
    238431  texture ++;
    239   glTexCoord2fv(vTexture->getArray()+(atoi(texture)-1)*2);
     432  glTexCoord2fv(currentGroup->vTexture->getArray()+(atoi(texture)-1 - currentGroup->firstVertexTexture)*2);
    240433
    241434  char* normal;
     
    245438      normal ++;
    246439      //glArrayElement(atoi(vertex)-1);
    247       glNormal3fv(normals->getArray() +(atoi(normal)-1)*3);
    248     }
    249   glVertex3fv(vertices->getArray() +(atoi(vertex)-1)*3);
    250 
    251 }
    252 
     440      glNormal3fv(currentGroup->normals->getArray() +(atoi(normal)-1 - currentGroup->firstNormal)*3);
     441    }
     442  glVertex3fv(currentGroup->vertices->getArray() +(atoi(vertex)-1 - currentGroup->firstVertex)*3);
     443
     444}
     445
     446/**
     447   \brief parses a vertexNormal-String
     448   If a vertexNormal line is found this function will inject it into the vertexNormal-Array
     449   \param normalString The String that will be parsed.
     450*/
    253451bool Object::readVertexNormal (char* normalString)
    254452{
    255   readVertices = true;
     453  readingVertices = true;
    256454  char subbuffer1[20];
    257455  char subbuffer2[20];
     
    260458  if (verbose >=3 )
    261459    printf("found vertex-Normal %s, %s, %s\n", subbuffer1,subbuffer2,subbuffer3);
    262   normals->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3));
     460  currentGroup->normals->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3));
    263461  return true;
    264462}
    265463
     464/**
     465   \brief parses a vertexTextureCoordinate-String
     466   If a vertexTextureCoordinate line is found this function will inject it into the vertexTexture-Array
     467   \param vTextureString The String that will be parsed.
     468*/
    266469bool Object::readVertexTexture (char* vTextureString)
    267470{
    268   readVertices = true;
     471  readingVertices = true;
    269472  char subbuffer1[20];
    270473  char subbuffer2[20];
     
    272475  if (verbose >=3 )
    273476    printf("found vertex-Texture %s, %s\n", subbuffer1,subbuffer2);
    274   vTexture->addEntry(atof(subbuffer1));
    275   vTexture->addEntry(atof(subbuffer2));
     477  currentGroup->vTexture->addEntry(atof(subbuffer1));
     478  currentGroup->vTexture->addEntry(atof(subbuffer2));
    276479  return true;
    277480}
    278481
    279 
     482/**
     483   \brief parses a group String
     484   This function initializes a new Group.
     485   With it you should be able to import .obj-files with more than one Objects inside.
     486   \param groupString the new Group to create
     487*/
     488bool Object::readGroup (char* groupString)
     489{
     490  //  printf ("test\n");
     491  if (!strcmp(groupString, "default"))
     492    {
     493      if (groupCount != 0)
     494        {
     495          Group* newGroup = new Group;
     496          finalizeGroup(currentGroup);
     497          currentGroup->nextGroup = newGroup;
     498          initGroup(newGroup);
     499          currentGroup = newGroup; // must be after init see initGroup for more info
     500        }
     501      ++groupCount;
     502    }
     503  else
     504    {
     505     
     506      currentGroup->name = new char [strlen(groupString)];     
     507      strcpy(currentGroup->name, groupString);
     508    }
     509}
     510
     511/**
     512    \brief Function to read in a mtl File.
     513    this Function parses all Lines of an mtl File
     514    \param mtlFile The .mtl file to read
     515*/
    280516bool Object::readMtlLib (char* mtlFile)
    281517{
     
    291527    printf ("Opening mtlFile: %s\n", mtlFileName);
    292528  char Buffer[500];
    293   vertices = new Array();
    294   material = new Material();
    295529  Material* tmpMat = material;
    296530  while(!MTL_FILE->eof())
     
    328562          tmpMat->setSpecular(Buffer+3);
    329563        }
     564      // setting The Specular Shininess
     565      else if (!strncmp(Buffer, "Ns", 2))
     566        {
     567          tmpMat->setShininess(Buffer+3);
     568        }
     569      // setting up transparency
     570      else if (!strncmp(Buffer, "d", 1))
     571        {
     572          tmpMat->setTransparency(Buffer+2);
     573        }
     574      else if (!strncpy(Buffer, "Tf", 2))
     575        {
     576          tmpMat->setTransparency(Buffer+3);
     577        }
     578
    330579    }
    331580  return true;
    332581}
     582
     583/**
     584   \brief Function that selects a material, if changed in the obj file.
     585   \param matString the Material that will be set.
     586*/
    333587
    334588bool Object::readUseMtl (char* matString)
     
    341595    }
    342596     
    343   if (faceMode != -1)
     597  if (currentGroup->faceMode != -1)
    344598    glEnd();
    345   faceMode = 0;
     599  currentGroup->faceMode = 0;
    346600  if (verbose >= 2)
    347601    printf ("using material %s for coming Faces.\n", matString);
     
    349603}
    350604
    351 
     605/**
     606   \brief Includes a default object
     607   This will inject a Cube, because this is the most basic object.
     608*/
    352609void Object::BoxObject(void)
    353610{
Note: See TracChangeset for help on using the changeset viewer.