Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7785 in orxonox.OLD for trunk/src/lib/graphics/importer/texture.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/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}
Note: See TracChangeset for help on using the changeset viewer.