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