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