Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: updated debug.h: now possibility to log per module (compile-time)

  1. write in the cc-file at the beginnig !!BEFORE ANY INCLUDES!!

#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_[MODULENAME]
where [MODULNAME] is a name of a module that can be defined in debug.h

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