Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: resource manager working, player uses it.

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    \brief Function to read in a mtl File.
191    \param mtlFile The .mtl file to read
192
193    This Function parses all Lines of an mtl File.
194    The reason for it not to be in the materials-class is,
195    that a material does not have to be able to read itself in from a File.
196
197*/
198bool OBJModel::readMtlLib (char* mtlFile)
199{
200  this->mtlFileName = new char [strlen(mtlFile)+1];
201  strcpy(this->mtlFileName, mtlFile);
202  char* fileName = new char [strlen(objPath) + strlen(this->mtlFileName)+1];
203  strcpy(fileName, this->objPath);
204  strcat(fileName, this->mtlFileName);
205 
206
207  PRINTF(4)("Opening mtlFile: %s\n", fileName);
208
209  ifstream* MTL_FILE = new ifstream (fileName);
210  if (MTL_FILE->fail())
211    {
212      PRINTF(2)("unable to open file: %s\n", fileName);
213      MTL_FILE->close();
214      delete []fileName;
215      delete MTL_FILE;
216      return false;
217    }
218  char Buffer[500];
219  Material* tmpMat = material;
220  while(!MTL_FILE->eof())
221    {
222      MTL_FILE->getline(Buffer, 500);
223      PRINTF(5)("found line in mtlFile: %s\n", Buffer);
224     
225
226      // create new Material
227      if (!strncmp(Buffer, "newmtl ", 7))
228        {
229          tmpMat = tmpMat->addMaterial(Buffer+7);
230          //      PRINTF(2)("%s, %p\n", tmpMat->getName(), tmpMat);
231        }
232      // setting a illumMode
233      else if (!strncmp(Buffer, "illum ", 6))
234        {
235          tmpMat->setIllum(Buffer+6);
236
237        }
238      // setting Diffuse Color
239      else if (!strncmp(Buffer, "Kd ", 3))
240        {
241          tmpMat->setDiffuse(Buffer+3);
242        }
243      // setting Ambient Color
244      else if (!strncmp(Buffer, "Ka ", 3))
245        {
246          tmpMat->setAmbient(Buffer+3);
247        }
248      // setting Specular Color
249      else if (!strncmp(Buffer, "Ks ", 3))
250        {
251          tmpMat->setSpecular(Buffer+3);
252        }
253      // setting The Specular Shininess
254      else if (!strncmp(Buffer, "Ns ", 3))
255        {
256          tmpMat->setShininess(Buffer+3);
257        }
258      // setting up transparency
259      else if (!strncmp(Buffer, "d ", 2))
260        {
261          tmpMat->setTransparency(Buffer+2);
262        }
263      else if (!strncmp(Buffer, "Tf ", 3))
264        {
265          tmpMat->setTransparency(Buffer+3);
266        }
267     
268      else if (!strncmp(Buffer, "map_Kd ", 7))
269        {
270          tmpMat->setDiffuseMap(Buffer+7);
271        }
272      else if (!strncmp(Buffer, "map_Ka ", 7))
273        {
274          tmpMat->setAmbientMap(Buffer+7);
275        }
276      else if (!strncmp(Buffer, "map_Ks ", 7))
277        {
278          tmpMat->setSpecularMap(Buffer+7);
279        }
280      else if (!strncmp(Buffer, "bump ", 5))
281        {
282          tmpMat->setBump(Buffer+7);
283        }
284     
285
286    }
287  MTL_FILE->close();
288  delete []fileName;
289  delete MTL_FILE;
290  return true;
291}
Note: See TracBrowser for help on using the repository browser.