Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: gl_multi_texture check in the material. This should resolve the Material::select() bug

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