Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Material, and Model update, some const-issues

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