Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/graphics/importer/material.cc @ 7887

Last change on this file since 7887 was 7574, checked in by stefalie, 19 years ago

check in material changes

File size: 10.2 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 "util/loading/resource_manager.h"
24#include <stdlib.h>
25#include <string.h>
26
27//! @todo check if we are in RESOURCE MANAGER-mode
28#include "util/loading/resource_manager.h"
29
30using namespace std;
31
32/**
33 * creates a Material.
34 * @param mtlName Name of the Material to be added to the Material List
35 */
36Material::Material (const std::string& mtlName)
37{
38  this->setClassID(CL_MATERIAL, "Material");
39
40  this->setIllum(3);
41  this->setDiffuse(1,1,1);
42  this->setAmbient(0,0,0);
43  this->setSpecular(.5,.5,.5);
44  this->setShininess(2.0);
45  this->setTransparency(1.0);
46
47  this->ambientTexture = NULL;
48  this->specularTexture = NULL;
49  this->sFactor = GL_SRC_ALPHA;
50  this->tFactor = GL_ONE;
51
52  this->setName(mtlName);
53}
54
55/**
56  *  deletes a Material
57*/
58Material::~Material()
59{
60  PRINTF(5)("delete Material %s.\n", this->getName());
61
62  while(!this->textures.empty())
63  {
64    if (this->textures.back() != NULL)
65      ResourceManager::getInstance()->unload(this->textures.back());
66    this->textures.pop_back();
67  }
68
69  if (this->ambientTexture != NULL)
70    ResourceManager::getInstance()->unload(this->ambientTexture);
71  if (this->specularTexture != NULL)
72    ResourceManager::getInstance()->unload(this->specularTexture);
73
74  if (this == Material::selectedMaterial)
75    Material::selectedMaterial = NULL;
76}
77
78
79const Material* Material::selectedMaterial = NULL;
80
81const GLenum Material::glTextureArbs[] =
82{
83  GL_TEXTURE0,
84  GL_TEXTURE1,
85  GL_TEXTURE2,
86  GL_TEXTURE3,
87  GL_TEXTURE4,
88  GL_TEXTURE5,
89  GL_TEXTURE6,
90  GL_TEXTURE7
91};
92
93
94/// TODO FIX THIS
95// Material& Material::operator=(const Material& m)
96// {
97//   this->setIllum(m.illumModel);
98//   this->setDiffuse(m.diffuse[0],m.diffuse[1],m.diffuse[2]);
99//   this->setAmbient(m.ambient[0],m.ambient[1],m.ambient[2]);
100//   this->setSpecular(m.specular[0],m.specular[1],m.specular[2]);
101//   this->setShininess(m.shininess);
102//   this->setTransparency(m.transparency);
103//
104//   if (this->diffuseTexture != NULL)
105//     ResourceManager::getInstance()->unload(this->diffuseTexture);
106//   if (m.diffuseTexture != NULL)
107//     this->diffuseTexture = (Texture*)ResourceManager::getInstance()->copy(m.diffuseTexture);
108//   this->ambientTexture = NULL; /// FIXME
109//   this->specularTexture = NULL; /// FIXME
110//
111//   this->setName(m.getName());
112// }
113
114
115
116/**
117 *  sets the material with which the following Faces will be painted
118 */
119bool Material::select() const
120{
121  if (unlikely(this == Material::selectedMaterial))
122      return true;
123
124
125  // setting diffuse color
126  glColor4f (diffuse[0], diffuse[1], diffuse[2], this->transparency);
127  // setting ambient color
128  glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient);
129  // setting up Sprecular
130  glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular);
131  // setting up Shininess
132  glMaterialf(GL_FRONT, GL_SHININESS, this->shininess);
133
134
135  // setting the transparency
136  if (this->transparency < 1.0 ||       /* This allows alpha blending of 2D textures with the scene */
137      (likely(!this->textures.empty() && this->textures[0] != NULL) && this->textures[0]->hasAlpha()))
138  {
139    glEnable(GL_BLEND);
140    glBlendFunc(this->sFactor, this->tFactor);
141  }
142  else
143  {
144    glDisable(GL_BLEND);
145  }
146
147
148  // setting illumination Model
149  if (this->illumModel == 1) //! @todo make this work, if no vertex-normals are read.
150    glShadeModel(GL_FLAT);
151  else if (this->illumModel >= 2)
152    glShadeModel(GL_SMOOTH);
153
154  if (likely(Material::selectedMaterial != NULL))
155  {
156    for(unsigned int i = 0; i < Material::selectedMaterial->textures.size(); ++i)
157    {
158        glActiveTexture(Material::glTextureArbs[i]);
159        glBindTexture(GL_TEXTURE_2D, 0);
160        glDisable(GL_TEXTURE_2D);
161    }
162  }
163
164  for(unsigned int i = 0; i < this->textures.size(); ++i)
165  {
166    if (likely(this->textures[i] != NULL))
167    {
168      glActiveTexture(Material::glTextureArbs[i]);
169      glEnable(GL_TEXTURE_2D);
170      if(this->textures[i]->hasAlpha())
171      {
172        glEnable(GL_BLEND);
173      }
174      glBindTexture(GL_TEXTURE_2D, this->textures[i]->getTexture());
175    }
176  }
177  Material::selectedMaterial = this;
178
179  /*  if (this->diffuseTexture != NULL)
180      {
181        glEnable(GL_TEXTURE_2D);
182        glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture());
183      }
184    else
185      {
186        glDisable(GL_TEXTURE_2D);
187        glBindTexture(GL_TEXTURE_2D, 0);
188      }*/
189}
190
191/**
192 *  Sets the Material Illumination Model.
193 *  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->getName(), illum);
198  this->illumModel = illum;
199}
200
201/**
202 *  Sets the Material Illumination Model.
203 *  illu illumination Model in char* form
204 */
205void Material::setIllum (char* illum)
206{
207  this->setIllum (atoi(illum));
208}
209
210/**
211 *  Sets the Material Diffuse Color.
212 * @param r Red Color Channel.a
213 * @param g Green Color Channel.
214 * @param b Blue Color Channel.
215 */
216void Material::setDiffuse (float r, float g, float b)
217{
218  PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
219  this->diffuse[0] = r;
220  this->diffuse[1] = g;
221  this->diffuse[2] = b;
222  this->diffuse[3] = 1.0;
223
224}
225
226/**
227 *  Sets the Material Diffuse Color.
228 * @param rgb The red, green, blue channel in char format (with spaces between them)
229 */
230void Material::setDiffuse (char* rgb)
231{
232  float r,g,b;
233  sscanf (rgb, "%f %f %f", &r, &g, &b);
234  this->setDiffuse (r, g, b);
235}
236
237/**
238 *  Sets the Material Ambient Color.
239 * @param r Red Color Channel.
240 * @param g Green Color Channel.
241 * @param b Blue Color Channel.
242*/
243void Material::setAmbient (float r, float g, float b)
244{
245  PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
246  this->ambient[0] = r;
247  this->ambient[1] = g;
248  this->ambient[2] = b;
249  this->ambient[3] = 1.0;
250}
251
252/**
253 *  Sets the Material Ambient Color.
254 * @param rgb The red, green, blue channel in char format (with spaces between them)
255 */
256void Material::setAmbient (char* rgb)
257{
258  float r,g,b;
259  sscanf (rgb, "%f %f %f", &r, &g, &b);
260  this->setAmbient (r, g, b);
261}
262
263/**
264 *  Sets the Material Specular Color.
265 * @param r Red Color Channel.
266 * @param g Green Color Channel.
267 * @param b Blue Color Channel.
268 */
269void Material::setSpecular (float r, float g, float b)
270{
271  PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
272  this->specular[0] = r;
273  this->specular[1] = g;
274  this->specular[2] = b;
275  this->specular[3] = 1.0;
276}
277
278/**
279 *  Sets the Material Specular Color.
280 * @param rgb The red, green, blue channel in char format (with spaces between them)
281*/
282void Material::setSpecular (char* rgb)
283{
284  float r,g,b;
285  sscanf (rgb, "%f %f %f", &r, &g, &b);
286  this->setSpecular (r, g, b);
287}
288
289/**
290 *  Sets the Material Shininess.
291 * @param shini stes the Shininess from float.
292*/
293void Material::setShininess (float shini)
294{
295  this->shininess = shini;
296}
297/**
298 *  Sets the Material Shininess.
299 * @param shini stes the Shininess from char*.
300*/
301void Material::setShininess (char* shini)
302{
303  this->setShininess (atof(shini));
304}
305
306/**
307 *  Sets the Material Transparency.
308 * @param trans stes the Transparency from int.
309*/
310void Material::setTransparency (float trans)
311{
312  PRINTF(4)("setting Transparency of Material %s to %f.\n", this->getName(), trans);
313  this->transparency = trans;
314}
315/**
316 *  Sets the Material Transparency.
317 * @param trans stes the Transparency from char*.
318*/
319void Material::setTransparency (char* trans)
320{
321  this->setTransparency (atof(trans));
322}
323
324/**
325 *  Adds a Texture Path to the List of already existing Paths
326 * @param pathName The Path to add.
327*/
328void Material::addTexturePath(const std::string& pathName)
329{
330  ResourceManager::getInstance()->addImageDir(pathName);
331}
332
333// MAPPING //
334
335/**
336 *  Sets the Materials Diffuse Map
337 * @param dMap the Name of the Image to Use
338*/
339void Material::setDiffuseMap(const std::string& dMap, GLenum target, unsigned int textureNumber)
340{
341  assert(textureNumber < Material::getMaxTextureUnits());
342
343  PRINTF(5)("setting Diffuse Map %s\n", dMap);
344  if (this->textures.size() > textureNumber && this->textures[textureNumber] != NULL)
345    ResourceManager::getInstance()->unload(this->textures[textureNumber]);
346
347  if (this->textures.size() <= textureNumber)
348    this->textures.resize(textureNumber+1, NULL);
349
350  //! @todo check if RESOURCE MANAGER is availiable
351  if (!dMap.empty())
352  {
353
354    this->textures[textureNumber] = (Texture*)ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target);
355  }
356  else
357  {
358    this->textures[textureNumber] = NULL;
359  }
360}
361
362/**
363 *  Sets the Materials Diffuse Map
364 * @param surface pointer to SDL_Surface which shall be used as texture
365*/
366void Material::setSDLDiffuseMap(SDL_Surface *surface, GLenum target, unsigned int textureNumber)
367{
368  assert(textureNumber < Material::getMaxTextureUnits());
369
370 
371  if (this->textures.size() > textureNumber && this->textures[textureNumber] != NULL)
372    ResourceManager::getInstance()->unload(this->textures[textureNumber]);
373
374  if (this->textures.size() <= textureNumber)
375    this->textures.resize(textureNumber+1, NULL);
376
377  if(surface != NULL)
378  {
379    this->textures[textureNumber] = new Texture(surface, GL_TEXTURE_2D);
380  }
381  else
382  {
383    this->textures[textureNumber] = NULL;
384  }
385
386}
387
388
389/**
390 *  Sets the Materials Ambient Map
391 * @param aMap the Name of the Image to Use
392   @todo implement this
393*/
394void Material::setAmbientMap(const std::string& aMap, GLenum target)
395{
396  SDL_Surface* ambientMap;
397
398}
399
400/**
401 *  Sets the Materials Specular Map
402 * @param sMap the Name of the Image to Use
403   @todo implement this
404*/
405void Material::setSpecularMap(const std::string& sMap, GLenum target)
406{
407  SDL_Surface* specularMap;
408
409}
410
411/**
412 *  Sets the Materials Bumpiness
413 * @param bump the Name of the Image to Use
414   @todo implemet this
415*/
416void Material::setBump(const std::string& bump)
417{
418}
419
420
421
422int Material::getMaxTextureUnits()
423{
424  int maxTexUnits = 0;
425  glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTexUnits);
426  return maxTexUnits;
427}
Note: See TracBrowser for help on using the repository browser.