Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/object.cc @ 2816

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

orxonox/trunk: merged the ObjectImporter to trunk: merged with
svn merge branches/importer/src/ trunk/src/ -r 2794:HEAD

File size: 6.1 KB
Line 
1#include "object.h"
2
3int verbose = 1;
4Object::Object ()
5{
6
7  initialize();
8
9  importFile ("reaphigh.obj");
10
11  finalize();
12}
13
14Object::Object(char* fileName)
15{
16  initialize();
17
18  importFile (fileName);
19
20  finalize();
21}
22
23bool Object::importFile (char* fileName)
24{
25  if (verbose >=3)
26    printf("preparing to read in file: %s\n", fileName);   
27  objFileName = fileName;
28  this->readFromObjFile (objFileName);
29  return true;
30}
31
32bool Object::initialize (void)
33{
34  if (verbose >=3)
35    printf("new 3D-Object is being created\n"); 
36  faceMode = -1;
37  if ( (listNumber = glGenLists(1)) == 0 )
38    {
39      printf ("list could not be created for this Object\n");
40      return false;
41    }
42  glNewList (listNumber, GL_COMPILE);
43  glEnableClientState (GL_VERTEX_ARRAY);
44  //  glEnableClientState (GL_NORMAL_ARRAY);
45  //  glEnableClientState (GL_TEXTURE_COORD_ARRAY);
46
47  return true;
48}
49
50bool Object::finalize(void)
51{
52  if (verbose >=3)
53    printf("finalizing the 3D-Object\n"); 
54  glEndList();
55  return true;
56}
57
58void Object::draw (void)
59{
60  if (verbose >=3)
61    printf("drawing the 3D-Object\n"); 
62  glCallList (listNumber);
63}
64
65
66bool Object::readFromObjFile (char* fileName)
67{
68  OBJ_FILE = new ifstream (fileName);
69  if (!OBJ_FILE->is_open())
70    {
71      if (verbose >=1)
72        printf ("unable to open .OBJ file: %s\n", fileName);
73      return false;
74    }
75  objFileName = fileName;
76  char Buffer[500];
77  vertices = new Array();
78  normals = 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        //      printf ("verticeTangent found");
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    }
153
154  readVertices = false;
155  char subbuffer1[20];
156  char subbuffer2[20];
157  char subbuffer3[20];
158  char subbuffer4[20] ="";
159  sscanf (faceString, "%s %s %s %s", subbuffer1, subbuffer2, subbuffer3, subbuffer4);
160  if (!strcmp(subbuffer4, ""))
161    {
162      if (faceMode != 3)
163        {
164          if (faceMode != -1)
165            glEnd();
166          glBegin(GL_TRIANGLES);
167        }
168     
169      faceMode = 3;
170      if (verbose >=3)
171        printf ("found triag: %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3);
172      addGLElement(subbuffer1);
173      addGLElement(subbuffer2);
174      addGLElement(subbuffer3);
175      return true;
176    }
177  else
178    {
179      if (faceMode != 4)
180        {
181          if (faceMode != -1)
182            glEnd();
183          glBegin(GL_QUADS);
184        }
185      faceMode = 4;
186      if (verbose >=3 )
187        printf ("found quad: %s, %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3, subbuffer4);
188      addGLElement(subbuffer1);
189      addGLElement(subbuffer2);
190      addGLElement(subbuffer3);
191      addGLElement(subbuffer4);
192      return true;
193    }
194}
195
196bool Object::addGLElement (char* elementString)
197{
198  char* vertex = elementString;
199  char* texture;
200  char* normal;
201  texture = strstr (vertex, "/");
202  texture[0] = '\0';
203  texture ++;
204  normal = strstr (texture, "/");
205  normal[0] = '\0';
206  normal ++;
207  if (verbose >= 4)
208    printf ("importing grafical Element.... including to openGL\n");
209  glNormal3fv(normals->getArray() +(atoi(normal)-1)*3);
210  glArrayElement(atoi(vertex)-1); //  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.