Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6948 was 6871, checked in by bensch, 19 years ago

orxonox/trunk: small fixes (cleanup)

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