Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/importer: added heavy logging

File size: 6.0 KB
Line 
1#include "object.h"
2
3Object::Object ()
4{
5
6  initialize();
7
8  importFile ("reaphigh.obj");
9
10  finalize();
11}
12
13Object::Object(char* fileName)
14{
15  initialize();
16
17  importFile (fileName);
18
19  finalize();
20}
21
22bool Object::importFile (char* fileName)
23{
24  if (verbose >=3)
25    printf("preparing to read in file: %s\n", fileName);   
26  objFileName = fileName;
27  this->readFromObjFile (objFileName);
28  return true;
29}
30
31bool Object::initialize (void)
32{
33  if (verbose >=3)
34    printf("new 3D-Object is being created\n"); 
35  faceMode = -1;
36  if ( (listNumber = glGenLists(1)) == 0 )
37    {
38      printf ("list could not be created for this Object\n");
39      return false;
40    }
41  glNewList (listNumber, GL_COMPILE);
42  glEnableClientState (GL_VERTEX_ARRAY);
43  glEnableClientState (GL_NORMAL_ARRAY);
44  //  glEnableClientState (GL_TEXTURE_COORD_ARRAY);
45
46  return true;
47}
48
49bool Object::finalize(void)
50{
51  if (verbose >=3)
52    printf("finalizing the 3D-Object\n"); 
53  glEndList();
54  return true;
55}
56
57void Object::draw (void)
58{
59  if (verbose >=3)
60    printf("drawing the 3D-Object\n"); 
61  glCallList (listNumber);
62}
63
64
65bool Object::readFromObjFile (char* fileName)
66{
67  OBJ_FILE = new ifstream (fileName);
68  if (!OBJ_FILE->is_open())
69    {
70      if (verbose >=1)
71        printf ("unable to open .OBJ file: %s\n", fileName);
72      return false;
73    }
74  objFileName = fileName;
75  char Buffer[500];
76  vertices = new Array();
77  normals = new Array();
78  while(!OBJ_FILE->eof())
79    {
80      OBJ_FILE->getline(Buffer, 500);
81      if (verbose >=4)
82        printf ("Read input line: %s\n",Buffer);
83     
84
85      // case vertice
86      if (!strncmp(Buffer, "v ", 2))
87        {
88          readVertex(Buffer+2);
89        }
90
91      // case face
92      else if (!strncmp(Buffer, "f ", 2))
93        {
94          readFace (Buffer+2);
95        }
96     
97      else if (!strncmp(Buffer, "mtllib", 6))
98        {
99          readMtlLib (Buffer+7);
100        }
101
102      else if (!strncmp(Buffer, "usemtl", 6))
103        {
104          readUseMtl (Buffer+7);
105        }
106
107      // case VertexNormal
108      else if (!strncmp(Buffer, "vn ", 2))
109      {
110        readVertexNormal(Buffer+3);
111      }
112
113      // case vt
114      else if (!strncmp(Buffer, "vt ", 2))
115      {
116        //      printf ("verticeTangent found");
117      }
118         
119
120    }
121 
122
123 
124  OBJ_FILE->close();
125  glEnd();
126 
127}
128
129
130bool Object::readVertex (char* vertexString)
131{
132  readVertices = true;
133  char subbuffer1[20];
134  char subbuffer2[20];
135  char subbuffer3[20];
136  sscanf (vertexString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3);
137  if (verbose >= 3)
138    printf ("reading in a vertex: %s %s %s\n", subbuffer1, subbuffer2, subbuffer3);
139  vertices->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3));
140  return true;
141}
142
143bool Object::readFace (char* faceString)
144{
145  if (readVertices == true)
146    {
147      vertices->finalizeArray();
148      glVertexPointer(3, GL_FLOAT, 0, vertices->getArray());
149      normals->finalizeArray();
150      glNormalPointer(GL_FLOAT, 0, normals->getArray());
151    }
152
153  readVertices = false;
154  char subbuffer1[20];
155  char subbuffer2[20];
156  char subbuffer3[20];
157  char subbuffer4[20] ="";
158  sscanf (faceString, "%s %s %s %s", subbuffer1, subbuffer2, subbuffer3, subbuffer4);
159  if (!strcmp(subbuffer4, ""))
160    {
161      if (faceMode != 3)
162        {
163          if (faceMode != -1)
164            glEnd();
165          glBegin(GL_TRIANGLES);
166        }
167     
168      faceMode = 3;
169      if (verbose >=3)
170        printf ("found triag: %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3);
171      addGLElement(subbuffer1);
172      addGLElement(subbuffer2);
173      addGLElement(subbuffer3);
174      return true;
175    }
176  else
177    {
178      if (faceMode != 4)
179        {
180          if (faceMode != -1)
181            glEnd();
182          glBegin(GL_QUADS);
183        }
184      faceMode = 4;
185      if (verbose >=3 )
186        printf ("found quad: %s, %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3, subbuffer4);
187      addGLElement(subbuffer1);
188      addGLElement(subbuffer2);
189      addGLElement(subbuffer3);
190      addGLElement(subbuffer4);
191      return true;
192    }
193}
194
195bool Object::addGLElement (char* elementString)
196{
197  char* vertex = elementString;
198  char* texture;
199  char* normal;
200  texture = strstr (vertex, "/");
201  texture[0] = '\0';
202  texture ++;
203  normal = strstr (texture, "/");
204  normal[0] = '\0';
205  normal ++;
206  if (verbose >= 4)
207    printf ("importing grafical Element.... including to openGL\n");
208  //glArrayElement(atoi(vertex)-1);
209  glNormal3fv(normals->getArray() +(atoi(normal)-1)*3);
210  glVertex3fv(vertices->getArray() +(atoi(vertex)-1)*3);
211
212}
213
214bool Object::readVertexNormal (char* normalString)
215{
216  readVertices = true;
217  char subbuffer1[20];
218  char subbuffer2[20];
219  char subbuffer3[20];
220  sscanf (normalString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3);
221  if (verbose >=3 )
222    printf("found vertex-Normal %s, %s, %s\n", subbuffer1,subbuffer2,subbuffer3);
223  normals->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3));
224  return true;
225}
226
227bool Object::readMtlLib (char* mtlFile)
228{
229  MTL_FILE = new ifstream (mtlFile);
230  if (!MTL_FILE->is_open())
231    {
232      if (verbose >= 1)
233        printf ("unable to open file: %s\n", mtlFile);
234      return false;
235    }
236  mtlFileName = mtlFile;
237  if (verbose >=2)
238    printf ("Opening mtlFile: %s\n", mtlFileName);
239  char Buffer[500];
240  vertices = new Array();
241  material = new Material();
242  Material* tmpMat = material;
243  while(!MTL_FILE->eof())
244    {
245      MTL_FILE->getline(Buffer, 500);
246      if (verbose >= 4)
247        printf("found line in mtlFile: %s\n", Buffer);
248     
249
250      // create new Material
251      if (!strncmp(Buffer, "newmtl ", 2))
252        {
253          tmpMat = tmpMat->addMaterial(Buffer+7);
254          //      printf ("%s, %p\n", tmpMat->getName(), tmpMat);
255        }
256      // setting a illumMode
257      else if (!strncmp(Buffer, "illum", 5))
258        {
259          tmpMat->setIllum(Buffer+6);
260
261        }
262      // setting Diffuse Color
263      else if (!strncmp(Buffer, "Kd", 2))
264        {
265          tmpMat->setDiffuse(Buffer+3);
266        }
267      // setting Ambient Color
268      else if (!strncmp(Buffer, "Ka", 2))
269        {
270          tmpMat->setAmbient(Buffer+3);
271        }
272      // setting Specular Color
273      else if (!strncmp(Buffer, "Ks", 2))
274        {
275          tmpMat->setSpecular(Buffer+3);
276        }
277    }
278  return true;
279}
280
281bool Object::readUseMtl (char* matString)
282{
283  if (faceMode != -1)
284    glEnd();
285  faceMode = 0;
286  if (verbose >= 2)
287    printf ("using material %s for coming Faces.\n", matString);
288  material->search(matString)->select();
289}
290
291
Note: See TracBrowser for help on using the repository browser.