Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some initial work at MultiFrameTexture.cc

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)
119    {
120      if (glIsTexture(this->texture))
121        glDeleteTextures(1,&this->texture);
122      this->setTexture(0);
123    }
124
125  if (this->image != NULL)
126    {
127      PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());
128      this->setTexture(loadTexToGL(this->image));
129    }
130
131}
132
133/**
134 * converts surface to a new SDL_Surface, that is loadable by openGL
135 * @param surface the Surface to convert
136 * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false.
137 * @returns a !!new!! Surface, that is loadable by openGL.
138 */
139SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha)
140{
141  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
142
143  SDL_Surface* retSurface;
144  SDL_Rect area;
145  Uint32 saved_flags;
146  Uint8  saved_alpha;
147
148  hasAlpha = false;
149  retSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
150                               surface->w, surface->h,
151                               32,
152#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
153                               0x000000FF,
154                               0x0000FF00,
155                               0x00FF0000,
156                               0xFF000000
157#else
158                               0xFF000000,
159                               0x00FF0000,
160                               0x0000FF00,
161                               0x000000FF
162#endif
163                               );
164  if ( retSurface == NULL )
165  {
166    return NULL;
167  }
168
169  /* Save the alpha blending attributes */
170  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
171  saved_alpha = surface->format->alpha;
172  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
173    SDL_SetAlpha(surface, 0, 0);
174  }
175
176  /* Copy the surface into the GL texture image */
177  area.x = 0;
178  area.y = 0;
179  area.w = surface->w;
180  area.h = surface->h;
181  SDL_BlitSurface(surface, &area, retSurface, &area);
182
183  /* Restore the alpha blending attributes */
184  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
185  {
186    SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
187    hasAlpha = true;
188  }
189
190  return (retSurface);
191}
192
193/**
194 * set the surface this Texture handles
195 * @param newSurface the new Surface to set as the image for this Texture.
196 *
197 * This deletes the old version of the stored Texture,
198 * and sets the newly given Surface as current.
199 */
200bool Texture::setSurface(SDL_Surface* newSurface)
201{
202  if (this->image != NULL)
203    SDL_FreeSurface(this->image);
204
205  this->image = newSurface;
206
207  return (this->image != NULL);
208}
209
210
211bool Texture::texturesEnabled = true;
212
213/**
214 * enables, disables textures
215 * @param texturesEnabled true if the textures should be enabled
216 */
217void Texture::setTextureEnableState(bool texturesEnabled)
218{
219  Texture::texturesEnabled = texturesEnabled;
220}
221
222
223//////////////////////////////////////
224// UTILITY FUNCTIONALITY OF TEXTURE //
225//////////////////////////////////////
226/**
227 *  Loads a Texture to the openGL-environment.
228 * @param surface the Image to load to openGL
229 * @returns The ID of the texture.
230 */
231GLuint Texture::loadTexToGL (const SDL_Surface* surface)
232{
233//   if (this->texture != 0 && glIsTexture(this->texture))
234//     glDeleteTextures(1, &this->texture);
235//   this->texture = 0;
236
237  GLuint texture;
238
239  if (surface == NULL)
240    return 0;
241
242  /* Create an OpenGL texture for the image */
243  glGenTextures(1, &texture);
244  glBindTexture(GL_TEXTURE_2D, texture);
245  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
246  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
247  // build the Texture
248  glTexImage2D(GL_TEXTURE_2D,
249               0,
250               GL_RGBA,
251               surface->w, surface->h,
252               0,
253               GL_RGBA,
254               GL_UNSIGNED_BYTE,
255               surface->pixels);
256  // build the MipMaps
257  gluBuild2DMipmaps(GL_TEXTURE_2D,
258                    GL_RGBA,
259                    surface->w,
260                    surface->h,
261                    GL_RGBA,
262                    GL_UNSIGNED_BYTE,
263                    surface->pixels);
264  glBindTexture(GL_TEXTURE_2D, 0);
265  return texture;
266}
Note: See TracBrowser for help on using the repository browser.