Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8162 was 8037, checked in by bensch, 19 years ago

trunk: merged the water back
merged with command
svn merge -r7798:HEAD https://svn.orxonox.net/orxonox/branches/water .

conflicts are all resolved

File size: 10.9 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;
109
[8037]110/// !!  HACK !!! FIX THIS IN MODEL ///
111  else if (likely(Material::selectedMaterial != NULL))
112  {
113  Material::unselect();
114//     for(unsigned int i = 0; i < Material::selectedMaterial->textures.size(); ++i)
115//     {
116//         glActiveTexture(Material::glTextureArbs[i]);
117//         glBindTexture(GL_TEXTURE_2D, 0);
118//         glDisable(GL_TEXTURE_2D);
119//     }
120  }
121
[7919]122  if (likely(Material::selectedMaterial != NULL))
123  {
124    for(unsigned int i = 0; i < Material::selectedMaterial->textures.size(); ++i)
125    {
[7922]126        glActiveTexture(Material::glTextureArbs[i]);
[7919]127        //glBindTexture(GL_TEXTURE_2D, 0);
128        glDisable(GL_TEXTURE_2D);
129    }
130  }
[7785]131
[7919]132    // setting diffuse color
[6763]133  glColor4f (diffuse[0], diffuse[1], diffuse[2], this->transparency);
[3140]134  // setting ambient color
[3195]135  glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient);
[3140]136  // setting up Sprecular
[3195]137  glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular);
[3140]138  // setting up Shininess
[3195]139  glMaterialf(GL_FRONT, GL_SHININESS, this->shininess);
[4584]140
[3790]141  // setting the transparency
[6812]142  if (this->transparency < 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  {
[7922]162      glActiveTexture(Material::glTextureArbs[i]);
[7785]163      glEnable(GL_TEXTURE_2D);
[7788]164      if(this->textures[i].hasAlpha())
[7785]165      {
166        glEnable(GL_BLEND);
167      }
[7788]168      glBindTexture(GL_TEXTURE_2D, this->textures[i].getTexture());
[7785]169  }
170  Material::selectedMaterial = this;
[3140]171}
172
[8037]173void Material::unselect()
174{
175  Material::selectedMaterial = NULL;
176    for(unsigned int i = 0; i < 8; ++i)
177    {
178        glActiveTexture(Material::glTextureArbs[i]);
179        glBindTexture(GL_TEXTURE_2D, 0);
180        glDisable(GL_TEXTURE_2D);
181    }
182}
183
[3140]184/**
[4836]185 *  Sets the Material Illumination Model.
186 *  illu illumination Model in int form
[5308]187 */
[2776]188void Material::setIllum (int illum)
189{
[4584]190  PRINTF(4)("setting illumModel of Material %s to %i\n", this->getName(), illum);
[3195]191  this->illumModel = illum;
[2776]192}
[5308]193
[2842]194/**
[4836]195 *  Sets the Material Illumination Model.
196 *  illu illumination Model in char* form
[5308]197 */
198void Material::setIllum (char* illum)
[2776]199{
[3195]200  this->setIllum (atoi(illum));
[2776]201}
202
[2842]203/**
[4836]204 *  Sets the Material Diffuse Color.
[7785]205 * @param r Red Color Channel.a
[4836]206 * @param g Green Color Channel.
207 * @param b Blue Color Channel.
[5308]208 */
[2776]209void Material::setDiffuse (float r, float g, float b)
210{
[4584]211  PRINTF(4)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
[3195]212  this->diffuse[0] = r;
213  this->diffuse[1] = g;
[4584]214  this->diffuse[2] = b;
[3195]215  this->diffuse[3] = 1.0;
[2780]216
[2776]217}
[5308]218
[2842]219/**
[4836]220 *  Sets the Material Diffuse Color.
221 * @param rgb The red, green, blue channel in char format (with spaces between them)
[5308]222 */
[2776]223void Material::setDiffuse (char* rgb)
224{
[3140]225  float r,g,b;
226  sscanf (rgb, "%f %f %f", &r, &g, &b);
[3195]227  this->setDiffuse (r, g, b);
[2776]228}
229
[2842]230/**
[4836]231 *  Sets the Material Ambient Color.
232 * @param r Red Color Channel.
233 * @param g Green Color Channel.
234 * @param b Blue Color Channel.
[2842]235*/
[2776]236void Material::setAmbient (float r, float g, float b)
237{
[4584]238  PRINTF(4)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
[3195]239  this->ambient[0] = r;
240  this->ambient[1] = g;
241  this->ambient[2] = b;
242  this->ambient[3] = 1.0;
[2776]243}
[5308]244
[2842]245/**
[4836]246 *  Sets the Material Ambient Color.
247 * @param rgb The red, green, blue channel in char format (with spaces between them)
[5308]248 */
[2776]249void Material::setAmbient (char* rgb)
250{
[3140]251  float r,g,b;
252  sscanf (rgb, "%f %f %f", &r, &g, &b);
[3195]253  this->setAmbient (r, g, b);
[2776]254}
255
[2842]256/**
[4836]257 *  Sets the Material Specular Color.
258 * @param r Red Color Channel.
259 * @param g Green Color Channel.
260 * @param b Blue Color Channel.
[5308]261 */
[2776]262void Material::setSpecular (float r, float g, float b)
263{
[4584]264  PRINTF(4)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->getName(), r, g, b);
[3195]265  this->specular[0] = r;
266  this->specular[1] = g;
267  this->specular[2] = b;
268  this->specular[3] = 1.0;
[7785]269}
[5308]270
[2842]271/**
[4836]272 *  Sets the Material Specular Color.
273 * @param rgb The red, green, blue channel in char format (with spaces between them)
[2842]274*/
[2776]275void Material::setSpecular (char* rgb)
276{
[3140]277  float r,g,b;
278  sscanf (rgb, "%f %f %f", &r, &g, &b);
[3195]279  this->setSpecular (r, g, b);
[2776]280}
281
[2842]282/**
[4836]283 *  Sets the Material Shininess.
284 * @param shini stes the Shininess from float.
[2842]285*/
[2836]286void Material::setShininess (float shini)
287{
[3195]288  this->shininess = shini;
[2836]289}
[2842]290/**
[4836]291 *  Sets the Material Shininess.
292 * @param shini stes the Shininess from char*.
[2842]293*/
[2836]294void Material::setShininess (char* shini)
295{
[3195]296  this->setShininess (atof(shini));
[2836]297}
[2776]298
[2842]299/**
[4836]300 *  Sets the Material Transparency.
301 * @param trans stes the Transparency from int.
[2842]302*/
[2776]303void Material::setTransparency (float trans)
304{
[4584]305  PRINTF(4)("setting Transparency of Material %s to %f.\n", this->getName(), trans);
[3195]306  this->transparency = trans;
[2776]307}
[2842]308/**
[4836]309 *  Sets the Material Transparency.
310 * @param trans stes the Transparency from char*.
[2842]311*/
[2776]312void Material::setTransparency (char* trans)
313{
[3195]314  this->setTransparency (atof(trans));
[2776]315}
[2778]316
[3140]317/**
[4836]318 *  Adds a Texture Path to the List of already existing Paths
319 * @param pathName The Path to add.
[3140]320*/
[7221]321void Material::addTexturePath(const std::string& pathName)
[3140]322{
[3658]323  ResourceManager::getInstance()->addImageDir(pathName);
[3140]324}
325
[3070]326// MAPPING //
327
[7788]328void Material::setDiffuseMap(const Texture& texture, unsigned int textureNumber)
[7785]329{
330  assert(textureNumber < Material::getMaxTextureUnits());
331
332  if (this->textures.size() <= textureNumber)
[7788]333    this->textures.resize(textureNumber+1, Texture());
[7785]334
335  //! @todo check if RESOURCE MANAGER is availiable
336  this->textures[textureNumber] = texture;
337}
338
339
[2842]340/**
[7785]341 * @brief Sets the Materials Diffuse Map
[4836]342 * @param dMap the Name of the Image to Use
[3070]343*/
[7785]344void Material::setDiffuseMap(const std::string& dMap, GLenum target, unsigned int textureNumber)
[3070]345{
[7785]346  assert(textureNumber < Material::getMaxTextureUnits());
347
[7676]348  PRINTF(5)("setting Diffuse Map %s\n", dMap.c_str());
[7785]349  if (this->textures.size() <= textureNumber)
[7788]350    this->textures.resize(textureNumber+1, Texture());
[7785]351
[4834]352  //! @todo check if RESOURCE MANAGER is availiable
[7221]353  if (!dMap.empty())
[7785]354  {
[7848]355    Texture* tex = dynamic_cast<Texture*>(ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target));
356    if (tex != NULL)
357    this->textures[textureNumber] = *tex;
358    else
359      this->textures[textureNumber] = Texture();
[7785]360  }
[4834]361  else
[7785]362  {
[7788]363    this->textures[textureNumber] = Texture();
[7785]364  }
[3070]365}
366
367/**
[7788]368 * @brief Sets the Materials Diffuse Map
[7785]369 * @param surface pointer to SDL_Surface which shall be used as texture
370*/
371void Material::setSDLDiffuseMap(SDL_Surface *surface, GLenum target, unsigned int textureNumber)
372{
373  assert(textureNumber < Material::getMaxTextureUnits());
374
375
376  if (this->textures.size() <= textureNumber)
[7788]377    this->textures.resize(textureNumber+1, Texture());
[7785]378
379  if(surface != NULL)
380  {
[7788]381    this->textures[textureNumber] = Texture(surface, GL_TEXTURE_2D);
[7785]382  }
383  else
384  {
[7788]385    this->textures[textureNumber] = Texture();
[7785]386  }
387
388}
389
[8037]390/**
391 * @brief renders viewport buffer (?? or another buffer ;-)) to a texture
392 * @param textureNumber select the texture unit that will be overwritten
393*/
394void Material::renderToTexture(unsigned int textureNumber, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height)
395{
396  assert(textureNumber < Material::getMaxTextureUnits());
397  assert(textureNumber < this->textures.size());
[7785]398
[8037]399  glActiveTexture(0);
400   glEnable(GL_TEXTURE_2D);
401   glBindTexture(GL_TEXTURE_2D, this->textures[textureNumber].getTexture());
402  glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height);
403
404}
405
[7785]406/**
[7788]407 * @brief Sets the Materials Ambient Map
[8037]408 * @todo implement this
[3070]409*/
[7221]410void Material::setAmbientMap(const std::string& aMap, GLenum target)
[3070]411{
412  SDL_Surface* ambientMap;
413
414}
415
416/**
[7788]417 * @brief Sets the Materials Specular Map
[4836]418 * @param sMap the Name of the Image to Use
419   @todo implement this
[3070]420*/
[7221]421void Material::setSpecularMap(const std::string& sMap, GLenum target)
[3070]422{
423  SDL_Surface* specularMap;
424
425}
426
427/**
[7788]428 * @brief Sets the Materials Bumpiness
[4836]429 * @param bump the Name of the Image to Use
[7788]430 * @todo implemet this
[3070]431*/
[7221]432void Material::setBump(const std::string& bump)
[3070]433{
[7785]434}
[3070]435
[7785]436
437
438int Material::getMaxTextureUnits()
439{
440  int maxTexUnits = 0;
441  glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTexUnits);
442  return maxTexUnits;
[3070]443}
Note: See TracBrowser for help on using the repository browser.