Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: importer: having color again

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