Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/importer/object.cc @ 3332

Last change on this file since 3332 was 3207, checked in by bensch, 20 years ago

orxonox/trunk/importer: object.cc in new look (with PRINTF())

File size: 25.4 KB
RevLine 
[2823]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
[2748]16#include "object.h"
[3196]17int verbose = 1; //! \todo should be GLOBAL
[3140]18using namespace std;
[2776]19
[2842]20/**
[3186]21   \brief Creates a 3D-Object, but does not load any 3D-models.
22
23   This Constructor is pretty useless, because why load no object in an object-loader??
[2842]24*/
[2748]25Object::Object ()
26{
[2765]27
[3195]28  this->initialize();
[2847]29
[3195]30  this->BoxObject();
[2765]31
[3195]32  this->importToGL ();
[3064]33
[3195]34  this->cleanup();
[2767]35}
36
[2842]37/**
[3186]38   \brief Crates a 3D-Object and loads in a File.
[2842]39   \param fileName file to parse and load (must be a .obj file)
40*/
[2773]41Object::Object(char* fileName)
42{
[3195]43  this->initialize();
[2773]44
[3195]45  this->importFile (fileName);
[2773]46
[3195]47  this->importToGL ();
[3064]48
[3195]49  this->cleanup();
[2773]50}
51
[2842]52/**
53   \brief Crates a 3D-Object, loads in a File and scales it.
54   \param fileName file to parse and load (must be a .obj file)
55   \param scaling The factor that the object will be scaled with.
56*/
[2833]57Object::Object(char* fileName, float scaling)
[2767]58{
[3195]59  this->initialize();
60  this->scaleFactor = scaling;
[2833]61
[3195]62  this->importFile (fileName);
[2833]63
[3195]64  this->importToGL ();
[3064]65
[3195]66  this->cleanup();
[2767]67}
68
[2847]69/**
[3186]70   \brief deletes an Object.
71
72   Looks if any from object allocated space is still in use, and if so deleted it.
[2847]73*/
74Object::~Object()
75{
[3206]76  PRINTF(2)("Deleting display Lists.\n");
[3195]77  Group* walker = this->firstGroup;
[2850]78  while (walker != NULL)
79    {
80      glDeleteLists (walker->listNumber, 1);
[3066]81      Group* delWalker = walker;
[3063]82      walker = walker->next;
[3066]83      delete delWalker;
[3140]84    }
85
[3195]86  if (this->objPath)
87    delete []this->objPath;
88  if (this->objFileName)
89    delete []this->objFileName;
90  if (this->mtlFileName)
91    delete []this->mtlFileName;
[3206]92  PRINTF(2)("Deleting Materials.\n");
[3195]93  if (this->material)
94    delete this->material;
[2847]95}
96
[2833]97
[2842]98/**
[2851]99   \brief Draws the Objects of all Groups.
100   It does this by just calling the Lists that must have been created earlier.
[2842]101*/
[3063]102void Object::draw (void) const
[2748]103{
[3207]104  PRINTF(2)("drawing the 3D-Objects\n"); 
[3195]105  Group* walker = this->firstGroup;
[2850]106  while (walker != NULL)
107    {
[3207]108      PRINTF(3)("Drawing object %s\n", walker->name);
[2850]109      glCallList (walker->listNumber);
[3063]110      walker = walker->next;
[2850]111    }
[2748]112}
[2754]113
[2842]114/**
[2851]115   \brief Draws the Object number groupNumber
[3186]116   \param groupNumber The number of the group that will be displayed.
117
[2851]118   It does this by just calling the List that must have been created earlier.
119*/
[3063]120void Object::draw (int groupNumber) const 
[2851]121{
[3195]122  if (groupNumber >= this->groupCount)
[2852]123    {
[3207]124      PRINTF(1)("You requested object number %i, but this File only contains of %i Objects.\n", groupNumber-1, this->groupCount);
[2852]125      return;
126    }
[3207]127  PRINTF(2)("drawing the requested 3D-Objects if found.\n"); 
[3195]128  Group* walker = this->firstGroup;
[2851]129  int counter = 0;
130  while (walker != NULL)
131    {
132      if (counter == groupNumber)
133        {
[3207]134          PRINTF(2)("Drawing object number %i named %s\n", counter, walker->name);
[2851]135          glCallList (walker->listNumber);
136          return;
137        }
138      ++counter;
[3063]139      walker = walker->next;
[2851]140    }
[3207]141  PRINTF(1)("Object number %i in %s not Found.\n", groupNumber, this->objFileName);
[2851]142  return;
143
144}
[2852]145
146/**
[3063]147   \brief Draws the Object with a specific groupName
[3186]148   \param groupName The name of the group that will be displayed.
149
[2852]150   It does this by just calling the List that must have been created earlier.
151*/
[3063]152void Object::draw (char* groupName) const
[2851]153{
[3207]154  PRINTF(2)("drawing the requested 3D-Objects if found.\n"); 
[3195]155  Group* walker = this->firstGroup;
[2851]156  while (walker != NULL)
157    {
158      if (!strcmp(walker->name, groupName))
159        {
[3207]160          PRINTF(2)("Drawing object %s\n", walker->name);
[2851]161          glCallList (walker->listNumber);
162          return;
163        }
[3063]164      walker = walker->next;
[2851]165    }
[3207]166  PRINTF(1)("Object Named %s in %s not Found.\n", groupName, this->objFileName);
[2851]167  return;
168}
169
[2852]170/**
171   \returns Count of the Objects in this File
172*/
[3063]173int Object::getGroupCount (void) const
[2852]174{
[3195]175  return this->groupCount;
[2852]176}
[2851]177
178/**
[3186]179    \brief initializes the Object.
180
181    This Function initializes all the needed arrays, Lists and clientStates.
182    It also defines default values.
[3066]183*/
184bool Object::initialize (void)
185{
[3207]186  PRINTF(2)("new 3D-Object is being created\n"); 
[3066]187
188  // setting the start group;
[3195]189  this->firstGroup = new Group;
190  this->currentGroup = this->firstGroup;
191  this->groupCount = 0;
[3066]192 
[3195]193  this->initGroup (this->currentGroup);
194  this->objPath = NULL;
195  this->objFileName = NULL;
196  this->mtlFileName = NULL;
197  this->scaleFactor = 1;
198  this->material = new Material();
[3066]199
[3195]200  this->vertices = new Array();
201  this->vTexture = new Array();
202  this->normals = new Array();
[3066]203
204  return true;
205}
206
207/**
[2850]208   \brief initializes a new Group object
[3186]209   \param group the group that should be initialized.
210   \todo Maybe Group should be a Class, because it does a lot of stuff
211   
[2850]212*/
213bool Object::initGroup(Group* group)
214{
[3207]215  PRINTF(3)("Adding new Group\n");
[2863]216  group->name = "";
[2850]217  group->faceMode = -1;
[2967]218  group->faceCount = 0; 
[3063]219  group->next = NULL;
[2850]220
[3063]221  group->firstFace = new Face;
[3195]222  this->initFace (group->firstFace);
[3063]223  group->currentFace = group->firstFace;
[2850]224}
225
226/**
[3066]227   \brief initializes a new Face. (sets default Values)
[3186]228   \param face The face to initialize
[2850]229*/
[3066]230bool Object::initFace (Face* face)
[2850]231{
[3066]232  face->vertexCount = 0;
[2967]233
[3066]234  face->firstElem = NULL;
235 
236  face->materialString = NULL;
237 
238  face->next = NULL;
239
240  return true;
[2850]241}
[2863]242
[2850]243/**
[3066]244   \brief finalizes an Object.
245   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.
246*/
247bool Object::cleanup(void)
248{
[3207]249  PRINTF(3)("cleaning up the 3D-Object to save Memory.\n");
[3066]250
[3195]251  if (this->vertices)
252    delete this->vertices;
253  if (this->vTexture)
254    delete this->vTexture;
255  if (this->normals)
256    delete this->normals;
[3066]257
[3195]258  this->cleanupGroup(this->firstGroup);
[3066]259  return true; 
260}
261
262/**
[3068]263   \brief Cleans up all groups starting from group.
264   \param group the first Group to clean
265*/
266bool Object::cleanupGroup (Group* group)
267{
[3207]268  PRINTF(3)("Cleaning up group\n");
[3068]269  if (group->firstFace != NULL)
270    {
271      cleanupFace (group->firstFace);
272      delete group->firstFace;
273    }
274
275  if (group->next !=NULL)
276    cleanupGroup (group->next);
277  return true;
278}
279
280/**
[3186]281   \brief Cleans up all Faces starting from face until NULL is reached.
282   \param face the first face to clean.
[3068]283*/
284bool Object::cleanupFace (Face* face)
285{
[3207]286  PRINTF(3)("Cleaning up Face\n");
[3068]287
288  if (face->materialString != NULL)
[3080]289      delete []face->materialString;
[3068]290  if (face->firstElem != NULL)
291    {
[3195]292      this->cleanupFaceElement(face->firstElem);
[3068]293      delete face->firstElem;
294    }
295     
296  if (face->next != NULL)
297    {
[3195]298      this->cleanupFace (face->next);
[3068]299      delete face->next;
300    }
301     
302}
303
304
305/**
306   \brief Cleans up all FaceElements starting from faceElem.
307   \param faceElem the first FaceElement to clean.
308*/
309bool Object::cleanupFaceElement(FaceElement* faceElem)
310{
311  if (faceElem->next != NULL)
312    {
[3195]313      this->cleanupFaceElement (faceElem->next);
[3072]314      delete faceElem->next;
[3068]315    }
316}
317
318/**
[3066]319   \brief Imports a obj file and handles the the relative location
320   \param fileName The file to import
321*/
322bool Object::importFile (char* fileName)
323{
[3207]324  PRINTF(3)("preparing to read in file: %s\n", fileName);
[3140]325
326
327#ifdef __WIN32__
328  // win32 path reading
329  char pathSplitter= '\\';
330#else /* __WIN32__ */
331  // unix path reading
332  char pathSplitter='/';
333#endif /* __WIN32__ */
334  char* tmpName = fileName;
335  if (tmpName[0] == pathSplitter)
336    tmpName++;
337  char* name = tmpName;
338  while (( tmpName = strchr (tmpName+1, pathSplitter)))
339    {
340      name = tmpName+1;
341    }
[3195]342  this->objPath = new char[name-fileName];
343  strncpy(this->objPath, fileName, name-fileName);
344  this->objPath[name-fileName] = '\0';
[3140]345  if (verbose >=2)
346    if (strlen(objPath)> 0)
[3207]347      PRINTF(0)("Resolved file %s to folder: %s.\n", name, objPath);
[3140]348    else
[3207]349      PRINTF(0)("Resolved file %s.\n", name);
[3140]350 
[3195]351  if (this->material)
352    this->material->addTexturePath(this->objPath);
353  this->objFileName = new char[strlen(name)+1];
354  strcpy (this->objFileName, name);
[3140]355  this->readFromObjFile ();
[3066]356  return true;
357}
358
359/**
[2842]360   \brief Reads in the .obj File and sets all the Values.
361   This function does read the file, parses it for the occurence of things like vertices, faces and so on, and executes the specific tasks
362*/
[3140]363bool Object::readFromObjFile (void)
[2754]364{
[3140]365  char* fileName = new char [strlen(objPath)+strlen(objFileName)+1];
[3195]366  if (this->objFileName != NULL && !strcmp(this->objFileName, ""))
[3140]367    return false;
[3195]368  strcpy(fileName, this->objPath);
369  strcat(fileName, this->objFileName);
[3140]370
371  ifstream* OBJ_FILE = new ifstream(fileName);
372  if (OBJ_FILE->fail())
[2775]373    {
[3207]374      PRINTF(1)("unable to open .OBJ file: %s\n Loading Box Object instead.\n", fileName);
[2821]375      BoxObject();
[3195]376      OBJ_FILE->close();
[3140]377      delete []fileName;
[3195]378      delete OBJ_FILE;
[2775]379      return false;
380    }
[3207]381  PRINTF(2)("Reading from opened file %s\n", fileName);
[2931]382  char Buffer[10000];
[2765]383  while(!OBJ_FILE->eof())
384    {
[2931]385      OBJ_FILE->getline(Buffer, 10000);
[3207]386      PRINTF(3)("Read input line: %s\n", Buffer);
[2765]387     
388
389      // case vertice
390      if (!strncmp(Buffer, "v ", 2))
391        {
[3195]392          this->readVertex(Buffer+2);
[2765]393        }
394
395      // case face
396      else if (!strncmp(Buffer, "f ", 2))
397        {
[3195]398          this->readFace (Buffer+2);
[2765]399        }
[2776]400     
[3063]401      else if (!strncmp(Buffer, "mtllib ", 7))
[2776]402        {
[3195]403          this->readMtlLib (Buffer+7);
[2776]404        }
[2765]405
[3063]406      else if (!strncmp(Buffer, "usemtl ", 7))
[2769]407        {
[3195]408          this->readUseMtl (Buffer+7);
[2769]409        }
[2794]410
411      // case VertexNormal
[3063]412      else if (!strncmp(Buffer, "vn ", 3))
[3195]413        {
414          this->readVertexNormal(Buffer+3);
415        }
416     
[2848]417      // case VertexTextureCoordinate
[3063]418      else if (!strncmp(Buffer, "vt ", 3))
[3195]419        {
420          this->readVertexTexture(Buffer+3);
421        }
[2848]422      // case group
[3063]423      else if (!strncmp(Buffer, "g ", 2))
[2848]424        {
[3195]425          this->readGroup (Buffer+2);
[2848]426        }
[3195]427      else if (!strncmp(Buffer, "s ", 2)) //! \todo smoothing groups have to be implemented
[2995]428        {
[3069]429          if (verbose >= 2)
[3207]430            PRINTF(2)("smoothing groups not supportet yet. line: %s\n", Buffer);
[2995]431        }
[2765]432    }
[2846]433  OBJ_FILE->close();
[3195]434  delete OBJ_FILE;
[3140]435  delete []fileName;
[2863]436  return true;
[2765]437
[2767]438}
[2765]439
[2842]440/**
[3066]441   \brief parses a group String
[3186]442   \param groupString the new Group to create
443
[3066]444   This function initializes a new Group.
445   With it you should be able to import .obj-files with more than one Objects inside.
446*/
447bool Object::readGroup (char* groupString)
448{
[3207]449  PRINTF(3)("Read Group: %s.\n", groupString);
[3195]450  if (this->groupCount != 0 && this->currentGroup->faceCount>0)
[3066]451    {
452      //      finalizeGroup(currentGroup);
[3195]453      this->currentGroup = this->currentGroup->next = new Group;
454      this->initGroup(this->currentGroup);
[3066]455    }
[3140]456  // setting the group name if not default.
457  if (strcmp(groupString, "default"))
458    {
[3195]459      this->currentGroup->name = new char [strlen(groupString)+1];
460      strcpy(this->currentGroup->name, groupString);
[3140]461    }
[3195]462  ++this->groupCount;
[3066]463
464}
465
466/**
[2842]467   \brief parses a vertex-String
[3186]468   \param vertexString The String that will be parsed.
469
[2842]470   If a vertex line is found this function will inject it into the vertex-Array
471*/
[2767]472bool Object::readVertex (char* vertexString)
473{
[3071]474  float subbuffer1;
475  float subbuffer2;
476  float subbuffer3;
477  sscanf (vertexString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3);
[3207]478  PRINTF(3)("reading in a vertex: %f %f %f\n", &subbuffer1, &subbuffer2, &subbuffer3);
[3195]479  this->vertices->addEntry(subbuffer1*scaleFactor, subbuffer2*scaleFactor, subbuffer3*scaleFactor);
[2767]480  return true;
481}
482
[2842]483/**
484   \brief parses a face-string
[3186]485   \param faceString The String that will be parsed.
486
[2842]487   If a face line is found this function will add it to the glList.
488   The function makes a difference between QUADS and TRIANGLES, and will if changed re-open, set and re-close the gl-processe.
489*/
[2767]490bool Object::readFace (char* faceString)
491{
[3195]492  if (this->currentGroup->faceCount >0)
493    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
494  this->initFace (this->currentGroup->currentFace);
[2767]495
[3195]496  FaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new FaceElement;
[3068]497  tmpElem->next = NULL;
[2934]498  while(strcmp (faceString, "\0"))
[2767]499    {
[3195]500      if (this->currentGroup->currentFace->vertexCount>0)
[2934]501          tmpElem = tmpElem->next = new FaceElement;
502      tmpElem->next = NULL;
503
[3064]504      char tmpValue [50];
[3072]505      int tmpLen;
506      char* vertex = NULL;
507      char* texture = NULL;
508      char* normal = NULL;
509
[3063]510      sscanf (faceString, "%s", tmpValue);
[3072]511      tmpLen = strlen(tmpValue);
512      vertex = tmpValue;
[2934]513
[3072]514      if ((texture = strstr (vertex, "/")) != NULL)
515        {
516          texture[0] = '\0';
517          texture ++;
518         
519          if ((normal = strstr (texture, "/")) !=NULL)
520            {
521              normal[0] = '\0';
522              normal ++;
523            }     
524        }
525      if (vertex)
[3073]526        tmpElem->vertexNumber = atoi(vertex)-1;
[3072]527      else
[3073]528        tmpElem->vertexNumber = -1;
[3072]529      if (texture)
[3073]530        tmpElem->texCoordNumber = atoi(texture)-1;
[3072]531      else
[3073]532        tmpElem->texCoordNumber = -1;
[3072]533      if (normal)
[3073]534        tmpElem->normalNumber = atoi(normal)-1;
[3072]535      else
[3073]536        tmpElem->normalNumber = -1;
[3072]537
538      faceString += tmpLen;
[2934]539      if (strcmp (faceString, "\0"))
540        faceString++;
[3195]541      this->currentGroup->currentFace->vertexCount++;
[2934]542    }
543
[3195]544  this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount -2;
[2754]545}
[2768]546
[2842]547/**
548   \brief parses a vertexNormal-String
[3186]549   \param normalString The String that will be parsed.
550
[2842]551   If a vertexNormal line is found this function will inject it into the vertexNormal-Array
552*/
[2794]553bool Object::readVertexNormal (char* normalString)
554{
[3071]555  float subbuffer1;
556  float subbuffer2;
557  float subbuffer3;
558  sscanf (normalString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3);
[3207]559  PRINTF(3)("found vertex-Normal %f, %f, %f\n", &subbuffer1,&subbuffer2,&subbuffer3);
[3195]560  this->normals->addEntry(subbuffer1, subbuffer2, subbuffer3);
[2794]561  return true;
562}
563
[2842]564/**
565   \brief parses a vertexTextureCoordinate-String
566   \param vTextureString The String that will be parsed.
[3186]567
568   If a vertexTextureCoordinate line is found,
569   this function will inject it into the vertexTexture-Array
[2842]570*/
[2820]571bool Object::readVertexTexture (char* vTextureString)
572{
[3071]573  float subbuffer1;
574  float subbuffer2;
575  sscanf (vTextureString, "%f %f", &subbuffer1, &subbuffer2);
[3207]576  PRINTF(3)("found vertex-Texture %f, %f\n", &subbuffer1, &subbuffer2);
[3195]577  this->vTexture->addEntry(subbuffer1);
578  this->vTexture->addEntry(subbuffer2);
[2820]579  return true;
580}
581
[3066]582/**
583    \brief Function to read in a mtl File.
584    \param mtlFile The .mtl file to read
[3186]585
586    This Function parses all Lines of an mtl File.
587    The reason for it not to be in the materials-class is,
588    that a material does not have to be able to read itself in from a File.
589
[2850]590*/
[3066]591bool Object::readMtlLib (char* mtlFile)
[2850]592{
[3195]593  this->mtlFileName = new char [strlen(mtlFile)+1];
594  strcpy(this->mtlFileName, mtlFile);
595  char* fileName = new char [strlen(objPath) + strlen(this->mtlFileName)+1];
596  strcpy(fileName, this->objPath);
597  strcat(fileName, this->mtlFileName);
[3140]598 
599
[3207]600  PRINTF(2)("Opening mtlFile: %s\n", fileName);
[3140]601
602  ifstream* MTL_FILE = new ifstream (fileName);
603  if (MTL_FILE->fail())
[2850]604    {
[3066]605      if (verbose >= 1)
[3207]606        PRINTF(2)("unable to open file: %s\n", fileName);
[3195]607      MTL_FILE->close();
[3140]608      delete []fileName;
[3195]609      delete MTL_FILE;
[3066]610      return false;
[2850]611    }
[3066]612  char Buffer[500];
613  Material* tmpMat = material;
614  while(!MTL_FILE->eof())
[2850]615    {
[3066]616      MTL_FILE->getline(Buffer, 500);
[3207]617      PRINTF(4)("found line in mtlFile: %s\n", Buffer);
[3066]618     
[2863]619
[3066]620      // create new Material
621      if (!strncmp(Buffer, "newmtl ", 7))
622        {
623          tmpMat = tmpMat->addMaterial(Buffer+7);
[3207]624          //      PRINTF(2)("%s, %p\n", tmpMat->getName(), tmpMat);
[3066]625        }
626      // setting a illumMode
627      else if (!strncmp(Buffer, "illum ", 6))
628        {
629          tmpMat->setIllum(Buffer+6);
[2863]630
[3066]631        }
632      // setting Diffuse Color
633      else if (!strncmp(Buffer, "Kd ", 3))
634        {
635          tmpMat->setDiffuse(Buffer+3);
636        }
637      // setting Ambient Color
638      else if (!strncmp(Buffer, "Ka ", 3))
639        {
640          tmpMat->setAmbient(Buffer+3);
641        }
642      // setting Specular Color
643      else if (!strncmp(Buffer, "Ks ", 3))
644        {
645          tmpMat->setSpecular(Buffer+3);
646        }
647      // setting The Specular Shininess
648      else if (!strncmp(Buffer, "Ns ", 3))
649        {
650          tmpMat->setShininess(Buffer+3);
651        }
652      // setting up transparency
653      else if (!strncmp(Buffer, "d ", 2))
654        {
655          tmpMat->setTransparency(Buffer+2);
656        }
[3070]657      else if (!strncmp(Buffer, "Tf ", 3))
[3066]658        {
659          tmpMat->setTransparency(Buffer+3);
660        }
[3070]661     
662      else if (!strncmp(Buffer, "map_Kd ", 7))
663        {
664          tmpMat->setDiffuseMap(Buffer+7);
665        }
666      else if (!strncmp(Buffer, "map_Ka ", 7))
667        {
668          tmpMat->setAmbientMap(Buffer+7);
669        }
670      else if (!strncmp(Buffer, "map_Ks ", 7))
671        {
672          tmpMat->setSpecularMap(Buffer+7);
673        }
674      else if (!strncmp(Buffer, "bump ", 5))
675        {
676          tmpMat->setBump(Buffer+7);
677        }
678     
[3066]679
680    }
[3195]681  MTL_FILE->close();
[3140]682  delete []fileName;
[3195]683  delete MTL_FILE;
[3066]684  return true;
[2850]685}
686
[3066]687/**
688   \brief Function that selects a material, if changed in the obj file.
689   \param matString the Material that will be set.
690*/
691bool Object::readUseMtl (char* matString)
[3063]692{
[3195]693  if (!this->mtlFileName)
[3066]694    {
[3207]695      PRINTF(4)("Not using new defined material, because no mtlFile found yet\n");
[3066]696      return false;
697    }
698     
[3195]699  if (this->currentGroup->faceCount >0)
700    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
701  this->initFace (this->currentGroup->currentFace);
[3063]702 
[3195]703  this->currentGroup->currentFace->materialString = new char[strlen(matString)+1];
704  strcpy (this->currentGroup->currentFace->materialString, matString);
[3063]705 
[3195]706  if (this->currentGroup->faceCount == 0)
707    this->currentGroup->faceCount ++;
[3063]708
709}
710
[3066]711/**
712   \brief reads and includes the Faces/Materials into the openGL state Machine
713*/
[3063]714bool Object::importToGL (void)
715{
716
717  // finalize the Arrays
[3195]718  this->vertices->finalizeArray();
719  this->vTexture->finalizeArray();
[3075]720  if (normals->getCount() == 0) // vertices-Array must be uilt for this
[3195]721    this->buildVertexNormals();
722  this->normals->finalizeArray();
[3063]723
[3195]724  this->currentGroup = this->firstGroup;
[3063]725
[3195]726  while (this->currentGroup != NULL)
[3063]727    {
728
729      // creating a glList for the Group
[3195]730      if ((this->currentGroup->listNumber = glGenLists(1)) == 0)
[3063]731        {
[3207]732          PRINTF(1)("list could not be created for this Object\n");
[3063]733          return false;
734        }
[3195]735      glNewList (this->currentGroup->listNumber, GL_COMPILE);
[3063]736
737      // Putting Faces to GL
[3195]738      Face* tmpFace = this->currentGroup->firstFace;
[3063]739      while (tmpFace != NULL)
740        {
[3065]741          if (tmpFace->vertexCount == 0 && tmpFace->materialString != NULL)
[3063]742            {
[3195]743              if (this->currentGroup->faceMode != -1)
[3065]744                glEnd();
[3195]745              this->currentGroup->faceMode = 0;
[3207]746              PRINTF(2)("using material %s for coming Faces.\n", tmpFace->materialString);
[3069]747              Material* tmpMat;
748              if ((tmpMat = material->search(tmpFace->materialString)) != NULL)
749                tmpMat->select();
[3065]750
751            }
752
753          else if (tmpFace->vertexCount == 3)
754            {
[3195]755              if (this->currentGroup->faceMode != 3)
[3063]756                {
[3195]757                  if (this->currentGroup->faceMode != -1)
[3063]758                    glEnd();
759                  glBegin(GL_TRIANGLES);
760                }
761             
[3195]762              this->currentGroup->faceMode = 3;
[3207]763              PRINTF(3)("found triag.\n");
[3063]764            }
765         
766          else if (tmpFace->vertexCount == 4)
767            {
[3195]768              if (this->currentGroup->faceMode != 4)
[3063]769                {
[3195]770                  if (this->currentGroup->faceMode != -1)
[3063]771                    glEnd();
772                  glBegin(GL_QUADS);
773                }
[3195]774              this->currentGroup->faceMode = 4;
[3207]775              PRINTF(3)("found quad.\n");
[3063]776            }
777         
778          else if (tmpFace->vertexCount > 4)
779            {
[3195]780              if (this->currentGroup->faceMode != -1)
[3063]781                glEnd();
782              glBegin(GL_POLYGON);
[3207]783              PRINTF(3)("Polygon with %i faces found.", tmpFace->vertexCount);
[3195]784              this->currentGroup->faceMode = tmpFace->vertexCount;
[3063]785            }
786         
787          FaceElement* tmpElem = tmpFace->firstElem;
788          while (tmpElem != NULL)
789            {
[3207]790              //      PRINTF(2)("%s\n", tmpElem->value);
[3195]791              this->addGLElement(tmpElem);
[3063]792              tmpElem = tmpElem->next;
793            }
794          tmpFace = tmpFace->next;
795        }
796      glEnd();
797      glEndList();
[3195]798
799      this->currentGroup = this->currentGroup->next;
[3063]800    } 
801}
802
[3066]803/**
804   \brief Adds a Face-element (one vertex of a face) with all its information.
[3186]805   \param elem The FaceElement to add to the OpenGL-environment.
806
[3066]807   It does this by searching:
808   1. The Vertex itself
809   2. The VertexNormale
810   3. The VertexTextureCoordinate
811   merging this information, the face will be drawn.
[2842]812*/
[3072]813bool Object::addGLElement (FaceElement* elem)
[2776]814{
[3207]815  PRINTF(3)("importing grafical Element to openGL.\n");
[3066]816
[3073]817  if (elem->texCoordNumber != -1)
[3195]818    glTexCoord2fv(this->vTexture->getArray() + elem->texCoordNumber * 2);
[3073]819  if (elem->normalNumber != -1)
[3195]820    glNormal3fv(this->normals->getArray() + elem->normalNumber * 3);
[3073]821  if (elem->vertexNumber != -1)
[3195]822    glVertex3fv(this->vertices->getArray() + elem->vertexNumber * 3);
[2769]823
[2776]824}
825
[3079]826/**
827   \brief A routine that is able to create normals.
[3186]828
[3079]829   The algorithm does the following:
830   1. It calculates creates Vectors for each normale, and sets them to zero.
831   2. It then Walks through a) all the Groups b) all the Faces c) all the FaceElements
832   3. It searches for a points two neighbours per Face, takes Vecotrs to them calculates FaceNormals and adds it to the Points Normal.
833   4. It goes through all the normale-Points and calculates the VertexNormale and includes it in the normals-Array.
834*/
[3075]835bool Object::buildVertexNormals ()
836{
[3081]837 
[3207]838  PRINTF(2)("Normals are being calculated.\n");
[3075]839
840  Vector* normArray = new Vector [vertices->getCount()/3];
[3079]841  for (int i=0; i<vertices->getCount()/3;i++)
[3075]842    normArray[i] = Vector(.0,.0,.0);
[3081]843 
[3075]844  int firstTouch;
845  int secondTouch;
846  Vector prevV;
847  Vector nextV;
848  Vector curV;
849
850  Group* tmpGroup = firstGroup;
851  while (tmpGroup)
852    {
853      Face* tmpFace = tmpGroup->firstFace;
854      while (tmpFace)
855        {
856          if (tmpFace->firstElem)
857            {
858              FaceElement* firstElem = tmpFace->firstElem;
[3082]859              FaceElement* prevElem;
860              FaceElement* curElem = firstElem;
861              FaceElement* nextElem;
[3081]862              FaceElement* lastElem;
[3082]863              // 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.
[3081]864              while (curElem)
865                {
866                  prevElem = curElem;
867                  curElem = curElem->next;
868                }
869              lastElem = prevElem;
[3075]870             
[3081]871              curElem = firstElem;
872              for (int j=0; j<tmpFace->vertexCount; j++)
[3075]873                {
[3082]874                  if (!(nextElem = curElem->next))
875                    nextElem = firstElem;
[3075]876                  curElem->normalNumber = curElem->vertexNumber;
877                 
878                  curV = Vector (vertices->getArray()[curElem->vertexNumber*3], vertices->getArray()[curElem->vertexNumber*3+1], vertices->getArray()[curElem->vertexNumber*3+2]);
879                  prevV = Vector (vertices->getArray()[prevElem->vertexNumber*3], vertices->getArray()[prevElem->vertexNumber*3+1], vertices->getArray()[prevElem->vertexNumber*3+2]) - curV;
[3082]880                  nextV = Vector (vertices->getArray()[nextElem->vertexNumber*3], vertices->getArray()[nextElem->vertexNumber*3+1], vertices->getArray()[nextElem->vertexNumber*3+2]) - curV;
[3081]881                  normArray[curElem->vertexNumber] = normArray[curElem->vertexNumber] + nextV.cross(prevV);
[3075]882
883                  prevElem = curElem;
884                  curElem = curElem->next;
885                }
886            }
887          tmpFace = tmpFace->next;
888        }
889      tmpGroup = tmpGroup->next;
890    }
891
892  for (int i=0; i<vertices->getCount()/3;i++)
893    {
894      normArray[i].normalize();
[3207]895      PRINTF(3)("Found Normale number %d: (%f; %f, %f).\n", i, normArray[i].x, normArray[i].y, normArray[i].z);
[3079]896
[3195]897      this->normals->addEntry(normArray[i].x, normArray[i].y, normArray[i].z);
[3075]898
899    }
[3080]900  delete []normArray; 
[3081]901 
[3075]902}
903
904
[2842]905/**
906   \brief Includes a default object
[3186]907
[2842]908   This will inject a Cube, because this is the most basic object.
909*/
[2821]910void Object::BoxObject(void)
911{
[3195]912  this->readVertex ("-0.5 -0.5 0.5");
913  this->readVertex ("0.5 -0.5 0.5");
914  this->readVertex ("-0.5 0.5 0.5");
915  this->readVertex ("0.5 0.5 0.5");
916  this->readVertex ("-0.5 0.5 -0.5");
917  this->readVertex ("0.5 0.5 -0.5");
918  this->readVertex ("-0.5 -0.5 -0.5");
919  this->readVertex ("0.5 -0.5 -0.5");
[2967]920
[3195]921  this->readVertexTexture ("0.0 0.0");
922  this->readVertexTexture ("1.0 0.0");
923  this->readVertexTexture ("0.0 1.0");
924  this->readVertexTexture ("1.0 1.0");
925  this->readVertexTexture ("0.0 2.0");
926  this->readVertexTexture ("1.0 2.0");
927  this->readVertexTexture ("0.0 3.0");
928  this->readVertexTexture ("1.0 3.0");
929  this->readVertexTexture ("0.0 4.0");
930  this->readVertexTexture ("1.0 4.0");
931  this->readVertexTexture ("2.0 0.0");
932  this->readVertexTexture ("2.0 1.0");
933  this->readVertexTexture ("-1.0 0.0");
934  this->readVertexTexture ("-1.0 1.0");
[3081]935
[3195]936  this->readVertexNormal ("0.0 0.0 1.0");
937  this->readVertexNormal ("0.0 0.0 1.0");
938  this->readVertexNormal ("0.0 0.0 1.0");
939  this->readVertexNormal ("0.0 0.0 1.0");
940  this->readVertexNormal ("0.0 1.0 0.0");
941  this->readVertexNormal ("0.0 1.0 0.0");
942  this->readVertexNormal ("0.0 1.0 0.0");
943  this->readVertexNormal ("0.0 1.0 0.0");
944  this->readVertexNormal ("0.0 0.0 -1.0");
945  this->readVertexNormal ("0.0 0.0 -1.0");
946  this->readVertexNormal ("0.0 0.0 -1.0");
947  this->readVertexNormal ("0.0 0.0 -1.0");
948  this->readVertexNormal ("0.0 -1.0 0.0");
949  this->readVertexNormal ("0.0 -1.0 0.0");
950  this->readVertexNormal ("0.0 -1.0 0.0");
951  this->readVertexNormal ("0.0 -1.0 0.0");
952  this->readVertexNormal ("1.0 0.0 0.0");
953  this->readVertexNormal ("1.0 0.0 0.0");
954  this->readVertexNormal ("1.0 0.0 0.0");
955  this->readVertexNormal ("1.0 0.0 0.0");
956  this->readVertexNormal ("-1.0 0.0 0.0");
957  this->readVertexNormal ("-1.0 0.0 0.0");
958  this->readVertexNormal ("-1.0 0.0 0.0");
959  this->readVertexNormal ("-1.0 0.0 0.0");
[2821]960
[3083]961  /* normaleLess-testingMode
[3195]962  this->readFace ("1 2 4 3");
963  this->readFace ("3 4 6 5");
964  this->readFace ("5 6 8 7");
965  this->readFace ("7 8 2 1");
966  this->readFace ("2 8 6 4");
967  this->readFace ("7 1 3 5");
[3083]968  */
[3081]969
[3195]970  this->readFace ("1/1/1 2/2/2 4/4/3 3/3/4");
971  this->readFace ("3/3/5 4/4/6 6/6/7 5/5/8");
972  this->readFace ("5/5/9 6/6/10 8/8/11 7/7/12");
973  this->readFace ("7/7/13 8/8/14 2/10/15 1/9/16");
974  this->readFace ("2/2/17 8/11/18 6/12/19 4/4/20");
975  this->readFace ("7/13/21 1/1/22 3/3/23 5/14/24");
[3083]976
[2821]977}
Note: See TracBrowser for help on using the repository browser.