Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: strange colors when importing. but for syncing reasons

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