Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/graphics/importer/material.cc @ 9854

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

orxonox/new_class_id: almost killed off the old ResourceManager

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