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