Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/graphics/importer/texture.cc @ 7522

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

Textures are way improved now

File size: 8.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#include <math.h>
23
24// INCLUDING SDL_Image
25#ifdef HAVE_SDL_IMAGE_H
26#include <SDL_image.h>
27#else
28#include <SDL/SDL_image.h>
29#endif
30
31/**
32 *  Constructor for a Texture
33*/
34Texture::Texture(const std::string& imageName, GLenum target)
35{
36  this->init();
37
38  if (!imageName.empty())
39  {
40    this->setName(imageName);
41    this->loadImage(imageName, target);
42  }
43}
44
45Texture::Texture(SDL_Surface* surface, GLenum target)
46{
47  this->init();
48
49  if(surface != NULL)
50  {
51    this->loadSurface(surface, target);
52
53  }
54}
55
56void Texture::init()
57{
58  this->setClassID(CL_TEXTURE, "Texture");
59
60  this->bAlpha = false;
61  this->texture = 0;
62  this->image = NULL;
63  this->priority = 0.5;
64}
65
66/**
67 *  Destructor of a Texture
68
69   Frees Data, and deletes the textures from GL
70*/
71Texture::~Texture()
72{
73  if (this->texture != 0)
74    glDeleteTextures(1, &this->texture);
75  if (this->image != NULL)
76    SDL_FreeSurface(this->image);
77}
78
79
80/**
81 * @brief loads an Image from a file to a Texture
82 * @param imageName The image to load
83*/
84bool Texture::loadImage(const std::string& imageName, GLenum target)
85{
86  if (Texture::texturesEnabled)
87  {
88    if (!imageName.empty())
89    {
90      SDL_Surface* tmpSurf;
91
92      // load the new Image to memory
93      tmpSurf = IMG_Load(imageName.c_str());
94      if(tmpSurf != NULL)
95      {
96        this->loadSurface(tmpSurf, target);
97        SDL_FreeSurface(tmpSurf);
98        return true;
99      }
100      else
101      {
102        PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
103        this->texture = 0;
104        return false;
105      }
106    }
107    else
108    {
109      PRINTF(2)("Image-Name not specified\n");
110      return false;
111    }
112  }
113  return false;
114}
115
116/**
117 * @brief Loads an SDL_Surface.
118 */
119bool Texture::loadSurface(SDL_Surface* surface, GLenum target)
120{
121  if (Texture::texturesEnabled)
122  {
123    // some image was loaded before
124    if (this->image != NULL)
125    {
126      SDL_FreeSurface(this->image);
127      this->image = NULL;
128    }
129
130    // unload the old Texture.
131    if (this->texture != 0 && glIsTexture(this->texture))
132    {
133      glDeleteTextures(1, &this->texture);
134      this->texture = 0;
135    }
136
137    bool hasAlpha;
138    SDL_Surface* newSurf = this->prepareSurface(surface, hasAlpha);
139    if (newSurf != NULL)
140    {
141      this->setSurface(newSurf);
142      this->setAlpha(hasAlpha);
143      this->setTexture(Texture::loadTexToGL(newSurf, target));
144      return true;
145    }
146  }
147  return false;
148}
149
150
151/**
152 * rebuilds the texture.
153 * reloads the Texture from Memory to OpenGL.
154 */
155bool Texture::rebuild()
156{
157  if (this->texture != 0)
158  {
159    if (glIsTexture(this->texture))
160      glDeleteTextures(1,&this->texture);
161    this->setTexture(0);
162  }
163
164  if (this->image != NULL)
165  {
166    PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());
167    this->setTexture(loadTexToGL(this->image));
168  }
169}
170
171
172/**
173 * set the surface this Texture handles
174 * @param newSurface the new Surface to set as the image for this Texture.
175 *
176 * This deletes the old version of the stored Texture,
177 * and sets the newly given Surface as current.
178 */
179bool Texture::setSurface(SDL_Surface* newSurface)
180{
181  if (this->image != NULL)
182    SDL_FreeSurface(this->image);
183
184  this->image = newSurface;
185
186  return (this->image != NULL);
187}
188
189
190bool Texture::texturesEnabled = true;
191
192/**
193 * enables, disables textures
194 * @param texturesEnabled true if the textures should be enabled
195 */
196void Texture::setTextureEnableState(bool texturesEnabled)
197{
198  Texture::texturesEnabled = texturesEnabled;
199}
200
201
202//////////////////////////////////////
203// UTILITY FUNCTIONALITY OF TEXTURE //
204//////////////////////////////////////
205/**
206 * converts surface to a new SDL_Surface, that is loadable by openGL
207 * @param surface the Surface to convert
208 * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false.
209 * @returns a !!new!! Surface, that is loadable by openGL.
210 */
211SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) const
212{
213  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
214
215  SDL_Surface* retSurface;
216  SDL_Rect area;
217  Uint32 saved_flags;
218  Uint8  saved_alpha;
219
220  hasAlpha = false;
221  retSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
222                                    surface->w, surface->h,
223                                    32,
224#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
225                                    0x000000FF,
226                                    0x0000FF00,
227                                    0x00FF0000,
228                                    0xFF000000
229#else
230                                    0xFF000000,
231                                    0x00FF0000,
232                                    0x0000FF00,
233                                    0x000000FF
234#endif
235                                   );
236  if ( retSurface == NULL )
237  {
238    return NULL;
239  }
240
241  /* Save the alpha blending attributes */
242  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
243  saved_alpha = surface->format->alpha;
244  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
245  {
246    SDL_SetAlpha(surface, 0, 0);
247  }
248
249  /* Copy the surface into the GL texture image */
250  area.x = 0;
251  area.y = 0;
252  area.w = surface->w;
253  area.h = surface->h;
254  SDL_BlitSurface(surface, &area, retSurface, &area);
255
256  /* Restore the alpha blending attributes */
257  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
258  {
259    SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
260    hasAlpha = true;
261  }
262
263  return (retSurface);
264}
265
266
267/**
268 *  Loads a Texture to the openGL-environment.
269 * @param surface the Image to load to openGL
270 * @returns The ID of the texture.
271 */
272GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target) const
273{
274  //   if (this->texture != 0 && glIsTexture(this->texture))
275  //     glDeleteTextures(1, &this->texture);
276  //   this->texture = 0;
277
278  int      errorCode = 0;           //!< the error code for the texture loading functions
279  GLuint   texture;                 //!< the OpenGL texture handle
280  int      mipmapLevel = 0;         //!< the maximum mipmap level for this texture
281  int      mipmapWidth = 0;         //!< the width of the mipmap
282  int      mipmapHight = 0;         //!< the height of the mipmap
283
284
285  if (surface == NULL)
286    return 0;
287
288  /* Create an OpenGL texture for the image */
289  glGenTextures(1, &texture);
290  glBindTexture(target, texture);
291
292  glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
293  glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
294
295  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
296  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
297
298  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);
299
300
301  /* control the mipmap levels */
302  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);
303  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
304
305  /* build the Texture  OpenGL V >= 1.1 */
306  glTexImage2D(target,
307               0,
308               GL_RGBA,
309               surface->w,
310               surface->h,
311               0,
312               GL_RGBA,
313               GL_UNSIGNED_BYTE,
314               surface->pixels);
315  //  printf("%s, w:%d h:%d, 0x%x\n", this->getName(), surface->w, surface->h, target);
316
317  // build the MipMaps automaticaly
318  errorCode = gluBuild2DMipmaps(target,
319                                GL_RGBA,
320                                surface->w,
321                                surface->h,
322                                GL_RGBA,
323                                GL_UNSIGNED_BYTE,
324                                surface->pixels
325                               );
326  if(unlikely(errorCode != 0))
327    PRINTF(1)("Error while loading texture (mipmap generation), gluBuild2DMipmaps returned %i\n", errorCode);
328
329  return texture;
330}
331
Note: See TracBrowser for help on using the repository browser.