Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/powerups/src/lib/graphics/importer/texture.cc @ 6284

Last change on this file since 6284 was 6165, checked in by patrick, 19 years ago

branches: added a branche christmas for some christmas tests

File size: 7.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#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)
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);
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)
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));
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    SDL_SetAlpha(surface, 0, 0);
217  }
218
219  /* Copy the surface into the GL texture image */
220  area.x = 0;
221  area.y = 0;
222  area.w = surface->w;
223  area.h = surface->h;
224  SDL_BlitSurface(surface, &area, retSurface, &area);
225
226  /* Restore the alpha blending attributes */
227  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
228  {
229    SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
230    hasAlpha = true;
231  }
232
233  return (retSurface);
234}
235
236
237/**
238 *  Loads a Texture to the openGL-environment.
239 * @param surface the Image to load to openGL
240 * @returns The ID of the texture.
241 */
242GLuint Texture::loadTexToGL (const SDL_Surface* surface) const
243{
244//   if (this->texture != 0 && glIsTexture(this->texture))
245//     glDeleteTextures(1, &this->texture);
246//   this->texture = 0;
247
248  int      errorCode = 0;           //!< the error code for the texture loading functions
249  GLuint   texture;                 //!< the OpenGL texture handle
250  int      mipmapLevel = 0;         //!< the maximum mipmap level for this texture
251  int      mipmapWidth = 0;         //!< the width of the mipmap
252  int      mipmapHight = 0;         //!< the height of the mipmap
253
254
255  if (surface == NULL)
256    return 0;
257
258  /* Create an OpenGL texture for the image */
259  glGenTextures(1, &texture);
260  glBindTexture(GL_TEXTURE_2D, texture);
261
262  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
263  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
264
265
266  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
267  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
268
269  /* control the mipmap levels */
270//   glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);f
271//   glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
272  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);
273
274  /* build the Texture  OpenGL V >= 1.1 */
275  glTexImage2D(GL_TEXTURE_2D,
276               0,
277               GL_RGBA,
278               surface->w, surface->h,
279               0,
280               GL_RGBA,
281               GL_UNSIGNED_BYTE,
282               surface->pixels);
283
284  // build the MipMaps automaticaly
285  errorCode = gluBuild2DMipmaps(GL_TEXTURE_2D,
286                                GL_RGBA,
287                                surface->w,
288                                surface->h,
289                                GL_RGBA,
290                                GL_UNSIGNED_BYTE,
291                                surface->pixels
292                               );
293  if(unlikely(errorCode != 0))
294    PRINTF(1)("Error while loading texture (mipmap generation), gluBuild2DMipmaps returned %i\n", errorCode);
295
296  glBindTexture(GL_TEXTURE_2D, 0);
297  return texture;
298}
299
Note: See TracBrowser for help on using the repository browser.