Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5753 in orxonox.OLD


Ignore:
Timestamp:
Nov 24, 2005, 11:36:49 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: cleaner Texture Loading

Location:
trunk/src/lib/graphics/importer
Files:
2 edited

Legend:

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

    r5306 r5753  
    3737  this->bAlpha = false;
    3838  this->texture = 0;
     39  this->image = NULL;
    3940  if (imageName != NULL)
    4041    this->loadImage(imageName);
     
    5051  if (this->texture != 0)
    5152    glDeleteTextures(1, &this->texture);
     53  if (this->image != NULL)
     54    SDL_FreeSurface(this->image);
    5255}
    5356
    54 /**
    55  *  Loads a Texture to the openGL-environment.
    56  * @param surface the Image to load to openGL
    57  * @returns The ID of the texture.
    58 */
    59 GLuint Texture::loadTexToGL (SDL_Surface* surface)
    60 {
    61   if (GraphicsEngine::texturesEnabled)
    62     {
    63       PRINTF(4)("Loading texture to OpenGL-Environment.\n");
    64 
    65       int w, h;
    66       SDL_Surface *image;
    67       SDL_Rect area;
    68       Uint32 saved_flags;
    69       Uint8  saved_alpha;
    70 
    71       w = surface->w;
    72       h = surface->h;
    73 
    74       image = SDL_CreateRGBSurface(SDL_SWSURFACE,
    75                                    w, h,
    76                                    32,
    77 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
    78                                    0x000000FF,
    79                                    0x0000FF00,
    80                                    0x00FF0000,
    81                                    0xFF000000
    82 #else
    83                                    0xFF000000,
    84                                    0x00FF0000,
    85                                    0x0000FF00,
    86                                    0x000000FF
    87 #endif
    88                                    );
    89       if ( image == NULL ) {
    90         return 0;
    91       }
    92 
    93       /* Save the alpha blending attributes */
    94       saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
    95       saved_alpha = surface->format->alpha;
    96       if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
    97         SDL_SetAlpha(surface, 0, 0);
    98       }
    99 
    100       /* Copy the surface into the GL texture image */
    101       area.x = 0;
    102       area.y = 0;
    103       area.w = surface->w;
    104       area.h = surface->h;
    105       SDL_BlitSurface(surface, &area, image, &area);
    106 
    107       /* Restore the alpha blending attributes */
    108       if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
    109         SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
    110         this->bAlpha = true;
    111       }
    112 
    113       /* Create an OpenGL texture for the image */
    114       glGenTextures(1, &this->texture);
    115       glBindTexture(GL_TEXTURE_2D, this->texture);
    116       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    117       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    118       // build the Texture
    119       glTexImage2D(GL_TEXTURE_2D,
    120                    0,
    121                    GL_RGBA,
    122                    w, h,
    123                    0,
    124                    GL_RGBA,
    125                    GL_UNSIGNED_BYTE,
    126                    image->pixels);
    127       // build the MipMaps
    128       gluBuild2DMipmaps(GL_TEXTURE_2D,
    129                         GL_RGBA,
    130                         w,
    131                         h,
    132                         GL_RGBA,
    133                         GL_UNSIGNED_BYTE,
    134                         image->pixels);
    135 
    136       SDL_FreeSurface(image); /* No longer needed */
    137       glBindTexture(GL_TEXTURE_2D, 0);
    138     }
    139 }
    14057
    14158/**
     
    15471          // load the new Image to memory
    15572          tmpSurf = IMG_Load(imageName);
    156           if(tmpSurf == NULL)
     73          if(tmpSurf != NULL)
     74          {
     75            PRINTF(3)("loading Image %s\n", imageName);
     76            this->image = this->prepareSurface(tmpSurf);
     77            this->texture = loadTexToGL(this->image);
     78            SDL_FreeSurface(tmpSurf);
     79            return true;
     80          }
     81          else
    15782            {
    15883              PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
     
    16085              return false;
    16186            }
    162           else
    163           {
    164             PRINTF(3)("loading Image %s\n", imageName);
    165             loadTexToGL(tmpSurf);
    166             SDL_FreeSurface(tmpSurf);
    167             return true;
    168           }
    16987        }
    17088      else
     
    17593    return false;
    17694}
     95
     96SDL_Surface* Texture::prepareSurface(SDL_Surface* surface)
     97{
     98  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
     99 
     100  SDL_Surface *image;
     101  SDL_Rect area;
     102  Uint32 saved_flags;
     103  Uint8  saved_alpha;
     104 
     105  image = SDL_CreateRGBSurface(SDL_SWSURFACE,
     106                               surface->w, surface->h,
     107                               32,
     108#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
     109                               0x000000FF,
     110                               0x0000FF00,
     111                               0x00FF0000,
     112                               0xFF000000
     113#else
     114                               0xFF000000,
     115                               0x00FF0000,
     116                               0x0000FF00,
     117                               0x000000FF
     118#endif
     119                               );
     120  if ( image == NULL ) {
     121    return 0;
     122  }
     123 
     124  /* Save the alpha blending attributes */
     125  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
     126  saved_alpha = surface->format->alpha;
     127  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
     128    SDL_SetAlpha(surface, 0, 0);
     129  }
     130 
     131  /* Copy the surface into the GL texture image */
     132  area.x = 0;
     133  area.y = 0;
     134  area.w = surface->w;
     135  area.h = surface->h;
     136  SDL_BlitSurface(surface, &area, image, &area);
     137 
     138  /* Restore the alpha blending attributes */
     139  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
     140    {
     141      SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
     142      this->bAlpha = true;
     143    }
     144  return image;
     145}
     146
     147
     148/**
     149 *  Loads a Texture to the openGL-environment.
     150 * @param surface the Image to load to openGL
     151 * @returns The ID of the texture.
     152 */
     153GLuint Texture::loadTexToGL (SDL_Surface* surface) const
     154{
     155  GLuint texture = 0;
     156  /* Create an OpenGL texture for the image */
     157  glGenTextures(1, &texture);
     158  glBindTexture(GL_TEXTURE_2D, texture);
     159  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     160  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     161  // build the Texture
     162  glTexImage2D(GL_TEXTURE_2D,
     163               0,
     164               GL_RGBA,
     165               surface->w, surface->h,
     166               0,
     167               GL_RGBA,
     168               GL_UNSIGNED_BYTE,
     169               surface->pixels);
     170  // build the MipMaps
     171  gluBuild2DMipmaps(GL_TEXTURE_2D,
     172                    GL_RGBA,
     173                    surface->w,
     174                    surface->h,
     175                    GL_RGBA,
     176                    GL_UNSIGNED_BYTE,
     177                    surface->pixels);
     178  glBindTexture(GL_TEXTURE_2D, 0);
     179  return texture;
     180}
  • trunk/src/lib/graphics/importer/texture.h

    r5308 r5753  
    2626  Texture(const char* imageName = NULL);
    2727  //  Texture(TEXTURE_TYPE type, int resolution);
     28  ~Texture();
    2829
    29   ~Texture();
     30  bool loadImage(const char* imageName);
     31  bool reload();
    3032
    3133  /** @returns The textureID of this texture.  */
    3234  inline GLuint getTexture() const { return this->texture; };
    33   GLuint loadTexToGL (SDL_Surface* surface);
    3435  /** @returns true if texture has alpha, false otherwise */
    3536  inline bool hasAlpha() const {return bAlpha;}
    3637
    37   bool loadImage(const char* imageName);
    38 
     38 private:
     39  SDL_Surface* prepareSurface(SDL_Surface* input);
     40  GLuint loadTexToGL (SDL_Surface* surface) const;
    3941
    4042 private:
    41   GLuint     texture;          //!< The Texture-ID of opengl from this Texture.
    42   bool       bAlpha;           //!< if the texture has an alpha channel.
     43  GLuint        texture;            //!< The Texture-ID of opengl from this Texture.
     44  bool          bAlpha;             //!< if the texture has an alpha channel.
     45  SDL_Surface*  image;   
    4346};
    4447
Note: See TracChangeset for help on using the changeset viewer.