Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/graphics/importer/objModel.cc @ 3549

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

orxonox/trunk: debug information in importer set like they should be

File size: 6.9 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 "objModel.h"
17
18#include <fstream>
19
20#include "debug.h"
21
22/**
23   \brief Crates a 3D-Model and loads in a File.
24   \param fileName file to parse and load (must be a .obj file)
25*/
26OBJModel::OBJModel(char* fileName)
27{
28  this->initializeOBJ();
29
30  this->importFile (fileName);
31
32  this->importToGL ();
33
34  this->cleanup();
35}
36
37/**
38   \brief Crates a 3D-Model, loads in a File and scales it.
39   \param fileName file to parse and load (must be a .obj file)
40   \param scaling The factor that the model will be scaled with.
41*/
42OBJModel::OBJModel(char* fileName, float scaling)
43{
44  this->initializeOBJ();
45  this->scaleFactor = scaling;
46
47  this->importFile (fileName);
48
49  this->importToGL ();
50
51  this->cleanup();
52}
53
54/**
55   \brief deletes an OBJModel.
56
57   Looks if any from model allocated space is still in use, and if so deleted it.
58*/
59OBJModel::~OBJModel()
60{
61  PRINTF(4)("Deleting the obj-names\n");
62  if (this->objPath)
63    delete []this->objPath;
64  if (this->objFileName)
65    delete []this->objFileName;
66  if (this->mtlFileName)
67    delete []this->mtlFileName;
68}
69
70/**
71   \brief Initializes an obj-model
72*/
73void OBJModel::initializeOBJ(void)
74{
75  this->objPath = NULL;
76  this->objFileName = NULL;
77  this->mtlFileName = NULL;
78
79  this->initialize();
80}
81
82/**
83   \brief Imports a obj file and handles the the relative location
84   \param fileName The file to import
85*/
86bool OBJModel::importFile (char* fileName)
87{
88  PRINTF(4)("preparing to read in file: %s\n", fileName);
89
90
91#ifdef __WIN32__
92  // win32 path reading
93  char pathSplitter= '\\';
94#else /* __WIN32__ */
95  // unix path reading
96  char pathSplitter='/';
97#endif /* __WIN32__ */
98  char* tmpName = fileName;
99  if (tmpName[0] == pathSplitter)
100    tmpName++;
101  char* name = tmpName;
102  while (( tmpName = strchr (tmpName+1, pathSplitter)))
103    {
104      name = tmpName+1;
105    }
106  this->objPath = new char[name-fileName+1];
107  strncpy(this->objPath, fileName, name-fileName);
108  this->objPath[name-fileName] = '\0';
109  if (strlen(objPath)> 0)
110    PRINTF(5)("Resolved file %s to folder: %s.\n", name, objPath);
111  else
112    PRINTF(5)("Resolved file %s.\n", name);
113 
114  this->setName(name);
115  if (this->material)
116    this->material->addTexturePath(this->objPath);
117  this->objFileName = new char[strlen(name)+1];
118  strcpy (this->objFileName, name);
119  this->readFromObjFile ();
120  return true;
121}
122
123/**
124   \brief Reads in the .obj File and sets all the Values.
125   This function does read the file, parses it for the occurence of things like vertices, faces and so on, and executes the specific tasks
126*/
127bool OBJModel::readFromObjFile (void)
128{
129  char* fileName = new char [strlen(objPath)+strlen(objFileName)+1];
130  if (this->objFileName != NULL && !strcmp(this->objFileName, ""))
131    return false;
132  strcpy(fileName, this->objPath);
133  strcat(fileName, this->objFileName);
134
135  ifstream* OBJ_FILE = new ifstream(fileName);
136  if (OBJ_FILE->fail())
137    {
138      PRINTF(1)("unable to open .OBJ file: %s\n Loading cube-Model instead.\n", fileName);
139      cubeModel();
140      OBJ_FILE->close();
141      delete []fileName;
142      delete OBJ_FILE;
143      return false;
144    }
145  PRINTF(4)("Reading from opened file %s\n", fileName);
146  char Buffer[10000];
147  while(!OBJ_FILE->eof())
148    {
149      OBJ_FILE->getline(Buffer, 10000);
150      PRINTF(5)("Read input line: %s\n", Buffer);
151     
152
153      // case vertice
154      if (!strncmp(Buffer, "v ", 2))
155        {
156          this->addVertex(Buffer+2);
157        }
158
159      // case face
160      else if (!strncmp(Buffer, "f ", 2))
161        {
162          this->addFace (Buffer+2);
163        }
164     
165      else if (!strncmp(Buffer, "mtllib ", 7))
166        {
167          this->readMtlLib (Buffer+7);
168        }
169
170      else if (!strncmp(Buffer, "usemtl ", 7))
171        {
172          this->addUseMtl (Buffer+7);
173        }
174
175      // case VertexNormal
176      else if (!strncmp(Buffer, "vn ", 3))
177        {
178          this->addVertexNormal(Buffer+3);
179        }
180     
181      // case VertexTextureCoordinate
182      else if (!strncmp(Buffer, "vt ", 3))
183        {
184          this->addVertexTexture(Buffer+3);
185        }
186      // case group
187      else if (!strncmp(Buffer, "g ", 2))
188        {
189          this->addGroup (Buffer+2);
190        }
191      else if (!strncmp(Buffer, "s ", 2)) //! \todo smoothing groups have to be implemented
192        {
193          PRINTF(3)("smoothing groups not supportet yet. line: %s\n", Buffer);
194        }
195    }
196  OBJ_FILE->close();
197  delete OBJ_FILE;
198  delete []fileName;
199  return true;
200
201}
202
203/**
204    \brief Function to read in a mtl File.
205    \param mtlFile The .mtl file to read
206
207    This Function parses all Lines of an mtl File.
208    The reason for it not to be in the materials-class is,
209    that a material does not have to be able to read itself in from a File.
210
211*/
212bool OBJModel::readMtlLib (char* mtlFile)
213{
214  this->mtlFileName = new char [strlen(mtlFile)+1];
215  strcpy(this->mtlFileName, mtlFile);
216  char* fileName = new char [strlen(objPath) + strlen(this->mtlFileName)+1];
217  strcpy(fileName, this->objPath);
218  strcat(fileName, this->mtlFileName);
219 
220
221  PRINTF(4)("Opening mtlFile: %s\n", fileName);
222
223  ifstream* MTL_FILE = new ifstream (fileName);
224  if (MTL_FILE->fail())
225    {
226      PRINTF(2)("unable to open file: %s\n", fileName);
227      MTL_FILE->close();
228      delete []fileName;
229      delete MTL_FILE;
230      return false;
231    }
232  char Buffer[500];
233  Material* tmpMat = material;
234  while(!MTL_FILE->eof())
235    {
236      MTL_FILE->getline(Buffer, 500);
237      PRINTF(5)("found line in mtlFile: %s\n", Buffer);
238     
239
240      // create new Material
241      if (!strncmp(Buffer, "newmtl ", 7))
242        {
243          tmpMat = tmpMat->addMaterial(Buffer+7);
244          //      PRINTF(2)("%s, %p\n", tmpMat->getName(), tmpMat);
245        }
246      // setting a illumMode
247      else if (!strncmp(Buffer, "illum ", 6))
248        {
249          tmpMat->setIllum(Buffer+6);
250
251        }
252      // setting Diffuse Color
253      else if (!strncmp(Buffer, "Kd ", 3))
254        {
255          tmpMat->setDiffuse(Buffer+3);
256        }
257      // setting Ambient Color
258      else if (!strncmp(Buffer, "Ka ", 3))
259        {
260          tmpMat->setAmbient(Buffer+3);
261        }
262      // setting Specular Color
263      else if (!strncmp(Buffer, "Ks ", 3))
264        {
265          tmpMat->setSpecular(Buffer+3);
266        }
267      // setting The Specular Shininess
268      else if (!strncmp(Buffer, "Ns ", 3))
269        {
270          tmpMat->setShininess(Buffer+3);
271        }
272      // setting up transparency
273      else if (!strncmp(Buffer, "d ", 2))
274        {
275          tmpMat->setTransparency(Buffer+2);
276        }
277      else if (!strncmp(Buffer, "Tf ", 3))
278        {
279          tmpMat->setTransparency(Buffer+3);
280        }
281     
282      else if (!strncmp(Buffer, "map_Kd ", 7))
283        {
284          tmpMat->setDiffuseMap(Buffer+7);
285        }
286      else if (!strncmp(Buffer, "map_Ka ", 7))
287        {
288          tmpMat->setAmbientMap(Buffer+7);
289        }
290      else if (!strncmp(Buffer, "map_Ks ", 7))
291        {
292          tmpMat->setSpecularMap(Buffer+7);
293        }
294      else if (!strncmp(Buffer, "bump ", 5))
295        {
296          tmpMat->setBump(Buffer+7);
297        }
298     
299
300    }
301  MTL_FILE->close();
302  delete []fileName;
303  delete MTL_FILE;
304  return true;
305}
Note: See TracBrowser for help on using the repository browser.