Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: importer: functionality improvement

File size: 6.5 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 <stdio.h>
21#include <string.h>
22#include <stdlib.h>
23
24#define PARSELINELENGTH 8192
25
26#include "debug.h"
27
28/**
29   \brief Crates a 3D-Model, loads in a File and scales it.
30   \param fileName file to parse and load (must be a .obj file)
31   \param scaling The factor that the model will be scaled with.
32*/
33OBJModel::OBJModel(char* fileName, float scaling)
34{
35  this->initializeOBJ();
36  this->scaleFactor = scaling;
37
38  this->importFile (fileName);
39
40  this->finalize();
41}
42
43/**
44   \brief deletes an OBJModel.
45
46   Looks if any from model allocated space is still in use, and if so deleted it.
47*/
48OBJModel::~OBJModel()
49{
50  PRINTF(4)("Deleting the obj-names\n");
51  if (this->objPath)
52    delete []this->objPath;
53  if (this->objFileName)
54    delete []this->objFileName;
55  if (this->mtlFileName)
56    delete []this->mtlFileName;
57}
58
59/**
60   \brief Initializes an obj-model
61*/
62void OBJModel::initializeOBJ(void)
63{
64  this->objPath = NULL;
65  this->objFileName = NULL;
66  this->mtlFileName = NULL;
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  FILE* stream;
123  if( (stream = fopen (fileName, "r")) == NULL)
124    {
125      printf("IniParser could not open %s\n", fileName);
126      return false;
127    }
128
129  char buffer[PARSELINELENGTH];
130  while(fgets(buffer, PARSELINELENGTH, stream))
131    {
132      // line termiated with \0 not \n
133      if (buffer[strlen(buffer)-1] == '\n')
134        buffer[strlen(buffer)-1] = '\0';
135
136      // case vertice
137      if (!strncmp(buffer, "v ", 2))
138        {
139          this->addVertex(buffer+2);
140        }
141
142      // case face
143      else if (!strncmp(buffer, "f ", 2))
144        {
145          this->addFace (buffer+2);
146        }
147     
148      else if (!strncmp(buffer, "mtllib ", 7))
149        {
150          this->readMtlLib (buffer+7);
151        }
152
153      else if (!strncmp(buffer, "usemtl ", 7))
154        {
155          this->setMaterial (buffer+7);
156        }
157
158      // case VertexNormal
159      else if (!strncmp(buffer, "vn ", 3))
160        {
161          this->addVertexNormal(buffer+3);
162        }
163     
164      // case VertexTextureCoordinate
165      else if (!strncmp(buffer, "vt ", 3))
166        {
167          this->addVertexTexture(buffer+3);
168        }
169      // case group
170      else if (!strncmp(buffer, "g ", 2))
171        {
172          this->addGroup (buffer+2);
173        }
174      else if (!strncmp(buffer, "s ", 2)) //! \todo smoothing groups have to be implemented
175        {
176          PRINTF(2)("smoothing groups not supportet yet. line: %s\n", buffer);
177        }
178    }
179  fclose (stream);
180  delete []fileName;
181  return true;
182}
183
184/**
185    \brief Function to read in a mtl File.
186    \param mtlFile The .mtl file to read
187
188    This Function parses all Lines of an mtl File.
189    The reason for it not to be in the materials-class is,
190    that a material does not have to be able to read itself in from a File.
191
192*/
193bool OBJModel::readMtlLib (char* mtlFile)
194{
195  this->mtlFileName = new char [strlen(mtlFile)+1];
196  strcpy(this->mtlFileName, mtlFile);
197  char* fileName = new char [strlen(objPath) + strlen(this->mtlFileName)+1];
198  strcpy(fileName, this->objPath);
199  strcat(fileName, this->mtlFileName);
200 
201
202  FILE* stream;
203  if( (stream = fopen (fileName, "r")) == NULL)
204    {
205      printf("IniParser could not open %s\n", fileName);
206      return false;
207    }
208
209  char buffer[PARSELINELENGTH];
210  Material* tmpMat = material;
211
212  while(fgets(buffer, PARSELINELENGTH, stream))
213    {
214      PRINTF(5)("found line in mtlFile: %s\n", buffer);
215
216      // line termiated with \0 not \n
217      if (buffer[strlen(buffer)-1] == '\n')
218        buffer[strlen(buffer)-1] = '\0';
219
220      // create new Material
221      if (!strncmp(buffer, "newmtl ", 7))
222        {
223          tmpMat = tmpMat->addMaterial(buffer+7);
224        }
225      // setting a illumMode
226      else if (!strncmp(buffer, "illum ", 6))
227        {
228          tmpMat->setIllum(buffer+6);
229
230        }
231      // setting Diffuse Color
232      else if (!strncmp(buffer, "Kd ", 3))
233        {
234          tmpMat->setDiffuse(buffer+3);
235        }
236      // setting Ambient Color
237      else if (!strncmp(buffer, "Ka ", 3))
238        {
239          tmpMat->setAmbient(buffer+3);
240        }
241      // setting Specular Color
242      else if (!strncmp(buffer, "Ks ", 3))
243        {
244          tmpMat->setSpecular(buffer+3);
245        }
246      // setting The Specular Shininess
247      else if (!strncmp(buffer, "Ns ", 3))
248        {
249          tmpMat->setShininess(buffer+3);
250        }
251      // setting up transparency
252      else if (!strncmp(buffer, "d ", 2))
253        {
254          tmpMat->setTransparency(buffer+2);
255        }
256      else if (!strncmp(buffer, "Tf ", 3))
257        {
258          tmpMat->setTransparency(buffer+3);
259        }
260     
261      else if (!strncmp(buffer, "map_Kd ", 7))
262        {
263          tmpMat->setDiffuseMap(buffer+7);
264        }
265      else if (!strncmp(buffer, "map_Ka ", 7))
266        {
267          tmpMat->setAmbientMap(buffer+7);
268        }
269      else if (!strncmp(buffer, "map_Ks ", 7))
270        {
271          tmpMat->setSpecularMap(buffer+7);
272        }
273      else if (!strncmp(buffer, "bump ", 5))
274        {
275          tmpMat->setBump(buffer+7);
276        }
277     
278
279    }
280  fclose(stream);
281  delete []fileName;
282  return true;
283}
Note: See TracBrowser for help on using the repository browser.