Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 2850 in orxonox.OLD for orxonox/trunk/importer/object.cc


Ignore:
Timestamp:
Nov 14, 2004, 6:16:42 AM (20 years ago)
Author:
bensch
Message:

orxonox/trunk/importer: included groups to filereader

File:
1 edited

Legend:

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

    r2848 r2850  
    6565{
    6666  if (verbose >= 2)
    67     printf ("Don't know what to delete, or how to destroy a glList\n");
    68 
     67    printf ("Deleting display List.\n");
     68  Group* walker = firstGroup;
     69  while (walker != NULL)
     70    {
     71      glDeleteLists (walker->listNumber, 1);
     72      Group* lastWalker = walker;
     73      walker = walker->nextGroup;
     74      delete lastWalker;
     75    }
    6976}
    7077
     
    7784  if (verbose >=3)
    7885    printf("new 3D-Object is being created\n");
    79   faceMode = -1;
    80   if ( (listNumber = glGenLists(1)) == 0 )
    81     {
    82       printf ("list could not be created for this Object\n");
    83       return false;
    84     }
    85 
     86
     87  // setting the start group;
     88  firstGroup = new Group;
     89  currentGroup = firstGroup;
     90  groupCount = 0;
     91 
     92  initGroup (currentGroup);
    8693  mtlFileName = "";
    8794  scaleFactor = 1;
    88   vertices = new Array();
    89   normals = new Array();
    90   vTexture = new Array();
    91 
    9295  material = new Material();
    9396
    94   glNewList (listNumber, GL_COMPILE);
    9597  glEnableClientState (GL_VERTEX_ARRAY);
    96   glEnableClientState (GL_NORMAL_ARRAY);
     98  //  glEnableClientState (GL_NORMAL_ARRAY);
    9799  //  glEnableClientState (GL_TEXTURE_COORD_ARRAY);
     100
    98101
    99102  return true;
     
    119122bool Object::finalize(void)
    120123{
     124  //  if (verbose >=3)
     125    printf("finalizing the 3D-Object\n");
     126  finalizeGroup (currentGroup);
     127  if (material != NULL)
     128    delete material;
     129  return true;
     130}
     131
     132/**
     133   \brief Draws the Object
     134   It does this by just calling the List that must have been created earlier.
     135*/
     136void Object::draw (void)
     137{
    121138  if (verbose >=3)
    122     printf("finalizing the 3D-Object\n");
     139    printf("drawing the 3D-Object\n");
     140  Group* walker = firstGroup;
     141  while (walker != NULL)
     142    {
     143      glCallList (walker->listNumber);
     144      walker = walker->nextGroup;
     145    }
     146}
     147
     148/**
     149   \brief initializes a new Group object
     150*/
     151bool Object::initGroup(Group* group)
     152{
     153  if (verbose >= 2)
     154    printf("Adding new Group\n");
     155  group->faceMode = -1;
     156  if ((group->listNumber = glGenLists(1)) == 0 )
     157    {
     158      printf ("list could not be created for this Object\n");
     159      return false;
     160    }
     161
     162  if (groupCount == 0)
     163    {
     164      group->firstVertex = 0;
     165      group->firstNormal = 0;
     166      group->firstNormal = 0;
     167    }
     168  else
     169    {
     170      group->firstVertex = currentGroup->firstVertex + currentGroup->vertices->getCount()/3;
     171      group->firstNormal = currentGroup->firstNormal + currentGroup->normals->getCount()/3;
     172      group->firstVertexTexture = currentGroup->firstVertexTexture + currentGroup->vTexture->getCount()/2;
     173    }
     174
     175  group->vertices = new Array();
     176  group->normals = new Array();
     177  group->vTexture = new Array();
     178
     179  glNewList (group->listNumber, GL_COMPILE);
     180}
     181
     182/**
     183   \brief finalizes a Group.
     184*/
     185bool Object::finalizeGroup(Group* group)
     186{
    123187  glEnd();
    124188  glEndList();
    125   delete vertices;
    126   delete normals;
    127   delete vTexture;
    128   if (material != NULL)
    129     delete material;
    130   return true;
    131 }
    132 
    133 /**
    134    \brief Draws the Object
    135    It does this by just calling the List that must have been created earlier.
    136 */
    137 void Object::draw (void)
    138 {
    139   if (verbose >=3)
    140     printf("drawing the 3D-Object\n");
    141   glCallList (listNumber);
    142 }
    143 
     189 
     190  delete group->vertices;
     191  delete group->normals;
     192  delete group->vTexture;
     193}
    144194/**
    145195   \brief Reads in the .obj File and sets all the Values.
     
    202252      else if (!strncmp(Buffer, "g", 1))
    203253        {
    204           printf("ERROR GROUPS NOT IMPLEMENTED YET\n");
     254          readGroup (Buffer+2);
    205255        }
    206256    }
     
    216266bool Object::readVertex (char* vertexString)
    217267{
    218   readVertices = true;
     268  readingVertices = true;
    219269  char subbuffer1[20];
    220270  char subbuffer2[20];
     
    223273  if (verbose >= 3)
    224274    printf ("reading in a vertex: %s %s %s\n", subbuffer1, subbuffer2, subbuffer3);
    225   vertices->addEntry(atof(subbuffer1)*scaleFactor, atof(subbuffer2)*scaleFactor, atof(subbuffer3)*scaleFactor);
     275  currentGroup->vertices->addEntry(atof(subbuffer1)*scaleFactor, atof(subbuffer2)*scaleFactor, atof(subbuffer3)*scaleFactor);
    226276  return true;
    227277}
     
    235285bool Object::readFace (char* faceString)
    236286{
    237   if (readVertices == true)
    238     {
    239       vertices->finalizeArray();
    240       glVertexPointer(3, GL_FLOAT, 0, vertices->getArray());
    241       normals->finalizeArray();
    242       glNormalPointer(GL_FLOAT, 0, normals->getArray());
    243       vTexture->finalizeArray();
    244     }
    245 
    246   readVertices = false;
     287  // finalize the Arrays;
     288  if (readingVertices == true)
     289    {
     290      currentGroup->vertices->finalizeArray();
     291      glVertexPointer(3, GL_FLOAT, 0, currentGroup->vertices->getArray());
     292      currentGroup->normals->finalizeArray();
     293      glNormalPointer(GL_FLOAT, 0, currentGroup->normals->getArray());
     294      currentGroup->vTexture->finalizeArray();
     295    }
     296
     297  readingVertices = false;
    247298  char subbuffer1[20];
    248299  char subbuffer2[20];
     
    252303  if (!strcmp(subbuffer4, ""))
    253304    {
    254       if (faceMode != 3)
    255         {
    256           if (faceMode != -1)
     305      if (currentGroup->faceMode != 3)
     306        {
     307          if (currentGroup->faceMode != -1)
    257308            glEnd();
    258309          glBegin(GL_TRIANGLES);
    259310        }
    260311     
    261       faceMode = 3;
     312      currentGroup->faceMode = 3;
    262313      if (verbose >=3)
    263314        printf ("found triag: %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3);
     
    269320  else
    270321    {
    271       if (faceMode != 4)
    272         {
    273           if (faceMode != -1)
     322      if (currentGroup->faceMode != 4)
     323        {
     324          if (currentGroup->faceMode != -1)
    274325            glEnd();
    275326          glBegin(GL_QUADS);
    276327        }
    277       faceMode = 4;
     328      currentGroup->faceMode = 4;
    278329      if (verbose >=3 )
    279330        printf ("found quad: %s, %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3, subbuffer4);
     
    305356  texture[0] = '\0';
    306357  texture ++;
    307   glTexCoord2fv(vTexture->getArray()+(atoi(texture)-1)*2);
     358  glTexCoord2fv(currentGroup->vTexture->getArray()+(atoi(texture)-1 - currentGroup->firstVertexTexture)*2);
    308359
    309360  char* normal;
     
    313364      normal ++;
    314365      //glArrayElement(atoi(vertex)-1);
    315       glNormal3fv(normals->getArray() +(atoi(normal)-1)*3);
    316     }
    317   glVertex3fv(vertices->getArray() +(atoi(vertex)-1)*3);
     366      glNormal3fv(currentGroup->normals->getArray() +(atoi(normal)-1 - currentGroup->firstNormal)*3);
     367    }
     368  glVertex3fv(currentGroup->vertices->getArray() +(atoi(vertex)-1 - currentGroup->firstVertex)*3);
    318369
    319370}
     
    326377bool Object::readVertexNormal (char* normalString)
    327378{
    328   readVertices = true;
     379  readingVertices = true;
    329380  char subbuffer1[20];
    330381  char subbuffer2[20];
     
    333384  if (verbose >=3 )
    334385    printf("found vertex-Normal %s, %s, %s\n", subbuffer1,subbuffer2,subbuffer3);
    335   normals->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3));
     386  currentGroup->normals->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3));
    336387  return true;
    337388}
     
    344395bool Object::readVertexTexture (char* vTextureString)
    345396{
    346   readVertices = true;
     397  readingVertices = true;
    347398  char subbuffer1[20];
    348399  char subbuffer2[20];
     
    350401  if (verbose >=3 )
    351402    printf("found vertex-Texture %s, %s\n", subbuffer1,subbuffer2);
    352   vTexture->addEntry(atof(subbuffer1));
    353   vTexture->addEntry(atof(subbuffer2));
    354   return true;
     403  currentGroup->vTexture->addEntry(atof(subbuffer1));
     404  currentGroup->vTexture->addEntry(atof(subbuffer2));
     405  return true;
     406}
     407
     408/**
     409   \brief parses a group String
     410   This function initializes a new Group.
     411   With it you should be able to import .obj-files with more than one Objects inside.
     412   \param groupString the new Group to create
     413*/
     414bool Object::readGroup (char* groupString)
     415{
     416  //  printf ("test\n");
     417  if (!strcmp(groupString, "default"))
     418    {
     419      if (groupCount != 0)
     420        {
     421          Group* newGroup = new Group;
     422          finalizeGroup(currentGroup);
     423          currentGroup->nextGroup = newGroup;
     424          initGroup(newGroup);
     425          currentGroup = newGroup; // must be after init see initGroup for more info
     426        }
     427      ++groupCount;
     428    }
     429  else
     430    {
     431      currentGroup->name = groupString;
     432    }
    355433}
    356434
     
    441519    }
    442520     
    443   if (faceMode != -1)
     521  if (currentGroup->faceMode != -1)
    444522    glEnd();
    445   faceMode = 0;
     523  currentGroup->faceMode = 0;
    446524  if (verbose >= 2)
    447525    printf ("using material %s for coming Faces.\n", matString);
Note: See TracChangeset for help on using the changeset viewer.