Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/importer/model.cc @ 3458

Last change on this file since 3458 was 3454, checked in by bensch, 20 years ago

orxonox/trunk/importer: doxygen-tags

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