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