Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


Ignore:
Timestamp:
Dec 3, 2004, 1:59:29 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk/importer: Changed importToGL-process. now the import process startts after the end of reading the File.
Group-specific arrays have been reverted to obj-file-specific.
other minor bug-fixes.

THIS VERSION IS NOT COMPLETED. will do this in the next few releases

File:
1 edited

Legend:

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

    r2995 r3063  
    7070      glDeleteLists (walker->listNumber, 1);
    7171      Group* lastWalker = walker;
    72       walker = walker->nextGroup;
     72      walker = walker->next;
    7373      delete lastWalker;
    7474    }
     
    9494  material = new Material();
    9595
    96   //  glEnableClientState (GL_VERTEX_ARRAY);
    97   //  glEnableClientState (GL_NORMAL_ARRAY);
    98   //  glEnableClientState (GL_TEXTURE_COORD_ARRAY);
    99 
     96  vertices = new Array();
     97  vTexture = new Array();
     98  normals = new Array();
    10099
    101100  return true;
     
    117116/**
    118117  \brief finalizes an Object.
    119    This funcion is needed, to close the glList and all the other lists.
     118   This funcion is needed, to close the glList and all the other lists. This will be applied at the end of the importing Process.
    120119*/
    121120bool Object::finalize(void)
     
    124123    printf("finalizing the 3D-Object\n");
    125124  finalizeGroup (currentGroup);
     125
     126  importToGL ();
     127
     128  if (vertices != NULL)
     129    delete vertices;
     130  if (vTexture != NULL)
     131    delete vTexture;
     132  if (normals != NULL)
     133    delete normals;
     134
    126135  if (material != NULL)
    127136    delete material;
     
    133142   It does this by just calling the Lists that must have been created earlier.
    134143*/
    135 void Object::draw (void)
     144void Object::draw (void) const
    136145{
    137146  if (verbose >=2)
     
    143152        printf ("Drawing object %s\n", walker->name);
    144153      glCallList (walker->listNumber);
    145       walker = walker->nextGroup;
     154      walker = walker->next;
    146155    }
    147156}
     
    152161   \param groupNumber The number of the group that will be displayed.
    153162*/
    154 void Object::draw (int groupNumber)
     163void Object::draw (int groupNumber) const
    155164{
    156165  if (groupNumber >= groupCount)
    157166    {
    158       if (verbose>=2)
     167      if (verbose>=1)
    159168        printf ("You requested object number %i, but this File only contains of %i Objects.\n", groupNumber-1, groupCount);
    160169      return;
     
    169178        {
    170179          if (verbose >= 2)
    171             printf ("Drawing object number %s named %s\n", counter, walker->name);
     180            printf ("Drawing object number %i named %s\n", counter, walker->name);
    172181          glCallList (walker->listNumber);
    173182          return;
    174183        }
    175184      ++counter;
    176       walker = walker->nextGroup;
    177     }
    178   if (verbose >= 2)
     185      walker = walker->next;
     186    }
     187  if (verbose >= 1)
    179188    printf("Object number %i in %s not Found.\n", groupNumber, objFileName);
    180189  return;
     
    183192
    184193/**
    185    \brief Draws the Object with a specific groupname
     194   \brief Draws the Object with a specific groupName
    186195   It does this by just calling the List that must have been created earlier.
    187196   \param groupName The name of the group that will be displayed.
    188197*/
    189 void Object::draw (char* groupName)
     198void Object::draw (char* groupName) const
    190199{
    191200  if (verbose >=2)
     
    201210          return;
    202211        }
    203       walker = walker->nextGroup;
     212      walker = walker->next;
    204213    }
    205214  if (verbose >= 2)
     
    211220   \returns Count of the Objects in this File
    212221*/
    213 int Object::getGroupCount (void)
     222int Object::getGroupCount (void) const
    214223{
    215224  return groupCount;
     
    226235  group->faceMode = -1;
    227236  group->faceCount = 0; 
    228   group->nextGroup = NULL;
    229   if ((group->listNumber = glGenLists(1)) == 0 )
    230     {
    231       printf ("list could not be created for this Object\n");
    232       return false;
    233     }
    234  
    235   if (groupCount == 0)
    236     {
    237       group->firstVertex = 0;
    238       group->firstNormal = 0;
    239       group->firstVertexTexture = 0;
    240     }
    241   else
    242     {
    243       group->firstVertex = currentGroup->firstVertex + currentGroup->vertices->getCount()/3;
    244       group->firstNormal = currentGroup->firstNormal + currentGroup->normals->getCount()/3;
    245       group->firstVertexTexture = currentGroup->firstVertexTexture + currentGroup->vTexture->getCount()/2;
    246     }
    247   if (verbose >=2)
    248     printf ("Creating new Arrays, with starting points v:%i, vt:%i, vn:%i .\n", group->firstVertex, group->firstVertexTexture, group->firstNormal);
    249   group->vertices = new Array();
    250   group->normals = new Array();
    251   group->vTexture = new Array();
    252 
    253   glNewList (group->listNumber, GL_COMPILE);
     237  group->next = NULL;
     238
     239  group->firstFace = new Face;
     240  group->currentFace = group->firstFace;
    254241}
    255242
     
    258245   \param group the group to finalize.
    259246*/
    260 bool Object::finalizeGroup(Group* group)
     247bool Object::finalizeGroup(Group* group) // think it is not needed anymore
    261248{
    262249  if (verbose >=2)
    263250    printf ("Finalize group %s.\n", group->name);
    264   glEnd();
    265   glEndList();
    266 }
    267 
    268 /**
    269    \brief deletes the Arrays of the Group to save space.
    270    \param group the group to delete the arrays from.
    271 */
    272 bool Object::cleanupGroup(Group* group)
    273 {
    274   if (verbose >=2)
    275     printf ("cleaning up group %s.\n", group->name);
    276  
    277   delete group->vertices;
    278   delete group->normals;
    279   delete group->vTexture;
     251
    280252}
    281253
     
    316288        }
    317289     
    318       else if (!strncmp(Buffer, "mtllib", 6))
     290      else if (!strncmp(Buffer, "mtllib ", 7))
    319291        {
    320292          readMtlLib (Buffer+7);
    321293        }
    322294
    323       else if (!strncmp(Buffer, "usemtl", 6))
     295      else if (!strncmp(Buffer, "usemtl ", 7))
    324296        {
    325297          readUseMtl (Buffer+7);
     
    327299
    328300      // case VertexNormal
    329       else if (!strncmp(Buffer, "vn ", 2))
     301      else if (!strncmp(Buffer, "vn ", 3))
    330302      {
    331303        readVertexNormal(Buffer+3);
     
    333305
    334306      // case VertexTextureCoordinate
    335       else if (!strncmp(Buffer, "vt ", 2))
     307      else if (!strncmp(Buffer, "vt ", 3))
    336308      {
    337309        readVertexTexture(Buffer+3);
    338310      }
    339311      // case group
    340       else if (!strncmp(Buffer, "g", 1))
     312      else if (!strncmp(Buffer, "g ", 2))
    341313        {
    342314          readGroup (Buffer+2);
    343315        }
    344       else if (!strncmp(Buffer, "s", 1))
     316      else if (!strncmp(Buffer, "s ", 2))
    345317        {
    346318          if (verbose >= 1)
    347             printf("smoothing groups not suportet yet. line: %s\n", Buffer);
     319            printf("smoothing groups not supportet yet. line: %s\n", Buffer);
    348320        }
    349321    }
     
    360332bool Object::readVertex (char* vertexString)
    361333{
    362   readingVertices = true;
    363334  char subbuffer1[20];
    364335  char subbuffer2[20];
     
    367338  if (verbose >= 3)
    368339    printf ("reading in a vertex: %s %s %s\n", subbuffer1, subbuffer2, subbuffer3);
    369   currentGroup->vertices->addEntry(atof(subbuffer1)*scaleFactor, atof(subbuffer2)*scaleFactor, atof(subbuffer3)*scaleFactor);
     340  vertices->addEntry(atof(subbuffer1)*scaleFactor, atof(subbuffer2)*scaleFactor, atof(subbuffer3)*scaleFactor);
    370341  return true;
    371342}
     
    379350bool Object::readFace (char* faceString)
    380351{
    381   // finalize the Arrays;
    382   if (readingVertices == true)
    383     {
    384       currentGroup->vertices->finalizeArray();
    385       //      glVertexPointer(3, GL_FLOAT, 0, currentGroup->vertices->getArray());
    386       currentGroup->normals->finalizeArray();
    387       //      glNormalPointer(GL_FLOAT, 0, currentGroup->normals->getArray());
    388       currentGroup->vTexture->finalizeArray();
    389     }
    390 
    391   readingVertices = false;
    392   currentGroup->faceCount++;
    393 
    394   int elemCount = 0;
    395  
    396   FaceElement* firstElem = new FaceElement;
    397   FaceElement* tmpElem = firstElem;
    398 
    399  
     352  if (currentGroup->faceCount >0)
     353    currentGroup->currentFace = currentGroup->currentFace->next = new Face;
     354  initFace (currentGroup->currentFace);
     355
     356  FaceElement* tmpElem = currentGroup->currentFace->firstElem = new FaceElement;
     357
    400358  while(strcmp (faceString, "\0"))
    401359    {
    402       if (elemCount>0)
     360      if (currentGroup->currentFace->vertexCount>0)
    403361          tmpElem = tmpElem->next = new FaceElement;
    404362      tmpElem->next = NULL;
    405363
    406 
    407       sscanf (faceString, "%s", tmpElem->value);
     364      char* tmpValue = new char [50];
     365      sscanf (faceString, "%s", tmpValue);
     366      tmpElem->value = new char [strlen(tmpValue)];
     367      strcpy (tmpElem->value, tmpValue);
     368
     369      delete tmpValue; ////// DANGEROUS could be wrong to do this (useless) /////
    408370      faceString += strlen(tmpElem->value);
    409371      if (strcmp (faceString, "\0"))
    410372        faceString++;
    411       elemCount++;
    412 
    413 
    414     }
    415  
    416  
    417   if (elemCount == 3)
    418     {
    419       if (currentGroup->faceMode != 3)
    420         {
    421           if (currentGroup->faceMode != -1)
    422             glEnd();
    423           glBegin(GL_TRIANGLES);
    424         }
    425      
    426       currentGroup->faceMode = 3;
    427       if (verbose >=3)
    428         printf ("found triag.\n");
    429     }
    430  
    431   else if (elemCount == 4)
    432     {
    433       if (currentGroup->faceMode != 4)
    434         {
    435           if (currentGroup->faceMode != -1)
    436             glEnd();
    437           glBegin(GL_QUADS);
    438         }
    439       currentGroup->faceMode = 4;
    440       if (verbose >=3 )
    441         printf ("found quad.\n");
    442     }
    443  
    444   else if (elemCount > 4)
    445     {
    446       if (currentGroup->faceMode != -1)
    447         glEnd();
    448       glBegin(GL_POLYGON);
    449       if (verbose >=3)
    450         printf ("Polygon with %i faces found.", elemCount);
    451       currentGroup->faceMode = elemCount;
    452     }
    453 
    454   tmpElem = firstElem;
    455   FaceElement* delElem;
    456   while (tmpElem != NULL)
    457     {
    458       //      printf ("%s\n", tmpElem->value);
    459       addGLElement(tmpElem->value);
    460       delElem = tmpElem;
    461       tmpElem = tmpElem->next;
    462       delete delElem;
    463     }
    464 
     373      currentGroup->currentFace->vertexCount++;
     374    }
     375
     376  currentGroup->faceCount++;
    465377}
    466378
     
    485397      texture[0] = '\0';
    486398      texture ++;
    487       if (verbose>=3)
    488         printf ("includeing texture #%i, and mapping it to group texture #%i, textureArray has %i entries.\n", atoi(texture), (atoi(texture)-1 - currentGroup->firstVertexTexture)*2, currentGroup->vTexture->getCount());
    489       glTexCoord2fv(currentGroup->vTexture->getArray()+(atoi(texture)-1 - currentGroup->firstVertexTexture)*2);
     399      glTexCoord2fv(vTexture->getArray()+(atoi(texture)-1)*2);
    490400
    491401      char* normal;
     
    495405          normal ++;
    496406          //glArrayElement(atoi(vertex)-1);
    497           glNormal3fv(currentGroup->normals->getArray() +(atoi(normal)-1 - currentGroup->firstNormal)*3);
    498         }
    499     }
    500   if (verbose>=3)
    501     printf ("includeing vertex #%i, and mapping it to group vertex #%i, vertexArray has %i entries.\n", atoi(vertex), (atoi(vertex)-1 - currentGroup->firstVertex)*3, currentGroup->vertices->getCount());
    502   glVertex3fv(currentGroup->vertices->getArray() +(atoi(vertex)-1 - currentGroup->firstVertex)*3);
     407          glNormal3fv(normals->getArray() +(atoi(normal)-1)*3);
     408        }
     409    }
     410  glVertex3fv(vertices->getArray() +(atoi(vertex)-1)*3);
    503411
    504412}
     
    511419bool Object::readVertexNormal (char* normalString)
    512420{
    513   readingVertices = true;
    514421  char subbuffer1[20];
    515422  char subbuffer2[20];
     
    518425  if (verbose >=3 )
    519426    printf("found vertex-Normal %s, %s, %s\n", subbuffer1,subbuffer2,subbuffer3);
    520   currentGroup->normals->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3));
     427  normals->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3));
    521428  return true;
    522429}
     
    529436bool Object::readVertexTexture (char* vTextureString)
    530437{
    531   readingVertices = true;
    532438  char subbuffer1[20];
    533439  char subbuffer2[20];
     
    535441  if (verbose >=3 )
    536442    printf("found vertex-Texture %s, %s\n", subbuffer1,subbuffer2);
    537   currentGroup->vTexture->addEntry(atof(subbuffer1));
    538   currentGroup->vTexture->addEntry(atof(subbuffer2));
     443  vTexture->addEntry(atof(subbuffer1));
     444  vTexture->addEntry(atof(subbuffer2));
    539445  return true;
    540446}
     
    558464      Group* newGroup = new Group;
    559465      finalizeGroup(currentGroup);
    560       currentGroup->nextGroup = newGroup;
     466      currentGroup->next = newGroup;
    561467      initGroup(newGroup);
    562       cleanupGroup(currentGroup); // deletes the arrays of the group; must be after initGroup.
    563468      currentGroup = newGroup; // must be after init see initGroup for more info
    564469    }
     
    566471  ++groupCount;
    567472
     473}
     474
     475
     476bool Object::initFace (Face* face)
     477{
     478  face->vertexCount = 0;
     479
     480  face->firstElem = NULL;
     481 
     482  face->material = NULL;
     483 
     484  face->next = NULL;
     485
     486  return true;
     487}
     488
     489bool Object::importToGL (void)
     490{
     491
     492  // finalize the Arrays
     493  vertices->finalizeArray();
     494  vTexture->finalizeArray();
     495  normals->finalizeArray();
     496
     497  currentGroup = firstGroup;
     498
     499  while (currentGroup != NULL)
     500    {
     501
     502      // creating a glList for the Group
     503      if ((currentGroup->listNumber = glGenLists(1)) == 0)
     504        {
     505          printf ("list could not be created for this Object\n");
     506          return false;
     507        }
     508      glNewList (currentGroup->listNumber, GL_COMPILE);
     509
     510      // Putting Faces to GL
     511      Face* tmpFace = currentGroup->firstFace;
     512      while (tmpFace != NULL)
     513        {
     514          if (tmpFace->vertexCount == 3)
     515            {
     516              if (currentGroup->faceMode != 3)
     517                {
     518                  if (currentGroup->faceMode != -1)
     519                    glEnd();
     520                  glBegin(GL_TRIANGLES);
     521                }
     522             
     523              currentGroup->faceMode = 3;
     524              if (verbose >=3)
     525                printf ("found triag.\n");
     526            }
     527         
     528          else if (tmpFace->vertexCount == 4)
     529            {
     530              if (currentGroup->faceMode != 4)
     531                {
     532                  if (currentGroup->faceMode != -1)
     533                    glEnd();
     534                  glBegin(GL_QUADS);
     535                }
     536              currentGroup->faceMode = 4;
     537              if (verbose >=3 )
     538                printf ("found quad.\n");
     539            }
     540         
     541          else if (tmpFace->vertexCount > 4)
     542            {
     543              if (currentGroup->faceMode != -1)
     544                glEnd();
     545              glBegin(GL_POLYGON);
     546              if (verbose >=3)
     547                printf ("Polygon with %i faces found.", tmpFace->vertexCount);
     548              currentGroup->faceMode = tmpFace->vertexCount;
     549            }
     550         
     551          FaceElement* tmpElem = tmpFace->firstElem;
     552          FaceElement* delElem;
     553          while (tmpElem != NULL)
     554            {
     555              //      printf ("%s\n", tmpElem->value);
     556              addGLElement(tmpElem->value);
     557              delElem = tmpElem;
     558              tmpElem = tmpElem->next;
     559              delete delElem;
     560            }
     561          tmpFace = tmpFace->next;
     562        }
     563      glEnd();
     564      glEndList();
     565      currentGroup = currentGroup->next;
     566    }
    568567}
    569568
     
    595594
    596595      // create new Material
    597       if (!strncmp(Buffer, "newmtl ", 2))
     596      if (!strncmp(Buffer, "newmtl ", 7))
    598597        {
    599598          tmpMat = tmpMat->addMaterial(Buffer+7);
     
    601600        }
    602601      // setting a illumMode
    603       else if (!strncmp(Buffer, "illum", 5))
     602      else if (!strncmp(Buffer, "illum ", 6))
    604603        {
    605604          tmpMat->setIllum(Buffer+6);
     
    607606        }
    608607      // setting Diffuse Color
    609       else if (!strncmp(Buffer, "Kd", 2))
     608      else if (!strncmp(Buffer, "Kd ", 3))
    610609        {
    611610          tmpMat->setDiffuse(Buffer+3);
    612611        }
    613612      // setting Ambient Color
    614       else if (!strncmp(Buffer, "Ka", 2))
     613      else if (!strncmp(Buffer, "Ka ", 3))
    615614        {
    616615          tmpMat->setAmbient(Buffer+3);
    617616        }
    618617      // setting Specular Color
    619       else if (!strncmp(Buffer, "Ks", 2))
     618      else if (!strncmp(Buffer, "Ks ", 3))
    620619        {
    621620          tmpMat->setSpecular(Buffer+3);
    622621        }
    623622      // setting The Specular Shininess
    624       else if (!strncmp(Buffer, "Ns", 2))
     623      else if (!strncmp(Buffer, "Ns ", 3))
    625624        {
    626625          tmpMat->setShininess(Buffer+3);
    627626        }
    628627      // setting up transparency
    629       else if (!strncmp(Buffer, "d", 1))
     628      else if (!strncmp(Buffer, "d ", 2))
    630629        {
    631630          tmpMat->setTransparency(Buffer+2);
    632631        }
    633       else if (!strncpy(Buffer, "Tf", 2))
     632      else if (!strncpy(Buffer, "Tf ", 3))
    634633        {
    635634          tmpMat->setTransparency(Buffer+3);
Note: See TracChangeset for help on using the changeset viewer.