Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Material handles references… lets see if this works

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