Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 24, 2006, 2:49:58 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: image-data splitted out of Texture

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/water/src/lib/graphics/importer/texture.cc

    r7700 r7783  
    2020#include "debug.h"
    2121#include "compiler.h"
    22 #include <math.h>
    2322
    2423// INCLUDING SDL_Image
     
    3029
    3130
    32 
    33 
    34 Texture::Texture(GLenum target)
    35 {
    36   this->init();
    37   this->generateTexture(texture, target);
    38 }
    39 
    40 /**
    41  *  Constructor for a Texture
    42 */
    43 Texture::Texture(const std::string& imageName, GLenum target)
    44 {
    45   this->init();
    46 
    47   if (!imageName.empty())
    48   {
    49     this->setName(imageName);
    50     this->loadImage(imageName, target);
    51   }
    52 }
    53 
    54 
    55 
    56 Texture::Texture(SDL_Surface* surface, GLenum target)
    57 {
    58   this->init();
    59 
    60   if(surface != NULL)
    61   {
    62     this->loadSurface(surface, target);
    63 
    64   }
    65 }
    66 
    67 void Texture::init()
    68 {
    69   this->setClassID(CL_TEXTURE, "Texture");
    70 
     31TextureData::TextureData()
     32{
    7133  this->bAlpha = false;
    7234  this->texture = 0;
    7335  this->image = NULL;
    74   this->priority = 0.5;
    75 }
    76 
    77 /**
    78  * Destructor of a Texture
    79 
    80    Frees Data, and deletes the textures from GL
    81 */
    82 Texture::~Texture()
     36}
     37
     38
     39/**
     40 * @brief Destructor of a Texture
     41 *
     42 *  Frees Data, and deletes the textures from GL
     43 */
     44TextureData::~TextureData()
    8345{
    8446  if (this->texture != 0)
     
    9052
    9153/**
     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 *  Destructor of a Texture
     152
     153   Frees Data, and deletes the textures from GL
     154*/
     155Texture::~Texture()
     156{
     157}
     158
     159
     160/**
    92161 * @brief loads an Image from a file to a Texture
    93162 * @param imageName The image to load
     
    105174      if(tmpSurf != NULL)
    106175      {
    107         this->loadSurface(tmpSurf, target);
     176        this->data->loadSurface(tmpSurf, target);
    108177        SDL_FreeSurface(tmpSurf);
    109178        return true;
     
    112181      {
    113182        PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
    114         this->texture = 0;
     183        this->setTexture(0);
    115184        return false;
    116185      }
     
    126195
    127196/**
    128  * @brief Loads an SDL_Surface.
    129  */
    130 bool Texture::loadSurface(SDL_Surface* surface, GLenum target)
    131 {
    132   if (Texture::texturesEnabled)
    133   {
    134     // some image was loaded before
    135     if (this->image != NULL)
    136     {
    137       SDL_FreeSurface(this->image);
    138       this->image = NULL;
    139     }
    140 
    141     // unload the old Texture.
    142     if (this->texture != 0 && glIsTexture(this->texture))
    143     {
    144       glDeleteTextures(1, &this->texture);
    145       this->texture = 0;
    146     }
    147 
    148     bool hasAlpha;
    149     SDL_Surface* newSurf = this->prepareSurface(surface, hasAlpha);
    150     if (newSurf != NULL)
    151     {
    152       this->setSurface(newSurf);
    153       this->setAlpha(hasAlpha);
    154       this->setTexture(Texture::loadTexToGL(newSurf, target));
    155       return true;
    156     }
    157   }
    158   return false;
    159 }
    160 
    161 
    162 /**
    163197 * @brief rebuilds the texture.
    164198 * reloads the Texture from Memory to OpenGL.
     
    166200bool Texture::rebuild()
    167201{
    168   if (this->texture != 0)
    169   {
    170     if (glIsTexture(this->texture))
    171       glDeleteTextures(1,&this->texture);
    172     this->setTexture(0);
    173   }
    174 
    175   if (this->image != NULL)
     202  this->data->setTexture(0);
     203
     204  if (this->data->getStoredImage() != NULL)
    176205  {
    177206    PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());
    178     this->setTexture(loadTexToGL(this->image));
    179   }
    180 }
    181 
    182 
    183 /**
    184  * @brief set the surface this Texture handles
    185  * @param newSurface the new Surface to set as the image for this Texture.
    186  *
    187  * This deletes the old version of the stored Texture,
    188  * and sets the newly given Surface as current.
    189  */
    190 bool Texture::setSurface(SDL_Surface* newSurface)
    191 {
    192   if (this->image != NULL)
    193     SDL_FreeSurface(this->image);
    194 
    195   this->image = newSurface;
    196 
    197   return (this->image != NULL);
    198 }
    199 
     207    this->setTexture(Texture::loadTexToGL(this->data->getStoredImage()));
     208  }
     209}
    200210
    201211bool Texture::texturesEnabled = true;
     
    220230 * @returns a !!new!! Surface, that is loadable by openGL.
    221231 */
    222 SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) const
     232SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha)
    223233{
    224234  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
     
    258268  }
    259269
    260   /* Copy the surface into the GL texture image */
     270  /* Copy the surface into the GL texture this->data->getStoredImage() */
    261271  area.x = 0;
    262272  area.y = 0;
     
    281291 * @returns The ID of the texture.
    282292 */
    283 GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target) const
    284 {
    285   //   if (this->texture != 0 && glIsTexture(this->texture))
    286   //     glDeleteTextures(1, &this->texture);
    287   //   this->texture = 0;
     293GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target)
     294{
     295  //   if (this->data->getTexture() != 0 && glIsTexture(this->data->getTexture()))
     296  //     glDeleteTextures(1, &this->data->getTexture());
     297  //   this->data->getTexture() = 0;
    288298
    289299  int      errorCode = 0;           //!< the error code for the texture loading functions
     
    297307    return 0;
    298308
    299   /* Create an OpenGL texture for the image */
    300   this->generateTexture(texture, target);
    301   glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);
     309  /* Create an OpenGL texture for the this->data->getStoredImage() */
     310  Texture::generateTexture(texture, target);
     311
     312  /// TODO CHECK THIS BACK in
     313  //glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);
    302314
    303315  /* build the Texture  OpenGL V >= 1.1 */
     
    330342void Texture::generateTexture(GLuint& texture, GLenum target)
    331343{
    332   if (!glIsTexture(texture))
     344  if (texture == 0 && !glIsTexture(texture))
    333345  {
    334346    glGenTextures(1, &texture);
     
    346358  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
    347359}
    348 
Note: See TracChangeset for help on using the changeset viewer.