Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7785 in orxonox.OLD for trunk/src/lib/graphics/importer/material.cc


Ignore:
Timestamp:
May 24, 2006, 3:17:19 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the Changes from the water branche back to the trunk.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/graphics/importer/material.cc

    r7676 r7785  
    4545  this->setTransparency(1.0);
    4646
    47   this->diffuseTexture = NULL;
    4847  this->ambientTexture = NULL;
    4948  this->specularTexture = NULL;
     
    6160  PRINTF(5)("delete Material %s.\n", this->getName());
    6261
    63   if (this->diffuseTexture != NULL)
    64   {
    65     ResourceManager::getInstance()->unload(this->diffuseTexture);
    66   }
     62  while(!this->textures.empty())
     63  {
     64    if (this->textures.back() != NULL)
     65      ResourceManager::getInstance()->unload(this->textures.back());
     66    this->textures.pop_back();
     67  }
     68
    6769  if (this->ambientTexture != NULL)
    6870    ResourceManager::getInstance()->unload(this->ambientTexture);
    6971  if (this->specularTexture != NULL)
    7072    ResourceManager::getInstance()->unload(this->specularTexture);
    71 }
    72 
    73 Material& Material::operator=(const Material& m)
    74 {
    75   this->setIllum(m.illumModel);
    76   this->setDiffuse(m.diffuse[0],m.diffuse[1],m.diffuse[2]);
    77   this->setAmbient(m.ambient[0],m.ambient[1],m.ambient[2]);
    78   this->setSpecular(m.specular[0],m.specular[1],m.specular[2]);
    79   this->setShininess(m.shininess);
    80   this->setTransparency(m.transparency);
    81 
    82   if (this->diffuseTexture != NULL)
    83     ResourceManager::getInstance()->unload(this->diffuseTexture);
    84   if (m.diffuseTexture != NULL)
    85     this->diffuseTexture = (Texture*)ResourceManager::getInstance()->copy(m.diffuseTexture);
    86   this->ambientTexture = NULL; /// FIXME
    87   this->specularTexture = NULL; /// FIXME
    88 
    89   this->setName(m.getName());
    90 }
     73
     74  if (this == Material::selectedMaterial)
     75    Material::selectedMaterial = NULL;
     76}
     77
     78
     79const Material* Material::selectedMaterial = NULL;
     80
     81const GLenum Material::glTextureArbs[] =
     82{
     83  GL_TEXTURE0,
     84  GL_TEXTURE1,
     85  GL_TEXTURE2,
     86  GL_TEXTURE3,
     87  GL_TEXTURE4,
     88  GL_TEXTURE5,
     89  GL_TEXTURE6,
     90  GL_TEXTURE7
     91};
     92
     93
     94/// TODO FIX THIS
     95// Material& Material::operator=(const Material& m)
     96// {
     97//   this->setIllum(m.illumModel);
     98//   this->setDiffuse(m.diffuse[0],m.diffuse[1],m.diffuse[2]);
     99//   this->setAmbient(m.ambient[0],m.ambient[1],m.ambient[2]);
     100//   this->setSpecular(m.specular[0],m.specular[1],m.specular[2]);
     101//   this->setShininess(m.shininess);
     102//   this->setTransparency(m.transparency);
     103//
     104//   if (this->diffuseTexture != NULL)
     105//     ResourceManager::getInstance()->unload(this->diffuseTexture);
     106//   if (m.diffuseTexture != NULL)
     107//     this->diffuseTexture = (Texture*)ResourceManager::getInstance()->copy(m.diffuseTexture);
     108//   this->ambientTexture = NULL; /// FIXME
     109//   this->specularTexture = NULL; /// FIXME
     110//
     111//   this->setName(m.getName());
     112// }
     113
    91114
    92115
     
    94117 *  sets the material with which the following Faces will be painted
    95118 */
    96 bool Material::select () const
    97 {
     119bool Material::select() const
     120{
     121  if (unlikely(this == Material::selectedMaterial))
     122      return true;
     123
     124
    98125  // setting diffuse color
    99126  glColor4f (diffuse[0], diffuse[1], diffuse[2], this->transparency);
    100 //  glMaterialfv(GL_FRONT, GL_DIFFUSE, this->diffuse);
    101 
    102127  // setting ambient color
    103128  glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient);
    104 
    105129  // setting up Sprecular
    106130  glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular);
    107 
    108131  // setting up Shininess
    109132  glMaterialf(GL_FRONT, GL_SHININESS, this->shininess);
    110133
     134
    111135  // setting the transparency
    112136  if (this->transparency < 1.0 ||       /* This allows alpha blending of 2D textures with the scene */
    113       (this->diffuseTexture && this->diffuseTexture->hasAlpha()))
     137      (likely(!this->textures.empty() && this->textures[0] != NULL) && this->textures[0]->hasAlpha()))
    114138  {
    115139    glEnable(GL_BLEND);
     
    117141  }
    118142  else
    119     {
    120       glDisable(GL_BLEND);
    121       //glColor4f(*(this->diffuse), *(this->diffuse+1), *(this->diffuse+2), 1);
    122     }
     143  {
     144    glDisable(GL_BLEND);
     145  }
    123146
    124147
     
    129152    glShadeModel(GL_SMOOTH);
    130153
    131   if (this->diffuseTexture != NULL)
     154  if (likely(Material::selectedMaterial != NULL))
     155  {
     156    for(unsigned int i = 0; i < Material::selectedMaterial->textures.size(); ++i)
    132157    {
     158        glActiveTexture(Material::glTextureArbs[i]);
     159        glBindTexture(GL_TEXTURE_2D, 0);
     160        glDisable(GL_TEXTURE_2D);
     161    }
     162  }
     163
     164  for(unsigned int i = 0; i < this->textures.size(); ++i)
     165  {
     166    if (likely(this->textures[i] != NULL))
     167    {
     168      glActiveTexture(Material::glTextureArbs[i]);
    133169      glEnable(GL_TEXTURE_2D);
    134       glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture());
     170      if(this->textures[i]->hasAlpha())
     171      {
     172        glEnable(GL_BLEND);
     173      }
     174      glBindTexture(GL_TEXTURE_2D, this->textures[i]->getTexture());
    135175    }
    136   else
    137     {
    138       glDisable(GL_TEXTURE_2D);
    139       glBindTexture(GL_TEXTURE_2D, 0);
    140     }
     176  }
     177  Material::selectedMaterial = this;
     178
     179  /*  if (this->diffuseTexture != NULL)
     180      {
     181        glEnable(GL_TEXTURE_2D);
     182        glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture());
     183      }
     184    else
     185      {
     186        glDisable(GL_TEXTURE_2D);
     187        glBindTexture(GL_TEXTURE_2D, 0);
     188      }*/
    141189}
    142190
     
    162210/**
    163211 *  Sets the Material Diffuse Color.
    164  * @param r Red Color Channel.
     212 * @param r Red Color Channel.a
    165213 * @param g Green Color Channel.
    166214 * @param b Blue Color Channel.
     
    226274  this->specular[2] = b;
    227275  this->specular[3] = 1.0;
    228  }
     276}
    229277
    230278/**
     
    285333// MAPPING //
    286334
     335void Material::setDiffuseMap(Texture* texture, unsigned int textureNumber)
     336{
     337  assert(textureNumber < Material::getMaxTextureUnits());
     338
     339  if (this->textures.size() > textureNumber && this->textures[textureNumber] != NULL)
     340    ResourceManager::getInstance()->unload(this->textures[textureNumber]);
     341
     342  if (this->textures.size() <= textureNumber)
     343    this->textures.resize(textureNumber+1, NULL);
     344
     345  //! @todo check if RESOURCE MANAGER is availiable
     346  this->textures[textureNumber] = texture;
     347}
     348
     349
     350/**
     351 * @brief Sets the Materials Diffuse Map
     352 * @param dMap the Name of the Image to Use
     353*/
     354void Material::setDiffuseMap(const std::string& dMap, GLenum target, unsigned int textureNumber)
     355{
     356  assert(textureNumber < Material::getMaxTextureUnits());
     357
     358  PRINTF(5)("setting Diffuse Map %s\n", dMap.c_str());
     359  if (this->textures.size() > textureNumber && this->textures[textureNumber] != NULL)
     360    ResourceManager::getInstance()->unload(this->textures[textureNumber]);
     361
     362  if (this->textures.size() <= textureNumber)
     363    this->textures.resize(textureNumber+1, NULL);
     364
     365  //! @todo check if RESOURCE MANAGER is availiable
     366  if (!dMap.empty())
     367  {
     368
     369    this->textures[textureNumber] = (Texture*)ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target);
     370  }
     371  else
     372  {
     373    this->textures[textureNumber] = NULL;
     374  }
     375}
     376
    287377/**
    288378 *  Sets the Materials Diffuse Map
    289  * @param dMap the Name of the Image to Use
    290 */
    291 void Material::setDiffuseMap(const std::string& dMap, GLenum target)
    292 {
    293   PRINTF(5)("setting Diffuse Map %s\n", dMap.c_str());
    294   if (this->diffuseTexture != NULL)
    295     ResourceManager::getInstance()->unload(this->diffuseTexture);
    296 
    297   //! @todo check if RESOURCE MANAGER is availiable
    298   //! @todo Textures from .mtl-file need special care.
    299   if (!dMap.empty())
    300     this->diffuseTexture = (Texture*)ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target);
     379 * @param surface pointer to SDL_Surface which shall be used as texture
     380*/
     381void Material::setSDLDiffuseMap(SDL_Surface *surface, GLenum target, unsigned int textureNumber)
     382{
     383  assert(textureNumber < Material::getMaxTextureUnits());
     384
     385
     386  if (this->textures.size() > textureNumber && this->textures[textureNumber] != NULL)
     387    ResourceManager::getInstance()->unload(this->textures[textureNumber]);
     388
     389  if (this->textures.size() <= textureNumber)
     390    this->textures.resize(textureNumber+1, NULL);
     391
     392  if(surface != NULL)
     393  {
     394    this->textures[textureNumber] = new Texture(surface, GL_TEXTURE_2D);
     395  }
    301396  else
    302     this->diffuseTexture = NULL;
    303 }
     397  {
     398    this->textures[textureNumber] = NULL;
     399  }
     400
     401}
     402
    304403
    305404/**
     
    332431void Material::setBump(const std::string& bump)
    333432{
    334 
    335 }
     433}
     434
     435
     436
     437int Material::getMaxTextureUnits()
     438{
     439  int maxTexUnits = 0;
     440  glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTexUnits);
     441  return maxTexUnits;
     442}
Note: See TracChangeset for help on using the changeset viewer.