Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/powerups/src/lib/graphics/importer/objModel.cc @ 6474

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

orxonox/trunk: merged the spaceshipcontroll branche into the trunk
merged with command
svn merge -r6036:HEAD spaceshipcontrol/ ../trunk/
no conflicts

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#include "compiler.h"
28
29/**
30 *  Crates a 3D-Model, loads in a File and scales it.
31 * @param fileName file to parse and load (must be a .obj file)
32 * @param scaling The factor that the model will be scaled with.
33*/
34OBJModel::OBJModel(const char* fileName, float scaling) : StaticModel(fileName)
35{
36  this->setClassID(CL_OBJ_MODEL, "OBJModel");
37
38  this->objPath = "./";
39
40  this->scaleFactor = scaling;
41
42  this->importFile (fileName);
43
44  this->finalize();
45}
46
47/**
48 *  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}
58
59/**
60 *  Imports a obj file and handles the the relative location
61 * @param fileName The file to import
62
63   Splits the FileName from the DirectoryName
64*/
65bool OBJModel::importFile (const char* fileName)
66{
67  PRINTF(4)("preparing to read in file: %s\n", fileName);
68  // splitting the
69  char* split = NULL;
70
71  if (!(split = strrchr(fileName, '/')))
72    split = strrchr(fileName, '\\'); // windows Case
73  if (split)
74    {
75      int len = split - fileName+1;
76      this->objPath = new char[len +2];
77      strncpy(this->objPath, fileName, len);
78      this->objPath[len] = '\0';
79      PRINTF(4)("Resolved file %s to Path %s.\n", fileName, this->objPath);
80    }
81  else
82    this->objPath = "./";
83  Material::addTexturePath(this->objPath);
84
85  this->readFromObjFile (fileName);
86  return true;
87}
88
89/**
90 *  Reads in the .obj File and sets all the Values.
91   This function does read the file, parses it for the occurence of things like vertices, faces and so on, and executes the specific tasks
92*/
93bool OBJModel::readFromObjFile(const char* fileName)
94{
95  FILE* stream;
96  if( (stream = fopen (fileName, "r")) == NULL)
97    {
98      PRINTF(2)("Object File Could not be Opened %s\n", fileName);
99      return false;
100    }
101
102  char buffer[PARSELINELENGTH];
103  while(fgets(buffer, PARSELINELENGTH, stream))
104    {
105      // line termiated with \0 not \n
106      if (buffer[strlen(buffer)-1] == '\n')
107        buffer[strlen(buffer)-1] = '\0';
108
109      // case vertice
110      if (!strncmp(buffer, "v ", 2))
111        {
112          this->addVertex(buffer+2);
113        }
114
115      // case face
116      else if (!strncmp(buffer, "f ", 2))
117        {
118          this->addFace (buffer+2);
119        }
120
121      else if (!strncmp(buffer, "mtllib ", 7))
122        {
123          this->readMtlLib (buffer+7);
124        }
125
126      else if (!strncmp(buffer, "usemtl ", 7))
127        {
128          this->setMaterial (buffer+7);
129        }
130
131      // case VertexNormal
132      else if (!strncmp(buffer, "vn ", 3))
133        {
134          this->addVertexNormal(buffer+3);
135        }
136
137      // case VertexTextureCoordinate
138      else if (!strncmp(buffer, "vt ", 3))
139        {
140          this->addVertexTexture(buffer+3);
141        }
142      // case group
143      else if (!strncmp(buffer, "g ", 2))
144        {
145          this->addGroup (buffer+2);
146        }
147      else if (!strncmp(buffer, "s ", 2)) //! @todo smoothing groups have to be implemented
148        {
149          PRINTF(3)("smoothing groups not supportet yet. line: %s\n", buffer);
150        }
151    }
152  fclose (stream);
153  return true;
154}
155
156/**
157 * Function to read in a mtl File.
158 * @param mtlFile The .mtl file to read
159 *
160 * This Function parses all Lines of an mtl File.
161 * The reason for it not to be in the materials-class is,
162 * that a material does not have to be able to read itself in from a File.
163 */
164bool OBJModel::readMtlLib (const char* mtlFile)
165{
166  char* fileName = new char [strlen(this->objPath) + strlen(mtlFile)+1];
167  sprintf(fileName, "%s%s", this->objPath, mtlFile);
168
169  FILE* stream;
170  if( (stream = fopen (fileName, "r")) == NULL)
171    {
172      PRINTF(2)("MaterialLibrary could not be opened %s\n", fileName);
173      delete[] fileName;
174      return false;
175    }
176
177  char buffer[PARSELINELENGTH];
178  Material* tmpMat = NULL;
179
180  while(fgets(buffer, PARSELINELENGTH, stream) != NULL)
181    {
182      PRINTF(5)("found line in mtlFile: %s\n", buffer);
183
184      // line termiated with \0 not \n
185      if (buffer[strlen(buffer)-1] == '\n')
186        buffer[strlen(buffer)-1] = '\0';
187
188      // create new Material
189      if (!strncmp(buffer, "newmtl ", 7))
190        {
191          tmpMat = this->addMaterial(buffer+7);//tmpMat->addMaterial(buffer+7);
192        }
193      // setting a illumMode
194      else if (!strncmp(buffer, "illum ", 6))
195        {
196          if (likely(tmpMat != NULL))
197            tmpMat->setIllum(buffer+6);
198
199        }
200      // setting Diffuse Color
201      else if (!strncmp(buffer, "Kd ", 3))
202        {
203          if (likely(tmpMat != NULL))
204            tmpMat->setDiffuse(buffer+3);
205        }
206      // setting Ambient Color
207      else if (!strncmp(buffer, "Ka ", 3))
208        {
209          if (likely(tmpMat != NULL))
210            tmpMat->setAmbient(buffer+3);
211        }
212      // setting Specular Color
213      else if (!strncmp(buffer, "Ks ", 3))
214        {
215          if (likely(tmpMat != NULL))
216            tmpMat->setSpecular(buffer+3);
217        }
218      // setting The Specular Shininess
219      else if (!strncmp(buffer, "Ns ", 3))
220        {
221          if (likely(tmpMat != NULL))
222            tmpMat->setShininess(buffer+3);
223        }
224      // setting up transparency
225      else if (!strncmp(buffer, "d ", 2))
226        {
227          if (likely(tmpMat != NULL))
228            tmpMat->setTransparency(buffer+2);
229        }
230      else if (!strncmp(buffer, "Tf ", 3))
231        {
232          if (likely(tmpMat != NULL))
233            tmpMat->setTransparency(buffer+3);
234        }
235
236      else if (!strncmp(buffer, "map_Kd ", 7))
237        {
238          if (likely(tmpMat != NULL))
239            tmpMat->setDiffuseMap(buffer+7);
240        }
241      else if (!strncmp(buffer, "map_Ka ", 7))
242        {
243          if (likely(tmpMat != NULL))
244            tmpMat->setAmbientMap(buffer+7);
245        }
246      else if (!strncmp(buffer, "map_Ks ", 7))
247        {
248          if (likely(tmpMat != NULL))
249            tmpMat->setSpecularMap(buffer+7);
250        }
251      else if (!strncmp(buffer, "bump ", 5))
252        {
253          if (likely(tmpMat != NULL))
254            tmpMat->setBump(buffer+7);
255        }
256
257
258    }
259  fclose(stream);
260  delete []fileName;
261  return true;
262}
Note: See TracBrowser for help on using the repository browser.