Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/importer/texture.cc @ 7785

Last change on this file since 7785 was 7785, checked in by bensch, 18 years ago

orxonox/trunk: merged the Changes from the water branche back to the trunk.

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