Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8011 was 7790, checked in by bensch, 19 years ago

orxonox/trunk: No More Valgrind warnings from the Texture

File size: 9.0 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
103Texture::Texture()
104{
105  this->init();
106}
107
108
109Texture::Texture(const Texture& texture)
110  : data(texture.data)
111{
112  this->setClassID(CL_TEXTURE, "Texture");
113  this->priority = 0.5;
114}
115
116
117Texture::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 */
128Texture::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
141Texture::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
151void 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 */
165Texture::~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*/
174bool 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 */
211bool 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}
221
222bool Texture::texturesEnabled = true;
223
224/**
225 * @brief enables, disables textures
226 * @param texturesEnabled true if the textures should be enabled
227 */
228void Texture::setTextureEnableState(bool texturesEnabled)
229{
230  Texture::texturesEnabled = texturesEnabled;
231}
232
233
234//////////////////////////////////////
235// UTILITY FUNCTIONALITY OF TEXTURE //
236//////////////////////////////////////
237/**
238 * @brief converts surface to a new SDL_Surface, that is loadable by openGL
239 * @param surface the Surface to convert
240 * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false.
241 * @returns a !!new!! Surface, that is loadable by openGL.
242 */
243SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha)
244{
245  assert(surface != NULL);
246  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
247
248  SDL_Surface* retSurface;
249  SDL_Rect area;
250  Uint32 saved_flags;
251  Uint8  saved_alpha;
252
253  hasAlpha = false;
254  int pixelDepth = 24;
255
256  /* Save the alpha blending attributes */
257  saved_flags = surface->flags&(SDL_SRCALPHA | SDL_RLEACCELOK);
258  saved_alpha = surface->format->alpha;
259  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
260  {
261    SDL_SetAlpha(surface, 0, 0);
262    hasAlpha = true;
263    pixelDepth = 32;
264  }
265
266  retSurface = SDL_CreateRGBSurface(SDL_HWSURFACE,
267                                    surface->w, surface->h,
268                                    pixelDepth,
269#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
270                                    0x000000FF,
271                                    0x0000FF00,
272                                    0x00FF0000,
273                                    0xFF000000
274#else
275                                    0xFF000000,
276                                    0x00FF0000,
277                                    0x0000FF00,
278                                    0x000000FF
279#endif
280                                   );
281  if ( retSurface == NULL )
282    return NULL;
283
284  /* Copy the surface into the GL texture this->data->getStoredImage() */
285  area.x = 0;
286  area.y = 0;
287  area.w = surface->w;
288  area.h = surface->h;
289  SDL_BlitSurface(surface, &area, retSurface, &area);
290
291  /* Restore the alpha blending attributes */
292  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
293  {
294    SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
295    hasAlpha = true;
296  }
297
298  return (retSurface);
299}
300
301
302/**
303 * @brief Loads a Texture to the openGL-environment.
304 * @param surface the Image to load to openGL
305 * @returns The ID of the texture.
306 */
307GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target)
308{
309  //   if (this->data->getTexture() != 0 && glIsTexture(this->data->getTexture()))
310  //     glDeleteTextures(1, &this->data->getTexture());
311  //   this->data->getTexture() = 0;
312  assert(surface != NULL);
313
314  int      errorCode = 0;           //!< the error code for the texture loading functions
315  GLuint   texture = 0;             //!< the OpenGL texture handle
316  int      mipmapLevel = 0;         //!< the maximum mipmap level for this texture
317  int      mipmapWidth = 0;         //!< the width of the mipmap
318  int      mipmapHight = 0;         //!< the height of the mipmap
319  GLenum   format = GL_RGB;
320  if (surface->format->BitsPerPixel == 32)
321  {
322    format = GL_RGBA;
323    assert(surface->format->BitsPerPixel == 32);
324  }
325  else
326  {
327    assert(surface->format->BitsPerPixel == 24);
328  }
329
330  /* Create an OpenGL texture for the this->data->getStoredImage() */
331  Texture::generateTexture(texture, target);
332
333//   glTexImage2D(target,  0,  format,
334//                surface->w,  surface->h,
335//                0, format,  GL_UNSIGNED_BYTE,
336//                surface->pixels);
337
338///  glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
339///  glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
340
341  /// TODO CHECK THIS BACK in
342  //glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);
343
344  /* build the Texture  OpenGL V >= 1.1 */
345
346  //  printf("%s, w:%d h:%d, 0x%x\n", this->getName(), surface->w, surface->h, target);
347
348  // build the MipMaps automaticaly
349  errorCode = gluBuild2DMipmaps(target, format,
350                                surface->w,  surface->h,
351                                format,  GL_UNSIGNED_BYTE,
352                                surface->pixels
353                               );
354  if(unlikely(errorCode != 0))
355    PRINTF(1)("Error while loading texture (mipmap generation), gluBuild2DMipmaps returned %i\n", errorCode);
356
357  return texture;
358}
359
360void 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  /* control the mipmap levels */
375  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);
376  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
377}
Note: See TracBrowser for help on using the repository browser.