Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/water/src/lib/graphics/importer/material.cc @ 8034

Last change on this file since 8034 was 8034, checked in by stefalie, 18 years ago

water: refraction doesnt work with reflection

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