Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 14, 2006, 10:37:08 AM (18 years ago)
Author:
bensch
Message:

trunk: splitted Texture and TextureData into two files.
Also fixed the Creator-Function for Textures with empty textures with size

File:
1 copied

Legend:

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

    r8362 r8363  
    2121#include "compiler.h"
    2222
    23 #ifdef HAVE_SDL_SDL_H
    24 #include <SDL/SDL_image.h>
    25 #include <SDL/SDL_endian.h>
    26 #include <SDL/SDL_byteorder.h>
    27 #else
    28 #include <SDL_endian.h>
    29 #include <SDL_image.h>
    30 #include <SDL_byteorder.h>
    31 #endif
    32 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
    33 /*
    34  * On the BIG_ENDIAN architecture, the 24 and 32bit bitmaps have
    35  * different masks. If you don't do this distinction properly,
    36  * you will get weird-looking textures.
     23#include "sdlincl.h"
     24
     25/**
     26 * @brief creates a new Texture Data.
    3727 */
    38 Uint32 alphaMask[] = {
    39         0xFF000000,
    40         0x00FF0000,
    41         0x0000FF00,
    42         0x000000FF,
    43 };
    44 
    45 Uint32 opaqueMask[] = {
    46         0x00FF0000,
    47         0x0000FF00,
    48         0x000000FF,
    49         0xFF000000
    50 };
    51 #else
    52 /*
    53  * On the LIL_ENDIAN architecture everything is fine and easy. The 24
    54  * and 32bit bitmaps have the same masks.
    55  */
    56 Uint32 alphaMask[] = {
    57         0x000000FF,
    58         0x0000FF00,
    59         0x00FF0000,
    60         0xFF000000,
    61 };
    62 
    63 Uint32 *opaqueMask = alphaMask;
    64 
    65 #endif
    66 
    6728TextureData::TextureData()
    6829{
     
    8950/**
    9051 * @brief Loads an SDL_Surface.
     52 * @param surface the Surface to Load.
     53 * @param target the GL-Target to load the Surface to default GL_TEXTURE_2D
     54 * @returns true on success, false otherwise.
    9155 */
    9256bool TextureData::loadSurface(SDL_Surface* surface, GLenum target)
     
    11074 * @brief set the surface this Texture handles
    11175 * @param newSurface the new Surface to set as the image for this Texture.
     76 * @returns true on success.
    11277 *
    11378 * This deletes the old version of the stored Texture,
     
    12590
    12691
    127 
     92/**
     93 * @brief sets a new Texture to the Data.
     94 * @param texture the Texture
     95 * @returns true on success.
     96 */
    12897bool TextureData::setTexture(GLuint texture)
    12998{
     
    136105  return (texture != 0);
    137106}
    138 
    139 Texture::Texture()
    140 {
    141   this->init();
    142 }
    143 
    144 
    145 Texture::Texture(const Texture& texture)
    146   : data(texture.data)
    147 {
    148   this->setClassID(CL_TEXTURE, "Texture");
    149   this->priority = 0.5;
    150 }
    151 
    152 
    153 Texture::Texture(GLenum target, unsigned int width, unsigned int height, unsigned int channels, GLenum type)
    154 {
    155   this->init();
    156   GLuint texture = 0;
    157   Texture::generateTexture(texture, target);
    158 
    159   glBindTexture(target, texture);
    160 
    161   unsigned int* pixels = new unsigned int[width * height * channels];
    162   memset(pixels, 0, width * height * channels * sizeof(unsigned int));
    163 
    164 
    165   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    166   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    167   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    168   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    169 
    170   glTexImage2D(target, 0, channels, width, height, 0, type, GL_UNSIGNED_INT, pixels);
    171 
    172 
    173 
    174   delete[] pixels;
    175 
    176   this->data->setTexture(texture);
    177 }
    178 
    179 /**
    180  *  Constructor for a Texture
    181  */
    182 Texture::Texture(const std::string& imageName, GLenum target)
    183 {
    184   this->init();
    185 
    186   if (!imageName.empty())
    187   {
    188     this->setName(imageName);
    189     this->loadImage(imageName, target);
    190   }
    191 }
    192 
    193 
    194 
    195 Texture::Texture(SDL_Surface* surface, GLenum target)
    196 {
    197   this->init();
    198 
    199   if(surface != NULL)
    200   {
    201     this->data->loadSurface(surface, target);
    202   }
    203 }
    204 
    205 void Texture::init()
    206 {
    207   this->setClassID(CL_TEXTURE, "Texture");
    208 
    209   this->data = CountPointer<TextureData>(new TextureData());
    210 
    211   this->priority = 0.5;
    212 }
    213 
    214 /**
    215  * @brief Destructor of a Texture
    216  *
    217  * Frees Data, and deletes the textures from GL
    218  */
    219 Texture::~Texture()
    220 {
    221 }
    222 
    223 
    224 /**
    225  * @brief loads an Image from a file to a Texture
    226  * @param imageName The image to load
    227 */
    228 bool Texture::loadImage(const std::string& imageName, GLenum target)
    229 {
    230   if (Texture::texturesEnabled)
    231   {
    232     if (!imageName.empty())
    233     {
    234       SDL_Surface* tmpSurf;
    235 
    236       // load the new Image to memory
    237       tmpSurf = IMG_Load(imageName.c_str());
    238       if(tmpSurf != NULL)
    239       {
    240         this->data->loadSurface(tmpSurf, target);
    241         SDL_FreeSurface(tmpSurf);
    242         return true;
    243       }
    244       else
    245       {
    246         PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
    247         this->setTexture(0);
    248         return false;
    249       }
    250     }
    251     else
    252     {
    253       PRINTF(2)("Image-Name not specified\n");
    254       return false;
    255     }
    256   }
    257   return false;
    258 }
    259 
    260 /**
    261  * @brief rebuilds the texture.
    262  *
    263  * reloads the Texture from Memory to OpenGL.
    264  */
    265 bool Texture::rebuild()
    266 {
    267   this->data->setTexture(0);
    268 
    269   if (this->data->getStoredImage() != NULL)
    270   {
    271     PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());
    272     this->setTexture(Texture::loadTexToGL(this->data->getStoredImage()));
    273   }
    274   return true;
    275 }
    276 
    277 bool Texture::texturesEnabled = true;
    278 
    279 /**
    280  * @brief enables, disables textures
    281  * @param texturesEnabled true if the textures should be enabled
    282  */
    283 void Texture::setTextureEnableState(bool texturesEnabled)
    284 {
    285   Texture::texturesEnabled = texturesEnabled;
    286 }
    287 
    288 
    289 //////////////////////////////////////
    290 // UTILITY FUNCTIONALITY OF TEXTURE //
    291 //////////////////////////////////////
    292 /**
    293  * @brief converts surface to a new SDL_Surface, that is loadable by openGL
    294  * @param surface the Surface to convert
    295  * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false.
    296  * @returns a !!new!! Surface, that is loadable by openGL.
    297  */
    298 SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha)
    299 {
    300   assert(surface != NULL);
    301   PRINTF(4)("Loading texture to OpenGL-Environment.\n");
    302 
    303   SDL_Surface* retSurface;
    304   SDL_Rect area;
    305   Uint32 saved_flags;
    306   Uint8  saved_alpha;
    307   hasAlpha = false;
    308   int pixelDepth = 24;
    309 
    310         Uint32* mask = opaqueMask;
    311 
    312   /* Save the alpha blending attributes */
    313   saved_flags = surface->flags&(SDL_SRCALPHA | SDL_RLEACCELOK);
    314   saved_alpha = surface->format->alpha;
    315   if ( saved_flags & SDL_SRCALPHA )
    316   {
    317     SDL_SetAlpha(surface, 0, 0);
    318     hasAlpha = true;
    319     pixelDepth = 32;
    320                 mask = alphaMask;
    321   }
    322 
    323   retSurface = SDL_CreateRGBSurface(SDL_HWSURFACE,
    324                                     surface->w, surface->h,
    325                                     pixelDepth,
    326                                                                                                                                                 mask[0], mask[1], mask[2], mask[3] );
    327   if ( retSurface == NULL )
    328     return NULL;
    329 
    330   /* Copy the surface into the GL texture this->data->getStoredImage() */
    331   area.x = 0;
    332   area.y = 0;
    333   area.w = surface->w;
    334   area.h = surface->h;
    335   SDL_BlitSurface(surface, &area, retSurface, &area);
    336 
    337   /* Restore the alpha blending attributes */
    338   if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
    339   {
    340     SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
    341     hasAlpha = true;
    342   }
    343 
    344   return (retSurface);
    345 }
    346 
    347 
    348 /**
    349  * @brief Loads a Texture to the openGL-environment.
    350  * @param surface the Image to load to openGL
    351  * @returns The ID of the texture.
    352  */
    353 GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target)
    354 {
    355   //   if (this->data->getTexture() != 0 && glIsTexture(this->data->getTexture()))
    356   //     glDeleteTextures(1, &this->data->getTexture());
    357   //   this->data->getTexture() = 0;
    358   assert(surface != NULL);
    359 
    360   int      errorCode = 0;           //!< the error code for the texture loading functions
    361   GLuint   texture = 0;             //!< the OpenGL texture handle
    362   //int      mipmapLevel = 0;         //!< the maximum mipmap level for this texture
    363   //int      mipmapWidth = 0;         //!< the width of the mipmap
    364   //int      mipmapHight = 0;         //!< the height of the mipmap
    365   GLenum   format = GL_RGB;
    366   if (surface->format->BitsPerPixel == 32)
    367   {
    368     format = GL_RGBA;
    369     assert(surface->format->BitsPerPixel == 32);
    370   }
    371   else
    372   {
    373     assert(surface->format->BitsPerPixel == 24);
    374   }
    375 
    376   /* Create an OpenGL texture for the this->data->getStoredImage() */
    377   Texture::generateTexture(texture, target);
    378 
    379 //   glTexImage2D(target,  0,  format,
    380 //                surface->w,  surface->h,
    381 //                0, format,  GL_UNSIGNED_BYTE,
    382 //                surface->pixels);
    383 
    384 ///  glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
    385 ///  glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
    386 
    387   /// TODO CHECK THIS BACK in
    388   //glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);
    389 
    390   /* build the Texture  OpenGL V >= 1.1 */
    391 
    392   //  printf("%s, w:%d h:%d, 0x%x\n", this->getName(), surface->w, surface->h, target);
    393 
    394   /* control the mipmap levels */
    395   glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);
    396   glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
    397 
    398   // build the MipMaps automaticaly
    399   errorCode = gluBuild2DMipmaps(target, format,
    400                                 surface->w,  surface->h,
    401                                 format,  GL_UNSIGNED_BYTE,
    402                                 surface->pixels
    403                                );
    404   if(unlikely(errorCode != 0))
    405     PRINTF(1)("Error while loading texture (mipmap generation), gluBuild2DMipmaps returned %i\n", errorCode);
    406 
    407   return texture;
    408 }
    409 
    410 void Texture::generateTexture(GLuint& texture, GLenum target)
    411 {
    412   if (texture == 0 && !glIsTexture(texture))
    413   {
    414     glGenTextures(1, &texture);
    415   }
    416   glBindTexture(target, texture);
    417 
    418   glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
    419   glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
    420 
    421   glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    422   glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    423 
    424 }
Note: See TracChangeset for help on using the changeset viewer.