Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/graphics/importer/model.cc @ 3915

Last change on this file since 3915 was 3915, checked in by bensch, 19 years ago

orxonox/trunk: material is now only dependent on tList<Material>* in model

File size: 23.7 KB
Line 
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
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER
17
18#include "model.h"
19
20#include <math.h>
21#include <stdarg.h>
22
23#include "array.h"
24#include "vector.h"
25#include "list.h"
26
27using namespace std;
28
29//////////////////
30// DE-CONSTRUCT //
31//////////////////
32/**
33   \brief Creates a 3D-Model. and assigns it a Name.
34*/
35Model::Model(const char* modelName)
36{
37  PRINTF(4)("new 3D-Model is being created\n"); 
38  this->name = NULL;
39  this->setName(modelName);
40
41
42  this->finalized = false;
43  // setting the start group;
44  this->firstGroup = new Group;
45  this->currentGroup = this->firstGroup;
46  this->groupCount = 0;
47 
48  this->initGroup (this->currentGroup);
49  this->scaleFactor = 1;
50
51  this->vertices = new Array();
52  this->vTexture = new Array();
53  this->normals = new Array();
54
55  this->materialList = new tList<Material>;
56}
57
58/**
59   \brief deletes an Model.
60
61   Looks if any from model allocated space is still in use, and if so deleted it.
62*/
63Model::~Model(void)
64{
65  PRINTF(4)("Deleting Model ");
66  if (this->name)
67    {
68      PRINT(4)("%s\n", this->name);
69      delete []this->name;
70    }
71  else
72      PRINT(4)("\n");
73
74  PRINTF(5)("Deleting display Lists.\n");
75  Group* walker = this->firstGroup;
76  while (walker != NULL)
77    {
78      glDeleteLists (walker->listNumber, 1);
79      Group* delWalker = walker;
80      walker = walker->next;
81      delete delWalker;
82    }
83
84  // deleting the MaterialList
85  PRINTF(5)("Deleting Materials.\n");
86
87  tIterator<Material>* tmpIt = this->materialList->getIterator();
88  Material* material = tmpIt->nextElement();
89  while(material)
90    {
91      delete material;
92      material = tmpIt->nextElement();
93    }
94  delete tmpIt;
95  delete materialList;
96}
97
98/**
99   \brief Finalizes an Object. This can be done outside of the Class.
100*/
101void Model::finalize(void)
102{
103  this->importToGL ();
104 
105  this->cleanup();
106
107  this->finalized = true;
108}
109
110//////////
111// DRAW //
112//////////
113/**
114   \brief Draws the Models of all Groups.
115   It does this by just calling the Lists that must have been created earlier.
116*/
117void Model::draw (void) const
118{
119  PRINTF(4)("drawing the 3D-Models\n"); 
120  Group* walker = this->firstGroup;
121  while (walker != NULL)
122    {
123      PRINTF(5)("Drawing model %s\n", walker->name);
124      glCallList (walker->listNumber);
125      walker = walker->next;
126    }
127}
128
129/**
130   \brief Draws the Model number groupNumber
131   \param groupNumber The number of the group that will be displayed.
132
133   It does this by just calling the List that must have been created earlier.
134*/
135void Model::draw (int groupNumber) const 
136{
137  if (groupNumber >= this->groupCount)
138    {
139      PRINTF(2)("You requested model number %i, but this File only contains of %i Models.\n", groupNumber-1, this->groupCount);
140      return;
141    }
142  PRINTF(4)("drawing the requested 3D-Models if found.\n"); 
143  Group* walker = this->firstGroup;
144  int counter = 0;
145  while (walker != NULL)
146    {
147      if (counter == groupNumber)
148        {
149          PRINTF(4)("Drawing model number %i named %s\n", counter, walker->name);
150          glCallList (walker->listNumber);
151          return;
152        }
153      ++counter;
154      walker = walker->next;
155    }
156  PRINTF(2)("Model number %i in %s not Found.\n", groupNumber, this->name);
157  return;
158
159}
160
161/**
162   \brief Draws the Model with a specific groupName
163   \param groupName The name of the group that will be displayed.
164
165   It does this by just calling the List that must have been created earlier.
166*/
167void Model::draw (char* groupName) const
168{
169  PRINTF(4)("drawing the requested 3D-Models if found.\n"); 
170  Group* walker = this->firstGroup;
171  while (walker != NULL)
172    {
173      if (!strcmp(walker->name, groupName))
174        {
175          PRINTF(4)("Drawing model %s\n", walker->name);
176          glCallList (walker->listNumber);
177          return;
178        }
179      walker = walker->next;
180    }
181  PRINTF(2)("Model Named %s in %s not Found.\n", groupName, this->name);
182  return;
183}
184
185/**
186   \returns Count of the Models in this File
187*/
188int Model::getGroupCount (void) const
189{
190  return this->groupCount;
191}
192
193//////////
194// INIT //
195//////////
196/**
197   \brief sets a name to the Model
198   \param name The name to set to this Model
199*/
200void Model::setName(const char* name)
201{
202  if (this->name) 
203    delete this->name;
204  if (name)
205    {
206      this->name = new char[strlen(name)+1];
207      strcpy(this->name, name);
208    }
209  else 
210    this->name = NULL;
211}
212
213/**
214   \brief initializes a new Group model
215   \param group the group that should be initialized.
216   \todo Maybe Group should be a Class, because it does a lot of stuff
217   
218*/
219bool Model::initGroup(Group* group)
220{
221  PRINTF(4)("Adding new Group\n");
222  group->name = "";
223  group->faceMode = -1;
224  group->faceCount = 0; 
225  group->next = NULL;
226
227  group->firstFace = new Face;
228  this->initFace (group->firstFace);
229  group->currentFace = group->firstFace;
230}
231
232/**
233   \brief initializes a new Face. (sets default Values)
234   \param face The face to initialize
235*/
236bool Model::initFace (Face* face)
237{
238  face->vertexCount = 0;
239
240  face->firstElem = NULL;
241 
242  face->material = NULL;
243 
244  face->next = NULL;
245
246  return true;
247}
248
249/**
250   \brief finalizes an Model.
251   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.
252*/
253bool Model::cleanup(void)
254{
255  PRINTF(4)("cleaning up the 3D-Model to save Memory.\n");
256
257  if (this->vertices)
258    delete this->vertices;
259  if (this->vTexture)
260    delete this->vTexture;
261  if (this->normals)
262    delete this->normals;
263
264  this->cleanupGroup(this->firstGroup);
265  return true; 
266}
267
268/**
269   \brief Cleans up all groups starting from group.
270   \param group the first Group to clean
271*/
272bool Model::cleanupGroup (Group* group)
273{
274  PRINTF(5)("Cleaning up group\n");
275  if (group->firstFace != NULL)
276    {
277      cleanupFace (group->firstFace);
278      delete group->firstFace;
279    }
280
281  if (group->next !=NULL)
282    cleanupGroup (group->next);
283  return true;
284}
285
286/**
287   \brief Cleans up all Faces starting from face until NULL is reached.
288   \param face the first face to clean.
289*/
290bool Model::cleanupFace (Face* face)
291{
292  PRINTF(5)("Cleaning up Face\n");
293
294  if (face->firstElem != NULL)
295    {
296      this->cleanupFaceElement(face->firstElem);
297      delete face->firstElem;
298    }
299     
300  if (face->next != NULL)
301    {
302      this->cleanupFace (face->next);
303      delete face->next;
304    }
305}
306
307/**
308   \brief Cleans up all FaceElements starting from faceElem.
309   \param faceElem the first FaceElement to clean.
310*/
311bool Model::cleanupFaceElement(FaceElement* faceElem)
312{
313  if (faceElem->next != NULL)
314    {
315      this->cleanupFaceElement (faceElem->next);
316      delete faceElem->next;
317    }
318}
319
320//////////
321// MESH //
322//////////
323/**
324   \brief adds a new Material to the Material List
325   \param material the Material to add
326   \returns the added material
327
328   !! beware the Material will be deleted when the Model gets deleted
329*/
330Material* Model::addMaterial(Material* material)
331{
332  this->materialList->add(material);
333  return material;
334}
335
336/**
337   \brief adds a new Material to the Material List
338   \param material the name of the Material to add
339   \returns the added material
340*/
341Material* Model::addMaterial(const char* materialName)
342{
343 
344  Material* newMat = new Material();
345  newMat->setName(materialName);
346
347  // adding material to the List of materials
348  this->materialList->add(newMat); 
349  return newMat;
350}
351
352/**
353   \brief finds a Material by its name and returns it
354   \param materialName the Name of the material to search for.
355   \returns the Material if found, NULL otherwise
356*/
357Material* Model::findMaterialByName(const char* materialName)
358{
359  tIterator<Material>* tmpIt = this->materialList->getIterator();
360  Material* material = tmpIt->nextElement();
361  while(material)
362    {
363      if (!strcmp(material->getName(), materialName))
364        {
365          delete tmpIt;
366          return material;
367        }
368      material = tmpIt->nextElement();
369    }
370  delete tmpIt;
371  return NULL;
372}
373
374/**
375   \brief parses a group String
376   \param groupString the new Group to create
377
378   This function initializes a new Group.
379   With it you should be able to import .obj-files with more than one Models inside.
380*/
381bool Model::addGroup (const char* groupString)
382{
383  PRINTF(5)("Read Group: %s.\n", groupString);
384  if (this->groupCount != 0 && this->currentGroup->faceCount>0)
385    {
386      //      finalizeGroup(currentGroup);
387      this->currentGroup = this->currentGroup->next = new Group;
388      this->initGroup(this->currentGroup);
389    }
390  // setting the group name if not default.
391  if (strcmp(groupString, "default"))
392    {
393      this->currentGroup->name = new char [strlen(groupString)+1];
394      strcpy(this->currentGroup->name, groupString);
395    }
396  ++this->groupCount;
397}
398
399/**
400   \brief parses a vertex-String
401   \param vertexString The String that will be parsed.
402
403   If a vertex line is found this function will inject it into the vertex-Array
404*/
405bool Model::addVertex (const char* vertexString)
406{
407  float subbuffer1;
408  float subbuffer2;
409  float subbuffer3;
410  sscanf (vertexString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3);
411  PRINTF(5)("reading in a vertex: %f %f %f\n", &subbuffer1, &subbuffer2, &subbuffer3);
412  this->vertices->addEntry(subbuffer1*scaleFactor, subbuffer2*scaleFactor, subbuffer3*scaleFactor);
413  return true;
414}
415
416/**
417   \brief parses a vertex-String
418   \param x the X-coordinate of the Vertex to add.
419   \param y the Y-coordinate of the Vertex to add.
420   \param z the Z-coordinate of the Vertex to add.
421   
422*/
423bool Model::addVertex(float x, float y, float z)
424{
425  PRINTF(5)("reading in a vertex: %f %f %f\n", x, y, z);
426  this->vertices->addEntry(x*scaleFactor, y*scaleFactor, z*scaleFactor);
427  return true;
428}
429
430/**
431   \brief parses a vertexNormal-String
432   \param normalString The String that will be parsed.
433
434   If a vertexNormal line is found this function will inject it into the vertexNormal-Array
435*/
436bool Model::addVertexNormal (const char* normalString)
437{
438  float subbuffer1;
439  float subbuffer2;
440  float subbuffer3;
441  sscanf (normalString, "%f %f %f", &subbuffer1, &subbuffer2, &subbuffer3);
442  PRINTF(5)("found vertex-Normal %f, %f, %f\n", &subbuffer1,&subbuffer2,&subbuffer3);
443  this->normals->addEntry(subbuffer1, subbuffer2, subbuffer3);
444  return true;
445}
446
447/**
448   \brief adds a VertexNormal.
449   \param x The x coordinate of the Normal.
450   \param y The y coordinate of the Normal.
451   \param z The z coordinate of the Normal.
452
453   If a vertexNormal line is found this function will inject it into the vertexNormal-Array
454*/
455bool Model::addVertexNormal(float x, float y, float z)
456{
457  PRINTF(5)("found vertex-Normal %f, %f, %f\n", x, y, z);
458  this->normals->addEntry(x, y, z);
459}
460
461/**
462   \brief parses a vertexTextureCoordinate-String
463   \param vTextureString The String that will be parsed.
464
465   If a vertexTextureCoordinate line is found,
466   this function will inject it into the vertexTexture-Array
467*/
468bool Model::addVertexTexture (const char* vTextureString)
469{
470  float subbuffer1;
471  float subbuffer2;
472  sscanf (vTextureString, "%f %f", &subbuffer1, &subbuffer2);
473  PRINTF(5)("found vertex-Texture %f, %f\n", &subbuffer1, &subbuffer2);
474  this->vTexture->addEntry(subbuffer1);
475  this->vTexture->addEntry(subbuffer2);
476  return true;
477}
478
479/**
480   \brief adds a Texture Coordinate
481   \param u The u coordinate of the TextureCoordinate.
482   \param v The y coordinate of the TextureCoordinate.
483
484   If a TextureCoordinate line is found this function will inject it into the TextureCoordinate-Array
485*/
486bool Model::addVertexTexture(float u, float v)
487{
488  PRINTF(5)("found vertex-Texture %f, %f\n", u, v);
489  this->vTexture->addEntry(u);
490  this->vTexture->addEntry(v);
491}
492
493/**
494   \brief parses a face-string
495   \param faceString The String that will be parsed.
496
497   If a face line is found this function will add it to the glList.
498*/
499bool Model::addFace (const char* faceString)
500{
501  if (this->currentGroup->faceCount >0)
502    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
503  this->initFace (this->currentGroup->currentFace);
504
505  FaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new FaceElement;
506  tmpElem->next = NULL;
507  while(strcmp (faceString, "\0"))
508    {
509      if (this->currentGroup->currentFace->vertexCount>0)
510          tmpElem = tmpElem->next = new FaceElement;
511      tmpElem->next = NULL;
512
513      char tmpValue [50];
514      int tmpLen;
515      char* vertex = NULL;
516      char* texture = NULL;
517      char* normal = NULL;
518
519      sscanf (faceString, "%s", tmpValue);
520      tmpLen = strlen(tmpValue);
521      vertex = tmpValue;
522
523      if ((texture = strstr (vertex, "/")) != NULL)
524        {
525          texture[0] = '\0';
526          texture ++;
527         
528          if ((normal = strstr (texture, "/")) !=NULL)
529            {
530              normal[0] = '\0';
531              normal ++;
532            }     
533        }
534      if (vertex)
535        tmpElem->vertexNumber = atoi(vertex)-1;
536      else
537        tmpElem->vertexNumber = -1;
538      if (texture)
539        tmpElem->texCoordNumber = atoi(texture)-1;
540      else
541        tmpElem->texCoordNumber = -1;
542      if (normal)
543        tmpElem->normalNumber = atoi(normal)-1;
544      else
545        tmpElem->normalNumber = -1;
546
547      faceString += tmpLen;
548      if (strcmp (faceString, "\0"))
549        faceString++;
550      this->currentGroup->currentFace->vertexCount++;
551    }
552
553  this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount -2;
554}
555
556/**
557   \brief adds a new Face
558   \param faceElemCount the number of Vertices to add to the Face.
559   \param type 0: vertex only, 1: vertex and normal, 2: vertex and Texture, 3 vertex, normal and texture
560*/
561bool Model::addFace(int faceElemCount, VERTEX_FORMAT type, ...)
562{
563   if (this->currentGroup->faceCount > 0)
564    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
565  this->initFace (this->currentGroup->currentFace);
566
567  FaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new FaceElement;
568  tmpElem->next = NULL;
569 
570  va_list itemlist;
571  va_start (itemlist, type);
572
573  for (int i = 0; i < faceElemCount; i++)
574    {
575      if (this->currentGroup->currentFace->vertexCount>0)
576          tmpElem = tmpElem->next = new FaceElement;
577      tmpElem->next = NULL;
578
579      tmpElem->vertexNumber = va_arg (itemlist, int) -1;
580      if (type & TEXCOORD)
581        tmpElem->texCoordNumber = va_arg (itemlist, int) -1;
582      if (type & NORMAL)
583        tmpElem->normalNumber = va_arg(itemlist, int) -1;
584      this->currentGroup->currentFace->vertexCount++;
585    }
586  va_end(itemlist);
587
588  this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount - 2;
589}
590
591/**
592   \brief Function that selects a material, if changed in the obj file.
593   \param matString the Material that will be set.
594*/
595bool Model::setMaterial(const char* matString)
596{
597  if (this->currentGroup->faceCount > 0)
598    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
599  this->initFace (this->currentGroup->currentFace);
600 
601  this->currentGroup->currentFace->material = this->findMaterialByName(matString);
602
603  if (this->currentGroup->faceCount == 0)
604    this->currentGroup->faceCount ++;
605}
606
607/**
608   \brief Function that selects a material, if changed in the obj file.
609   \param mtl the Material that will be set.
610*/
611bool Model::setMaterial(Material* mtl)
612{
613  if (this->currentGroup->faceCount > 0)
614    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
615  this->initFace (this->currentGroup->currentFace);
616 
617  this->currentGroup->currentFace->material = mtl;
618
619  if (this->currentGroup->faceCount == 0)
620    this->currentGroup->faceCount ++;
621}
622
623/**
624   \brief A routine that is able to create normals.
625
626   The algorithm does the following:
627   1. It calculates creates Vectors for each normale, and sets them to zero.
628   2. It then Walks through a) all the Groups b) all the Faces c) all the FaceElements
629   3. It searches for a points two neighbours per Face, takes Vecotrs to them calculates FaceNormals and adds it to the Points Normal.
630   4. It goes through all the normale-Points and calculates the VertexNormale and includes it in the normals-Array.
631*/
632bool Model::buildVertexNormals ()
633{ 
634  PRINTF(4)("Normals are being calculated.\n");
635
636  Vector* normArray = new Vector [vertices->getCount()/3];
637  for (int i=0; i<vertices->getCount()/3;i++)
638    normArray[i] = Vector(.0,.0,.0);
639 
640  int firstTouch;
641  int secondTouch;
642  Vector prevV;
643  Vector nextV;
644  Vector curV;
645
646  Group* tmpGroup = firstGroup;
647  while (tmpGroup)
648    {
649      Face* tmpFace = tmpGroup->firstFace;
650      while (tmpFace)
651        {
652          if (tmpFace->firstElem)
653            {
654              FaceElement* firstElem = tmpFace->firstElem;
655              FaceElement* prevElem;
656              FaceElement* curElem = firstElem;
657              FaceElement* nextElem;
658              FaceElement* lastElem;
659              // 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.
660              while (curElem)
661                {
662                  prevElem = curElem;
663                  curElem = curElem->next;
664                }
665              lastElem = prevElem;
666             
667              curElem = firstElem;
668              for (int j=0; j<tmpFace->vertexCount; j++)
669                {
670                  if (!(nextElem = curElem->next))
671                    nextElem = firstElem;
672                  curElem->normalNumber = curElem->vertexNumber;
673                 
674                  curV = Vector (vertices->getArray()[curElem->vertexNumber*3], vertices->getArray()[curElem->vertexNumber*3+1], vertices->getArray()[curElem->vertexNumber*3+2]);
675                  prevV = Vector (vertices->getArray()[prevElem->vertexNumber*3], vertices->getArray()[prevElem->vertexNumber*3+1], vertices->getArray()[prevElem->vertexNumber*3+2]) - curV;
676                  nextV = Vector (vertices->getArray()[nextElem->vertexNumber*3], vertices->getArray()[nextElem->vertexNumber*3+1], vertices->getArray()[nextElem->vertexNumber*3+2]) - curV;
677                  normArray[curElem->vertexNumber] = normArray[curElem->vertexNumber] + nextV.cross(prevV);
678
679                  prevElem = curElem;
680                  curElem = curElem->next;
681                }
682            }
683          tmpFace = tmpFace->next;
684        }
685      tmpGroup = tmpGroup->next;
686    }
687
688  for (int i=0; i<vertices->getCount()/3;i++)
689    {
690      normArray[i].normalize();
691      PRINTF(5)("Found Normale number %d: (%f; %f, %f).\n", i, normArray[i].x, normArray[i].y, normArray[i].z);
692     
693      this->normals->addEntry(normArray[i].x, normArray[i].y, normArray[i].z);
694
695    }
696  delete []normArray; 
697}
698
699////////////
700// openGL //
701////////////
702/**
703   \brief reads and includes the Faces/Materials into the openGL state Machine
704*/
705bool Model::importToGL (void)
706{
707
708  // finalize the Arrays
709  this->vertices->finalizeArray();
710  this->vTexture->finalizeArray();
711  if (normals->getCount() == 0) // vertices-Array must be built for this
712    this->buildVertexNormals();
713  this->normals->finalizeArray();
714
715  this->currentGroup = this->firstGroup;
716
717  while (this->currentGroup != NULL)
718    {
719
720      // creating a glList for the Group
721      if ((this->currentGroup->listNumber = glGenLists(1)) == 0)
722        {
723          PRINTF(2)("glList could not be created for this Model\n");
724          return false;
725        }
726      glNewList (this->currentGroup->listNumber, GL_COMPILE);
727
728      // Putting Faces to GL
729      Face* tmpFace = this->currentGroup->firstFace;
730      while (tmpFace != NULL)
731        {
732          if (tmpFace->vertexCount == 0 && tmpFace->material != NULL)
733            {
734              if (this->currentGroup->faceMode != -1)
735                glEnd();
736              this->currentGroup->faceMode = 0;
737              Material* tmpMat;
738              if (tmpFace->material != NULL)
739                {
740                  tmpFace->material->select();
741                  PRINTF(5)("using material %s for coming Faces.\n", tmpFace->material->getName());
742                }
743            }
744
745          else if (tmpFace->vertexCount == 3)
746            {
747              if (this->currentGroup->faceMode != 3)
748                {
749                  if (this->currentGroup->faceMode != -1)
750                    glEnd();
751                  glBegin(GL_TRIANGLES);
752                }
753             
754              this->currentGroup->faceMode = 3;
755              PRINTF(5)("found triag.\n");
756            }
757         
758          else if (tmpFace->vertexCount == 4)
759            {
760              if (this->currentGroup->faceMode != 4)
761                {
762                  if (this->currentGroup->faceMode != -1)
763                    glEnd();
764                  glBegin(GL_QUADS);
765                }
766              this->currentGroup->faceMode = 4;
767              PRINTF(5)("found quad.\n");
768            }
769         
770          else if (tmpFace->vertexCount > 4)
771            {
772              if (this->currentGroup->faceMode != -1)
773                glEnd();
774              glBegin(GL_POLYGON);
775              PRINTF(5)("Polygon with %i faces found.", tmpFace->vertexCount);
776              this->currentGroup->faceMode = tmpFace->vertexCount;
777            }
778         
779          FaceElement* tmpElem = tmpFace->firstElem;
780          while (tmpElem != NULL)
781            {
782              //      PRINTF(2)("%s\n", tmpElem->value);
783              this->addGLElement(tmpElem);
784              tmpElem = tmpElem->next;
785            }
786          tmpFace = tmpFace->next;
787        }
788      glEnd();
789      glEndList();
790
791      this->currentGroup = this->currentGroup->next;
792    } 
793}
794
795/**
796   \brief Adds a Face-element (one vertex of a face) with all its information.
797   \param elem The FaceElement to add to the OpenGL-environment.
798
799   It does this by searching:
800   1. The Vertex itself
801   2. The VertexNormale
802   3. The VertexTextureCoordinate
803   merging this information, the face will be drawn.
804*/
805bool Model::addGLElement (FaceElement* elem)
806{
807  PRINTF(5)("importing grafical Element to openGL.\n");
808
809  if (elem->texCoordNumber != -1)
810    glTexCoord2fv(this->vTexture->getArray() + elem->texCoordNumber * 2);
811  if (elem->normalNumber != -1)
812    glNormal3fv(this->normals->getArray() + elem->normalNumber * 3);
813  if (elem->vertexNumber != -1)
814    glVertex3fv(this->vertices->getArray() + elem->vertexNumber * 3);
815
816}
817
818/**
819   \brief Includes a default model
820
821   This will inject a Cube, because this is the most basic model.
822*/
823void Model::cubeModel(void)
824{
825  this->addVertex (-0.5, -0.5, 0.5);
826  this->addVertex (0.5, -0.5, 0.5);
827  this->addVertex (-0.5, 0.5, 0.5);
828  this->addVertex (0.5, 0.5, 0.5);
829  this->addVertex (-0.5, 0.5, -0.5);
830  this->addVertex (0.5, 0.5, -0.5);
831  this->addVertex (-0.5, -0.5, -0.5);
832  this->addVertex (0.5, -0.5, -0.5);
833
834  this->addVertexTexture (0.0, 0.0);
835  this->addVertexTexture (1.0, 0.0);
836  this->addVertexTexture (0.0, 1.0);
837  this->addVertexTexture (1.0, 1.0);
838  this->addVertexTexture (0.0, 2.0);
839  this->addVertexTexture (1.0, 2.0);
840  this->addVertexTexture (0.0, 3.0);
841  this->addVertexTexture (1.0, 3.0);
842  this->addVertexTexture (0.0, 4.0);
843  this->addVertexTexture (1.0, 4.0);
844  this->addVertexTexture (2.0, 0.0);
845  this->addVertexTexture (2.0, 1.0);
846  this->addVertexTexture (-1.0, 0.0);
847  this->addVertexTexture (-1.0, 1.0);
848
849  this->addVertexNormal (0.0, 0.0, 1.0);
850  this->addVertexNormal (0.0, 0.0, 1.0);
851  this->addVertexNormal (0.0, 0.0, 1.0);
852  this->addVertexNormal (0.0, 0.0, 1.0);
853  this->addVertexNormal (0.0, 1.0, 0.0);
854  this->addVertexNormal (0.0, 1.0, 0.0);
855  this->addVertexNormal (0.0, 1.0, 0.0);
856  this->addVertexNormal (0.0, 1.0, 0.0);
857  this->addVertexNormal (0.0, 0.0, -1.0);
858  this->addVertexNormal (0.0, 0.0, -1.0);
859  this->addVertexNormal (0.0, 0.0, -1.0);
860  this->addVertexNormal (0.0, 0.0, -1.0);
861  this->addVertexNormal (0.0, -1.0, 0.0);
862  this->addVertexNormal (0.0, -1.0, 0.0);
863  this->addVertexNormal (0.0, -1.0, 0.0);
864  this->addVertexNormal (0.0, -1.0, 0.0);
865  this->addVertexNormal (1.0, 0.0, 0.0);
866  this->addVertexNormal (1.0, 0.0, 0.0);
867  this->addVertexNormal (1.0, 0.0, 0.0);
868  this->addVertexNormal (1.0, 0.0, 0.0);
869  this->addVertexNormal (-1.0, 0.0, 0.0);
870  this->addVertexNormal (-1.0, 0.0, 0.0);
871  this->addVertexNormal (-1.0, 0.0, 0.0);
872  this->addVertexNormal (-1.0, 0.0, 0.0);
873
874  /* normaleLess-testingMode
875  this->addFace ("1 2 4 3");
876  this->addFace ("3 4 6 5");
877  this->addFace ("5 6 8 7");
878  this->addFace ("7 8 2 1");
879  this->addFace ("2 8 6 4");
880  this->addFace ("7 1 3 5");
881  */
882
883  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,1,1, 2,2,2, 4,4,3, 3,3,4);
884  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 3,3,5, 4,4,6, 6,6,7, 5,5,8);
885  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 5,5,9, 6,6,10, 8,8,11, 7,7,12);
886  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 7,7,13, 8,8,14, 2,10,15, 1,9,16);
887  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,2,17, 8,11,18, 6,12,19, 4,4,20);
888  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 7,13,21, 1,1,22, 3,3,23, 5,14,24);
889
890}
Note: See TracBrowser for help on using the repository browser.