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