Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7687 in orxonox.OLD


Ignore:
Timestamp:
May 18, 2006, 3:16:46 PM (18 years ago)
Author:
stefalie
Message:

branches/water: activate/deactivate reflection/refraction functions added

Location:
branches/water/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/water/src/defs/class_id.h

    r7486 r7687  
    179179  CL_PLANET                     =    0x00000309,
    180180  CL_BUILDING                   =    0x00000310,
     181  CL_MAPPED_WATER               =    0x00000311,
    181182
    182183  // Playables
  • branches/water/src/lib/graphics/importer/material.cc

    r7221 r7687  
    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/**
     
    289337 * @param dMap the Name of the Image to Use
    290338*/
    291 void Material::setDiffuseMap(const std::string& dMap, GLenum target)
    292 {
     339void Material::setDiffuseMap(const std::string& dMap, GLenum target, unsigned int textureNumber)
     340{
     341  assert(textureNumber < Material::getMaxTextureUnits());
     342
    293343  PRINTF(5)("setting Diffuse Map %s\n", dMap);
    294   if (this->diffuseTexture != NULL)
    295     ResourceManager::getInstance()->unload(this->diffuseTexture);
     344  if (this->textures.size() > textureNumber && this->textures[textureNumber] != NULL)
     345    ResourceManager::getInstance()->unload(this->textures[textureNumber]);
     346
     347  if (this->textures.size() <= textureNumber)
     348    this->textures.resize(textureNumber+1, NULL);
    296349
    297350  //! @todo check if RESOURCE MANAGER is availiable
    298   //! @todo Textures from .mtl-file need special care.
    299351  if (!dMap.empty())
    300     this->diffuseTexture = (Texture*)ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target);
     352  {
     353
     354    this->textures[textureNumber] = (Texture*)ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target);
     355  }
    301356  else
    302     this->diffuseTexture = NULL;
    303 }
     357  {
     358    this->textures[textureNumber] = NULL;
     359  }
     360}
     361
     362/**
     363 *  Sets the Materials Diffuse Map
     364 * @param surface pointer to SDL_Surface which shall be used as texture
     365*/
     366void Material::setSDLDiffuseMap(SDL_Surface *surface, GLenum target, unsigned int textureNumber)
     367{
     368  assert(textureNumber < Material::getMaxTextureUnits());
     369
     370 
     371  if (this->textures.size() > textureNumber && this->textures[textureNumber] != NULL)
     372    ResourceManager::getInstance()->unload(this->textures[textureNumber]);
     373
     374  if (this->textures.size() <= textureNumber)
     375    this->textures.resize(textureNumber+1, NULL);
     376
     377  if(surface != NULL)
     378  {
     379    this->textures[textureNumber] = new Texture(surface, GL_TEXTURE_2D);
     380  }
     381  else
     382  {
     383    this->textures[textureNumber] = NULL;
     384  }
     385
     386}
     387
    304388
    305389/**
     
    332416void Material::setBump(const std::string& bump)
    333417{
    334 
    335 }
     418}
     419
     420
     421
     422int Material::getMaxTextureUnits()
     423{
     424  int maxTexUnits = 0;
     425  glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTexUnits);
     426  return maxTexUnits;
     427}
  • branches/water/src/lib/graphics/importer/material.h

    r7221 r7687  
    1010#include "base_object.h"
    1111
     12
    1213#if HAVE_CONFIG_H
    1314#include <config.h>
    1415#endif /* HAVE_CONFIG_H */
    1516
    16 #ifndef NULL
    17 #define NULL 0            //!< a pointer to NULL
    18 #endif
     17#include <vector>
     18#include "SDL_image.h"
    1919
    2020#include "texture.h"
     
    2525class Material : public BaseObject
    2626{
    27  public:
    28   Material (const std::string& mtlName = "");
    29   virtual ~Material ();
     27  public:
     28    Material (const std::string& mtlName = "");
     29    virtual ~Material ();
    3030
    31   Material& operator=(const Material& material);
     31    Material& operator=(const Material& material);
    3232
    33   bool select () const;
     33    bool select () const;
    3434
    35   void setIllum (int illum);
    36   void setIllum (char* illum);
    37   int getIllumModel() const { return this->illumModel; };
    38   void setDiffuse (float r, float g, float b);
    39   void setDiffuse (char* rgb);
    40   void setAmbient (float r, float g, float b);
    41   void setAmbient (char* rgb);
    42   void setSpecular (float r, float g, float b);
    43   void setSpecular (char* rgb);
    44   void setShininess (float shini);
    45   void setShininess (char* shini);
    46   void setTransparency (float trans);
    47   void setTransparency (char* trans);
    48   void setBlendFunc(GLenum sFactor, GLenum tFactor) { this->sFactor = sFactor; this->tFactor = tFactor; };
     35    void setIllum (int illum);
     36    int getIllumModel() const { return this->illumModel; };
     37    void setDiffuse (float r, float g, float b);
     38    void setAmbient (float r, float g, float b);
     39    void setSpecular (float r, float g, float b);
     40    void setShininess (float shini);
     41    void setTransparency (float trans);
     42    void setBlendFunc(GLenum sFactor, GLenum tFactor) { this->sFactor = sFactor; this->tFactor = tFactor; };
    4943
    50  // MAPPING //
    51   void setDiffuseMap(const std::string& dMap, GLenum target = GL_TEXTURE_2D);
    52   void setAmbientMap(const std::string& aMap, GLenum target = GL_TEXTURE_2D);
    53   void setSpecularMap(const std::string& sMap, GLenum target = GL_TEXTURE_2D);
    54   void setBump(const std::string& bump);
    55   GLuint getDiffuseTexture() const { return (this->diffuseTexture)? this->diffuseTexture->getTexture() : 0; };
    5644
    57   static void addTexturePath(const std::string& pathName);
     45    // TODO Move them out of here
     46    void setIllum (char* illum);
     47    void setDiffuse (char* rgb);
     48    void setAmbient (char* rgb);
     49    void setSpecular (char* rgb);
     50    void setShininess (char* shini);
     51    void setTransparency (char* trans);
     52
     53
     54    // MAPPING //
     55    void setDiffuseMap(const std::string& dMap, GLenum target = GL_TEXTURE_2D, unsigned int textureNumber = 0);
     56    void setSDLDiffuseMap(SDL_Surface *surface, GLenum target = GL_TEXTURE_2D, unsigned int textureNumber = 0);
     57
     58    void setAmbientMap(const std::string& aMap, GLenum target = GL_TEXTURE_2D);
     59    void setSpecularMap(const std::string& sMap, GLenum target = GL_TEXTURE_2D);
     60    void setBump(const std::string& bump);
     61    GLuint getDiffuseTexture(unsigned int i = 0) const { return (this->textures.size() > i)? this->textures[i]->getTexture() : 0; };
     62
     63    static void addTexturePath(const std::string& pathName);
     64
     65  public:
     66    static const GLenum glTextureArbs[];  //!< The Texture ARB's
     67
     68    static int getMaxTextureUnits();
    5869
    5970  private:
    60     int         illumModel;       //!< The IlluminationModel is either flat or smooth.
    61     float       diffuse [4];      //!< The diffuse color of the Material.
    62     float       ambient [4];      //!< The ambient color of the Material.
    63     float       specular [4];     //!< The specular color of the Material.
    64     float       shininess;        //!< The shininess of the Material.
    65     float       transparency;     //!< The transperency of the Material.
    66     GLenum      sFactor;
    67     GLenum      tFactor;
     71    static const Material* selectedMaterial; //!< The currently selected material.
    6872
    69     Texture*    diffuseTexture;   //!< The diffuse texture of the Material.
    70     Texture*    ambientTexture;   //!< The ambient texture of the Material.
    71     Texture*    specularTexture;  //!< The specular texture of the Material.
     73    int              illumModel;       //!< The IlluminationModel is either flat or smooth.
     74    float            diffuse [4];      //!< The diffuse color of the Material.
     75    float            ambient [4];      //!< The ambient color of the Material.
     76    float            specular [4];     //!< The specular color of the Material.
     77    float            shininess;        //!< The shininess of the Material.
     78    float            transparency;     //!< The transperency of the Material.
     79    GLenum           sFactor;
     80    GLenum           tFactor;
     81
     82    std::vector<Texture*> textures;    //!< An Array of Textures.
     83
     84    Texture*         ambientTexture;   //!< The ambient texture of the Material.
     85    Texture*         specularTexture;  //!< The specular texture of the Material.
     86
     87
    7288};
    7389#endif
  • branches/water/src/lib/graphics/importer/texture.cc

    r7221 r7687  
    3434Texture::Texture(const std::string& imageName, GLenum target)
    3535{
     36  this->init();
     37
     38  if (!imageName.empty())
     39  {
     40    this->setName(imageName);
     41    this->loadImage(imageName, target);
     42  }
     43}
     44
     45Texture::Texture(SDL_Surface* surface, GLenum target)
     46{
     47  this->init();
     48
     49  if(surface != NULL)
     50  {
     51    this->loadSurface(surface, target);
     52
     53  }
     54}
     55
     56void Texture::init()
     57{
    3658  this->setClassID(CL_TEXTURE, "Texture");
    3759
     
    4062  this->image = NULL;
    4163  this->priority = 0.5;
    42 
    43   if (!imageName.empty())
    44   {
    45     this->setName(imageName);
    46     this->loadImage(imageName, target);
    47   }
    48 }
    49 
     64}
    5065
    5166/**
     
    6479
    6580/**
    66  * loads an Image from a file to a Texture
     81 * @brief loads an Image from a file to a Texture
    6782 * @param imageName The image to load
    6883*/
     
    7186  if (Texture::texturesEnabled)
    7287  {
    73     if (this->image != NULL)
    74     {
    75       SDL_FreeSurface(this->image);
    76       this->image = NULL;
    77     }
    78     if (this->texture != 0)
    79     {
    80       glDeleteTextures(1, &this->texture);
    81       this->texture = 0;
    82     }
    8388    if (!imageName.empty())
    8489    {
    8590      SDL_Surface* tmpSurf;
    86       if (this->texture != 0 && glIsTexture(this->texture))
    87         glDeleteTextures(1, &this->texture);
     91
    8892      // load the new Image to memory
    8993      tmpSurf = IMG_Load(imageName.c_str());
    9094      if(tmpSurf != NULL)
    9195      {
    92         PRINTF(4)("loading Image %s\n", imageName);
    93         bool hasAlpha;
    94         SDL_Surface* newSurf = this->prepareSurface(tmpSurf, hasAlpha);
    95         if (newSurf != NULL)
    96         {
    97           this->setSurface(newSurf);
    98           this->setAlpha(hasAlpha);
    99           this->setTexture(Texture::loadTexToGL(newSurf, target));
    100         }
    101 
     96        this->loadSurface(tmpSurf, target);
    10297        SDL_FreeSurface(tmpSurf);
    10398        return true;
     
    114109      PRINTF(2)("Image-Name not specified\n");
    115110      return false;
     111    }
     112  }
     113  return false;
     114}
     115
     116/**
     117 * @brief Loads an SDL_Surface.
     118 */
     119bool Texture::loadSurface(SDL_Surface* surface, GLenum target)
     120{
     121  if (Texture::texturesEnabled)
     122  {
     123    // some image was loaded before
     124    if (this->image != NULL)
     125    {
     126      SDL_FreeSurface(this->image);
     127      this->image = NULL;
     128    }
     129
     130    // unload the old Texture.
     131    if (this->texture != 0 && glIsTexture(this->texture))
     132    {
     133      glDeleteTextures(1, &this->texture);
     134      this->texture = 0;
     135    }
     136
     137    bool hasAlpha;
     138    SDL_Surface* newSurf = this->prepareSurface(surface, hasAlpha);
     139    if (newSurf != NULL)
     140    {
     141      this->setSurface(newSurf);
     142      this->setAlpha(hasAlpha);
     143      this->setTexture(Texture::loadTexToGL(newSurf, target));
     144      return true;
    116145    }
    117146  }
     
    264293  glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
    265294
    266   glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR/*_MIPMAP_LINEAR*/);
     295  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    267296  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    268297
  • branches/water/src/lib/graphics/importer/texture.h

    r7221 r7687  
    1919    public:
    2020      Texture(const std::string& imageName = "", GLenum target = GL_TEXTURE_2D);
    21   //  Texture(TEXTURE_TYPE type, int resolution);
     21      Texture(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D);
    2222      virtual ~Texture();
    2323
    2424      bool loadImage(const std::string& imageName, GLenum target = GL_TEXTURE_2D);
     25      bool loadSurface(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D);
    2526      virtual bool rebuild();
    2627
     
    2829      inline GLuint getTexture() const { return this->texture; };
    2930      /** @returns true if texture has alpha, false otherwise */
    30       inline bool hasAlpha() const {return bAlpha;}
     31      inline bool hasAlpha() const  {return bAlpha; }
    3132      /** @returns the stored image of this Texture */
    3233      const SDL_Surface* const getStoredImage() const { return this->image; };
     
    4344
    4445    protected:
    45 
    4646      bool setSurface(SDL_Surface* newSurface);
    4747      bool setAlpha(bool hasAlpha) { this->bAlpha = hasAlpha; };
    4848      bool setTexture(GLuint texture) { this->texture = texture; };
    4949
     50    private:
     51      void init();
    5052
    5153    private:
  • branches/water/src/lib/util/executor/functor_list.h

    r7331 r7687  
    140140
    141141  FUNCTOR_LIST(3)(l_STRING, l_FLOAT, l_UINT);
     142  FUNCTOR_LIST(3)(l_STRING, l_INT, l_UINT);
     143  FUNCTOR_LIST(3)(l_STRING, l_UINT, l_UINT);
    142144
    143145
  • branches/water/src/world_entities/environments/mapped_water.cc

    r7675 r7687  
    1616#include "mapped_water.h"
    1717#include "util/loading/load_param.h"
     18#include "util/loading/factory.h"
     19#include "material.h"
     20
     21
     22CREATE_FACTORY(MappedWater, CL_MAPPED_WATER);
     23
    1824
    1925MappedWater::MappedWater(const TiXmlElement* root)
    2026{
    21 
     27  this->setClassID(CL_MAPPED_WATER, "MappedWater");
     28  this->toList(OM_ENVIRON);
     29 
    2230  if (root != NULL)
    2331    this->loadParams(root);
     
    2634MappedWater::~MappedWater()
    2735{
     36 
    2837}
    2938
     
    3241  WorldEntity::loadParams(root);
    3342
    34   LoadParam(root, "height", this, MappedWater, setHeight);
     43  LoadParam(root, "waterHeight", this, MappedWater, setHeight);
    3544}
    3645
     
    3847void MappedWater::draw() const
    3948{
     49  glPushMatrix();
     50 
     51  /*
     52  glTranslatef (this->getAbsCoor ().x,
     53                this->getAbsCoor ().y,
     54                this->getAbsCoor ().z);
     55  */
     56  glTranslatef(0,this->waterHeight,0);
    4057
     58  //glEnable(GL_LIGHTING);
     59
     60  Material mat;
     61  mat.setDiffuseMap("pictures/ground.tga", GL_TEXTURE_2D, 0);
     62  //mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 1);
     63  //mat.setTransparency(1.0);
     64  //mat.setDiffuse(1.0, 0, .1);
     65  mat.select();
     66
     67  glBegin(GL_QUADS);
     68    glNormal3f(0,1,0);
     69    glTexCoord2f(0,0);
     70    //glMultiTexCoord2f(GL_TEXTURE1, 0,0);
     71    glVertex3f(0,0,0);
     72    glTexCoord2f(1,0);
     73    //glMultiTexCoord2f(GL_TEXTURE1, 1,0);
     74    glVertex3f(100,0,0);
     75    glTexCoord2f(1,1);
     76    //glMultiTexCoord2f(GL_TEXTURE1, 1,1);
     77    glVertex3f(100,0,-100);
     78    glTexCoord2f(0,1);
     79    //glMultiTexCoord2f(GL_TEXTURE1, 0,1);
     80    glVertex3f(0,0,-100);
     81  glEnd();
     82
     83  glPopMatrix();
    4184}
    4285
     
    5093  this->waterHeight = height;
    5194}
     95
     96void MappedWater::activateReflection() {}
     97void MappedWater::deactivateReflection() {}
     98
     99void MappedWater::activateRefraction() {}
     100void MappedWater::deactivateRefraction() {}
  • branches/water/src/world_entities/environments/mapped_water.h

    r7672 r7687  
    2020    void loadParams(const TiXmlElement* root);
    2121
     22    void activateReflection();
     23    void deactivateReflection();
     24   
     25    void activateRefraction();
     26    void deactivateRefraction();
     27   
    2228    void draw() const;
    2329    void tick(float dt);
Note: See TracChangeset for help on using the changeset viewer.