Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/importer/src/object.cc @ 2808

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

orxonox/branches/importer: importer includes into the source

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