Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3655 was 3655, checked in by bensch, 19 years ago

orxonox/trunk: moved protoclass to folder proto
added protosingleton
added resourceManager
modiefied some stuff to work better

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