Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

check in material changes

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