Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/graphics/importer/material.cc @ 3698

Last change on this file since 3698 was 3672, checked in by bensch, 20 years ago

orxonox/trunk: ResourceManager not totally static anymore, list-iterators, and so on

File size: 8.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
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER
18
19#include "material.h"
20
21#include "texture.h"
22#include "debug.h"
23#include "resource_manager.h"
24#include <stdlib.h>
25#include <string.h>
26
27//! \todo check if we are in RESOURCE MANAGER-mode
28#include "resource_manager.h"
29
30using namespace std;
31
32
33/**
34   \brief creates a default Material with no Name
35   normally you call this to create a material List (for an obj-file) and then append with addMaterial()
36*/
37Material::Material()
38{
39  this->init();
40 
41  this->setName ("");
42}
43
44/**
45   \brief creates a Material.
46   \param mtlName Name of the Material to be added to the Material List
47*/
48Material::Material (char* mtlName)
49{
50  this->init();
51 
52  this->setName (mtlName);
53}
54
55/**
56    \brief deletes a Material
57*/
58Material::~Material()
59{
60  PRINTF(4)("delete Material %s.\n", this->name);
61  if (this->name)
62    delete []this->name;
63  if (this->diffuseTexture)
64    ResourceManager::getInstance()->unload(this->diffuseTexture);
65  if (this->nextMat)
66    delete this->nextMat;
67}
68
69/**
70   \brief adds a new Material to the List.
71   this Function will append a new Material to the end of a Material List.
72   \param mtlName The name of the Material to be added.
73*/
74Material* Material::addMaterial(char* mtlName)
75{
76  PRINTF(4)("adding Material %s.\n", mtlName);
77   Material* tmpMat = this;
78  while (tmpMat->nextMat != NULL)
79    {
80      tmpMat = tmpMat->nextMat;
81    }
82  tmpMat->nextMat = new Material(mtlName);
83  return tmpMat->nextMat;
84 
85}
86
87/**
88   \brief initializes a new Material with its default Values
89*/
90void Material::init(void)
91{
92  PRINTF(4)("initializing new Material.\n");
93  this->nextMat = NULL;
94  this->name ="";
95  this->setIllum(3);
96  this->setDiffuse(0,0,0);
97  this->setAmbient(0,0,0);
98  this->setSpecular(.5,.5,.5);
99  this->setShininess(2.0);
100  this->setTransparency(0.0);
101
102
103  this->diffuseTexture = NULL;
104  this->ambientTexture = NULL;
105  this->specularTexture = NULL;
106
107  this->diffuseTextureSet = false;
108  this->ambientTextureSet = false;
109  this->specularTextureSet = false;
110}
111
112/**
113   \brief Search for a Material called mtlName
114   \param mtlName the Name of the Material to search for
115   \returns Material named mtlName if it is found. NULL otherwise.
116*/
117Material* Material::search(char* mtlName)
118{
119  PRINTF(5)("Searching for material %s", mtlName);
120  Material* searcher = this;
121  while (searcher != NULL)
122    {
123      PRINT(5)(".");
124      if (!strcmp (searcher->getName(), mtlName))
125        {
126          PRINT(5)("found.\n");
127          return searcher;
128        }
129      searcher = searcher->nextMat;
130    }
131  PRINT(2)("material %s not found\n", mtlName);
132  return NULL;
133}
134
135/**
136   \brief sets the material with which the following Faces will be painted
137*/
138bool Material::select (void)
139{
140  // setting diffuse color
141  //  glColor3f (diffuse[0], diffuse[1], diffuse[2]);
142  glMaterialfv(GL_FRONT, GL_DIFFUSE, this->diffuse);
143
144  // setting ambient color
145  glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient);
146
147  // setting up Sprecular
148  glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular);
149
150  // setting up Shininess
151  glMaterialf(GL_FRONT, GL_SHININESS, this->shininess);
152 
153  // setting illumination Model
154  if (this->illumModel == 1) //! \todo make this work, if no vertex-normals are read.
155    glShadeModel(GL_FLAT);
156  else if (this->illumModel >= 2)
157    glShadeModel(GL_SMOOTH);
158
159  if (this->diffuseTextureSet)
160    {
161      glEnable(GL_TEXTURE_2D);
162      glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture());
163    }
164  else
165    {
166      glDisable(GL_TEXTURE_2D);
167      glBindTexture(GL_TEXTURE_2D, 0);
168    }
169}
170
171/**
172   \brief Set the Name of the Material. (Important for searching)
173   \param mtlName the Name of the Material to be set.
174*/ 
175void Material::setName (char* mtlName)
176{
177  PRINTF(4)("setting Material Name to %s.\n", this->name);
178  this->name = new char [strlen(mtlName)+1];
179  strcpy(this->name, mtlName);
180}
181
182/**
183   \returns The Name of The Material
184*/
185char* Material::getName (void)
186{
187  return this->name;
188}
189
190/**
191   \brief Sets the Material Illumination Model.
192   \brief illu illumination Model in int form
193*/
194void Material::setIllum (int illum)
195{
196  PRINTF(4)("setting illumModel of Material %s to %i\n", this->name, illum);
197  this->illumModel = illum;
198}
199/**
200   \brief Sets the Material Illumination Model.
201   \brief illu illumination Model in char* form
202*/void Material::setIllum (char* illum)
203{
204  this->setIllum (atoi(illum));
205}
206
207/**
208   \brief Sets the Material Diffuse Color.
209   \param r Red Color Channel.
210   \param g Green Color Channel.
211   \param b Blue Color Channel.
212*/
213void Material::setDiffuse (float r, float g, float b)
214{
215  PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b);
216  this->diffuse[0] = r;
217  this->diffuse[1] = g;
218  this->diffuse[2] = b; 
219  this->diffuse[3] = 1.0;
220
221}
222/**
223   \brief Sets the Material Diffuse Color.
224   \param rgb The red, green, blue channel in char format (with spaces between them)
225*/
226void Material::setDiffuse (char* rgb)
227{
228  float r,g,b;
229  sscanf (rgb, "%f %f %f", &r, &g, &b);
230  this->setDiffuse (r, g, b);
231}
232
233/**
234   \brief Sets the Material Ambient Color.
235   \param r Red Color Channel.
236   \param g Green Color Channel.
237   \param b Blue Color Channel.
238*/
239void Material::setAmbient (float r, float g, float b)
240{
241  PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b);
242  this->ambient[0] = r;
243  this->ambient[1] = g;
244  this->ambient[2] = b;
245  this->ambient[3] = 1.0;
246}
247/**
248   \brief Sets the Material Ambient Color.
249   \param rgb The red, green, blue channel in char format (with spaces between them)
250*/
251void Material::setAmbient (char* rgb)
252{
253  float r,g,b;
254  sscanf (rgb, "%f %f %f", &r, &g, &b);
255  this->setAmbient (r, g, b);
256}
257
258/**
259   \brief Sets the Material Specular Color.
260   \param r Red Color Channel.
261   \param g Green Color Channel.
262   \param b Blue Color Channel.
263*/
264void Material::setSpecular (float r, float g, float b)
265{
266  PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b);
267  this->specular[0] = r;
268  this->specular[1] = g;
269  this->specular[2] = b;
270  this->specular[3] = 1.0;
271 }
272/**
273   \brief Sets the Material Specular Color.
274   \param rgb The red, green, blue channel in char format (with spaces between them)
275*/
276void Material::setSpecular (char* rgb)
277{
278  float r,g,b;
279  sscanf (rgb, "%f %f %f", &r, &g, &b);
280  this->setSpecular (r, g, b);
281}
282
283/**
284   \brief Sets the Material Shininess.
285   \param shini stes the Shininess from float.
286*/
287void Material::setShininess (float shini)
288{
289  this->shininess = shini;
290}
291/**
292   \brief Sets the Material Shininess.
293   \param shini stes the Shininess from char*.
294*/
295void Material::setShininess (char* shini)
296{
297  this->setShininess (atof(shini));
298}
299
300/**
301   \brief Sets the Material Transparency.
302   \param trans stes the Transparency from int.
303*/
304void Material::setTransparency (float trans)
305{
306  PRINTF(4)("setting Transparency of Material %s to %f.\n", this->name, trans);
307  this->transparency = trans;
308}
309/**
310   \brief Sets the Material Transparency.
311   \param trans stes the Transparency from char*.
312*/
313void Material::setTransparency (char* trans)
314{
315  this->setTransparency (atof(trans));
316}
317
318/**
319   \brief Adds a Texture Path to the List of already existing Paths
320   \param pathName The Path to add.
321*/
322void Material::addTexturePath(char* pathName)
323{
324  ResourceManager::getInstance()->addImageDir(pathName);
325}
326
327// MAPPING //
328
329/**
330   \brief Sets the Materials Diffuse Map
331   \param dMap the Name of the Image to Use
332*/
333void Material::setDiffuseMap(char* dMap)
334{
335  PRINTF(4)("setting Diffuse Map %s\n", dMap);
336  //    diffuseTexture = new Texture();
337  //    this->diffuseTextureSet = diffuseTexture->loadImage(dMap);
338
339  //! \todo check if RESOURCE MANAGER is availiable
340  //! \todo Textures from .mtl-file need special care.
341  this->diffuseTextureSet = this->diffuseTexture = (Texture*)ResourceManager::getInstance()->load(dMap, IMAGE);
342}
343
344/**
345   \brief Sets the Materials Ambient Map
346   \param aMap the Name of the Image to Use
347   \todo implement this
348*/
349void Material::setAmbientMap(char* aMap)
350{
351  SDL_Surface* ambientMap;
352
353}
354
355/**
356   \brief Sets the Materials Specular Map
357   \param sMap the Name of the Image to Use
358   \todo implement this
359*/
360void Material::setSpecularMap(char* sMap)
361{
362  SDL_Surface* specularMap;
363
364}
365
366/**
367   \brief Sets the Materials Bumpiness
368   \param bump the Name of the Image to Use
369   \todo implemet this
370*/
371void Material::setBump(char* bump)
372{
373
374}
Note: See TracBrowser for help on using the repository browser.