[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" |
---|
[3622] | 21 | #include "graphics_engine.h" |
---|
| 22 | |
---|
[4662] | 23 | #ifdef HAVE_SDL_IMAGE_H |
---|
[4357] | 24 | #include <SDL_image.h> |
---|
[4662] | 25 | #else |
---|
| 26 | #include <SDL/SDL_image.h> |
---|
| 27 | #endif |
---|
[4357] | 28 | |
---|
[3341] | 29 | /** |
---|
[4836] | 30 | * Constructor for a Texture |
---|
[3344] | 31 | */ |
---|
[3655] | 32 | Texture::Texture(const char* imageName) |
---|
| 33 | { |
---|
[5212] | 34 | this->bAlpha = false; |
---|
[3655] | 35 | this->texture = 0; |
---|
[3905] | 36 | if (imageName) |
---|
| 37 | this->loadImage(imageName); |
---|
[4662] | 38 | } |
---|
[3655] | 39 | |
---|
| 40 | /** |
---|
[4836] | 41 | * Destructor of a Texture |
---|
[4662] | 42 | |
---|
[3344] | 43 | Frees Data, and deletes the textures from GL |
---|
| 44 | */ |
---|
[4746] | 45 | Texture::~Texture() |
---|
[3344] | 46 | { |
---|
[5211] | 47 | if (this->texture != 0) |
---|
[3344] | 48 | glDeleteTextures(1, &this->texture); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | /** |
---|
[4836] | 52 | * Loads a Texture to the openGL-environment. |
---|
| 53 | * @param surface the Image to load to openGL |
---|
| 54 | * @returns The ID of the texture. |
---|
[3341] | 55 | */ |
---|
[3905] | 56 | GLuint Texture::loadTexToGL (SDL_Surface* surface) |
---|
[3341] | 57 | { |
---|
[3622] | 58 | if (GraphicsEngine::texturesEnabled) |
---|
| 59 | { |
---|
| 60 | PRINTF(4)("Loading texture to OpenGL-Environment.\n"); |
---|
[3905] | 61 | |
---|
| 62 | GLuint texture; |
---|
| 63 | int w, h; |
---|
| 64 | SDL_Surface *image; |
---|
| 65 | SDL_Rect area; |
---|
| 66 | Uint32 saved_flags; |
---|
| 67 | Uint8 saved_alpha; |
---|
[4662] | 68 | |
---|
[3905] | 69 | w = surface->w; |
---|
| 70 | h = surface->h; |
---|
[4662] | 71 | |
---|
[3905] | 72 | image = SDL_CreateRGBSurface(SDL_SWSURFACE, |
---|
[4662] | 73 | w, h, |
---|
| 74 | 32, |
---|
[3905] | 75 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
---|
[4662] | 76 | 0x000000FF, |
---|
| 77 | 0x0000FF00, |
---|
| 78 | 0x00FF0000, |
---|
| 79 | 0xFF000000 |
---|
[3905] | 80 | #else |
---|
[4662] | 81 | 0xFF000000, |
---|
| 82 | 0x00FF0000, |
---|
| 83 | 0x0000FF00, |
---|
| 84 | 0x000000FF |
---|
[3905] | 85 | #endif |
---|
[4662] | 86 | ); |
---|
[3905] | 87 | if ( image == NULL ) { |
---|
[4662] | 88 | return 0; |
---|
[3905] | 89 | } |
---|
[4662] | 90 | |
---|
[3905] | 91 | /* Save the alpha blending attributes */ |
---|
| 92 | saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); |
---|
| 93 | saved_alpha = surface->format->alpha; |
---|
| 94 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
---|
[4662] | 95 | SDL_SetAlpha(surface, 0, 0); |
---|
[3905] | 96 | } |
---|
[4662] | 97 | |
---|
[3905] | 98 | /* Copy the surface into the GL texture image */ |
---|
| 99 | area.x = 0; |
---|
| 100 | area.y = 0; |
---|
| 101 | area.w = surface->w; |
---|
| 102 | area.h = surface->h; |
---|
| 103 | SDL_BlitSurface(surface, &area, image, &area); |
---|
[4662] | 104 | |
---|
[3905] | 105 | /* Restore the alpha blending attributes */ |
---|
| 106 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
---|
[4662] | 107 | SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha); |
---|
| 108 | this->bAlpha = true; |
---|
[3905] | 109 | } |
---|
[4662] | 110 | |
---|
[3905] | 111 | /* Create an OpenGL texture for the image */ |
---|
| 112 | glGenTextures(1, &texture); |
---|
| 113 | glBindTexture(GL_TEXTURE_2D, texture); |
---|
[5228] | 114 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
---|
| 115 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
---|
[3966] | 116 | // build the Texture |
---|
| 117 | glTexImage2D(GL_TEXTURE_2D, |
---|
[4662] | 118 | 0, |
---|
| 119 | GL_RGBA, |
---|
| 120 | w, h, |
---|
| 121 | 0, |
---|
| 122 | GL_RGBA, |
---|
| 123 | GL_UNSIGNED_BYTE, |
---|
| 124 | image->pixels); |
---|
[3966] | 125 | // build the MipMaps |
---|
[3905] | 126 | gluBuild2DMipmaps(GL_TEXTURE_2D, |
---|
[4662] | 127 | GL_RGBA, |
---|
| 128 | w, |
---|
| 129 | h, |
---|
| 130 | GL_RGBA, |
---|
| 131 | GL_UNSIGNED_BYTE, |
---|
| 132 | image->pixels); |
---|
| 133 | |
---|
[3905] | 134 | SDL_FreeSurface(image); /* No longer needed */ |
---|
[5228] | 135 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
[3905] | 136 | return texture; |
---|
[3622] | 137 | } |
---|
[3341] | 138 | } |
---|
| 139 | |
---|
[3863] | 140 | /** |
---|
[4836] | 141 | * loads an Image from a file to a Texture |
---|
| 142 | * @param imageName The image to load |
---|
[3863] | 143 | */ |
---|
[3655] | 144 | bool Texture::loadImage(const char* imageName) |
---|
[3341] | 145 | { |
---|
[3622] | 146 | if (GraphicsEngine::texturesEnabled) |
---|
[3341] | 147 | { |
---|
[3660] | 148 | if (imageName) |
---|
[4662] | 149 | { |
---|
| 150 | SDL_Surface* tmpSurf; |
---|
| 151 | if (this->texture) |
---|
| 152 | glDeleteTextures(1, &this->texture); |
---|
| 153 | // load the new Image to memory |
---|
| 154 | tmpSurf = IMG_Load(imageName); |
---|
| 155 | if(!tmpSurf) |
---|
| 156 | { |
---|
| 157 | PRINTF(1)("IMG_Load: %s\n", IMG_GetError()); |
---|
| 158 | return false; |
---|
| 159 | } |
---|
[3790] | 160 | |
---|
[4662] | 161 | GLubyte* pixels = (GLubyte*)tmpSurf->pixels; |
---|
[3905] | 162 | |
---|
[4662] | 163 | PRINTF(3)("loading Image %s\n", imageName); |
---|
| 164 | if (tmpSurf) |
---|
| 165 | this->texture = loadTexToGL(tmpSurf); |
---|
[3905] | 166 | |
---|
[4662] | 167 | |
---|
| 168 | SDL_FreeSurface(tmpSurf); |
---|
| 169 | return true; |
---|
| 170 | } |
---|
[3622] | 171 | else |
---|
[4662] | 172 | { |
---|
| 173 | PRINTF(2)("Image not Found: %s\n", imageName); |
---|
| 174 | return false; |
---|
| 175 | } |
---|
[3341] | 176 | } |
---|
| 177 | } |
---|