Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3912 in orxonox.OLD for orxonox


Ignore:
Timestamp:
Apr 21, 2005, 1:19:53 AM (20 years ago)
Author:
bensch
Message:

orxonox/trunk: model rearangement.

File:
1 edited

Legend:

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

    r3911 r3912  
    100100}
    101101
     102//////////
     103// DRAW //
     104//////////
    102105/**
    103106   \brief Draws the Models of all Groups.
     
    180183}
    181184
     185//////////
     186// INIT //
     187//////////
    182188/**
    183189   \brief sets a name to the Model
     
    196202    this->name = NULL;
    197203}
     204
    198205/**
    199206   \brief initializes a new Group model
     
    288295      delete face->next;
    289296    }
    290      
    291 }
    292 
     297}
    293298
    294299/**
     
    305310}
    306311
     312//////////
     313// MESH //
     314//////////
    307315/**
    308316   \brief parses a group String
     
    328336    }
    329337  ++this->groupCount;
    330 
    331338}
    332339
     
    360367  this->vertices->addEntry(x*scaleFactor, y*scaleFactor, z*scaleFactor);
    361368  return true;
     369}
     370
     371/**
     372   \brief parses a vertexNormal-String
     373   \param normalString The String that will be parsed.
     374
     375   If a vertexNormal line is found this function will inject it into the vertexNormal-Array
     376*/
     377bool Model::addVertexNormal (const char* normalString)
     378{
     379  float subbuffer1;
     380  float subbuffer2;
     381  float subbuffer3;
     382  sscanf (normalString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3);
     383  PRINTF(5)("found vertex-Normal %f, %f, %f\n", &subbuffer1,&subbuffer2,&subbuffer3);
     384  this->normals->addEntry(subbuffer1, subbuffer2, subbuffer3);
     385  return true;
     386}
     387
     388/**
     389   \brief adds a VertexNormal.
     390   \param x The x coordinate of the Normal.
     391   \param y The y coordinate of the Normal.
     392   \param z The z coordinate of the Normal.
     393
     394   If a vertexNormal line is found this function will inject it into the vertexNormal-Array
     395*/
     396bool Model::addVertexNormal(float x, float y, float z)
     397{
     398  PRINTF(5)("found vertex-Normal %f, %f, %f\n", x, y, z);
     399  this->normals->addEntry(x, y, z);
     400}
     401
     402/**
     403   \brief parses a vertexTextureCoordinate-String
     404   \param vTextureString The String that will be parsed.
     405
     406   If a vertexTextureCoordinate line is found,
     407   this function will inject it into the vertexTexture-Array
     408*/
     409bool Model::addVertexTexture (const char* vTextureString)
     410{
     411  float subbuffer1;
     412  float subbuffer2;
     413  sscanf (vTextureString, "%f %f", &subbuffer1, &subbuffer2);
     414  PRINTF(5)("found vertex-Texture %f, %f\n", &subbuffer1, &subbuffer2);
     415  this->vTexture->addEntry(subbuffer1);
     416  this->vTexture->addEntry(subbuffer2);
     417  return true;
     418}
     419
     420/**
     421   \brief adds a Texture Coordinate
     422   \param u The u coordinate of the TextureCoordinate.
     423   \param v The y coordinate of the TextureCoordinate.
     424
     425   If a TextureCoordinate line is found this function will inject it into the TextureCoordinate-Array
     426*/
     427bool Model::addVertexTexture(float u, float v)
     428{
     429  PRINTF(5)("found vertex-Texture %f, %f\n", u, v);
     430  this->vTexture->addEntry(u);
     431  this->vTexture->addEntry(v);
    362432}
    363433
     
    461531
    462532/**
    463    \brief parses a vertexNormal-String
    464    \param normalString The String that will be parsed.
    465 
    466    If a vertexNormal line is found this function will inject it into the vertexNormal-Array
    467 */
    468 bool Model::addVertexNormal (const char* normalString)
    469 {
    470   float subbuffer1;
    471   float subbuffer2;
    472   float subbuffer3;
    473   sscanf (normalString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3);
    474   PRINTF(5)("found vertex-Normal %f, %f, %f\n", &subbuffer1,&subbuffer2,&subbuffer3);
    475   this->normals->addEntry(subbuffer1, subbuffer2, subbuffer3);
    476   return true;
    477 }
    478 
    479 /**
    480    \brief adds a VertexNormal.
    481    \param x The x coordinate of the Normal.
    482    \param y The y coordinate of the Normal.
    483    \param z The z coordinate of the Normal.
    484 
    485    If a vertexNormal line is found this function will inject it into the vertexNormal-Array
    486 */
    487 bool Model::addVertexNormal(float x, float y, float z)
    488 {
    489   PRINTF(5)("found vertex-Normal %f, %f, %f\n", x, y, z);
    490   this->normals->addEntry(x, y, z);
    491 }
    492 
    493 /**
    494    \brief parses a vertexTextureCoordinate-String
    495    \param vTextureString The String that will be parsed.
    496 
    497    If a vertexTextureCoordinate line is found,
    498    this function will inject it into the vertexTexture-Array
    499 */
    500 bool Model::addVertexTexture (const char* vTextureString)
    501 {
    502   float subbuffer1;
    503   float subbuffer2;
    504   sscanf (vTextureString, "%f %f", &subbuffer1, &subbuffer2);
    505   PRINTF(5)("found vertex-Texture %f, %f\n", &subbuffer1, &subbuffer2);
    506   this->vTexture->addEntry(subbuffer1);
    507   this->vTexture->addEntry(subbuffer2);
    508   return true;
    509 }
    510 
    511 /**
    512    \brief adds a Texture Coordinate
    513    \param u The u coordinate of the TextureCoordinate.
    514    \param v The y coordinate of the TextureCoordinate.
    515 
    516    If a TextureCoordinate line is found this function will inject it into the TextureCoordinate-Array
    517 */
    518 bool Model::addVertexTexture(float u, float v)
    519 {
    520   PRINTF(5)("found vertex-Texture %f, %f\n", u, v);
    521   this->vTexture->addEntry(u);
    522   this->vTexture->addEntry(v);
    523 }
    524 
    525 /**
    526533   \brief Function that selects a material, if changed in the obj file.
    527534   \param matString the Material that will be set.
     
    555562}
    556563
     564/**
     565   \brief A routine that is able to create normals.
     566
     567   The algorithm does the following:
     568   1. It calculates creates Vectors for each normale, and sets them to zero.
     569   2. It then Walks through a) all the Groups b) all the Faces c) all the FaceElements
     570   3. It searches for a points two neighbours per Face, takes Vecotrs to them calculates FaceNormals and adds it to the Points Normal.
     571   4. It goes through all the normale-Points and calculates the VertexNormale and includes it in the normals-Array.
     572*/
     573bool Model::buildVertexNormals ()
     574{
     575 
     576  PRINTF(4)("Normals are being calculated.\n");
     577
     578  Vector* normArray = new Vector [vertices->getCount()/3];
     579  for (int i=0; i<vertices->getCount()/3;i++)
     580    normArray[i] = Vector(.0,.0,.0);
     581 
     582  int firstTouch;
     583  int secondTouch;
     584  Vector prevV;
     585  Vector nextV;
     586  Vector curV;
     587
     588  Group* tmpGroup = firstGroup;
     589  while (tmpGroup)
     590    {
     591      Face* tmpFace = tmpGroup->firstFace;
     592      while (tmpFace)
     593        {
     594          if (tmpFace->firstElem)
     595            {
     596              FaceElement* firstElem = tmpFace->firstElem;
     597              FaceElement* prevElem;
     598              FaceElement* curElem = firstElem;
     599              FaceElement* nextElem;
     600              FaceElement* lastElem;
     601              // 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.
     602              while (curElem)
     603                {
     604                  prevElem = curElem;
     605                  curElem = curElem->next;
     606                }
     607              lastElem = prevElem;
     608             
     609              curElem = firstElem;
     610              for (int j=0; j<tmpFace->vertexCount; j++)
     611                {
     612                  if (!(nextElem = curElem->next))
     613                    nextElem = firstElem;
     614                  curElem->normalNumber = curElem->vertexNumber;
     615                 
     616                  curV = Vector (vertices->getArray()[curElem->vertexNumber*3], vertices->getArray()[curElem->vertexNumber*3+1], vertices->getArray()[curElem->vertexNumber*3+2]);
     617                  prevV = Vector (vertices->getArray()[prevElem->vertexNumber*3], vertices->getArray()[prevElem->vertexNumber*3+1], vertices->getArray()[prevElem->vertexNumber*3+2]) - curV;
     618                  nextV = Vector (vertices->getArray()[nextElem->vertexNumber*3], vertices->getArray()[nextElem->vertexNumber*3+1], vertices->getArray()[nextElem->vertexNumber*3+2]) - curV;
     619                  normArray[curElem->vertexNumber] = normArray[curElem->vertexNumber] + nextV.cross(prevV);
     620
     621                  prevElem = curElem;
     622                  curElem = curElem->next;
     623                }
     624            }
     625          tmpFace = tmpFace->next;
     626        }
     627      tmpGroup = tmpGroup->next;
     628    }
     629
     630  for (int i=0; i<vertices->getCount()/3;i++)
     631    {
     632      normArray[i].normalize();
     633      PRINTF(5)("Found Normale number %d: (%f; %f, %f).\n", i, normArray[i].x, normArray[i].y, normArray[i].z);
     634     
     635      this->normals->addEntry(normArray[i].x, normArray[i].y, normArray[i].z);
     636
     637    }
     638  delete []normArray;
     639}
     640
     641////////////
     642// openGL //
     643////////////
    557644/**
    558645   \brief reads and includes the Faces/Materials into the openGL state Machine
     
    672759
    673760/**
    674    \brief A routine that is able to create normals.
    675 
    676    The algorithm does the following:
    677    1. It calculates creates Vectors for each normale, and sets them to zero.
    678    2. It then Walks through a) all the Groups b) all the Faces c) all the FaceElements
    679    3. It searches for a points two neighbours per Face, takes Vecotrs to them calculates FaceNormals and adds it to the Points Normal.
    680    4. It goes through all the normale-Points and calculates the VertexNormale and includes it in the normals-Array.
    681 */
    682 bool Model::buildVertexNormals ()
    683 {
    684  
    685   PRINTF(4)("Normals are being calculated.\n");
    686 
    687   Vector* normArray = new Vector [vertices->getCount()/3];
    688   for (int i=0; i<vertices->getCount()/3;i++)
    689     normArray[i] = Vector(.0,.0,.0);
    690  
    691   int firstTouch;
    692   int secondTouch;
    693   Vector prevV;
    694   Vector nextV;
    695   Vector curV;
    696 
    697   Group* tmpGroup = firstGroup;
    698   while (tmpGroup)
    699     {
    700       Face* tmpFace = tmpGroup->firstFace;
    701       while (tmpFace)
    702         {
    703           if (tmpFace->firstElem)
    704             {
    705               FaceElement* firstElem = tmpFace->firstElem;
    706               FaceElement* prevElem;
    707               FaceElement* curElem = firstElem;
    708               FaceElement* nextElem;
    709               FaceElement* lastElem;
    710               // 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.
    711               while (curElem)
    712                 {
    713                   prevElem = curElem;
    714                   curElem = curElem->next;
    715                 }
    716               lastElem = prevElem;
    717              
    718               curElem = firstElem;
    719               for (int j=0; j<tmpFace->vertexCount; j++)
    720                 {
    721                   if (!(nextElem = curElem->next))
    722                     nextElem = firstElem;
    723                   curElem->normalNumber = curElem->vertexNumber;
    724                  
    725                   curV = Vector (vertices->getArray()[curElem->vertexNumber*3], vertices->getArray()[curElem->vertexNumber*3+1], vertices->getArray()[curElem->vertexNumber*3+2]);
    726                   prevV = Vector (vertices->getArray()[prevElem->vertexNumber*3], vertices->getArray()[prevElem->vertexNumber*3+1], vertices->getArray()[prevElem->vertexNumber*3+2]) - curV;
    727                   nextV = Vector (vertices->getArray()[nextElem->vertexNumber*3], vertices->getArray()[nextElem->vertexNumber*3+1], vertices->getArray()[nextElem->vertexNumber*3+2]) - curV;
    728                   normArray[curElem->vertexNumber] = normArray[curElem->vertexNumber] + nextV.cross(prevV);
    729 
    730                   prevElem = curElem;
    731                   curElem = curElem->next;
    732                 }
    733             }
    734           tmpFace = tmpFace->next;
    735         }
    736       tmpGroup = tmpGroup->next;
    737     }
    738 
    739   for (int i=0; i<vertices->getCount()/3;i++)
    740     {
    741       normArray[i].normalize();
    742       PRINTF(5)("Found Normale number %d: (%f; %f, %f).\n", i, normArray[i].x, normArray[i].y, normArray[i].z);
    743      
    744       this->normals->addEntry(normArray[i].x, normArray[i].y, normArray[i].z);
    745 
    746     }
    747   delete []normArray;
    748  
    749 }
    750 
    751 
    752 /**
    753761   \brief Includes a default model
    754762
Note: See TracChangeset for help on using the changeset viewer.