[4662] | 1 | /* |
---|
[3341] | 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 | |
---|
[3590] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER |
---|
| 17 | |
---|
[3341] | 18 | #include "texture.h" |
---|
| 19 | |
---|
[4357] | 20 | #include "debug.h" |
---|
[6139] | 21 | #include "compiler.h" |
---|
| 22 | #include <math.h> |
---|
[3622] | 23 | |
---|
[5858] | 24 | // INCLUDING SDL_Image |
---|
[4662] | 25 | #ifdef HAVE_SDL_IMAGE_H |
---|
[4357] | 26 | #include <SDL_image.h> |
---|
[4662] | 27 | #else |
---|
| 28 | #include <SDL/SDL_image.h> |
---|
| 29 | #endif |
---|
[4357] | 30 | |
---|
[3341] | 31 | /** |
---|
[4836] | 32 | * Constructor for a Texture |
---|
[3344] | 33 | */ |
---|
[7221] | 34 | Texture::Texture(const std::string& imageName, GLenum target) |
---|
[3655] | 35 | { |
---|
[7522] | 36 | this->init(); |
---|
[5304] | 37 | |
---|
[7221] | 38 | if (!imageName.empty()) |
---|
[5769] | 39 | { |
---|
| 40 | this->setName(imageName); |
---|
[6465] | 41 | this->loadImage(imageName, target); |
---|
[5769] | 42 | } |
---|
[4662] | 43 | } |
---|
[3655] | 44 | |
---|
[7522] | 45 | Texture::Texture(SDL_Surface* surface, GLenum target) |
---|
| 46 | { |
---|
| 47 | this->init(); |
---|
[5863] | 48 | |
---|
[7522] | 49 | if(surface != NULL) |
---|
| 50 | { |
---|
| 51 | this->loadSurface(surface, target); |
---|
| 52 | |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | void Texture::init() |
---|
| 57 | { |
---|
| 58 | this->setClassID(CL_TEXTURE, "Texture"); |
---|
| 59 | |
---|
| 60 | this->bAlpha = false; |
---|
| 61 | this->texture = 0; |
---|
| 62 | this->image = NULL; |
---|
| 63 | this->priority = 0.5; |
---|
| 64 | } |
---|
| 65 | |
---|
[3655] | 66 | /** |
---|
[4836] | 67 | * Destructor of a Texture |
---|
[4662] | 68 | |
---|
[3344] | 69 | Frees Data, and deletes the textures from GL |
---|
| 70 | */ |
---|
[4746] | 71 | Texture::~Texture() |
---|
[3344] | 72 | { |
---|
[5211] | 73 | if (this->texture != 0) |
---|
[3344] | 74 | glDeleteTextures(1, &this->texture); |
---|
[5753] | 75 | if (this->image != NULL) |
---|
| 76 | SDL_FreeSurface(this->image); |
---|
[3344] | 77 | } |
---|
| 78 | |
---|
[5863] | 79 | |
---|
[3863] | 80 | /** |
---|
[7522] | 81 | * @brief loads an Image from a file to a Texture |
---|
[4836] | 82 | * @param imageName The image to load |
---|
[3863] | 83 | */ |
---|
[7221] | 84 | bool Texture::loadImage(const std::string& imageName, GLenum target) |
---|
[3341] | 85 | { |
---|
[5858] | 86 | if (Texture::texturesEnabled) |
---|
[6859] | 87 | { |
---|
[7221] | 88 | if (!imageName.empty()) |
---|
[6859] | 89 | { |
---|
| 90 | SDL_Surface* tmpSurf; |
---|
[7522] | 91 | |
---|
[6859] | 92 | // load the new Image to memory |
---|
[7221] | 93 | tmpSurf = IMG_Load(imageName.c_str()); |
---|
[6859] | 94 | if(tmpSurf != NULL) |
---|
| 95 | { |
---|
[7522] | 96 | this->loadSurface(tmpSurf, target); |
---|
[6859] | 97 | SDL_FreeSurface(tmpSurf); |
---|
| 98 | return true; |
---|
| 99 | } |
---|
[3622] | 100 | else |
---|
[6859] | 101 | { |
---|
| 102 | PRINTF(1)("IMG_Load: %s\n", IMG_GetError()); |
---|
| 103 | this->texture = 0; |
---|
| 104 | return false; |
---|
| 105 | } |
---|
[3341] | 106 | } |
---|
[6859] | 107 | else |
---|
| 108 | { |
---|
| 109 | PRINTF(2)("Image-Name not specified\n"); |
---|
| 110 | return false; |
---|
| 111 | } |
---|
| 112 | } |
---|
[5754] | 113 | return false; |
---|
[3341] | 114 | } |
---|
[5753] | 115 | |
---|
[7522] | 116 | /** |
---|
| 117 | * @brief Loads an SDL_Surface. |
---|
| 118 | */ |
---|
| 119 | bool Texture::loadSurface(SDL_Surface* surface, GLenum target) |
---|
| 120 | { |
---|
| 121 | if (Texture::texturesEnabled) |
---|
| 122 | { |
---|
| 123 | // some image was loaded before |
---|
| 124 | if (this->image != NULL) |
---|
| 125 | { |
---|
| 126 | SDL_FreeSurface(this->image); |
---|
| 127 | this->image = NULL; |
---|
| 128 | } |
---|
[5863] | 129 | |
---|
[7522] | 130 | // unload the old Texture. |
---|
| 131 | if (this->texture != 0 && glIsTexture(this->texture)) |
---|
| 132 | { |
---|
| 133 | glDeleteTextures(1, &this->texture); |
---|
| 134 | this->texture = 0; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | bool hasAlpha; |
---|
| 138 | SDL_Surface* newSurf = this->prepareSurface(surface, hasAlpha); |
---|
| 139 | if (newSurf != NULL) |
---|
| 140 | { |
---|
| 141 | this->setSurface(newSurf); |
---|
| 142 | this->setAlpha(hasAlpha); |
---|
| 143 | this->setTexture(Texture::loadTexToGL(newSurf, target)); |
---|
| 144 | return true; |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | return false; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | |
---|
[5863] | 151 | /** |
---|
| 152 | * rebuilds the texture. |
---|
| 153 | * reloads the Texture from Memory to OpenGL. |
---|
| 154 | */ |
---|
[5755] | 155 | bool Texture::rebuild() |
---|
[5754] | 156 | { |
---|
[5860] | 157 | if (this->texture != 0) |
---|
[6859] | 158 | { |
---|
| 159 | if (glIsTexture(this->texture)) |
---|
| 160 | glDeleteTextures(1,&this->texture); |
---|
| 161 | this->setTexture(0); |
---|
| 162 | } |
---|
[5754] | 163 | |
---|
| 164 | if (this->image != NULL) |
---|
[6859] | 165 | { |
---|
| 166 | PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName()); |
---|
| 167 | this->setTexture(loadTexToGL(this->image)); |
---|
| 168 | } |
---|
[5863] | 169 | } |
---|
[5768] | 170 | |
---|
[5863] | 171 | |
---|
| 172 | /** |
---|
| 173 | * set the surface this Texture handles |
---|
| 174 | * @param newSurface the new Surface to set as the image for this Texture. |
---|
| 175 | * |
---|
| 176 | * This deletes the old version of the stored Texture, |
---|
| 177 | * and sets the newly given Surface as current. |
---|
| 178 | */ |
---|
| 179 | bool Texture::setSurface(SDL_Surface* newSurface) |
---|
| 180 | { |
---|
| 181 | if (this->image != NULL) |
---|
| 182 | SDL_FreeSurface(this->image); |
---|
| 183 | |
---|
| 184 | this->image = newSurface; |
---|
| 185 | |
---|
| 186 | return (this->image != NULL); |
---|
[5754] | 187 | } |
---|
| 188 | |
---|
[5863] | 189 | |
---|
| 190 | bool Texture::texturesEnabled = true; |
---|
| 191 | |
---|
[5754] | 192 | /** |
---|
[5863] | 193 | * enables, disables textures |
---|
| 194 | * @param texturesEnabled true if the textures should be enabled |
---|
| 195 | */ |
---|
| 196 | void Texture::setTextureEnableState(bool texturesEnabled) |
---|
| 197 | { |
---|
| 198 | Texture::texturesEnabled = texturesEnabled; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | ////////////////////////////////////// |
---|
| 203 | // UTILITY FUNCTIONALITY OF TEXTURE // |
---|
| 204 | ////////////////////////////////////// |
---|
| 205 | /** |
---|
[5754] | 206 | * converts surface to a new SDL_Surface, that is loadable by openGL |
---|
| 207 | * @param surface the Surface to convert |
---|
[5859] | 208 | * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false. |
---|
[5754] | 209 | * @returns a !!new!! Surface, that is loadable by openGL. |
---|
| 210 | */ |
---|
[6139] | 211 | SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) const |
---|
[5753] | 212 | { |
---|
| 213 | PRINTF(4)("Loading texture to OpenGL-Environment.\n"); |
---|
[5768] | 214 | |
---|
[5859] | 215 | SDL_Surface* retSurface; |
---|
[5753] | 216 | SDL_Rect area; |
---|
| 217 | Uint32 saved_flags; |
---|
| 218 | Uint8 saved_alpha; |
---|
[5768] | 219 | |
---|
[5859] | 220 | hasAlpha = false; |
---|
| 221 | retSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, |
---|
[5863] | 222 | surface->w, surface->h, |
---|
| 223 | 32, |
---|
[5753] | 224 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
---|
[6859] | 225 | 0x000000FF, |
---|
| 226 | 0x0000FF00, |
---|
| 227 | 0x00FF0000, |
---|
| 228 | 0xFF000000 |
---|
[5753] | 229 | #else |
---|
[6859] | 230 | 0xFF000000, |
---|
| 231 | 0x00FF0000, |
---|
| 232 | 0x0000FF00, |
---|
| 233 | 0x000000FF |
---|
[5753] | 234 | #endif |
---|
[5863] | 235 | ); |
---|
[5859] | 236 | if ( retSurface == NULL ) |
---|
[5768] | 237 | { |
---|
[5859] | 238 | return NULL; |
---|
[5768] | 239 | } |
---|
| 240 | |
---|
[5753] | 241 | /* Save the alpha blending attributes */ |
---|
| 242 | saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); |
---|
| 243 | saved_alpha = surface->format->alpha; |
---|
[6859] | 244 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) |
---|
| 245 | { |
---|
[5753] | 246 | SDL_SetAlpha(surface, 0, 0); |
---|
| 247 | } |
---|
[5768] | 248 | |
---|
[5753] | 249 | /* Copy the surface into the GL texture image */ |
---|
| 250 | area.x = 0; |
---|
| 251 | area.y = 0; |
---|
| 252 | area.w = surface->w; |
---|
| 253 | area.h = surface->h; |
---|
[5859] | 254 | SDL_BlitSurface(surface, &area, retSurface, &area); |
---|
[5768] | 255 | |
---|
[5753] | 256 | /* Restore the alpha blending attributes */ |
---|
[5768] | 257 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) |
---|
| 258 | { |
---|
| 259 | SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha); |
---|
[5859] | 260 | hasAlpha = true; |
---|
[5768] | 261 | } |
---|
| 262 | |
---|
[5859] | 263 | return (retSurface); |
---|
[5753] | 264 | } |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | /** |
---|
| 268 | * Loads a Texture to the openGL-environment. |
---|
| 269 | * @param surface the Image to load to openGL |
---|
| 270 | * @returns The ID of the texture. |
---|
| 271 | */ |
---|
[6465] | 272 | GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target) const |
---|
[5753] | 273 | { |
---|
[6859] | 274 | // if (this->texture != 0 && glIsTexture(this->texture)) |
---|
| 275 | // glDeleteTextures(1, &this->texture); |
---|
| 276 | // this->texture = 0; |
---|
[5768] | 277 | |
---|
[6139] | 278 | int errorCode = 0; //!< the error code for the texture loading functions |
---|
| 279 | GLuint texture; //!< the OpenGL texture handle |
---|
| 280 | int mipmapLevel = 0; //!< the maximum mipmap level for this texture |
---|
| 281 | int mipmapWidth = 0; //!< the width of the mipmap |
---|
| 282 | int mipmapHight = 0; //!< the height of the mipmap |
---|
[5856] | 283 | |
---|
[6139] | 284 | |
---|
[5856] | 285 | if (surface == NULL) |
---|
[5768] | 286 | return 0; |
---|
| 287 | |
---|
[5753] | 288 | /* Create an OpenGL texture for the image */ |
---|
[5856] | 289 | glGenTextures(1, &texture); |
---|
[6465] | 290 | glBindTexture(target, texture); |
---|
[6139] | 291 | |
---|
[6634] | 292 | glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT); |
---|
| 293 | glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT); |
---|
| 294 | |
---|
[7465] | 295 | glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
---|
[6465] | 296 | glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
---|
[6165] | 297 | |
---|
[6634] | 298 | glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority); |
---|
[6165] | 299 | |
---|
[6145] | 300 | |
---|
[6139] | 301 | /* control the mipmap levels */ |
---|
[6634] | 302 | glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5); |
---|
| 303 | glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0); |
---|
[6139] | 304 | |
---|
| 305 | /* build the Texture OpenGL V >= 1.1 */ |
---|
[6465] | 306 | glTexImage2D(target, |
---|
[5768] | 307 | 0, |
---|
| 308 | GL_RGBA, |
---|
[6523] | 309 | surface->w, |
---|
| 310 | surface->h, |
---|
[5768] | 311 | 0, |
---|
| 312 | GL_RGBA, |
---|
| 313 | GL_UNSIGNED_BYTE, |
---|
[5856] | 314 | surface->pixels); |
---|
[6871] | 315 | // printf("%s, w:%d h:%d, 0x%x\n", this->getName(), surface->w, surface->h, target); |
---|
[6139] | 316 | |
---|
| 317 | // build the MipMaps automaticaly |
---|
[6465] | 318 | errorCode = gluBuild2DMipmaps(target, |
---|
[6139] | 319 | GL_RGBA, |
---|
| 320 | surface->w, |
---|
| 321 | surface->h, |
---|
| 322 | GL_RGBA, |
---|
| 323 | GL_UNSIGNED_BYTE, |
---|
| 324 | surface->pixels |
---|
| 325 | ); |
---|
| 326 | if(unlikely(errorCode != 0)) |
---|
[6165] | 327 | PRINTF(1)("Error while loading texture (mipmap generation), gluBuild2DMipmaps returned %i\n", errorCode); |
---|
[6139] | 328 | |
---|
[5856] | 329 | return texture; |
---|
[5753] | 330 | } |
---|
[6139] | 331 | |
---|