Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 25, 2005, 9:29:41 AM (20 years ago)
Author:
patrick
Message:

orxonox/branches/physics: merged with trunk - with command svn merge -r 3866:HEAD

File:
1 edited

Legend:

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

    r3801 r3953  
    2323#include "array.h"
    2424#include "vector.h"
     25#include "list.h"
    2526
    2627using namespace std;
    2728
    28 /**
    29    \brief Creates a 3D-Model.
    30 
    31    This only initializes a 3D-Model, but does not cleanup the Faces.
    32 */
    33 Model::Model(void)
    34 {
    35   this->initialize();
    36 }
    37 
     29//////////////////
     30// DE-CONSTRUCT //
     31//////////////////
    3832/**
    3933   \brief Creates a 3D-Model. and assigns it a Name.
    4034*/
    41 Model::Model(char* modelName)
    42 {
    43   this->initialize();
     35Model::Model(const char* modelName, MODEL_TYPE type)
     36{
     37  PRINTF(4)("new 3D-Model is being created\n");
     38  this->name = NULL;
    4439  this->setName(modelName);
    45 }
    46 
    47 /**
    48    \brief deletes an Model.
    49 
    50    Looks if any from model allocated space is still in use, and if so deleted it.
    51 */
    52 Model::~Model(void)
    53 {
    54   PRINTF(4)("Deleting Model ");
    55   if (this->name)
    56     {
    57       PRINT(4)("%s\n", this->name);
    58       delete []this->name;
    59     }
    60   else
    61       PRINT(4)("\n");
    62 
    63   PRINTF(4)("Deleting display Lists.\n");
    64   Group* walker = this->firstGroup;
    65   while (walker != NULL)
    66     {
    67       glDeleteLists (walker->listNumber, 1);
    68       Group* delWalker = walker;
    69       walker = walker->next;
    70       delete delWalker;
    71     }
    72 
    73   PRINTF(4)("Deleting Materials.\n");
    74   if (this->material)
    75     delete this->material;
    76 }
    77 
    78 /**
    79    \brief Finalizes an Object. This can be done outside of the Class.
    80 */
    81 void Model::finalize(void)
    82 {
    83   this->importToGL ();
    84  
    85   this->cleanup();
    86 
    87   this->finalized = true;
    88 }
    89 
    90 /**
    91    \brief Draws the Models of all Groups.
    92    It does this by just calling the Lists that must have been created earlier.
    93 */
    94 void Model::draw (void) const
    95 {
    96   PRINTF(4)("drawing the 3D-Models\n");
    97   Group* walker = this->firstGroup;
    98   while (walker != NULL)
    99     {
    100       PRINTF(5)("Drawing model %s\n", walker->name);
    101       glCallList (walker->listNumber);
    102       walker = walker->next;
    103     }
    104 }
    105 
    106 /**
    107    \brief Draws the Model number groupNumber
    108    \param groupNumber The number of the group that will be displayed.
    109 
    110    It does this by just calling the List that must have been created earlier.
    111 */
    112 void Model::draw (int groupNumber) const
    113 {
    114   if (groupNumber >= this->groupCount)
    115     {
    116       PRINTF(2)("You requested model number %i, but this File only contains of %i Models.\n", groupNumber-1, this->groupCount);
    117       return;
    118     }
    119   PRINTF(4)("drawing the requested 3D-Models if found.\n");
    120   Group* walker = this->firstGroup;
    121   int counter = 0;
    122   while (walker != NULL)
    123     {
    124       if (counter == groupNumber)
    125         {
    126           PRINTF(2)("Drawing model number %i named %s\n", counter, walker->name);
    127           glCallList (walker->listNumber);
    128           return;
    129         }
    130       ++counter;
    131       walker = walker->next;
    132     }
    133   PRINTF(2)("Model number %i in %s not Found.\n", groupNumber, this->name);
    134   return;
    135 
    136 }
    137 
    138 /**
    139    \brief Draws the Model with a specific groupName
    140    \param groupName The name of the group that will be displayed.
    141 
    142    It does this by just calling the List that must have been created earlier.
    143 */
    144 void Model::draw (char* groupName) const
    145 {
    146   PRINTF(4)("drawing the requested 3D-Models if found.\n");
    147   Group* walker = this->firstGroup;
    148   while (walker != NULL)
    149     {
    150       if (!strcmp(walker->name, groupName))
    151         {
    152           PRINTF(4)("Drawing model %s\n", walker->name);
    153           glCallList (walker->listNumber);
    154           return;
    155         }
    156       walker = walker->next;
    157     }
    158   PRINTF(2)("Model Named %s in %s not Found.\n", groupName, this->name);
    159   return;
    160 }
    161 
    162 /**
    163    \returns Count of the Models in this File
    164 */
    165 int Model::getGroupCount (void) const
    166 {
    167   return this->groupCount;
    168 }
    169 
    170 /**
    171     \brief initializes the Model.
    172 
    173     This Function initializes all the needed arrays, Lists and clientStates.
    174     It also defines default values.
    175 */
    176 bool Model::initialize (void)
    177 {
    178   PRINTF(4)("new 3D-Model is being created\n");
    179 
    180   this->name = NULL;
     40  this->type = type;
     41
    18142  this->finalized = false;
    18243  // setting the start group;
     
    18748  this->initGroup (this->currentGroup);
    18849  this->scaleFactor = 1;
    189   this->material = new Material();
    19050
    19151  this->vertices = new Array();
     
    19353  this->normals = new Array();
    19454
    195   return true;
    196 }
    197 
     55  this->materialList = new tList<Material>;
     56
     57  if (this->type == MODEL_VERTEX_ARRAY)
     58    glEnableClientState(GL_VERTEX_ARRAY | GL_NORMAL_ARRAY | GL_TEXTURE_COORD_ARRAY);
     59}
     60
     61/**
     62   \brief deletes an Model.
     63
     64   Looks if any from model allocated space is still in use, and if so deleted it.
     65*/
     66Model::~Model(void)
     67{
     68  PRINTF(4)("Deleting Model ");
     69  if (this->name)
     70    {
     71      PRINT(4)("%s\n", this->name);
     72      delete []this->name;
     73    }
     74  else
     75      PRINT(4)("\n");
     76
     77  PRINTF(5)("Deleting display Lists.\n");
     78  Group* walker = this->firstGroup;
     79  while (walker != NULL)
     80    {
     81      glDeleteLists (walker->listNumber, 1);
     82      Group* delWalker = walker;
     83      walker = walker->next;
     84      delete delWalker;
     85    }
     86
     87  // deleting Arrays
     88  this->deleteArrays();
     89
     90  // deleting the MaterialList
     91  PRINTF(5)("Deleting Materials.\n");
     92  tIterator<Material>* tmpIt = this->materialList->getIterator();
     93  Material* material = tmpIt->nextElement();
     94  while(material)
     95    {
     96      delete material;
     97      material = tmpIt->nextElement();
     98    }
     99  delete tmpIt;
     100  delete materialList;
     101}
     102
     103/**
     104   \brief Finalizes an Object. This can be done outside of the Class.
     105*/
     106void Model::finalize(void)
     107{
     108  // this creates the display List.
     109  this->importToDisplayList();
     110 
     111
     112  // deletes everything we allocated.
     113  if (this->type == MODEL_DISPLAY_LIST)
     114    this->deleteArrays();
     115  this->cleanup();
     116
     117  this->finalized = true;
     118}
     119
     120//////////
     121// DRAW //
     122//////////
     123/**
     124   \brief Draws the Models of all Groups.
     125   It does this by just calling the Lists that must have been created earlier.
     126*/
     127void Model::draw (void) const
     128{
     129  PRINTF(4)("drawing the 3D-Models\n");
     130  Group* walker = this->firstGroup;
     131  while (walker != NULL)
     132    {
     133      PRINTF(5)("Drawing model %s\n", walker->name);
     134      glCallList (walker->listNumber);
     135      walker = walker->next;
     136    }
     137}
     138
     139/**
     140   \brief Draws the Model number groupNumber
     141   \param groupNumber The number of the group that will be displayed.
     142
     143   It does this by just calling the List that must have been created earlier.
     144*/
     145void Model::draw (int groupNumber) const
     146{
     147  if (groupNumber >= this->groupCount)
     148    {
     149      PRINTF(2)("You requested model number %i, but this File only contains of %i Models.\n", groupNumber-1, this->groupCount);
     150      return;
     151    }
     152  PRINTF(4)("drawing the requested 3D-Models if found.\n");
     153  Group* walker = this->firstGroup;
     154  int counter = 0;
     155  while (walker != NULL)
     156    {
     157      if (counter == groupNumber)
     158        {
     159          PRINTF(4)("Drawing model number %i named %s\n", counter, walker->name);
     160          glCallList (walker->listNumber);
     161          return;
     162        }
     163      ++counter;
     164      walker = walker->next;
     165    }
     166  PRINTF(2)("Model number %i in %s not Found.\n", groupNumber, this->name);
     167  return;
     168
     169}
     170
     171/**
     172   \brief Draws the Model with a specific groupName
     173   \param groupName The name of the group that will be displayed.
     174
     175   It does this by just calling the List that must have been created earlier.
     176*/
     177void Model::draw (char* groupName) const
     178{
     179  PRINTF(4)("drawing the requested 3D-Models if found.\n");
     180  Group* walker = this->firstGroup;
     181  while (walker != NULL)
     182    {
     183      if (!strcmp(walker->name, groupName))
     184        {
     185          PRINTF(4)("Drawing model %s\n", walker->name);
     186          glCallList (walker->listNumber);
     187          return;
     188        }
     189      walker = walker->next;
     190    }
     191  PRINTF(2)("Model Named %s in %s not Found.\n", groupName, this->name);
     192  return;
     193}
     194
     195/**
     196   \returns Count of the Models in this File
     197*/
     198int Model::getGroupCount (void) const
     199{
     200  return this->groupCount;
     201}
     202
     203//////////
     204// INIT //
     205//////////
    198206/**
    199207   \brief sets a name to the Model
     
    204212  if (this->name)
    205213    delete this->name;
    206   this->name = new char[strlen(name)+1];
    207   strcpy(this->name, name);
    208 }
     214  if (name)
     215    {
     216      this->name = new char[strlen(name)+1];
     217      strcpy(this->name, name);
     218    }
     219  else
     220    this->name = NULL;
     221}
     222
    209223/**
    210224   \brief initializes a new Group model
     
    244258
    245259/**
    246    \brief finalizes an Model.
    247    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.
    248 */
    249 bool Model::cleanup(void)
    250 {
    251   PRINTF(4)("cleaning up the 3D-Model to save Memory.\n");
    252 
     260   \brief deletes all the arrays
     261*/
     262bool Model::deleteArrays(void)
     263{
    253264  if (this->vertices)
    254265    delete this->vertices;
     
    257268  if (this->normals)
    258269    delete this->normals;
    259 
     270  this->vertices = NULL;
     271  this->vTexture = NULL;
     272  this->normals = NULL;
     273}
     274
     275/**
     276   \brief finalizes an Model.
     277   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.
     278*/
     279bool Model::cleanup(void)
     280{
     281  PRINTF(4)("cleaning up the 3D-Model to save Memory.\n");
    260282  this->cleanupGroup(this->firstGroup);
    261283  return true;
     
    299321      delete face->next;
    300322    }
    301      
    302 }
    303 
     323}
    304324
    305325/**
     
    316336}
    317337
     338//////////
     339// MESH //
     340//////////
     341/**
     342   \brief adds a new Material to the Material List
     343   \param material the Material to add
     344   \returns the added material
     345
     346   !! beware the Material will be deleted when the Model gets deleted
     347*/
     348Material* Model::addMaterial(Material* material)
     349{
     350  this->materialList->add(material);
     351  return material;
     352}
     353
     354/**
     355   \brief adds a new Material to the Material List
     356   \param material the name of the Material to add
     357   \returns the added material
     358*/
     359Material* Model::addMaterial(const char* materialName)
     360{
     361 
     362  Material* newMat = new Material();
     363  newMat->setName(materialName);
     364
     365  // adding material to the List of materials
     366  this->materialList->add(newMat); 
     367  return newMat;
     368}
     369
     370/**
     371   \brief finds a Material by its name and returns it
     372   \param materialName the Name of the material to search for.
     373   \returns the Material if found, NULL otherwise
     374*/
     375Material* Model::findMaterialByName(const char* materialName)
     376{
     377  tIterator<Material>* tmpIt = this->materialList->getIterator();
     378  Material* material = tmpIt->nextElement();
     379  while(material)
     380    {
     381      if (!strcmp(material->getName(), materialName))
     382        {
     383          delete tmpIt;
     384          return material;
     385        }
     386      material = tmpIt->nextElement();
     387    }
     388  delete tmpIt;
     389  return NULL;
     390}
     391
    318392/**
    319393   \brief parses a group String
     
    323397   With it you should be able to import .obj-files with more than one Models inside.
    324398*/
    325 bool Model::addGroup (char* groupString)
     399bool Model::addGroup (const char* groupString)
    326400{
    327401  PRINTF(5)("Read Group: %s.\n", groupString);
     
    339413    }
    340414  ++this->groupCount;
    341 
    342415}
    343416
     
    348421   If a vertex line is found this function will inject it into the vertex-Array
    349422*/
    350 bool Model::addVertex (char* vertexString)
     423bool Model::addVertex (const char* vertexString)
    351424{
    352425  float subbuffer1;
     
    366439   
    367440*/
    368 bool Model::addVertex(const float x, const float y, const float z)
     441bool Model::addVertex(float x, float y, float z)
    369442{
    370443  PRINTF(5)("reading in a vertex: %f %f %f\n", x, y, z);
     
    374447
    375448/**
     449   \brief parses a vertexNormal-String
     450   \param normalString The String that will be parsed.
     451
     452   If a vertexNormal line is found this function will inject it into the vertexNormal-Array
     453*/
     454bool Model::addVertexNormal (const char* normalString)
     455{
     456  float subbuffer1;
     457  float subbuffer2;
     458  float subbuffer3;
     459  sscanf (normalString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3);
     460  PRINTF(5)("found vertex-Normal %f, %f, %f\n", &subbuffer1,&subbuffer2,&subbuffer3);
     461  this->normals->addEntry(subbuffer1, subbuffer2, subbuffer3);
     462  return true;
     463}
     464
     465/**
     466   \brief adds a VertexNormal.
     467   \param x The x coordinate of the Normal.
     468   \param y The y coordinate of the Normal.
     469   \param z The z coordinate of the Normal.
     470
     471   If a vertexNormal line is found this function will inject it into the vertexNormal-Array
     472*/
     473bool Model::addVertexNormal(float x, float y, float z)
     474{
     475  PRINTF(5)("found vertex-Normal %f, %f, %f\n", x, y, z);
     476  this->normals->addEntry(x, y, z);
     477}
     478
     479/**
     480   \brief parses a vertexTextureCoordinate-String
     481   \param vTextureString The String that will be parsed.
     482
     483   If a vertexTextureCoordinate line is found,
     484   this function will inject it into the vertexTexture-Array
     485*/
     486bool Model::addVertexTexture (const char* vTextureString)
     487{
     488  float subbuffer1;
     489  float subbuffer2;
     490  sscanf (vTextureString, "%f %f", &subbuffer1, &subbuffer2);
     491  PRINTF(5)("found vertex-Texture %f, %f\n", &subbuffer1, &subbuffer2);
     492  this->vTexture->addEntry(subbuffer1);
     493  this->vTexture->addEntry(subbuffer2);
     494  return true;
     495}
     496
     497/**
     498   \brief adds a Texture Coordinate
     499   \param u The u coordinate of the TextureCoordinate.
     500   \param v The y coordinate of the TextureCoordinate.
     501
     502   If a TextureCoordinate line is found this function will inject it into the TextureCoordinate-Array
     503*/
     504bool Model::addVertexTexture(float u, float v)
     505{
     506  PRINTF(5)("found vertex-Texture %f, %f\n", u, v);
     507  this->vTexture->addEntry(u);
     508  this->vTexture->addEntry(v);
     509}
     510
     511/**
    376512   \brief parses a face-string
    377513   \param faceString The String that will be parsed.
     
    379515   If a face line is found this function will add it to the glList.
    380516*/
    381 bool Model::addFace (char* faceString)
     517bool Model::addFace (const char* faceString)
    382518{
    383519  if (this->currentGroup->faceCount >0)
     
    441577   \param type 0: vertex only, 1: vertex and normal, 2: vertex and Texture, 3 vertex, normal and texture
    442578*/
    443 bool Model::addFace(const float faceElemCount, int type, ...)
     579bool Model::addFace(int faceElemCount, VERTEX_FORMAT type, ...)
    444580{
    445581   if (this->currentGroup->faceCount > 0)
     
    472608
    473609/**
    474    \brief parses a vertexNormal-String
    475    \param normalString The String that will be parsed.
    476 
    477    If a vertexNormal line is found this function will inject it into the vertexNormal-Array
    478 */
    479 bool Model::addVertexNormal (char* normalString)
    480 {
    481   float subbuffer1;
    482   float subbuffer2;
    483   float subbuffer3;
    484   sscanf (normalString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3);
    485   PRINTF(5)("found vertex-Normal %f, %f, %f\n", &subbuffer1,&subbuffer2,&subbuffer3);
    486   this->normals->addEntry(subbuffer1, subbuffer2, subbuffer3);
    487   return true;
    488 }
    489 
    490 /**
    491    \brief adds a VertexNormal.
    492    \param x The x coordinate of the Normal.
    493    \param y The y coordinate of the Normal.
    494    \param z The z coordinate of the Normal.
    495 
    496    If a vertexNormal line is found this function will inject it into the vertexNormal-Array
    497 */
    498 bool Model::addVertexNormal(const float x, const float y, const float z)
    499 {
    500   PRINTF(3)("found vertex-Normal %f, %f, %f\n", x, y, z);
    501   this->normals->addEntry(x, y, z);
    502 }
    503 
    504 /**
    505    \brief parses a vertexTextureCoordinate-String
    506    \param vTextureString The String that will be parsed.
    507 
    508    If a vertexTextureCoordinate line is found,
    509    this function will inject it into the vertexTexture-Array
    510 */
    511 bool Model::addVertexTexture (char* vTextureString)
    512 {
    513   float subbuffer1;
    514   float subbuffer2;
    515   sscanf (vTextureString, "%f %f", &subbuffer1, &subbuffer2);
    516   PRINTF(5)("found vertex-Texture %f, %f\n", &subbuffer1, &subbuffer2);
    517   this->vTexture->addEntry(subbuffer1);
    518   this->vTexture->addEntry(subbuffer2);
    519   return true;
    520 }
    521 
    522 /**
    523    \brief adds a Texture Coordinate
    524    \param u The u coordinate of the TextureCoordinate.
    525    \param v The y coordinate of the TextureCoordinate.
    526 
    527    If a TextureCoordinate line is found this function will inject it into the TextureCoordinate-Array
    528 */
    529 bool Model::addVertexTexture(const float u, const float v)
    530 {
    531   PRINTF(3)("found vertex-Texture %f, %f\n", u, v);
    532   this->vTexture->addEntry(u);
    533   this->vTexture->addEntry(v);
    534 }
    535 
    536 /**
    537610   \brief Function that selects a material, if changed in the obj file.
    538611   \param matString the Material that will be set.
    539612*/
    540 bool Model::addUseMtl (char* matString)
    541 {
    542   /*
    543   if (!this->mtlFileName)
    544     {
    545       PRINTF(4)("Not using new defined material, because no mtlFile found yet\n");
    546       return false;
    547     }
    548   */     
     613bool Model::setMaterial(const char* matString)
     614{
    549615  if (this->currentGroup->faceCount > 0)
    550616    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
    551617  this->initFace (this->currentGroup->currentFace);
    552618 
    553   this->currentGroup->currentFace->material = material->search(matString);
     619  this->currentGroup->currentFace->material = this->findMaterialByName(matString);
    554620
    555621  if (this->currentGroup->faceCount == 0)
     
    561627   \param mtl the Material that will be set.
    562628*/
    563 bool Model::addUseMtl (Material* mtl)
     629bool Model::setMaterial(Material* mtl)
    564630{
    565631  if (this->currentGroup->faceCount > 0)
     
    574640
    575641/**
     642   \brief A routine that is able to create normals.
     643
     644   The algorithm does the following:
     645   1. It calculates creates Vectors for each normale, and sets them to zero.
     646   2. It then Walks through a) all the Groups b) all the Faces c) all the FaceElements
     647   3. It searches for a points two neighbours per Face, takes Vecotrs to them calculates FaceNormals and adds it to the Points Normal.
     648   4. It goes through all the normale-Points and calculates the VertexNormale and includes it in the normals-Array.
     649*/
     650bool Model::buildVertexNormals ()
     651
     652  PRINTF(4)("Normals are being calculated.\n");
     653
     654  Vector* normArray = new Vector [vertices->getCount()/3];
     655  for (int i=0; i<vertices->getCount()/3;i++)
     656    normArray[i] = Vector(.0,.0,.0);
     657 
     658  int firstTouch;
     659  int secondTouch;
     660  Vector prevV;
     661  Vector nextV;
     662  Vector curV;
     663
     664  Group* tmpGroup = firstGroup;
     665  while (tmpGroup)
     666    {
     667      Face* tmpFace = tmpGroup->firstFace;
     668      while (tmpFace)
     669        {
     670          if (tmpFace->firstElem)
     671            {
     672              FaceElement* firstElem = tmpFace->firstElem;
     673              FaceElement* prevElem;
     674              FaceElement* curElem = firstElem;
     675              FaceElement* nextElem;
     676              FaceElement* lastElem;
     677              // 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.
     678              while (curElem)
     679                {
     680                  prevElem = curElem;
     681                  curElem = curElem->next;
     682                }
     683              lastElem = prevElem;
     684             
     685              curElem = firstElem;
     686              for (int j=0; j<tmpFace->vertexCount; j++)
     687                {
     688                  if (!(nextElem = curElem->next))
     689                    nextElem = firstElem;
     690                  curElem->normalNumber = curElem->vertexNumber;
     691                 
     692                  curV = Vector (vertices->getArray()[curElem->vertexNumber*3], vertices->getArray()[curElem->vertexNumber*3+1], vertices->getArray()[curElem->vertexNumber*3+2]);
     693                  prevV = Vector (vertices->getArray()[prevElem->vertexNumber*3], vertices->getArray()[prevElem->vertexNumber*3+1], vertices->getArray()[prevElem->vertexNumber*3+2]) - curV;
     694                  nextV = Vector (vertices->getArray()[nextElem->vertexNumber*3], vertices->getArray()[nextElem->vertexNumber*3+1], vertices->getArray()[nextElem->vertexNumber*3+2]) - curV;
     695                  normArray[curElem->vertexNumber] = normArray[curElem->vertexNumber] + nextV.cross(prevV);
     696
     697                  prevElem = curElem;
     698                  curElem = curElem->next;
     699                }
     700            }
     701          tmpFace = tmpFace->next;
     702        }
     703      tmpGroup = tmpGroup->next;
     704    }
     705
     706  for (int i=0; i<vertices->getCount()/3;i++)
     707    {
     708      normArray[i].normalize();
     709      PRINTF(5)("Found Normale number %d: (%f; %f, %f).\n", i, normArray[i].x, normArray[i].y, normArray[i].z);
     710     
     711      this->normals->addEntry(normArray[i].x, normArray[i].y, normArray[i].z);
     712
     713    }
     714  delete []normArray;
     715}
     716
     717////////////
     718// openGL //
     719////////////
     720/**
    576721   \brief reads and includes the Faces/Materials into the openGL state Machine
    577722*/
    578 bool Model::importToGL (void)
    579 {
    580 
     723bool Model::importToDisplayList(void)
     724{
    581725  // finalize the Arrays
    582726  this->vertices->finalizeArray();
     
    667811
    668812/**
     813   \brief reads and includes the Faces/Materials into the openGL state Machine
     814*/
     815bool Model::importToVertexArray(void)
     816{
     817  // finalize the Arrays
     818  this->vertices->finalizeArray();
     819  this->vTexture->finalizeArray();
     820  if (normals->getCount() == 0) // vertices-Array must be built for this
     821    this->buildVertexNormals();
     822  this->normals->finalizeArray();
     823
     824  this->currentGroup = this->firstGroup;
     825  glVertexPointer(3, GL_FLOAT, 0, this->vertices->getArray());
     826  glNormalPointer(3, 0, this->normals->getArray());
     827  glTexCoordPointer(2, GL_FLOAT, 0, this->vTexture->getArray());
     828
     829}
     830
     831
     832
     833/**
    669834   \brief Adds a Face-element (one vertex of a face) with all its information.
    670835   \param elem The FaceElement to add to the OpenGL-environment.
     
    688853
    689854}
    690 
    691 /**
    692    \brief A routine that is able to create normals.
    693 
    694    The algorithm does the following:
    695    1. It calculates creates Vectors for each normale, and sets them to zero.
    696    2. It then Walks through a) all the Groups b) all the Faces c) all the FaceElements
    697    3. It searches for a points two neighbours per Face, takes Vecotrs to them calculates FaceNormals and adds it to the Points Normal.
    698    4. It goes through all the normale-Points and calculates the VertexNormale and includes it in the normals-Array.
    699 */
    700 bool Model::buildVertexNormals ()
    701 {
    702  
    703   PRINTF(4)("Normals are being calculated.\n");
    704 
    705   Vector* normArray = new Vector [vertices->getCount()/3];
    706   for (int i=0; i<vertices->getCount()/3;i++)
    707     normArray[i] = Vector(.0,.0,.0);
    708  
    709   int firstTouch;
    710   int secondTouch;
    711   Vector prevV;
    712   Vector nextV;
    713   Vector curV;
    714 
    715   Group* tmpGroup = firstGroup;
    716   while (tmpGroup)
    717     {
    718       Face* tmpFace = tmpGroup->firstFace;
    719       while (tmpFace)
    720         {
    721           if (tmpFace->firstElem)
    722             {
    723               FaceElement* firstElem = tmpFace->firstElem;
    724               FaceElement* prevElem;
    725               FaceElement* curElem = firstElem;
    726               FaceElement* nextElem;
    727               FaceElement* lastElem;
    728               // 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.
    729               while (curElem)
    730                 {
    731                   prevElem = curElem;
    732                   curElem = curElem->next;
    733                 }
    734               lastElem = prevElem;
    735              
    736               curElem = firstElem;
    737               for (int j=0; j<tmpFace->vertexCount; j++)
    738                 {
    739                   if (!(nextElem = curElem->next))
    740                     nextElem = firstElem;
    741                   curElem->normalNumber = curElem->vertexNumber;
    742                  
    743                   curV = Vector (vertices->getArray()[curElem->vertexNumber*3], vertices->getArray()[curElem->vertexNumber*3+1], vertices->getArray()[curElem->vertexNumber*3+2]);
    744                   prevV = Vector (vertices->getArray()[prevElem->vertexNumber*3], vertices->getArray()[prevElem->vertexNumber*3+1], vertices->getArray()[prevElem->vertexNumber*3+2]) - curV;
    745                   nextV = Vector (vertices->getArray()[nextElem->vertexNumber*3], vertices->getArray()[nextElem->vertexNumber*3+1], vertices->getArray()[nextElem->vertexNumber*3+2]) - curV;
    746                   normArray[curElem->vertexNumber] = normArray[curElem->vertexNumber] + nextV.cross(prevV);
    747 
    748                   prevElem = curElem;
    749                   curElem = curElem->next;
    750                 }
    751             }
    752           tmpFace = tmpFace->next;
    753         }
    754       tmpGroup = tmpGroup->next;
    755     }
    756 
    757   for (int i=0; i<vertices->getCount()/3;i++)
    758     {
    759       normArray[i].normalize();
    760       PRINTF(5)("Found Normale number %d: (%f; %f, %f).\n", i, normArray[i].x, normArray[i].y, normArray[i].z);
    761      
    762       this->normals->addEntry(normArray[i].x, normArray[i].y, normArray[i].z);
    763 
    764     }
    765   delete []normArray;
    766  
    767 }
    768 
    769855
    770856/**
     
    833919  */
    834920
    835   this->addFace (4, 3, 1,1,1, 2,2,2, 4,4,3, 3,3,4);
    836   this->addFace (4, 3, 3,3,5, 4,4,6, 6,6,7, 5,5,8);
    837   this->addFace (4, 3, 5,5,9, 6,6,10, 8,8,11, 7,7,12);
    838   this->addFace (4, 3, 7,7,13, 8,8,14, 2,10,15, 1,9,16);
    839   this->addFace (4, 3, 2,2,17, 8,11,18, 6,12,19, 4,4,20);
    840   this->addFace (4, 3, 7,13,21, 1,1,22, 3,3,23, 5,14,24);
    841 
    842 }
     921  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,1,1, 2,2,2, 4,4,3, 3,3,4);
     922  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 3,3,5, 4,4,6, 6,6,7, 5,5,8);
     923  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 5,5,9, 6,6,10, 8,8,11, 7,7,12);
     924  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 7,7,13, 8,8,14, 2,10,15, 1,9,16);
     925  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,2,17, 8,11,18, 6,12,19, 4,4,20);
     926  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 7,13,21, 1,1,22, 3,3,23, 5,14,24);
     927
     928}
Note: See TracChangeset for help on using the changeset viewer.