Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8548 was 8376, checked in by bensch, 19 years ago

orxonox/trunk: no more seg-fault when copying a Texture

File size: 10.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"
[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
[8376]134  glColor4f (diffuse.r(), diffuse.g(), diffuse.b(), diffuse.a());
[3140]135  // setting ambient color
[8376]136  glMaterialfv(GL_FRONT, GL_AMBIENT, &this->ambient[0]);
[3140]137  // setting up Sprecular
[8376]138  glMaterialfv(GL_FRONT, GL_SPECULAR, &this->specular[0]);
[3140]139  // setting up Shininess
[3195]140  glMaterialf(GL_FRONT, GL_SHININESS, this->shininess);
[4584]141
[3790]142  // setting the transparency
[8376]143  if (this->diffuse.a() < 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}
[8370]175/**
176 * @brief Deselect Material (if one is selected).
177 */
[8037]178void Material::unselect()
179{
180  Material::selectedMaterial = NULL;
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    }
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{
[4584]195  PRINTF(4)("setting illumModel of Material %s to %i\n", this->getName(), 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{
[4584]207  PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), 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{
[4584]220  PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), 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{
[4584]232  PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), 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{
[4584]251  PRINTF(4)("setting Transparency of Material %s to %f.\n", this->getName(), trans);
[8376]252  this->diffuse.a() = trans;
[2776]253}
[2778]254
[3140]255/**
[8370]256 * @brief Adds a Texture Path to the List of already existing Paths
[4836]257 * @param pathName The Path to add.
[3140]258*/
[7221]259void Material::addTexturePath(const std::string& pathName)
[3140]260{
[3658]261  ResourceManager::getInstance()->addImageDir(pathName);
[3140]262}
263
[3070]264// MAPPING //
265
[8370]266
267/**
268 * @brief Sets the Diffuse map of this Texture.
269 * @param texture The Texture to load
270 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS
271 */
[7788]272void Material::setDiffuseMap(const Texture& texture, unsigned int textureNumber)
[7785]273{
274  assert(textureNumber < Material::getMaxTextureUnits());
275
276  if (this->textures.size() <= textureNumber)
[7788]277    this->textures.resize(textureNumber+1, Texture());
[7785]278
279  //! @todo check if RESOURCE MANAGER is availiable
280  this->textures[textureNumber] = texture;
281}
282
283
[2842]284/**
[7785]285 * @brief Sets the Materials Diffuse Map
[4836]286 * @param dMap the Name of the Image to Use
[8370]287 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS
288 */
[7785]289void Material::setDiffuseMap(const std::string& dMap, GLenum target, unsigned int textureNumber)
[3070]290{
[7785]291  assert(textureNumber < Material::getMaxTextureUnits());
292
[7676]293  PRINTF(5)("setting Diffuse Map %s\n", dMap.c_str());
[7785]294  if (this->textures.size() <= textureNumber)
[7788]295    this->textures.resize(textureNumber+1, Texture());
[7785]296
[4834]297  //! @todo check if RESOURCE MANAGER is availiable
[7221]298  if (!dMap.empty())
[7785]299  {
[7848]300    Texture* tex = dynamic_cast<Texture*>(ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target));
301    if (tex != NULL)
[8376]302      this->textures[textureNumber] = *tex;
[7848]303    else
304      this->textures[textureNumber] = Texture();
[7785]305  }
[4834]306  else
[7785]307  {
[7788]308    this->textures[textureNumber] = Texture();
[7785]309  }
[3070]310}
311
312/**
[7788]313 * @brief Sets the Materials Diffuse Map
[8370]314 * @param surface pointer to SDL_Surface which shall be used as texture.
315 * @param target the GL-Target to load the Surface to.
316 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS.
317 */
[7785]318void Material::setSDLDiffuseMap(SDL_Surface *surface, GLenum target, unsigned int textureNumber)
319{
320  assert(textureNumber < Material::getMaxTextureUnits());
321
322
323  if (this->textures.size() <= textureNumber)
[7788]324    this->textures.resize(textureNumber+1, Texture());
[7785]325
326  if(surface != NULL)
327  {
[7788]328    this->textures[textureNumber] = Texture(surface, GL_TEXTURE_2D);
[7785]329  }
330  else
331  {
[7788]332    this->textures[textureNumber] = Texture();
[7785]333  }
334
335}
336
[8037]337/**
[8370]338 * @brief Renders to a Texture.
339 * @param textureNumber The Texture-Number from 0 to GL_MAX_TEXTURE_UNITS.
340 * @param target The GL-Target.
341 * @param level the MipMap-Level to render to.
342 * @param xoffset The offset in the Source from the left.
343 * @param yoffset The offset in the Source from the top (or bottom).
344 * @param x The Offset in the Destination from the left.
345 * @param y The Offset in the Destination from the top (or bottom).
346 * @param width The width of the region to copy.
347 * @param height The height of the region to copy.
348 */
[8037]349void Material::renderToTexture(unsigned int textureNumber, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height)
350{
351  assert(textureNumber < Material::getMaxTextureUnits());
352  assert(textureNumber < this->textures.size());
[7785]353
[8312]354  // HACK
355  glActiveTexture(textureNumber);
[8037]356   glEnable(GL_TEXTURE_2D);
357   glBindTexture(GL_TEXTURE_2D, this->textures[textureNumber].getTexture());
358  glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height);
359
360}
361
[7785]362/**
[7788]363 * @brief Sets the Materials Ambient Map
[8037]364 * @todo implement this
[8376]365 */
[7221]366void Material::setAmbientMap(const std::string& aMap, GLenum target)
[3070]367{
[8316]368  /// FIXME SDL_Surface* ambientMap;
[3070]369
370}
371
372/**
[7788]373 * @brief Sets the Materials Specular Map
[4836]374 * @param sMap the Name of the Image to Use
[8376]375 * @todo implement this
376 */
[7221]377void Material::setSpecularMap(const std::string& sMap, GLenum target)
[3070]378{
[8316]379  /// FIXME SDL_Surface* specularMap;
[3070]380
381}
382
383/**
[7788]384 * @brief Sets the Materials Bumpiness
[4836]385 * @param bump the Name of the Image to Use
[7788]386 * @todo implemet this
[8376]387 */
[7221]388void Material::setBump(const std::string& bump)
[3070]389{
[7785]390}
[3070]391
[7785]392
[8370]393/**
394 * @returns the Maximim Texture Unit the users OpenGL-Implementation supports.
395 */
[8316]396unsigned int Material::getMaxTextureUnits()
[7785]397{
398  int maxTexUnits = 0;
399  glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTexUnits);
400  return maxTexUnits;
[3070]401}
Note: See TracBrowser for help on using the repository browser.