Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8369 was 8369, checked in by bensch, 18 years ago

orxonox/trunk: finaly moved the *#\!3i& chars out of Material

File size: 8.3 KB
RevLine 
[5014]1/*
[3396]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
[3590]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER
17
[3396]18#include "objModel.h"
19
[3908]20#include <stdio.h>
21#include <string.h>
22#include <stdlib.h>
23
24#define PARSELINELENGTH 8192
25
[3475]26#include "debug.h"
[3915]27#include "compiler.h"
[3427]28
[3396]29/**
[8369]30 * @brief Crates a 3D-Model, loads in a File and scales it.
[4836]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.
[3396]33*/
[7221]34OBJModel::OBJModel(const std::string& fileName, float scaling)
35  : StaticModel(fileName)
[3396]36{
[6162]37  this->setClassID(CL_OBJ_MODEL, "OBJModel");
38
[4117]39  this->objPath = "./";
40
[3396]41  this->scaleFactor = scaling;
42
43  this->importFile (fileName);
44
[3911]45  this->finalize();
[3396]46}
47
48/**
[8369]49 * @brief deletes an OBJModel.
50 */
[3396]51OBJModel::~OBJModel()
[7221]52{ }
[3396]53
54/**
[4836]55 *  Imports a obj file and handles the the relative location
56 * @param fileName The file to import
[4117]57
58   Splits the FileName from the DirectoryName
[3396]59*/
[7221]60bool OBJModel::importFile (const std::string& fileNameInput)
[3396]61{
[7221]62  const char* fileName = fileNameInput.c_str();
[3548]63  PRINTF(4)("preparing to read in file: %s\n", fileName);
[5014]64  // splitting the
[4117]65  char* split = NULL;
[3396]66
[4117]67  if (!(split = strrchr(fileName, '/')))
68    split = strrchr(fileName, '\\'); // windows Case
69  if (split)
[3396]70    {
[4117]71      int len = split - fileName+1;
[7221]72      this->objPath =  fileName;
73      this->objPath.erase(len, this->objPath.size());
[4118]74      this->objPath[len] = '\0';
[7677]75      PRINTF(4)("Resolved file %s to Path %s.\n", fileName, this->objPath.c_str());
[3396]76    }
[4371]77  else
78    this->objPath = "./";
79  Material::addTexturePath(this->objPath);
80
[4117]81  this->readFromObjFile (fileName);
[3396]82  return true;
83}
84
85/**
[8369]86 * @brief Reads in the .obj File and sets all the Values.
87 * @param fileName the FileName of the Model.
88 *
89 * This function does read the file, parses it for the occurence of things like vertices, faces and so on, and executes the specific tasks
90 */
[7221]91bool OBJModel::readFromObjFile(const std::string& fileName)
[3396]92{
[3908]93  FILE* stream;
[7221]94  if( (stream = fopen (fileName.c_str(), "r")) == NULL)
[3396]95    {
[7221]96      PRINTF(2)("Object File Could not be Opened %s\n", fileName.c_str());
[3396]97      return false;
98    }
99
[3908]100  char buffer[PARSELINELENGTH];
[3910]101  while(fgets(buffer, PARSELINELENGTH, stream))
102    {
103      // line termiated with \0 not \n
104      if (buffer[strlen(buffer)-1] == '\n')
[5014]105        buffer[strlen(buffer)-1] = '\0';
[3910]106
[3396]107      // case vertice
[3908]108      if (!strncmp(buffer, "v ", 2))
[5014]109        {
110          this->addVertex(buffer+2);
111        }
[3396]112
113      // case face
[3908]114      else if (!strncmp(buffer, "f ", 2))
[5014]115        {
116          this->addFace (buffer+2);
117        }
118
[3908]119      else if (!strncmp(buffer, "mtllib ", 7))
[5014]120        {
121          this->readMtlLib (buffer+7);
122        }
[3396]123
[3908]124      else if (!strncmp(buffer, "usemtl ", 7))
[5014]125        {
126          this->setMaterial (buffer+7);
127        }
[3396]128
129      // case VertexNormal
[3908]130      else if (!strncmp(buffer, "vn ", 3))
[5014]131        {
132          this->addVertexNormal(buffer+3);
133        }
134
[3396]135      // case VertexTextureCoordinate
[3908]136      else if (!strncmp(buffer, "vt ", 3))
[5014]137        {
138          this->addVertexTexture(buffer+3);
139        }
[3396]140      // case group
[3908]141      else if (!strncmp(buffer, "g ", 2))
[5014]142        {
143          this->addGroup (buffer+2);
144        }
[4836]145      else if (!strncmp(buffer, "s ", 2)) //! @todo smoothing groups have to be implemented
[5014]146        {
[5300]147          PRINTF(3)("smoothing groups not supportet yet. line: %s\n", buffer);
[5014]148        }
[3396]149    }
[3908]150  fclose (stream);
[3396]151  return true;
152}
153
[5014]154/**
[8369]155 * @brief Function to read in a mtl File.
[5300]156 * @param mtlFile The .mtl file to read
157 *
158 * This Function parses all Lines of an mtl File.
159 * The reason for it not to be in the materials-class is,
160 * that a material does not have to be able to read itself in from a File.
161 */
[7221]162bool OBJModel::readMtlLib (const std::string& mtlFile)
[3396]163{
[7221]164  std::string fileName = this->objPath + mtlFile;
[3396]165
[3909]166  FILE* stream;
[7221]167  if( (stream = fopen (fileName.c_str(), "r")) == NULL)
[3396]168    {
[7221]169      PRINTF(2)("MaterialLibrary could not be opened %s\n", fileName.c_str());
[3396]170      return false;
171    }
[3909]172
173  char buffer[PARSELINELENGTH];
[3915]174  Material* tmpMat = NULL;
[3911]175
[5319]176  while(fgets(buffer, PARSELINELENGTH, stream) != NULL)
[3396]177    {
[3908]178      PRINTF(5)("found line in mtlFile: %s\n", buffer);
[3396]179
[3910]180      // line termiated with \0 not \n
181      if (buffer[strlen(buffer)-1] == '\n')
[5014]182        buffer[strlen(buffer)-1] = '\0';
[3910]183
[3396]184      // create new Material
[3908]185      if (!strncmp(buffer, "newmtl ", 7))
[5014]186        {
187          tmpMat = this->addMaterial(buffer+7);//tmpMat->addMaterial(buffer+7);
188        }
[3396]189      // setting a illumMode
[3908]190      else if (!strncmp(buffer, "illum ", 6))
[5014]191        {
192          if (likely(tmpMat != NULL))
[8369]193            setIllum(tmpMat, buffer+6);
[3396]194
[5014]195        }
[3396]196      // setting Diffuse Color
[3908]197      else if (!strncmp(buffer, "Kd ", 3))
[5014]198        {
199          if (likely(tmpMat != NULL))
[8369]200            setDiffuse(tmpMat, buffer+3);
[5014]201        }
[3396]202      // setting Ambient Color
[3908]203      else if (!strncmp(buffer, "Ka ", 3))
[5014]204        {
205          if (likely(tmpMat != NULL))
[8369]206            setAmbient(tmpMat, buffer+3);
[5014]207        }
[3396]208      // setting Specular Color
[3908]209      else if (!strncmp(buffer, "Ks ", 3))
[5014]210        {
211          if (likely(tmpMat != NULL))
[8369]212            setSpecular(tmpMat, buffer+3);
[5014]213        }
[3396]214      // setting The Specular Shininess
[3908]215      else if (!strncmp(buffer, "Ns ", 3))
[5014]216        {
217          if (likely(tmpMat != NULL))
[8369]218            setShininess(tmpMat, buffer+3);
[5014]219        }
[3396]220      // setting up transparency
[3908]221      else if (!strncmp(buffer, "d ", 2))
[5014]222        {
223          if (likely(tmpMat != NULL))
[8369]224            setTransparency(tmpMat, buffer+2);
[5014]225        }
[3908]226      else if (!strncmp(buffer, "Tf ", 3))
[5014]227        {
228          if (likely(tmpMat != NULL))
[8369]229            setTransparency(tmpMat, buffer+3);
[5014]230        }
231
[3908]232      else if (!strncmp(buffer, "map_Kd ", 7))
[5014]233        {
234          if (likely(tmpMat != NULL))
235            tmpMat->setDiffuseMap(buffer+7);
236        }
[3908]237      else if (!strncmp(buffer, "map_Ka ", 7))
[5014]238        {
239          if (likely(tmpMat != NULL))
240            tmpMat->setAmbientMap(buffer+7);
241        }
[3908]242      else if (!strncmp(buffer, "map_Ks ", 7))
[5014]243        {
244          if (likely(tmpMat != NULL))
245            tmpMat->setSpecularMap(buffer+7);
246        }
[3908]247      else if (!strncmp(buffer, "bump ", 5))
[5014]248        {
249          if (likely(tmpMat != NULL))
250            tmpMat->setBump(buffer+7);
251        }
[3396]252
[5014]253
[3396]254    }
[3909]255  fclose(stream);
[3396]256  return true;
257}
[8369]258
259
260/**
261 * @brief Sets the Material Illumination Model.
262 * @param material: the Material to apply the change to.
263 * @param illu: illumination Model in char* form
264 */
265void OBJModel::setIllum (Material* material, const char* illum)
266{
267  material->setIllum (atoi(illum));
268}
269
270/**
271 * @brief Sets the Material Diffuse Color.
272 * @param material: the Material to apply the change to.
273 * @param rgb The red, green, blue channel in char format (with spaces between them)
274 */
275void OBJModel::setDiffuse (Material* material, const char* rgb)
276{
277  float r,g,b;
278  sscanf (rgb, "%f %f %f", &r, &g, &b);
279  material->setDiffuse (r, g, b);
280}
281
282/**
283 * @brief Sets the Material Ambient Color.
284 * @param material: the Material to apply the change to.
285 * @param rgb The red, green, blue channel in char format (with spaces between them)
286 */
287void OBJModel::setAmbient (Material* material, const char* rgb)
288{
289  float r,g,b;
290  sscanf (rgb, "%f %f %f", &r, &g, &b);
291  material->setAmbient (r, g, b);
292}
293
294/**
295 * @brief Sets the Material Specular Color.
296 * @param material: the Material to apply the change to.
297 * @param rgb The red, green, blue channel in char format (with spaces between them)
298 */
299void OBJModel::setSpecular (Material* material, const char* rgb)
300{
301  float r,g,b;
302  sscanf (rgb, "%f %f %f", &r, &g, &b);
303  material->setSpecular (r, g, b);
304}
305
306/**
307 * @brief Sets the Material Shininess.
308 * @param material: the Material to apply the change to.
309 * @param shini stes the Shininess from char*.
310 */
311void OBJModel::setShininess (Material* material, const char* shini)
312{
313  material->setShininess (atof(shini));
314}
315
316/**
317 * @brief Sets the Material Transparency.
318 * @param material: the Material to apply the change to.
319 * @param trans stes the Transparency from char*.
320 */
321void OBJModel::setTransparency (Material* material, const char* trans)
322{
323  material->setTransparency (atof(trans));
324}
Note: See TracBrowser for help on using the repository browser.