Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/graphics/importer/material.cc @ 8596

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

init

File size: 13.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"
[8362]23#include "compiler.h"
[8571]24
25#include "loading/load_param.h"
26
[7193]27#include "util/loading/resource_manager.h"
[3427]28
[3186]29/**
[7788]30 * @brief creates a Material.
[4836]31 * @param mtlName Name of the Material to be added to the Material List
[5306]32 */
[7221]33Material::Material (const std::string& mtlName)
[2776]34{
[5303]35  this->setClassID(CL_MATERIAL, "Material");
36
[3790]37  this->setIllum(3);
[5374]38  this->setDiffuse(1,1,1);
[3790]39  this->setAmbient(0,0,0);
40  this->setSpecular(.5,.5,.5);
41  this->setShininess(2.0);
42  this->setTransparency(1.0);
43
44  this->ambientTexture = NULL;
45  this->specularTexture = NULL;
[7057]46  this->sFactor = GL_SRC_ALPHA;
47  this->tFactor = GL_ONE;
[3790]48
[3894]49  this->setName(mtlName);
[2776]50}
51
[8583]52Material& Material::operator=(const Material& material)
53{
[8596]54  this->illumModel = material.illumModel;
55  this->diffuse = material.diffuse;
[8583]56  this->specular = material.specular;
57  this->ambient = material.ambient;
[8596]58  this->shininess = material.shininess;
[8583]59
60  this->textures = material.textures;
61  this->sFactor = material.sFactor;
62  this->tFactor = material.tFactor;
63  this->setName(material.getName());
64
65  return *this;
66}
67
68
69
[8571]70void Material::loadParams(const TiXmlElement* root)
71{
72  LoadParam(root, "illum", this, Material, setIllum);
73
74  LoadParam(root, "diffuse-color", this, Material , setDiffuse);
75  LoadParam(root, "ambient-color", this, Material , setAmbient);
76  LoadParam(root, "specular-color", this, Material , setSpecular);
77  LoadParam(root, "transparency", this, Material , setTransparency);
78
79  LoadParam(root, "tex", this, Material, setDiffuseMap);
80  LoadParam(root, "blendfunc", this, Material, setBlendFuncS)
81      .defaultValues("ZERO", "ZERO");
82}
83
84
[4584]85/**
[7788]86 * @brief deletes a Material
87 */
[2847]88Material::~Material()
89{
[5308]90  PRINTF(5)("delete Material %s.\n", this->getName());
[4834]91
[5303]92  if (this->ambientTexture != NULL)
[4834]93    ResourceManager::getInstance()->unload(this->ambientTexture);
[5303]94  if (this->specularTexture != NULL)
[4834]95    ResourceManager::getInstance()->unload(this->specularTexture);
[7785]96
97  if (this == Material::selectedMaterial)
98    Material::selectedMaterial = NULL;
[2847]99}
100
[7785]101
102const Material* Material::selectedMaterial = NULL;
103
[6622]104
[7785]105/// TODO FIX THIS
106// Material& Material::operator=(const Material& m)
107// {
108//   this->setIllum(m.illumModel);
109//   this->setDiffuse(m.diffuse[0],m.diffuse[1],m.diffuse[2]);
110//   this->setAmbient(m.ambient[0],m.ambient[1],m.ambient[2]);
111//   this->setSpecular(m.specular[0],m.specular[1],m.specular[2]);
112//   this->setShininess(m.shininess);
113//   this->setTransparency(m.transparency);
114//
115//   if (this->diffuseTexture != NULL)
116//     ResourceManager::getInstance()->unload(this->diffuseTexture);
117//   if (m.diffuseTexture != NULL)
118//     this->diffuseTexture = (Texture*)ResourceManager::getInstance()->copy(m.diffuseTexture);
119//   this->ambientTexture = NULL; /// FIXME
120//   this->specularTexture = NULL; /// FIXME
121//
122//   this->setName(m.getName());
123// }
[6622]124
125
[7785]126
[2842]127/**
[7788]128 * @brief sets the material with which the following Faces will be painted
[5308]129 */
[8571]130bool Material::select() const
[3140]131{
[7785]132  if (unlikely(this == Material::selectedMaterial))
[8571]133    return true;
[7785]134
[8571]135  /// !!  HACK !!! FIX THIS IN MODEL ///
[8037]136  else if (likely(Material::selectedMaterial != NULL))
137  {
[8571]138    Material::unselect();
139    //     for(unsigned int i = 0; i < Material::selectedMaterial->textures.size(); ++i)
140    //     {
141    //         glActiveTexture(Material::glTextureArbs[i]);
142    //         glBindTexture(GL_TEXTURE_2D, 0);
143    //         glDisable(GL_TEXTURE_2D);
144    //     }
[8037]145  }
146
[7919]147  if (likely(Material::selectedMaterial != NULL))
148  {
149    for(unsigned int i = 0; i < Material::selectedMaterial->textures.size(); ++i)
150    {
[8571]151      glActiveTexture(Material::glTextureArbs[i]);
152      //glBindTexture(GL_TEXTURE_2D, 0);
153      glDisable(GL_TEXTURE_2D);
[7919]154    }
155  }
[7785]156
[8571]157  // setting diffuse color
[8376]158  glColor4f (diffuse.r(), diffuse.g(), diffuse.b(), diffuse.a());
[3140]159  // setting ambient color
[8376]160  glMaterialfv(GL_FRONT, GL_AMBIENT, &this->ambient[0]);
[3140]161  // setting up Sprecular
[8376]162  glMaterialfv(GL_FRONT, GL_SPECULAR, &this->specular[0]);
[3140]163  // setting up Shininess
[3195]164  glMaterialf(GL_FRONT, GL_SHININESS, this->shininess);
[4584]165
[3790]166  // setting the transparency
[8376]167  if (this->diffuse.a() < 1.0 ||       /* This allows alpha blending of 2D textures with the scene */
[7788]168      (likely(!this->textures.empty() && this->textures[0].hasAlpha())))
[6812]169  {
170    glEnable(GL_BLEND);
[7057]171    glBlendFunc(this->sFactor, this->tFactor);
[6812]172  }
[3966]173  else
[7785]174  {
175    glDisable(GL_BLEND);
176  }
[3790]177
[4371]178
[4584]179  // setting illumination Model
[4836]180  if (this->illumModel == 1) //! @todo make this work, if no vertex-normals are read.
[3140]181    glShadeModel(GL_FLAT);
[3195]182  else if (this->illumModel >= 2)
[3140]183    glShadeModel(GL_SMOOTH);
184
[7785]185  for(unsigned int i = 0; i < this->textures.size(); ++i)
186  {
[8571]187    glActiveTexture(Material::glTextureArbs[i]);
188    glEnable(GL_TEXTURE_2D);
189    if(this->textures[i].hasAlpha())
190    {
191      glEnable(GL_BLEND);
192    }
193    glBindTexture(GL_TEXTURE_2D, this->textures[i].getTexture());
[7785]194  }
195  Material::selectedMaterial = this;
[8316]196
197  return true;
[3140]198}
[8370]199/**
200 * @brief Deselect Material (if one is selected).
201 */
[8037]202void Material::unselect()
203{
204  Material::selectedMaterial = NULL;
[8571]205  for(unsigned int i = 0; i < 8; ++i)
206  {
207    glActiveTexture(Material::glTextureArbs[i]);
208    glBindTexture(GL_TEXTURE_2D, 0);
209    glDisable(GL_TEXTURE_2D);
210  }
[8037]211}
212
[3140]213/**
[8370]214 * @brief Sets the Material Illumination Model.
215 * @param illu illumination Model in int form
[5308]216 */
[2776]217void Material::setIllum (int illum)
218{
[4584]219  PRINTF(4)("setting illumModel of Material %s to %i\n", this->getName(), illum);
[3195]220  this->illumModel = illum;
[2776]221}
[5308]222
[2842]223/**
[8370]224 * @brief Sets the Material Diffuse Color.
[7785]225 * @param r Red Color Channel.a
[4836]226 * @param g Green Color Channel.
227 * @param b Blue Color Channel.
[5308]228 */
[2776]229void Material::setDiffuse (float r, float g, float b)
230{
[4584]231  PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
[8376]232  this->diffuse = Color(r, g, b, this->diffuse.a() );
[2776]233}
[5308]234
[2776]235
[2842]236/**
[8370]237 * @brief Sets the Material Ambient Color.
[4836]238 * @param r Red Color Channel.
239 * @param g Green Color Channel.
240 * @param b Blue Color Channel.
[2842]241*/
[2776]242void Material::setAmbient (float r, float g, float b)
243{
[4584]244  PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
[8376]245  this->ambient = Color(r, g, b, 1.0);
[2776]246}
[5308]247
[2842]248/**
[8370]249 * @brief Sets the Material Specular Color.
[4836]250 * @param r Red Color Channel.
251 * @param g Green Color Channel.
252 * @param b Blue Color Channel.
[5308]253 */
[2776]254void Material::setSpecular (float r, float g, float b)
255{
[4584]256  PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
[8376]257  this->specular = Color (r, g, b, 1.0);
[7785]258}
[5308]259
[2842]260/**
[8370]261 * @brief Sets the Material Shininess.
[4836]262 * @param shini stes the Shininess from float.
[2842]263*/
[2836]264void Material::setShininess (float shini)
265{
[3195]266  this->shininess = shini;
[2836]267}
[2776]268
[2842]269/**
[8370]270 * @brief Sets the Material Transparency.
[4836]271 * @param trans stes the Transparency from int.
[2842]272*/
[2776]273void Material::setTransparency (float trans)
274{
[4584]275  PRINTF(4)("setting Transparency of Material %s to %f.\n", this->getName(), trans);
[8376]276  this->diffuse.a() = trans;
[2776]277}
[2778]278
[3140]279/**
[8571]280 * @brief sets the Blend-Function Parameters
281 * @param sFactor the Source Parameter.
282 * @param tFactor the Desitnation Parameter.
283 */
284void Material::setBlendFuncS(const std::string& sFactor, const std::string& tFactor)
285{
286  this->setBlendFunc(Material::stringToBlendFunc(sFactor), Material::stringToBlendFunc(tFactor));
287}
288
289
290
291/**
[8370]292 * @brief Adds a Texture Path to the List of already existing Paths
[4836]293 * @param pathName The Path to add.
[3140]294*/
[7221]295void Material::addTexturePath(const std::string& pathName)
[3140]296{
[3658]297  ResourceManager::getInstance()->addImageDir(pathName);
[3140]298}
299
[3070]300// MAPPING //
301
[8370]302
303/**
304 * @brief Sets the Diffuse map of this Texture.
305 * @param texture The Texture to load
306 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS
307 */
[7788]308void Material::setDiffuseMap(const Texture& texture, unsigned int textureNumber)
[7785]309{
310  assert(textureNumber < Material::getMaxTextureUnits());
311
312  if (this->textures.size() <= textureNumber)
[7788]313    this->textures.resize(textureNumber+1, Texture());
[7785]314
315  //! @todo check if RESOURCE MANAGER is availiable
316  this->textures[textureNumber] = texture;
317}
318
319
[2842]320/**
[7785]321 * @brief Sets the Materials Diffuse Map
[4836]322 * @param dMap the Name of the Image to Use
[8370]323 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS
324 */
[7785]325void Material::setDiffuseMap(const std::string& dMap, GLenum target, unsigned int textureNumber)
[3070]326{
[7785]327  assert(textureNumber < Material::getMaxTextureUnits());
328
[7676]329  PRINTF(5)("setting Diffuse Map %s\n", dMap.c_str());
[7785]330  if (this->textures.size() <= textureNumber)
[7788]331    this->textures.resize(textureNumber+1, Texture());
[7785]332
[4834]333  //! @todo check if RESOURCE MANAGER is availiable
[7221]334  if (!dMap.empty())
[7785]335  {
[7848]336    Texture* tex = dynamic_cast<Texture*>(ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target));
337    if (tex != NULL)
[8376]338      this->textures[textureNumber] = *tex;
[7848]339    else
340      this->textures[textureNumber] = Texture();
[7785]341  }
[4834]342  else
[7785]343  {
[7788]344    this->textures[textureNumber] = Texture();
[7785]345  }
[3070]346}
347
348/**
[7788]349 * @brief Sets the Materials Diffuse Map
[8370]350 * @param surface pointer to SDL_Surface which shall be used as texture.
351 * @param target the GL-Target to load the Surface to.
352 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS.
353 */
[7785]354void Material::setSDLDiffuseMap(SDL_Surface *surface, GLenum target, unsigned int textureNumber)
355{
356  assert(textureNumber < Material::getMaxTextureUnits());
357
358
359  if (this->textures.size() <= textureNumber)
[7788]360    this->textures.resize(textureNumber+1, Texture());
[7785]361
362  if(surface != NULL)
363  {
[7788]364    this->textures[textureNumber] = Texture(surface, GL_TEXTURE_2D);
[7785]365  }
366  else
367  {
[7788]368    this->textures[textureNumber] = Texture();
[7785]369  }
370
371}
372
[8037]373/**
[8370]374 * @brief Renders to a Texture.
375 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS.
376 * @param target The GL-Target.
377 * @param level the MipMap-Level to render to.
378 * @param xoffset The offset in the Source from the left.
379 * @param yoffset The offset in the Source from the top (or bottom).
380 * @param x The Offset in the Destination from the left.
381 * @param y The Offset in the Destination from the top (or bottom).
382 * @param width The width of the region to copy.
383 * @param height The height of the region to copy.
384 */
[8037]385void Material::renderToTexture(unsigned int textureNumber, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height)
386{
387  assert(textureNumber < Material::getMaxTextureUnits());
388  assert(textureNumber < this->textures.size());
[7785]389
[8312]390  // HACK
391  glActiveTexture(textureNumber);
[8571]392  glEnable(GL_TEXTURE_2D);
393  glBindTexture(GL_TEXTURE_2D, this->textures[textureNumber].getTexture());
[8037]394  glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height);
395
396}
397
[7785]398/**
[7788]399 * @brief Sets the Materials Ambient Map
[8037]400 * @todo implement this
[8376]401 */
[7221]402void Material::setAmbientMap(const std::string& aMap, GLenum target)
[3070]403{
[8316]404  /// FIXME SDL_Surface* ambientMap;
[3070]405
406}
407
408/**
[7788]409 * @brief Sets the Materials Specular Map
[4836]410 * @param sMap the Name of the Image to Use
[8376]411 * @todo implement this
412 */
[7221]413void Material::setSpecularMap(const std::string& sMap, GLenum target)
[3070]414{
[8316]415  /// FIXME SDL_Surface* specularMap;
[3070]416
417}
418
419/**
[7788]420 * @brief Sets the Materials Bumpiness
[4836]421 * @param bump the Name of the Image to Use
[7788]422 * @todo implemet this
[8376]423 */
[7221]424void Material::setBump(const std::string& bump)
[8571]425{}
[3070]426
[7785]427
[8370]428/**
429 * @returns the Maximim Texture Unit the users OpenGL-Implementation supports.
430 */
[8316]431unsigned int Material::getMaxTextureUnits()
[7785]432{
433  int maxTexUnits = 0;
434  glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTexUnits);
435  return maxTexUnits;
[3070]436}
[8571]437
438const GLenum Material::glTextureArbs[] =
439{
440  GL_TEXTURE0,
441  GL_TEXTURE1,
442  GL_TEXTURE2,
443  GL_TEXTURE3,
444  GL_TEXTURE4,
445  GL_TEXTURE5,
446  GL_TEXTURE6,
447  GL_TEXTURE7
448};
449
450
[8572]451/**
452 * @param blendFunc the Enumerator to convert to a String.
453 * @returns the matching String if found
454 */
[8571]455const std::string& Material::blendFuncToString(GLenum blendFunc)
456{
457  for (unsigned int i = 0; i < 9; ++i)
[8572]458    if (blendFunc == Material::glBlendFuncParams[i])
459      return Material::blendFuncNames[i];
460  PRINTF(2)("Supplied an Invalid Enumerator to blendfunc %d\n", blendFunc);
461  return Material::blendFuncNames[0];
[8571]462}
463
[8572]464/**
465 * @param blendFuncString the String to convert into a gl-enumeration
466 * @returns the matching GL-enumeration if found.
467 */
[8571]468GLenum Material::stringToBlendFunc(const std::string& blendFuncString)
469{
470  for (unsigned int i = 0; i < 9; ++i)
[8572]471    if (blendFuncString == Material::blendFuncNames[i])
472      return Material::glBlendFuncParams[i];
473  PRINTF(2)("BlendFunction %s not recognized using %s\n", blendFuncString.c_str(), Material::blendFuncNames[0].c_str());
474  return Material::glBlendFuncParams[0];
[8571]475}
476
477
478const GLenum Material::glBlendFuncParams[] =
479  {
480    GL_ZERO,
481    GL_ONE,
482    GL_DST_COLOR,
483    GL_ONE_MINUS_DST_COLOR,
484    GL_SRC_ALPHA,
485    GL_ONE_MINUS_SRC_ALPHA,
486    GL_DST_ALPHA,
487    GL_ONE_MINUS_DST_ALPHA,
488    GL_SRC_ALPHA_SATURATE
489  };
490
491const std::string Material::blendFuncNames[] =
492  {
493    "ZERO",
494    "ONE",
495    "DST_COLOR",
496    "ONE_MINUS_DST_COLOR",
497    "SRC_ALPHA",
498    "ONE_MINUS_SRC_ALPHA",
499    "DST_ALPHA",
500    "ONE_MINUS_DST_ALPHA",
501    "SRC_ALPHA_SATURATE"
502
503  };
Note: See TracBrowser for help on using the repository browser.