Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk/importer: added TexCoord to readIn

File size: 6.5 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  vTexture = new Array();
79  while(!OBJ_FILE->eof())
80    {
81      OBJ_FILE->getline(Buffer, 500);
82      if (verbose >=4)
83        printf ("Read input line: %s\n",Buffer);
84     
85
86      // case vertice
87      if (!strncmp(Buffer, "v ", 2))
88        {
89          readVertex(Buffer+2);
90        }
91
92      // case face
93      else if (!strncmp(Buffer, "f ", 2))
94        {
95          readFace (Buffer+2);
96        }
97     
98      else if (!strncmp(Buffer, "mtllib", 6))
99        {
100          readMtlLib (Buffer+7);
101        }
102
103      else if (!strncmp(Buffer, "usemtl", 6))
104        {
105          readUseMtl (Buffer+7);
106        }
107
108      // case VertexNormal
109      else if (!strncmp(Buffer, "vn ", 2))
110      {
111        readVertexNormal(Buffer+3);
112      }
113
114      // case vt
115      else if (!strncmp(Buffer, "vt ", 2))
116      {
117        readVertexTexture(Buffer+3);
118      }
119         
120
121    }
122 
123
124 
125  OBJ_FILE->close();
126  glEnd();
127 
128}
129
130
131bool Object::readVertex (char* vertexString)
132{
133  readVertices = true;
134  char subbuffer1[20];
135  char subbuffer2[20];
136  char subbuffer3[20];
137  sscanf (vertexString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3);
138  if (verbose >= 3)
139    printf ("reading in a vertex: %s %s %s\n", subbuffer1, subbuffer2, subbuffer3);
140  vertices->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3));
141  return true;
142}
143
144bool Object::readFace (char* faceString)
145{
146  if (readVertices == true)
147    {
148      vertices->finalizeArray();
149      glVertexPointer(3, GL_FLOAT, 0, vertices->getArray());
150      normals->finalizeArray();
151      glNormalPointer(GL_FLOAT, 0, normals->getArray());
152      vTexture->finalizeArray();
153    }
154
155  readVertices = false;
156  char subbuffer1[20];
157  char subbuffer2[20];
158  char subbuffer3[20];
159  char subbuffer4[20] ="";
160  sscanf (faceString, "%s %s %s %s", subbuffer1, subbuffer2, subbuffer3, subbuffer4);
161  if (!strcmp(subbuffer4, ""))
162    {
163      if (faceMode != 3)
164        {
165          if (faceMode != -1)
166            glEnd();
167          glBegin(GL_TRIANGLES);
168        }
169     
170      faceMode = 3;
171      if (verbose >=3)
172        printf ("found triag: %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3);
173      addGLElement(subbuffer1);
174      addGLElement(subbuffer2);
175      addGLElement(subbuffer3);
176      return true;
177    }
178  else
179    {
180      if (faceMode != 4)
181        {
182          if (faceMode != -1)
183            glEnd();
184          glBegin(GL_QUADS);
185        }
186      faceMode = 4;
187      if (verbose >=3 )
188        printf ("found quad: %s, %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3, subbuffer4);
189      addGLElement(subbuffer1);
190      addGLElement(subbuffer2);
191      addGLElement(subbuffer3);
192      addGLElement(subbuffer4);
193      return true;
194    }
195}
196
197bool Object::addGLElement (char* elementString)
198{
199  char* vertex = elementString;
200  char* texture;
201  char* normal;
202  texture = strstr (vertex, "/");
203  texture[0] = '\0';
204  texture ++;
205  normal = strstr (texture, "/");
206  normal[0] = '\0';
207  normal ++;
208  if (verbose >= 4)
209    printf ("importing grafical Element.... including to openGL\n");
210  //glArrayElement(atoi(vertex)-1);
211  glNormal3fv(normals->getArray() +(atoi(normal)-1)*3);
212  glTexCoord2fv(vTexture->getArray()+(atoi(texture)-1)*2);
213  glVertex3fv(vertices->getArray() +(atoi(vertex)-1)*3);
214
215}
216
217bool Object::readVertexNormal (char* normalString)
218{
219  readVertices = true;
220  char subbuffer1[20];
221  char subbuffer2[20];
222  char subbuffer3[20];
223  sscanf (normalString, "%s %s %s", subbuffer1, subbuffer2, subbuffer3);
224  if (verbose >=3 )
225    printf("found vertex-Normal %s, %s, %s\n", subbuffer1,subbuffer2,subbuffer3);
226  normals->addEntry(atof(subbuffer1), atof(subbuffer2), atof(subbuffer3));
227  return true;
228}
229
230bool Object::readVertexTexture (char* vTextureString)
231{
232  readVertices = true;
233  char subbuffer1[20];
234  char subbuffer2[20];
235  sscanf (vTextureString, "%s %s", subbuffer1, subbuffer2);
236  if (verbose >=3 )
237    printf("found vertex-Texture %s, %s\n", subbuffer1,subbuffer2);
238  vTexture->addEntry(atof(subbuffer1));
239  vTexture->addEntry(atof(subbuffer2));
240  return true;
241}
242
243
244bool Object::readMtlLib (char* mtlFile)
245{
246  MTL_FILE = new ifstream (mtlFile);
247  if (!MTL_FILE->is_open())
248    {
249      if (verbose >= 1)
250        printf ("unable to open file: %s\n", mtlFile);
251      return false;
252    }
253  mtlFileName = mtlFile;
254  if (verbose >=2)
255    printf ("Opening mtlFile: %s\n", mtlFileName);
256  char Buffer[500];
257  vertices = new Array();
258  material = new Material();
259  Material* tmpMat = material;
260  while(!MTL_FILE->eof())
261    {
262      MTL_FILE->getline(Buffer, 500);
263      if (verbose >= 4)
264        printf("found line in mtlFile: %s\n", Buffer);
265     
266
267      // create new Material
268      if (!strncmp(Buffer, "newmtl ", 2))
269        {
270          tmpMat = tmpMat->addMaterial(Buffer+7);
271          //      printf ("%s, %p\n", tmpMat->getName(), tmpMat);
272        }
273      // setting a illumMode
274      else if (!strncmp(Buffer, "illum", 5))
275        {
276          tmpMat->setIllum(Buffer+6);
277
278        }
279      // setting Diffuse Color
280      else if (!strncmp(Buffer, "Kd", 2))
281        {
282          tmpMat->setDiffuse(Buffer+3);
283        }
284      // setting Ambient Color
285      else if (!strncmp(Buffer, "Ka", 2))
286        {
287          tmpMat->setAmbient(Buffer+3);
288        }
289      // setting Specular Color
290      else if (!strncmp(Buffer, "Ks", 2))
291        {
292          tmpMat->setSpecular(Buffer+3);
293        }
294    }
295  return true;
296}
297
298bool Object::readUseMtl (char* matString)
299{
300  if (faceMode != -1)
301    glEnd();
302  faceMode = 0;
303  if (verbose >= 2)
304    printf ("using material %s for coming Faces.\n", matString);
305  material->search(matString)->select();
306}
307
308
Note: See TracBrowser for help on using the repository browser.