Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: small changes in the Texture interface

File size: 6.6 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
22// INCLUDING SDL_Image
23#ifdef HAVE_SDL_IMAGE_H
24#include <SDL_image.h>
25#else
26#include <SDL/SDL_image.h>
27#endif
28
29/**
30 *  Constructor for a Texture
31*/
32Texture::Texture(const char* imageName)
33{
34  this->setClassID(CL_TEXTURE, "Texture");
35
36  this->bAlpha = false;
37  this->texture = 0;
38  this->image = NULL;
39
40  if (imageName != NULL)
41  {
42    this->setName(imageName);
43    this->loadImage(imageName);
44  }
45}
46
47/**
48 *  Destructor of a Texture
49
50   Frees Data, and deletes the textures from GL
51*/
52Texture::~Texture()
53{
54  if (this->texture != 0)
55    glDeleteTextures(1, &this->texture);
56  if (this->image != NULL)
57    SDL_FreeSurface(this->image);
58}
59
60/**
61 *  loads an Image from a file to a Texture
62 * @param imageName The image to load
63*/
64bool Texture::loadImage(const char* imageName)
65{
66  if (Texture::texturesEnabled)
67    {
68      if (this->image != NULL)
69        {
70          SDL_FreeSurface(this->image);
71          this->image = NULL;
72        }
73      if (this->texture != 0)
74        {
75          glDeleteTextures(1, &this->texture);
76          this->texture = 0;
77        }
78      if (imageName != NULL)
79        {
80          SDL_Surface* tmpSurf;
81          if (this->texture != 0 && glIsTexture(this->texture))
82            glDeleteTextures(1, &this->texture);
83          // load the new Image to memory
84          tmpSurf = IMG_Load(imageName);
85          if(tmpSurf != NULL)
86          {
87            PRINTF(4)("loading Image %s\n", imageName);
88            bool hasAlpha;
89            SDL_Surface* newSurf = this->prepareSurface(tmpSurf, hasAlpha);
90            if (newSurf != NULL)
91            {
92              this->setSurface(newSurf);
93              this->setAlpha(hasAlpha);
94              this->setTexture(Texture::loadTexToGL(newSurf));
95            }
96
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
116bool Texture::rebuild()
117{
118  if (this->texture != 0 && glIsTexture(this->texture))
119    {
120      glDeleteTextures(1,&this->texture);
121      this->setTexture(0);
122    }
123
124  if (this->image != NULL)
125    {
126      PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());
127      this->setTexture(loadTexToGL(this->image));
128    }
129
130}
131
132/**
133 * converts surface to a new SDL_Surface, that is loadable by openGL
134 * @param surface the Surface to convert
135 * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false.
136 * @returns a !!new!! Surface, that is loadable by openGL.
137 */
138SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha)
139{
140  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
141
142  SDL_Surface* retSurface;
143  SDL_Rect area;
144  Uint32 saved_flags;
145  Uint8  saved_alpha;
146
147  hasAlpha = false;
148  retSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
149                               surface->w, surface->h,
150                               32,
151#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
152                               0x000000FF,
153                               0x0000FF00,
154                               0x00FF0000,
155                               0xFF000000
156#else
157                               0xFF000000,
158                               0x00FF0000,
159                               0x0000FF00,
160                               0x000000FF
161#endif
162                               );
163  if ( retSurface == NULL )
164  {
165    return NULL;
166  }
167
168  /* Save the alpha blending attributes */
169  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
170  saved_alpha = surface->format->alpha;
171  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
172    SDL_SetAlpha(surface, 0, 0);
173  }
174
175  /* Copy the surface into the GL texture image */
176  area.x = 0;
177  area.y = 0;
178  area.w = surface->w;
179  area.h = surface->h;
180  SDL_BlitSurface(surface, &area, retSurface, &area);
181
182  /* Restore the alpha blending attributes */
183  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
184  {
185    SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
186    hasAlpha = true;
187  }
188
189  return (retSurface);
190}
191
192/**
193 * set the surface this Texture handles
194 * @param newSurface the new Surface to set as the image for this Texture.
195 *
196 * This deletes the old version of the stored Texture,
197 * and sets the newly given Surface as current.
198 */
199bool Texture::setSurface(SDL_Surface* newSurface)
200{
201  if (this->image != NULL)
202    SDL_FreeSurface(this->image);
203
204  this->image = newSurface;
205
206  return (this->image != NULL);
207}
208
209
210bool Texture::texturesEnabled = true;
211
212/**
213 * enables, disables textures
214 * @param texturesEnabled true if the textures should be enabled
215 */
216void Texture::setTextureEnableState(bool texturesEnabled)
217{
218  Texture::texturesEnabled = texturesEnabled;
219}
220
221
222//////////////////////////////////////
223// UTILITY FUNCTIONALITY OF TEXTURE //
224//////////////////////////////////////
225/**
226 *  Loads a Texture to the openGL-environment.
227 * @param surface the Image to load to openGL
228 * @returns The ID of the texture.
229 */
230GLuint Texture::loadTexToGL (const SDL_Surface* surface)
231{
232//   if (this->texture != 0 && glIsTexture(this->texture))
233//     glDeleteTextures(1, &this->texture);
234//   this->texture = 0;
235
236  GLuint texture;
237
238  if (surface == NULL)
239    return 0;
240
241  /* Create an OpenGL texture for the image */
242  glGenTextures(1, &texture);
243  glBindTexture(GL_TEXTURE_2D, texture);
244  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
245  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
246  // build the Texture
247  glTexImage2D(GL_TEXTURE_2D,
248               0,
249               GL_RGBA,
250               surface->w, surface->h,
251               0,
252               GL_RGBA,
253               GL_UNSIGNED_BYTE,
254               surface->pixels);
255  // build the MipMaps
256  gluBuild2DMipmaps(GL_TEXTURE_2D,
257                    GL_RGBA,
258                    surface->w,
259                    surface->h,
260                    GL_RGBA,
261                    GL_UNSIGNED_BYTE,
262                    surface->pixels);
263  glBindTexture(GL_TEXTURE_2D, 0);
264  return texture;
265}
Note: See TracBrowser for help on using the repository browser.