Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/importer/objModel.cc @ 3397

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

orxonox/trunk: importer: now name is io, the output was wrong :)

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#include "objModel.h"
17
18/**
19   \brief Crates a 3D-Model and loads in a File.
20   \param fileName file to parse and load (must be a .obj file)
21*/
22OBJModel::OBJModel(char* fileName)
23{
24  this->initializeOBJ();
25
26  this->importFile (fileName);
27
28  this->importToGL ();
29
30  this->cleanup();
31}
32
33/**
34   \brief Crates a 3D-Model, loads in a File and scales it.
35   \param fileName file to parse and load (must be a .obj file)
36   \param scaling The factor that the model will be scaled with.
37*/
38OBJModel::OBJModel(char* fileName, float scaling)
39{
40  this->initializeOBJ();
41  this->scaleFactor = scaling;
42
43  this->importFile (fileName);
44
45  this->importToGL ();
46
47  this->cleanup();
48}
49
50/**
51   \brief deletes an OBJModel.
52
53   Looks if any from model allocated space is still in use, and if so deleted it.
54*/
55OBJModel::~OBJModel()
56{
57  PRINTF(4)("Deleting the obj-names\n");
58  if (this->objPath)
59    delete []this->objPath;
60  if (this->objFileName)
61    delete []this->objFileName;
62  if (this->mtlFileName)
63    delete []this->mtlFileName;
64}
65
66/**
67   \brief Initializes an obj-model
68*/
69void OBJModel::initializeOBJ(void)
70{
71  this->objPath = NULL;
72  this->objFileName = NULL;
73  this->mtlFileName = NULL;
74
75  this->initialize();
76}
77
78/**
79   \brief Imports a obj file and handles the the relative location
80   \param fileName The file to import
81*/
82bool OBJModel::importFile (char* fileName)
83{
84  PRINTF(3)("preparing to read in file: %s\n", fileName);
85
86
87#ifdef __WIN32__
88  // win32 path reading
89  char pathSplitter= '\\';
90#else /* __WIN32__ */
91  // unix path reading
92  char pathSplitter='/';
93#endif /* __WIN32__ */
94  char* tmpName = fileName;
95  if (tmpName[0] == pathSplitter)
96    tmpName++;
97  char* name = tmpName;
98  while (( tmpName = strchr (tmpName+1, pathSplitter)))
99    {
100      name = tmpName+1;
101    }
102  this->objPath = new char[name-fileName+1];
103  strncpy(this->objPath, fileName, name-fileName);
104  this->objPath[name-fileName] = '\0';
105  if (verbose>=2)
106    if (strlen(objPath)> 0)
107      PRINTF(0)("Resolved file %s to folder: %s.\n", name, objPath);
108    else
109      PRINTF(0)("Resolved file %s.\n", name);
110 
111  if (this->name) delete this->name;
112  this->name = new char[strlen(name)+1];
113  strcpy(this->name, name);
114  if (this->material)
115    this->material->addTexturePath(this->objPath);
116  this->objFileName = new char[strlen(name)+1];
117  strcpy (this->objFileName, name);
118  this->readFromObjFile ();
119  return true;
120}
121
122/**
123   \brief Reads in the .obj File and sets all the Values.
124   This function does read the file, parses it for the occurence of things like vertices, faces and so on, and executes the specific tasks
125*/
126bool OBJModel::readFromObjFile (void)
127{
128  char* fileName = new char [strlen(objPath)+strlen(objFileName)+1];
129  if (this->objFileName != NULL && !strcmp(this->objFileName, ""))
130    return false;
131  strcpy(fileName, this->objPath);
132  strcat(fileName, this->objFileName);
133
134  ifstream* OBJ_FILE = new ifstream(fileName);
135  if (OBJ_FILE->fail())
136    {
137      PRINTF(1)("unable to open .OBJ file: %s\n Loading Box Model instead.\n", fileName);
138      BoxModel();
139      OBJ_FILE->close();
140      delete []fileName;
141      delete OBJ_FILE;
142      return false;
143    }
144  PRINTF(2)("Reading from opened file %s\n", fileName);
145  char Buffer[10000];
146  while(!OBJ_FILE->eof())
147    {
148      OBJ_FILE->getline(Buffer, 10000);
149      PRINTF(3)("Read input line: %s\n", Buffer);
150     
151
152      // case vertice
153      if (!strncmp(Buffer, "v ", 2))
154        {
155          this->addVertex(Buffer+2);
156        }
157
158      // case face
159      else if (!strncmp(Buffer, "f ", 2))
160        {
161          this->addFace (Buffer+2);
162        }
163     
164      else if (!strncmp(Buffer, "mtllib ", 7))
165        {
166          this->readMtlLib (Buffer+7);
167        }
168
169      else if (!strncmp(Buffer, "usemtl ", 7))
170        {
171          this->addUseMtl (Buffer+7);
172        }
173
174      // case VertexNormal
175      else if (!strncmp(Buffer, "vn ", 3))
176        {
177          this->addVertexNormal(Buffer+3);
178        }
179     
180      // case VertexTextureCoordinate
181      else if (!strncmp(Buffer, "vt ", 3))
182        {
183          this->addVertexTexture(Buffer+3);
184        }
185      // case group
186      else if (!strncmp(Buffer, "g ", 2))
187        {
188          this->addGroup (Buffer+2);
189        }
190      else if (!strncmp(Buffer, "s ", 2)) //! \todo smoothing groups have to be implemented
191        {
192          if (verbose >= 2)
193            PRINTF(2)("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(3)("Opening mtlFile: %s\n", fileName);
222
223  ifstream* MTL_FILE = new ifstream (fileName);
224  if (MTL_FILE->fail())
225    {
226      PRINTF(1)("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(4)("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.