Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8365 was 8362, checked in by bensch, 19 years ago

orxonox/trunk: removed stupid included in base_object.h
this should lead to faster compile-times

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