Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


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

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

Location:
trunk/src/lib/graphics/importer
Files:
4 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}
  • trunk/src/lib/graphics/importer/material.h

    r7221 r7785  
    11/*!
    2   \file material.h
    3   \brief Contains the Material Class that handles Material for 3D-Objects.
    4   @todo free SDL-surface when deleting Material.
    5   @todo delete imgNameWithPath after use creation.
    6 */
     2 * @file material.h
     3 * @brief Contains the Material Class that handles Material for 3D-Objects.
     4 * @todo free SDL-surface when deleting Material.
     5 * @todo delete imgNameWithPath after use creation.
     6 */
    77
    88#ifndef _MATERIAL_H
     
    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(Texture* texture, unsigned int textureNumber = 0);
     56    void setDiffuseMap(const std::string& dMap, GLenum target = GL_TEXTURE_2D, unsigned int textureNumber = 0);
     57    void setSDLDiffuseMap(SDL_Surface *surface, GLenum target = GL_TEXTURE_2D, unsigned int textureNumber = 0);
     58
     59    void setAmbientMap(const std::string& aMap, GLenum target = GL_TEXTURE_2D);
     60    void setSpecularMap(const std::string& sMap, GLenum target = GL_TEXTURE_2D);
     61    void setBump(const std::string& bump);
     62    GLuint getDiffuseTexture(unsigned int i = 0) const { return (this->textures.size() > i)? this->textures[i]->getTexture() : 0; };
     63
     64    static void addTexturePath(const std::string& pathName);
     65
     66  public:
     67    static const GLenum glTextureArbs[];  //!< The Texture ARB's
     68
     69    static int getMaxTextureUnits();
    5870
    5971  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;
     72    static const Material* selectedMaterial; //!< The currently selected material.
    6873
    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.
     74    int              illumModel;       //!< The IlluminationModel is either flat or smooth.
     75    float            diffuse [4];      //!< The diffuse color of the Material.
     76    float            ambient [4];      //!< The ambient color of the Material.
     77    float            specular [4];     //!< The specular color of the Material.
     78    float            shininess;        //!< The shininess of the Material.
     79    float            transparency;     //!< The transperency of the Material.
     80    GLenum           sFactor;
     81    GLenum           tFactor;
     82
     83    std::vector<Texture*> textures;    //!< An Array of Textures.
     84
     85    Texture*         ambientTexture;   //!< The ambient texture of the Material.
     86    Texture*         specularTexture;  //!< The specular texture of the Material.
     87
     88
    7289};
    7390#endif
  • trunk/src/lib/graphics/importer/texture.cc

    r7751 r7785  
    2020#include "debug.h"
    2121#include "compiler.h"
    22 #include <math.h>
    2322
    2423// INCLUDING SDL_Image
     
    2928#endif
    3029
    31 /**
    32  * @brief Constructor for a Texture
    33 */
    34 Texture::Texture(const std::string& imageName, GLenum target)
    35 {
    36   this->setClassID(CL_TEXTURE, "Texture");
    37 
     30
     31TextureData::TextureData()
     32{
    3833  this->bAlpha = false;
    3934  this->texture = 0;
    4035  this->image = NULL;
    41   this->priority = 0.5;
    42 
    43   if (!imageName.empty())
    44   {
    45     this->setName(imageName);
    46     this->loadImage(imageName, target);
    47   }
    4836}
    4937
     
    5240 * @brief Destructor of a Texture
    5341 *
    54  * Frees Data, and deletes the textures from GL
    55  */
    56 Texture::~Texture()
     42 *  Frees Data, and deletes the textures from GL
     43 */
     44TextureData::~TextureData()
    5745{
    5846  if (this->texture != 0)
     
    6452
    6553/**
     54 * @brief Loads an SDL_Surface.
     55 */
     56bool TextureData::loadSurface(SDL_Surface* surface, GLenum target)
     57{
     58  if (Texture::getTextureEnableState())
     59  {
     60    SDL_Surface* newSurf = Texture::prepareSurface(surface, this->bAlpha);
     61    if (newSurf != NULL)
     62    {
     63      this->setSurface(newSurf);
     64      this->setTexture(Texture::loadTexToGL(newSurf, target));
     65      return true;
     66    }
     67  }
     68  return false;
     69}
     70
     71
     72
     73/**
     74 * @brief set the surface this Texture handles
     75 * @param newSurface the new Surface to set as the image for this Texture.
     76 *
     77 * This deletes the old version of the stored Texture,
     78 * and sets the newly given Surface as current.
     79 */
     80bool TextureData::setSurface(SDL_Surface* newSurface)
     81{
     82  if (this->image != NULL)
     83    SDL_FreeSurface(this->image);
     84
     85  this->image = newSurface;
     86
     87  return (this->image != NULL);
     88}
     89
     90
     91
     92bool TextureData::setTexture(GLuint texture)
     93{
     94     // unload the old Texture.
     95  if (this->texture != 0 && glIsTexture(this->getTexture()))
     96  {
     97    glDeleteTextures(1, &this->texture);
     98  }
     99  this->texture = texture;
     100  return (texture != 0);
     101}
     102
     103
     104
     105
     106
     107Texture::Texture(GLenum target)
     108{
     109  this->init();
     110  GLuint texture;
     111  this->generateTexture(texture, target);
     112  this->data->setTexture(texture);
     113}
     114
     115/**
     116 *  Constructor for a Texture
     117 */
     118Texture::Texture(const std::string& imageName, GLenum target)
     119{
     120  this->init();
     121
     122  if (!imageName.empty())
     123  {
     124    this->setName(imageName);
     125    this->loadImage(imageName, target);
     126  }
     127}
     128
     129
     130
     131Texture::Texture(SDL_Surface* surface, GLenum target)
     132{
     133  this->init();
     134
     135  if(surface != NULL)
     136  {
     137    this->data->loadSurface(surface, target);
     138  }
     139}
     140
     141void Texture::init()
     142{
     143  this->setClassID(CL_TEXTURE, "Texture");
     144
     145  this->data = CountPointer<TextureData>(new TextureData());
     146
     147  this->priority = 0.5;
     148}
     149
     150/**
     151 * @brief Destructor of a Texture
     152 *
     153 * Frees Data, and deletes the textures from GL
     154 */
     155Texture::~Texture()
     156{
     157}
     158
     159
     160/**
    66161 * @brief loads an Image from a file to a Texture
    67162 * @param imageName The image to load
     
    71166  if (Texture::texturesEnabled)
    72167  {
    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     }
    83168    if (!imageName.empty())
    84169    {
    85170      SDL_Surface* tmpSurf;
    86       if (this->texture != 0 && glIsTexture(this->texture))
    87         glDeleteTextures(1, &this->texture);
     171
    88172      // load the new Image to memory
    89173      tmpSurf = IMG_Load(imageName.c_str());
    90174      if(tmpSurf != NULL)
    91175      {
    92         PRINTF(4)("loading Image %s\n", imageName.c_str());
    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 
     176        this->data->loadSurface(tmpSurf, target);
    102177        SDL_FreeSurface(tmpSurf);
    103178        return true;
     
    106181      {
    107182        PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
    108         this->texture = 0;
     183        this->setTexture(0);
    109184        return false;
    110185      }
     
    119194}
    120195
    121 
    122196/**
    123197 * @brief rebuilds the texture.
     
    127201bool Texture::rebuild()
    128202{
    129   if (this->texture != 0)
    130   {
    131     if (glIsTexture(this->texture))
    132       glDeleteTextures(1,&this->texture);
    133     this->setTexture(0);
    134   }
    135 
    136   if (this->image != NULL)
     203  this->data->setTexture(0);
     204
     205  if (this->data->getStoredImage() != NULL)
    137206  {
    138207    PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());
    139     this->setTexture(loadTexToGL(this->image));
    140   }
    141 }
    142 
    143 
    144 /**
    145  * @brief set the surface this Texture handles
    146  * @param newSurface the new Surface to set as the image for this Texture.
    147  *
    148  * This deletes the old version of the stored Texture,
    149  * and sets the newly given Surface as current.
    150  */
    151 bool Texture::setSurface(SDL_Surface* newSurface)
    152 {
    153   if (this->image != NULL)
    154     SDL_FreeSurface(this->image);
    155 
    156   this->image = newSurface;
    157 
    158   return (this->image != NULL);
    159 }
    160 
     208    this->setTexture(Texture::loadTexToGL(this->data->getStoredImage()));
     209  }
     210}
    161211
    162212bool Texture::texturesEnabled = true;
     
    181231 * @returns a !!new!! Surface, that is loadable by openGL.
    182232 */
    183 SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) const
     233SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha)
    184234{
    185235  assert(surface != NULL);
     
    222272    return NULL;
    223273
    224   /* Copy the surface into the GL texture image */
     274  /* Copy the surface into the GL texture this->data->getStoredImage() */
    225275  area.x = 0;
    226276  area.y = 0;
     
    245295 * @returns The ID of the texture.
    246296 */
    247 GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target) const
    248 {
    249   //   if (this->texture != 0 && glIsTexture(this->texture))
    250   //     glDeleteTextures(1, &this->texture);
    251   //   this->texture = 0;
     297GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target)
     298{
     299  //   if (this->data->getTexture() != 0 && glIsTexture(this->data->getTexture()))
     300  //     glDeleteTextures(1, &this->data->getTexture());
     301  //   this->data->getTexture() = 0;
    252302  assert(surface != NULL);
    253303
    254304  int      errorCode = 0;           //!< the error code for the texture loading functions
    255   GLuint   texture;                 //!< the OpenGL texture handle
     305  GLuint   texture = 0;             //!< the OpenGL texture handle
    256306  int      mipmapLevel = 0;         //!< the maximum mipmap level for this texture
    257307  int      mipmapWidth = 0;         //!< the width of the mipmap
    258308  int      mipmapHight = 0;         //!< the height of the mipmap
    259309  GLenum   format = GL_RGB;
    260   if (this->bAlpha)
     310  if (surface->format->BitsPerPixel == 32)
    261311  {
    262312    format = GL_RGBA;
     
    268318  }
    269319
    270   /* Create an OpenGL texture for the image */
    271   glGenTextures(1, &texture);
    272   glBindTexture(target, texture);
     320  /* Create an OpenGL texture for the this->data->getStoredImage() */
     321  Texture::generateTexture(texture, target);
    273322
    274323//   glTexImage2D(target,  0,  format,
     
    277326//                surface->pixels);
    278327
    279   glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
    280   glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
    281 
    282   glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR/*_MIPMAP_LINEAR*/);
    283   glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    284 
    285   glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);
    286 
    287 
    288   /* control the mipmap levels */
    289   glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);
    290   glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
     328///  glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
     329///  glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
     330
     331  /// TODO CHECK THIS BACK in
     332  //glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);
    291333
    292334  /* build the Texture  OpenGL V >= 1.1 */
     
    306348}
    307349
     350void Texture::generateTexture(GLuint& texture, GLenum target)
     351{
     352  if (texture == 0 && !glIsTexture(texture))
     353  {
     354    glGenTextures(1, &texture);
     355  }
     356  glBindTexture(target, texture);
     357
     358  glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
     359  glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
     360
     361  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
     362  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     363
     364  /* control the mipmap levels */
     365  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);
     366  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
     367}
  • trunk/src/lib/graphics/importer/texture.h

    r7727 r7785  
    1010
    1111#include "glincl.h"
     12#include "count_pointer.h"
    1213
    1314/* Forward Declaration */
    1415struct SDL_Surface;
    1516
    16 //! A Class, that reads in Textures from different fileformats.
    17   class Texture : public BaseObject
    18   {
    19     public:
    20       Texture(const std::string& imageName = "", GLenum target = GL_TEXTURE_2D);
    21   //  Texture(TEXTURE_TYPE type, int resolution);
    22       virtual ~Texture();
    2317
    24       bool loadImage(const std::string& imageName, GLenum target = GL_TEXTURE_2D);
    25       virtual bool rebuild();
     18class TextureData
     19{
     20  public:
     21    TextureData();
     22    ~TextureData();
    2623
    27       /** @returns The textureID of this texture.  */
    28       inline GLuint getTexture() const { return this->texture; };
    29       /** @returns true if texture has alpha, false otherwise */
    30       inline bool hasAlpha() const {return bAlpha;}
    31       /** @returns the stored image of this Texture */
    32       const SDL_Surface* const getStoredImage() const { return this->image; };
     24    inline GLuint getTexture() const { return this->texture; };
     25    /** @returns true if texture has alpha, false otherwise */
     26    inline bool hasAlpha() const  {return this->bAlpha; }
     27    /** @returns the stored image of this Texture */
     28    const SDL_Surface* const getStoredImage() const { return this->image; };
     29
     30    bool loadSurface(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D);
    3331
    3432
    3533
    36       static void setTextureEnableState(bool texturesEnabled);
    37       /** @returns true if Textures are enabled */
    38       inline static bool getTextureEnableState() { return Texture::texturesEnabled; };
     34    bool rebuild();
    3935
    40       // Utility functionality:
    41       SDL_Surface* prepareSurface(SDL_Surface* input, bool& hasAlpha) const;
    42       GLuint loadTexToGL (const SDL_Surface* surface, GLenum target = GL_TEXTURE_2D) const;
     36    bool setSurface(SDL_Surface* newSurface);
     37    bool setAlpha(bool hasAlpha) { this->bAlpha = hasAlpha; };
     38    bool setTexture(GLuint texture);
    4339
    44     protected:
    45       bool setSurface(SDL_Surface* newSurface);
    46       bool setAlpha(bool hasAlpha) { this->bAlpha = hasAlpha; };
    47       bool setTexture(GLuint texture) { this->texture = texture; };
     40  private:
     41    GLuint           texture;            //!< The Texture-ID of opengl from this Texture.
     42    bool             bAlpha;             //!< if the texture has an alpha channel.
     43    SDL_Surface*     image;              //!< The SDL_Surfce that stores the Texture on it.
     44};
    4845
    49     private:
    50       GLuint           texture;            //!< The Texture-ID of opengl from this Texture.
    51       bool             bAlpha;             //!< if the texture has an alpha channel.
    52       SDL_Surface*     image;              //!< The SDL_Surfce that stores the Texture on it.
    53       GLclampf         priority;           //!< the priority of the current texture (used for garphics cards with limited mem)
    5446
    55       static bool      texturesEnabled;    //!< If the Textures are enabled.
    56   };
     47//! A Class, that reads in Textures from different fileformats.
     48class Texture : public BaseObject
     49{
     50public:
     51
     52public:
     53  Texture(GLenum target = GL_TEXTURE_2D);
     54  Texture(const std::string& imageName, GLenum target = GL_TEXTURE_2D);
     55  Texture(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D);
     56
     57  virtual ~Texture();
     58
     59  bool loadImage(const std::string& imageName, GLenum target = GL_TEXTURE_2D);
     60  bool loadSurface(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D);
     61  virtual bool rebuild();
     62
     63  /** @returns The textureID of this texture.  */
     64  inline GLuint getTexture() const { return this->data->getTexture(); };
     65  /** @returns true if texture has alpha, false otherwise */
     66  inline bool hasAlpha() const  { return this->data->hasAlpha(); }
     67  /** @returns the stored image of this Texture */
     68  const SDL_Surface* const getStoredImage() const { return this->data->getStoredImage(); };
     69
     70
     71
     72  static void setTextureEnableState(bool texturesEnabled);
     73  /** @returns true if Textures are enabled */
     74  inline static bool getTextureEnableState() { return Texture::texturesEnabled; };
     75  // Utility functionality:
     76  static SDL_Surface* prepareSurface(SDL_Surface* input, bool& hasAlpha);
     77  static GLuint loadTexToGL (const SDL_Surface* surface, GLenum target = GL_TEXTURE_2D);
     78
     79protected:
     80  bool setSurface(SDL_Surface* newSurface) { return this->data->setSurface(newSurface); };
     81  bool setAlpha(bool hasAlpha) { return this->data->setAlpha(hasAlpha); };
     82  bool setTexture(GLuint texture) { return this->data->setTexture(texture); };
     83
     84private:
     85  void init();
     86  static void generateTexture(GLuint& texture, GLenum target);
     87
     88private:
     89  CountPointer<TextureData>     data;               //!< The TextureData
     90  GLclampf                      priority;           //!< the priority of the current texture (used for garphics cards with limited mem)
     91
     92  static bool                   texturesEnabled;    //!< If the Textures are enabled.
     93};
    5794
    5895#endif /* _TEXTURE_H */
Note: See TracChangeset for help on using the changeset viewer.