/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER #include "texture.h" #include "debug.h" #include "compiler.h" #include // INCLUDING SDL_Image #ifdef HAVE_SDL_IMAGE_H #include #else #include #endif /** * Constructor for a Texture */ Texture::Texture(const char* imageName, GLenum target) { this->setClassID(CL_TEXTURE, "Texture"); this->bAlpha = false; this->texture = 0; this->image = NULL; this->priority = 0.5; if (imageName != NULL) { this->setName(imageName); this->loadImage(imageName, target); } } /** * Destructor of a Texture Frees Data, and deletes the textures from GL */ Texture::~Texture() { if (this->texture != 0) glDeleteTextures(1, &this->texture); if (this->image != NULL) SDL_FreeSurface(this->image); } /** * loads an Image from a file to a Texture * @param imageName The image to load */ bool Texture::loadImage(const char* imageName, GLenum target) { if (Texture::texturesEnabled) { if (this->image != NULL) { SDL_FreeSurface(this->image); this->image = NULL; } if (this->texture != 0) { glDeleteTextures(1, &this->texture); this->texture = 0; } if (imageName != NULL) { SDL_Surface* tmpSurf; if (this->texture != 0 && glIsTexture(this->texture)) glDeleteTextures(1, &this->texture); // load the new Image to memory tmpSurf = IMG_Load(imageName); if(tmpSurf != NULL) { PRINTF(4)("loading Image %s\n", imageName); bool hasAlpha; SDL_Surface* newSurf = this->prepareSurface(tmpSurf, hasAlpha); if (newSurf != NULL) { this->setSurface(newSurf); this->setAlpha(hasAlpha); this->setTexture(Texture::loadTexToGL(newSurf, target)); } SDL_FreeSurface(tmpSurf); return true; } else { PRINTF(1)("IMG_Load: %s\n", IMG_GetError()); this->texture = 0; return false; } } else { PRINTF(2)("Image-Name not specified\n"); return false; } } return false; } /** * rebuilds the texture. * reloads the Texture from Memory to OpenGL. */ bool Texture::rebuild() { if (this->texture != 0) { if (glIsTexture(this->texture)) glDeleteTextures(1,&this->texture); this->setTexture(0); } if (this->image != NULL) { PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName()); this->setTexture(loadTexToGL(this->image)); } } /** * set the surface this Texture handles * @param newSurface the new Surface to set as the image for this Texture. * * This deletes the old version of the stored Texture, * and sets the newly given Surface as current. */ bool Texture::setSurface(SDL_Surface* newSurface) { if (this->image != NULL) SDL_FreeSurface(this->image); this->image = newSurface; return (this->image != NULL); } bool Texture::texturesEnabled = true; /** * enables, disables textures * @param texturesEnabled true if the textures should be enabled */ void Texture::setTextureEnableState(bool texturesEnabled) { Texture::texturesEnabled = texturesEnabled; } ////////////////////////////////////// // UTILITY FUNCTIONALITY OF TEXTURE // ////////////////////////////////////// /** * converts surface to a new SDL_Surface, that is loadable by openGL * @param surface the Surface to convert * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false. * @returns a !!new!! Surface, that is loadable by openGL. */ SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) const { PRINTF(4)("Loading texture to OpenGL-Environment.\n"); SDL_Surface* retSurface; SDL_Rect area; Uint32 saved_flags; Uint8 saved_alpha; hasAlpha = false; retSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h, 32, #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #else 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF #endif ); if ( retSurface == NULL ) { return NULL; } /* Save the alpha blending attributes */ saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); saved_alpha = surface->format->alpha; if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { SDL_SetAlpha(surface, 0, 0); } /* Copy the surface into the GL texture image */ area.x = 0; area.y = 0; area.w = surface->w; area.h = surface->h; SDL_BlitSurface(surface, &area, retSurface, &area); /* Restore the alpha blending attributes */ if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha); hasAlpha = true; } return (retSurface); } /** * Loads a Texture to the openGL-environment. * @param surface the Image to load to openGL * @returns The ID of the texture. */ GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target) const { // if (this->texture != 0 && glIsTexture(this->texture)) // glDeleteTextures(1, &this->texture); // this->texture = 0; int errorCode = 0; //!< the error code for the texture loading functions GLuint texture; //!< the OpenGL texture handle int mipmapLevel = 0; //!< the maximum mipmap level for this texture int mipmapWidth = 0; //!< the width of the mipmap int mipmapHight = 0; //!< the height of the mipmap if (surface == NULL) return 0; /* Create an OpenGL texture for the image */ glGenTextures(1, &texture); glBindTexture(target, texture); glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT); /* control the mipmap levels */ // glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);f // glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0); glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority); /* build the Texture OpenGL V >= 1.1 */ glTexImage2D(target, 0, GL_RGBA, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); // build the MipMaps automaticaly errorCode = gluBuild2DMipmaps(target, GL_RGBA, surface->w, surface->h, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels ); if(unlikely(errorCode != 0)) PRINTF(1)("Error while loading texture (mipmap generation), gluBuild2DMipmaps returned %i\n", errorCode); if (target == GL_TEXTURE_2D) glBindTexture(target, 0); return texture; }