| 1 | /* | 
|---|
| 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 |    Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
| 7 |    it under the terms of the GNU General Public License as published by | 
|---|
| 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 |    any later version. | 
|---|
| 10 |  | 
|---|
| 11 |    ### File Specific: | 
|---|
| 12 |    main-programmer: Benjamin Grauer | 
|---|
| 13 |    co-programmer: ... | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER | 
|---|
| 17 |  | 
|---|
| 18 | #include "texture.h" | 
|---|
| 19 |  | 
|---|
| 20 | #include "debug.h" | 
|---|
| 21 | #include "compiler.h" | 
|---|
| 22 | #include <math.h> | 
|---|
| 23 |  | 
|---|
| 24 | // INCLUDING SDL_Image | 
|---|
| 25 | #ifdef HAVE_SDL_IMAGE_H | 
|---|
| 26 | #include <SDL_image.h> | 
|---|
| 27 | #else | 
|---|
| 28 | #include <SDL/SDL_image.h> | 
|---|
| 29 | #endif | 
|---|
| 30 |  | 
|---|
| 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 |  | 
|---|
| 38 |   this->bAlpha = false; | 
|---|
| 39 |   this->texture = 0; | 
|---|
| 40 |   this->image = NULL; | 
|---|
| 41 |   this->priority = 0.5; | 
|---|
| 42 |  | 
|---|
| 43 |   if (!imageName.empty()) | 
|---|
| 44 |   { | 
|---|
| 45 |     this->setName(imageName); | 
|---|
| 46 |     this->loadImage(imageName, target); | 
|---|
| 47 |   } | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|
| 51 | /** | 
|---|
| 52 |  * @brief Destructor of a Texture | 
|---|
| 53 |  * | 
|---|
| 54 |  * Frees Data, and deletes the textures from GL | 
|---|
| 55 |  */ | 
|---|
| 56 | Texture::~Texture() | 
|---|
| 57 | { | 
|---|
| 58 |   if (this->texture != 0) | 
|---|
| 59 |     glDeleteTextures(1, &this->texture); | 
|---|
| 60 |   if (this->image != NULL) | 
|---|
| 61 |     SDL_FreeSurface(this->image); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 |  | 
|---|
| 65 | /** | 
|---|
| 66 |  * @brief loads an Image from a file to a Texture | 
|---|
| 67 |  * @param imageName The image to load | 
|---|
| 68 | */ | 
|---|
| 69 | bool Texture::loadImage(const std::string& imageName, GLenum target) | 
|---|
| 70 | { | 
|---|
| 71 |   if (Texture::texturesEnabled) | 
|---|
| 72 |   { | 
|---|
| 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 |     } | 
|---|
| 83 |     if (!imageName.empty()) | 
|---|
| 84 |     { | 
|---|
| 85 |       SDL_Surface* tmpSurf; | 
|---|
| 86 |       if (this->texture != 0 && glIsTexture(this->texture)) | 
|---|
| 87 |         glDeleteTextures(1, &this->texture); | 
|---|
| 88 |       // load the new Image to memory | 
|---|
| 89 |       tmpSurf = IMG_Load(imageName.c_str()); | 
|---|
| 90 |       if(tmpSurf != NULL) | 
|---|
| 91 |       { | 
|---|
| 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 |  | 
|---|
| 102 |         SDL_FreeSurface(tmpSurf); | 
|---|
| 103 |         return true; | 
|---|
| 104 |       } | 
|---|
| 105 |       else | 
|---|
| 106 |       { | 
|---|
| 107 |         PRINTF(1)("IMG_Load: %s\n", IMG_GetError()); | 
|---|
| 108 |         this->texture = 0; | 
|---|
| 109 |         return false; | 
|---|
| 110 |       } | 
|---|
| 111 |     } | 
|---|
| 112 |     else | 
|---|
| 113 |     { | 
|---|
| 114 |       PRINTF(2)("Image-Name not specified\n"); | 
|---|
| 115 |       return false; | 
|---|
| 116 |     } | 
|---|
| 117 |   } | 
|---|
| 118 |   return false; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 |  | 
|---|
| 122 | /** | 
|---|
| 123 |  * @brief rebuilds the texture. | 
|---|
| 124 |  * | 
|---|
| 125 |  * reloads the Texture from Memory to OpenGL. | 
|---|
| 126 |  */ | 
|---|
| 127 | bool Texture::rebuild() | 
|---|
| 128 | { | 
|---|
| 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) | 
|---|
| 137 |   { | 
|---|
| 138 |     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 |  | 
|---|
| 161 |  | 
|---|
| 162 | bool Texture::texturesEnabled = true; | 
|---|
| 163 |  | 
|---|
| 164 | /** | 
|---|
| 165 |  * @brief enables, disables textures | 
|---|
| 166 |  * @param texturesEnabled true if the textures should be enabled | 
|---|
| 167 |  */ | 
|---|
| 168 | void Texture::setTextureEnableState(bool texturesEnabled) | 
|---|
| 169 | { | 
|---|
| 170 |   Texture::texturesEnabled = texturesEnabled; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 |  | 
|---|
| 174 | ////////////////////////////////////// | 
|---|
| 175 | // UTILITY FUNCTIONALITY OF TEXTURE // | 
|---|
| 176 | ////////////////////////////////////// | 
|---|
| 177 | /** | 
|---|
| 178 |  * @brief converts surface to a new SDL_Surface, that is loadable by openGL | 
|---|
| 179 |  * @param surface the Surface to convert | 
|---|
| 180 |  * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false. | 
|---|
| 181 |  * @returns a !!new!! Surface, that is loadable by openGL. | 
|---|
| 182 |  */ | 
|---|
| 183 | SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) const | 
|---|
| 184 | { | 
|---|
| 185 |   assert(surface != NULL); | 
|---|
| 186 |   PRINTF(4)("Loading texture to OpenGL-Environment.\n"); | 
|---|
| 187 |  | 
|---|
| 188 |   SDL_Surface* retSurface; | 
|---|
| 189 |   SDL_Rect area; | 
|---|
| 190 |   Uint32 saved_flags; | 
|---|
| 191 |   Uint8  saved_alpha; | 
|---|
| 192 |  | 
|---|
| 193 |   hasAlpha = false; | 
|---|
| 194 |   int pixelDepth = 24; | 
|---|
| 195 |  | 
|---|
| 196 |   /* Save the alpha blending attributes */ | 
|---|
| 197 |   saved_flags = surface->flags&(SDL_SRCALPHA | SDL_RLEACCELOK); | 
|---|
| 198 |   saved_alpha = surface->format->alpha; | 
|---|
| 199 |   if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) | 
|---|
| 200 |   { | 
|---|
| 201 |     SDL_SetAlpha(surface, 0, 0); | 
|---|
| 202 |     hasAlpha = true; | 
|---|
| 203 |     pixelDepth = 32; | 
|---|
| 204 |   } | 
|---|
| 205 |  | 
|---|
| 206 |   retSurface = SDL_CreateRGBSurface(SDL_HWSURFACE, | 
|---|
| 207 |                                     surface->w, surface->h, | 
|---|
| 208 |                                     pixelDepth, | 
|---|
| 209 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ | 
|---|
| 210 |                                     0x000000FF, | 
|---|
| 211 |                                     0x0000FF00, | 
|---|
| 212 |                                     0x00FF0000, | 
|---|
| 213 |                                     0xFF000000 | 
|---|
| 214 | #else | 
|---|
| 215 |                                     0xFF000000, | 
|---|
| 216 |                                     0x00FF0000, | 
|---|
| 217 |                                     0x0000FF00, | 
|---|
| 218 |                                     0x000000FF | 
|---|
| 219 | #endif | 
|---|
| 220 |                                    ); | 
|---|
| 221 |   if ( retSurface == NULL ) | 
|---|
| 222 |     return NULL; | 
|---|
| 223 |  | 
|---|
| 224 |   /* Copy the surface into the GL texture image */ | 
|---|
| 225 |   area.x = 0; | 
|---|
| 226 |   area.y = 0; | 
|---|
| 227 |   area.w = surface->w; | 
|---|
| 228 |   area.h = surface->h; | 
|---|
| 229 |   SDL_BlitSurface(surface, &area, retSurface, &area); | 
|---|
| 230 |  | 
|---|
| 231 |   /* Restore the alpha blending attributes */ | 
|---|
| 232 |   if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) | 
|---|
| 233 |   { | 
|---|
| 234 |     SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha); | 
|---|
| 235 |     hasAlpha = true; | 
|---|
| 236 |   } | 
|---|
| 237 |  | 
|---|
| 238 |   return (retSurface); | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 |  | 
|---|
| 242 | /** | 
|---|
| 243 |  * @brief Loads a Texture to the openGL-environment. | 
|---|
| 244 |  * @param surface the Image to load to openGL | 
|---|
| 245 |  * @returns The ID of the texture. | 
|---|
| 246 |  */ | 
|---|
| 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; | 
|---|
| 252 |   assert(surface != NULL); | 
|---|
| 253 |  | 
|---|
| 254 |   int      errorCode = 0;           //!< the error code for the texture loading functions | 
|---|
| 255 |   GLuint   texture;                 //!< the OpenGL texture handle | 
|---|
| 256 |   int      mipmapLevel = 0;         //!< the maximum mipmap level for this texture | 
|---|
| 257 |   int      mipmapWidth = 0;         //!< the width of the mipmap | 
|---|
| 258 |   int      mipmapHight = 0;         //!< the height of the mipmap | 
|---|
| 259 |   GLenum   format = GL_RGB; | 
|---|
| 260 |   if (this->bAlpha) | 
|---|
| 261 |   { | 
|---|
| 262 |     format = GL_RGBA; | 
|---|
| 263 |     assert(surface->format->BitsPerPixel == 32); | 
|---|
| 264 |   } | 
|---|
| 265 |   else | 
|---|
| 266 |   { | 
|---|
| 267 |     assert(surface->format->BitsPerPixel == 24); | 
|---|
| 268 |   } | 
|---|
| 269 |  | 
|---|
| 270 |   /* Create an OpenGL texture for the image */ | 
|---|
| 271 |   glGenTextures(1, &texture); | 
|---|
| 272 |   glBindTexture(target, texture); | 
|---|
| 273 |  | 
|---|
| 274 |    glTexImage2D(target,  0,  format, | 
|---|
| 275 |                 surface->w,  surface->h, | 
|---|
| 276 |                 0, format,  GL_UNSIGNED_BYTE, | 
|---|
| 277 |                 surface->pixels); | 
|---|
| 278 |  | 
|---|
| 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); | 
|---|
| 291 |  | 
|---|
| 292 |   /* build the Texture  OpenGL V >= 1.1 */ | 
|---|
| 293 |  | 
|---|
| 294 |   //  printf("%s, w:%d h:%d, 0x%x\n", this->getName(), surface->w, surface->h, target); | 
|---|
| 295 |  | 
|---|
| 296 |   // build the MipMaps automaticaly | 
|---|
| 297 |   errorCode = gluBuild2DMipmaps(target, format, | 
|---|
| 298 |                                 surface->w,  surface->h, | 
|---|
| 299 |                                 format,  GL_UNSIGNED_BYTE, | 
|---|
| 300 |                                 surface->pixels | 
|---|
| 301 |                                ); | 
|---|
| 302 |   if(unlikely(errorCode != 0)) | 
|---|
| 303 |     PRINTF(1)("Error while loading texture (mipmap generation), gluBuild2DMipmaps returned %i\n", errorCode); | 
|---|
| 304 |  | 
|---|
| 305 |   return texture; | 
|---|
| 306 | } | 
|---|
| 307 |  | 
|---|