Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7783 in orxonox.OLD


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

orxonox/trunk: image-data splitted out of Texture

Location:
branches/water/src/lib/graphics/importer
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/water/src/lib/graphics/importer/material.h

    r7700 r7783  
    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
  • 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 
  • branches/water/src/lib/graphics/importer/texture.h

    r7700 r7783  
    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(GLenum target = GL_TEXTURE_2D);
    21       Texture(const std::string& imageName, GLenum target = GL_TEXTURE_2D);
    22       Texture(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D);
    23       virtual ~Texture();
    2417
    25       bool loadImage(const std::string& imageName, GLenum target = GL_TEXTURE_2D);
    26       bool loadSurface(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D);
    27       virtual bool rebuild();
     18class TextureData
     19{
     20  public:
     21    TextureData();
     22    ~TextureData();
    2823
    29       /** @returns The textureID of this texture.  */
    30       inline GLuint getTexture() const { return this->texture; };
    31       /** @returns true if texture has alpha, false otherwise */
    32       inline bool hasAlpha() const  {return bAlpha; }
    33       /** @returns the stored image of this Texture */
    34       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);
    3531
    3632
    3733
    38       static void setTextureEnableState(bool texturesEnabled);
    39       /** @returns true if Textures are enabled */
    40       inline static bool getTextureEnableState() { return Texture::texturesEnabled; };
     34    bool rebuild();
    4135
    42       // Utility functionality:
    43       SDL_Surface* prepareSurface(SDL_Surface* input, bool& hasAlpha) const;
    44       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);
    4539
    46     protected:
    47       bool setSurface(SDL_Surface* newSurface);
    48       bool setAlpha(bool hasAlpha) { this->bAlpha = hasAlpha; };
    49       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};
    5045
    51     private:
    52       void init();
    53       static void generateTexture(GLuint& texture, GLenum target);
    5446
    55     private:
    56       GLuint           texture;            //!< The Texture-ID of opengl from this Texture.
    57       bool             bAlpha;             //!< if the texture has an alpha channel.
    58       SDL_Surface*     image;              //!< The SDL_Surfce that stores the Texture on it.
    59       GLclampf         priority;           //!< the priority of the current texture (used for garphics cards with limited mem)
     47//! A Class, that reads in Textures from different fileformats.
     48class Texture : public BaseObject
     49{
     50public:
    6051
    61       static bool      texturesEnabled;    //!< If the Textures are enabled.
    62   };
     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
     76  // Utility functionality:
     77  static SDL_Surface* prepareSurface(SDL_Surface* input, bool& hasAlpha);
     78  static GLuint loadTexToGL (const SDL_Surface* surface, GLenum target = GL_TEXTURE_2D);
     79
     80
     81protected:
     82  bool setSurface(SDL_Surface* newSurface) { return this->data->setSurface(newSurface); };
     83  bool setAlpha(bool hasAlpha) { return this->data->setAlpha(hasAlpha); };
     84  bool setTexture(GLuint texture) { return this->data->setTexture(texture); };
     85
     86private:
     87  void init();
     88  static void generateTexture(GLuint& texture, GLenum target);
     89
     90private:
     91  CountPointer<TextureData>     data;               //!< The TextureData
     92  GLclampf                      priority;           //!< the priority of the current texture (used for garphics cards with limited mem)
     93
     94  static bool                   texturesEnabled;    //!< If the Textures are enabled.
     95};
    6396
    6497#endif /* _TEXTURE_H */
Note: See TracChangeset for help on using the changeset viewer.