Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/importer/object.cc @ 2988

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

orxonox/trunk/importer: default of Group→next set to NULL as it should be. Thanks to Adrian to ddd me to the error :)

File size: 18.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 "object.h"
17
18/**
19   \brief Creates a 3D-Object, but does not load any 3D-models
20   pretty useless
21*/
22Object::Object ()
23{
24
25  initialize();
26
27  BoxObject();
28
29  finalize();
30}
31
32/**
33   \brief Crates a 3D-Object and loads in a File
34   \param fileName file to parse and load (must be a .obj file)
35*/
36Object::Object(char* fileName)
37{
38  initialize();
39
40  importFile (fileName);
41
42  finalize();
43}
44
45/**
46   \brief Crates a 3D-Object, loads in a File and scales it.
47   \param fileName file to parse and load (must be a .obj file)
48   \param scaling The factor that the object will be scaled with.
49*/
50Object::Object(char* fileName, float scaling)
51{
52  initialize();
53  scaleFactor = scaling;
54
55  importFile (fileName);
56
57  finalize();
58}
59
60/**
61   \brief deletes an Object
62*/
63Object::~Object()
64{
65  if (verbose >= 2)
66    printf ("Deleting display List.\n");
67  Group* walker = firstGroup;
68  while (walker != NULL)
69    {
70      glDeleteLists (walker->listNumber, 1);
71      Group* lastWalker = walker;
72      walker = walker->nextGroup;
73      delete lastWalker;
74    } 
75}
76
77/**
78    \brief initializes the Object
79    This Function initializes all the needed arrays, Lists and clientStates
80*/
81bool Object::initialize (void)
82{
83  if (verbose >=3)
84    printf("new 3D-Object is being created\n"); 
85
86  // setting the start group;
87  firstGroup = new Group;
88  currentGroup = firstGroup;
89  groupCount = 0;
90 
91  initGroup (firstGroup);
92  mtlFileName = "";
93  scaleFactor = 1;
94  material = new Material();
95
96  //  glEnableClientState (GL_VERTEX_ARRAY);
97  //  glEnableClientState (GL_NORMAL_ARRAY);
98  //  glEnableClientState (GL_TEXTURE_COORD_ARRAY);
99
100
101  return true;
102}
103
104/**
105   \brief Imports a obj file and handles the the relative location
106   \param fileName The file to import
107*/
108bool Object::importFile (char* fileName)
109{
110  if (verbose >=3)
111    printf("preparing to read in file: %s\n", fileName);   
112  objFileName = fileName;
113  this->readFromObjFile (objFileName);
114  return true;
115}
116
117/**
118  \brief finalizes an Object.
119   This funcion is needed, to close the glList and all the other lists.
120*/
121bool Object::finalize(void)
122{
123  //  if (verbose >=3)
124    printf("finalizing the 3D-Object\n"); 
125  finalizeGroup (currentGroup);
126  if (material != NULL)
127    delete material;
128  return true;
129}
130
131/**
132   \brief Draws the Objects of all Groups.
133   It does this by just calling the Lists that must have been created earlier.
134*/
135void Object::draw (void)
136{
137  if (verbose >=2)
138    printf("drawing the 3D-Objects\n"); 
139  Group* walker = firstGroup;
140  while (walker != NULL)
141    {
142      if (verbose >= 3)
143        printf ("Drawing object %s\n", walker->name);
144      glCallList (walker->listNumber);
145      walker = walker->nextGroup;
146    }
147}
148
149/**
150   \brief Draws the Object number groupNumber
151   It does this by just calling the List that must have been created earlier.
152   \param groupNumber The number of the group that will be displayed.
153*/
154void Object::draw (int groupNumber)
155{
156  if (groupNumber >= groupCount)
157    {
158      if (verbose>=2)
159        printf ("You requested object number %i, but this File only contains of %i Objects.\n", groupNumber-1, groupCount);
160      return;
161    }
162  if (verbose >=2)
163    printf("drawing the requested 3D-Objects if found.\n"); 
164  Group* walker = firstGroup;
165  int counter = 0;
166  while (walker != NULL)
167    {
168      if (counter == groupNumber)
169        {
170          if (verbose >= 2)
171            printf ("Drawing object number %s named %s\n", counter, walker->name);
172          glCallList (walker->listNumber);
173          return;
174        }
175      ++counter;
176      walker = walker->nextGroup;
177    }
178  if (verbose >= 2)
179    printf("Object number %i in %s not Found.\n", groupNumber, objFileName);
180  return;
181
182}
183
184/**
185   \brief Draws the Object with a specific groupname
186   It does this by just calling the List that must have been created earlier.
187   \param groupName The name of the group that will be displayed.
188*/
189void Object::draw (char* groupName)
190{
191  if (verbose >=2)
192    printf("drawing the requested 3D-Objects if found.\n"); 
193  Group* walker = firstGroup;
194  while (walker != NULL)
195    {
196      if (!strcmp(walker->name, groupName))
197        {
198          if (verbose >= 2)
199            printf ("Drawing object %s\n", walker->name);
200          glCallList (walker->listNumber);
201          return;
202        }
203      walker = walker->nextGroup;
204    }
205  if (verbose >= 2)
206    printf("Object Named %s in %s not Found.\n", groupName, objFileName);
207  return;
208}
209
210/**
211   \returns Count of the Objects in this File
212*/
213int Object::getGroupCount (void)
214{
215  return groupCount;
216}
217
218/**
219   \brief initializes a new Group object
220*/
221bool Object::initGroup(Group* group)
222{
223  if (verbose >= 2)
224    printf("Adding new Group\n");
225  group->name = "";
226  group->faceMode = -1;
227  group->faceCount = 0; 
228  group->nextGroup = NULL;
229  if ((group->listNumber = glGenLists(1)) == 0 )
230    {
231      printf ("list could not be created for this Object\n");
232      return false;
233    }
234 
235  if (groupCount == 0)
236    {
237      group->firstVertex = 0;
238      group->firstNormal = 0;
239      group->firstVertexTexture = 0;
240    }
241  else
242    {
243      group->firstVertex = currentGroup->firstVertex + currentGroup->vertices->getCount()/3;
244      group->firstNormal = currentGroup->firstNormal + currentGroup->normals->getCount()/3;
245      group->firstVertexTexture = currentGroup->firstVertexTexture + currentGroup->vTexture->getCount()/2;
246    }
247  if (verbose >=2)
248    printf ("Creating new Arrays, with starting points v:%i, vt:%i, vn:%i .\n", group->firstVertex, group->firstVertexTexture, group->firstNormal);
249  group->vertices = new Array();
250  group->normals = new Array();
251  group->vTexture = new Array();
252
253  glNewList (group->listNumber, GL_COMPILE);
254}
255
256/**
257   \brief finalizes a Group.
258   \param group the group to finalize.
259*/
260bool Object::finalizeGroup(Group* group)
261{
262  if (verbose >=2)
263    printf ("Finalize group %s.\n", group->name);
264  glEnd();
265  glEndList();
266}
267
268/**
269   \brief deletes the Arrays of the Group to save space.
270   \param group the group to delete the arrays from.
271*/
272bool Object::cleanupGroup(Group* group)
273{
274  if (verbose >=2)
275    printf ("cleaning up group %s.\n", group->name);
276 
277  delete group->vertices;
278  delete group->normals;
279  delete group->vTexture;
280}
281
282/**
283   \brief Reads in the .obj File and sets all the Values.
284   This function does read the file, parses it for the occurence of things like vertices, faces and so on, and executes the specific tasks
285   \param fileName the File that will be parsed (.obj-file)
286*/
287bool Object::readFromObjFile (char* fileName)
288{
289  OBJ_FILE = new ifstream(fileName);
290  if (!OBJ_FILE->is_open())
291    {
292      if (verbose >=1)
293        printf ("unable to open .OBJ file: %s\n Loading Box Object instead.\n", fileName);
294      BoxObject();
295      return false;
296    }
297  objFileName = fileName;
298  char Buffer[10000];
299  while(!OBJ_FILE->eof())
300    {
301      OBJ_FILE->getline(Buffer, 10000);
302      if (verbose >=4)
303        printf ("Read input line: %s\n",Buffer);
304     
305
306      // case vertice
307      if (!strncmp(Buffer, "v ", 2))
308        {
309          readVertex(Buffer+2);
310        }
311
312      // case face
313      else if (!strncmp(Buffer, "f ", 2))
314        {
315          readFace (Buffer+2);
316        }
317     
318      else if (!strncmp(Buffer, "mtllib", 6))
319        {
320          readMtlLib (Buffer+7);
321        }
322
323      else if (!strncmp(Buffer, "usemtl", 6))
324        {
325          readUseMtl (Buffer+7);
326        }
327
328      // case VertexNormal
329      else if (!strncmp(Buffer, "vn ", 2))
330      {
331        readVertexNormal(Buffer+3);
332      }
333
334      // case VertexTextureCoordinate
335      else if (!strncmp(Buffer, "vt ", 2))
336      {
337        readVertexTexture(Buffer+3);
338      }
339      // case group
340      else if (!strncmp(Buffer, "g", 1))
341        {
342          readGroup (Buffer+2);
343        }
344    }
345  OBJ_FILE->close();
346  return true;
347
348}
349
350/**
351   \brief parses a vertex-String
352   If a vertex line is found this function will inject it into the vertex-Array
353   \param vertexString The String that will be parsed.
354*/
355bool Object::readVertex (char* vertexString)
356{
357  readingVertices = true;
358  char subbuffer1[20];
359  char subbuffer2[20];
360  char subbuffer3[20];
361  sscanf (vertexString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3);
362  if (verbose >= 3)
363    printf ("reading in a vertex: %s %s %s\n", subbuffer1, subbuffer2, subbuffer3);
364  currentGroup->vertices->addEntry(atof(subbuffer1)*scaleFactor, atof(subbuffer2)*scaleFactor, atof(subbuffer3)*scaleFactor);
365  return true;
366}
367
368/**
369   \brief parses a face-string
370   If a face line is found this function will add it to the glList.
371   The function makes a difference between QUADS and TRIANGLES, and will if changed re-open, set and re-close the gl-processe.
372   \param faceString The String that will be parsed.
373*/
374bool Object::readFace (char* faceString)
375{
376  // finalize the Arrays;
377  if (readingVertices == true)
378    {
379      currentGroup->vertices->finalizeArray();
380      //      glVertexPointer(3, GL_FLOAT, 0, currentGroup->vertices->getArray());
381      currentGroup->normals->finalizeArray();
382      //      glNormalPointer(GL_FLOAT, 0, currentGroup->normals->getArray());
383      currentGroup->vTexture->finalizeArray();
384    }
385
386  readingVertices = false;
387  currentGroup->faceCount++;
388
389  int elemCount = 0;
390 
391  FaceElement* firstElem = new FaceElement;
392  FaceElement* tmpElem = firstElem;
393
394 
395  while(strcmp (faceString, "\0"))
396    {
397      if (elemCount>0)
398          tmpElem = tmpElem->next = new FaceElement;
399      tmpElem->next = NULL;
400
401
402      sscanf (faceString, "%s", tmpElem->value);
403      faceString += strlen(tmpElem->value);
404      if (strcmp (faceString, "\0"))
405        faceString++;
406      elemCount++;
407
408
409    }
410 
411 
412  if (elemCount == 3)
413    {
414      if (currentGroup->faceMode != 3)
415        {
416          if (currentGroup->faceMode != -1)
417            glEnd();
418          glBegin(GL_TRIANGLES);
419        }
420     
421      currentGroup->faceMode = 3;
422      if (verbose >=3)
423        printf ("found triag.\n");
424    }
425 
426  else if (elemCount == 4)
427    {
428      if (currentGroup->faceMode != 4)
429        {
430          if (currentGroup->faceMode != -1)
431            glEnd();
432          glBegin(GL_QUADS);
433        }
434      currentGroup->faceMode = 4;
435      if (verbose >=3 )
436        printf ("found quad.\n");
437    }
438 
439  else if (elemCount > 4)
440    {
441      if (currentGroup->faceMode != -1)
442        glEnd();
443      glBegin(GL_POLYGON);
444      if (verbose >=3)
445        printf ("Polygon with %i faces found.", elemCount);
446      currentGroup->faceMode = elemCount;
447    }
448
449  tmpElem = firstElem;
450  FaceElement* delElem;
451  while (tmpElem != NULL)
452    {
453      //      printf ("%s\n", tmpElem->value);
454      addGLElement(tmpElem->value);
455      delElem = tmpElem;
456      tmpElem = tmpElem->next;
457      delete delElem;
458    }
459
460}
461
462/**
463   \brief Adds a Face-element (one vertex of a face) with all its information.
464   It does this by searching:
465   1. The Vertex itself
466   2. The VertexNormale
467   3. The VertexTextureCoordinate
468   merging this information, the face will be drawn.
469
470*/
471bool Object::addGLElement (char* elementString)
472{
473  if (verbose >=3)
474    printf ("importing grafical Element to openGL\n");
475  char* vertex = elementString;
476
477  char* texture;
478  if ((texture = strstr (vertex, "/")) != NULL)
479    {
480      texture[0] = '\0';
481      texture ++;
482      if (verbose>=3)
483        printf ("includeing texture #%i, and mapping it to group texture #%i, textureArray has %i entries.\n", atoi(texture), (atoi(texture)-1 - currentGroup->firstVertexTexture)*2, currentGroup->vTexture->getCount());
484      glTexCoord2fv(currentGroup->vTexture->getArray()+(atoi(texture)-1 - currentGroup->firstVertexTexture)*2);
485
486      char* normal;
487      if ((normal = strstr (texture, "/")) !=NULL)
488        {
489          normal[0] = '\0';
490          normal ++;
491          //glArrayElement(atoi(vertex)-1);
492          glNormal3fv(currentGroup->normals->getArray() +(atoi(normal)-1 - currentGroup->firstNormal)*3);
493        }
494    }
495  if (verbose>=3)
496    printf ("includeing vertex #%i, and mapping it to group vertex #%i, vertexArray has %i entries.\n", atoi(vertex), (atoi(vertex)-1 - currentGroup->firstVertex)*3, currentGroup->vertices->getCount());
497  glVertex3fv(currentGroup->vertices->getArray() +(atoi(vertex)-1 - currentGroup->firstVertex)*3);
498
499}
500
501/**
502   \brief parses a vertexNormal-String
503   If a vertexNormal line is found this function will inject it into the vertexNormal-Array
504   \param normalString The String that will be parsed.
505*/
506bool Object::readVertexNormal (char* normalString)
507{
508  readingVertices = true;
509  char subbuffer1[20];
510  char subbuffer2[20];
511  char subbuffer3[20];
512  sscanf (normalString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3);
513  if (verbose >=3 )
514    printf("found vertex-Normal %s, %s, %s\n", subbuffer1,subbuffer2,subbuffer3);
515  currentGroup->normals->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3));
516  return true;
517}
518
519/**
520   \brief parses a vertexTextureCoordinate-String
521   If a vertexTextureCoordinate line is found this function will inject it into the vertexTexture-Array
522   \param vTextureString The String that will be parsed.
523*/
524bool Object::readVertexTexture (char* vTextureString)
525{
526  readingVertices = true;
527  char subbuffer1[20];
528  char subbuffer2[20];
529  sscanf (vTextureString, "%s %s", subbuffer1, subbuffer2);
530  if (verbose >=3 )
531    printf("found vertex-Texture %s, %s\n", subbuffer1,subbuffer2);
532  currentGroup->vTexture->addEntry(atof(subbuffer1));
533  currentGroup->vTexture->addEntry(atof(subbuffer2));
534  return true;
535}
536
537/**
538   \brief parses a group String
539   This function initializes a new Group.
540   With it you should be able to import .obj-files with more than one Objects inside.
541   \param groupString the new Group to create
542*/
543bool Object::readGroup (char* groupString)
544{
545  // setting the group name if not default.
546  if (strcmp(currentGroup->name, "default"))
547    {
548      currentGroup->name = (char*) malloc ( strlen(groupString) * sizeof (char));
549      strcpy(currentGroup->name, groupString);
550    }
551  if (groupCount != 0 && currentGroup->faceCount>0)
552    {
553      Group* newGroup = new Group;
554      finalizeGroup(currentGroup);
555      currentGroup->nextGroup = newGroup;
556      initGroup(newGroup);
557      cleanupGroup(currentGroup); // deletes the arrays of the group; must be after initGroup.
558      currentGroup = newGroup; // must be after init see initGroup for more info
559    }
560
561  ++groupCount;
562
563}
564
565/**
566    \brief Function to read in a mtl File.
567    this Function parses all Lines of an mtl File
568    \param mtlFile The .mtl file to read
569*/
570bool Object::readMtlLib (char* mtlFile)
571{
572  MTL_FILE = new ifstream (mtlFile);
573  if (!MTL_FILE->is_open())
574    {
575      if (verbose >= 1)
576        printf ("unable to open file: %s\n", mtlFile);
577      return false;
578    }
579  mtlFileName = mtlFile;
580  if (verbose >=2)
581    printf ("Opening mtlFile: %s\n", mtlFileName);
582  char Buffer[500];
583  Material* tmpMat = material;
584  while(!MTL_FILE->eof())
585    {
586      MTL_FILE->getline(Buffer, 500);
587      if (verbose >= 4)
588        printf("found line in mtlFile: %s\n", Buffer);
589     
590
591      // create new Material
592      if (!strncmp(Buffer, "newmtl ", 2))
593        {
594          tmpMat = tmpMat->addMaterial(Buffer+7);
595          //      printf ("%s, %p\n", tmpMat->getName(), tmpMat);
596        }
597      // setting a illumMode
598      else if (!strncmp(Buffer, "illum", 5))
599        {
600          tmpMat->setIllum(Buffer+6);
601
602        }
603      // setting Diffuse Color
604      else if (!strncmp(Buffer, "Kd", 2))
605        {
606          tmpMat->setDiffuse(Buffer+3);
607        }
608      // setting Ambient Color
609      else if (!strncmp(Buffer, "Ka", 2))
610        {
611          tmpMat->setAmbient(Buffer+3);
612        }
613      // setting Specular Color
614      else if (!strncmp(Buffer, "Ks", 2))
615        {
616          tmpMat->setSpecular(Buffer+3);
617        }
618      // setting The Specular Shininess
619      else if (!strncmp(Buffer, "Ns", 2))
620        {
621          tmpMat->setShininess(Buffer+3);
622        }
623      // setting up transparency
624      else if (!strncmp(Buffer, "d", 1))
625        {
626          tmpMat->setTransparency(Buffer+2);
627        }
628      else if (!strncpy(Buffer, "Tf", 2))
629        {
630          tmpMat->setTransparency(Buffer+3);
631        }
632
633    }
634  return true;
635}
636
637/**
638   \brief Function that selects a material, if changed in the obj file.
639   \param matString the Material that will be set.
640*/
641bool Object::readUseMtl (char* matString)
642{
643  if (!strcmp (mtlFileName, ""))
644    {
645      if (verbose >= 1)
646        printf ("Not using new defined material, because no mtlFile found yet\n");
647      return false;
648    }
649     
650  if (currentGroup->faceMode != -1)
651    glEnd();
652  currentGroup->faceMode = 0;
653  if (verbose >= 2)
654    printf ("using material %s for coming Faces.\n", matString);
655  material->search(matString)->select();
656}
657
658/**
659   \brief Includes a default object
660   This will inject a Cube, because this is the most basic object.
661*/
662void Object::BoxObject(void)
663{
664  readVertex ("-0.500000 -0.500000 0.500000");
665  readVertex ("0.500000 -0.500000 0.500000");
666  readVertex ("-0.500000 0.500000 0.500000");
667  readVertex ("0.500000 0.500000 0.500000");
668  readVertex ("-0.500000 0.500000 -0.500000");
669  readVertex ("0.500000 0.500000 -0.500000");
670  readVertex ("-0.500000 -0.500000 -0.500000");
671  readVertex ("0.500000 -0.500000 -0.500000");
672
673  readVertexTexture ("0.000000 0.000000");
674  readVertexTexture ("1.000000 0.000000");
675  readVertexTexture ("0.000000 1.000000");
676  readVertexTexture ("1.000000 1.000000");
677  readVertexTexture ("0.000000 2.000000");
678  readVertexTexture ("1.000000 2.000000");
679  readVertexTexture ("0.000000 3.000000");
680  readVertexTexture ("1.000000 3.000000");
681  readVertexTexture ("0.000000 4.000000");
682  readVertexTexture ("1.000000 4.000000");
683  readVertexTexture ("2.000000 0.000000");
684  readVertexTexture ("2.000000 1.000000");
685  readVertexTexture ("-1.000000 0.000000");
686  readVertexTexture ("-1.000000 1.000000");
687 
688  readVertexNormal ("0.000000 0.000000 1.000000");
689  readVertexNormal ("0.000000 0.000000 1.000000");
690  readVertexNormal ("0.000000 0.000000 1.000000");
691  readVertexNormal ("0.000000 0.000000 1.000000");
692  readVertexNormal ("0.000000 1.000000 0.000000");
693  readVertexNormal ("0.000000 1.000000 0.000000");
694  readVertexNormal ("0.000000 1.000000 0.000000");
695  readVertexNormal ("0.000000 1.000000 0.000000");
696  readVertexNormal ("0.000000 0.000000 -1.000000");
697  readVertexNormal ("0.000000 0.000000 -1.000000");
698  readVertexNormal ("0.000000 0.000000 -1.000000");
699  readVertexNormal ("0.000000 0.000000 -1.000000");
700  readVertexNormal ("0.000000 -1.000000 0.000000");
701  readVertexNormal ("0.000000 -1.000000 0.000000");
702  readVertexNormal ("0.000000 -1.000000 0.000000");
703  readVertexNormal ("0.000000 -1.000000 0.000000");
704  readVertexNormal ("1.000000 0.000000 0.000000");
705  readVertexNormal ("1.000000 0.000000 0.000000");
706  readVertexNormal ("1.000000 0.000000 0.000000");
707  readVertexNormal ("1.000000 0.000000 0.000000");
708  readVertexNormal ("-1.000000 0.000000 0.000000");
709  readVertexNormal ("-1.000000 0.000000 0.000000");
710  readVertexNormal ("-1.000000 0.000000 0.000000");
711  readVertexNormal ("-1.000000 0.000000 0.000000");
712
713  readFace ("1/1/1 2/2/2 4/4/3 3/3/4");
714  readFace ("3/3/5 4/4/6 6/6/7 5/5/8");
715  readFace ("5/5/9 6/6/10 8/8/11 7/7/12");
716  readFace ("7/7/13 8/8/14 2/10/15 1/9/16");
717  readFace ("2/2/17 8/11/18 6/12/19 4/4/20");
718  readFace ("7/13/21 1/1/22 3/3/23 5/14/24");
719}
Note: See TracBrowser for help on using the repository browser.