Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: introducing multiframe-texture

File size: 6.1 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            if (this->prepareSurface(tmpSurf))
89              this->texture = Texture::loadTexToGL(this->image);
90
91            SDL_FreeSurface(tmpSurf);
92            return true;
93          }
94          else
95            {
96              PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
97              this->texture = 0;
98              return false;
99            }
100        }
101      else
102        {
103          PRINTF(2)("Image-Name not specified\n");
104          return false;
105        }
106    }
107  return false;
108}
109
110bool Texture::rebuild()
111{
112  if (this->texture != 0 && glIsTexture(this->texture))
113    {
114      glDeleteTextures(1,&this->texture);
115      this->texture = 0;
116    }
117
118  if (this->image != NULL)
119    {
120      PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());
121      this->texture = loadTexToGL(this->image);
122    }
123
124}
125
126/**
127 * converts surface to a new SDL_Surface, that is loadable by openGL
128 * @param surface the Surface to convert
129 * @returns a !!new!! Surface, that is loadable by openGL.
130 */
131bool Texture::prepareSurface(SDL_Surface* surface)
132{
133  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
134
135  SDL_Surface* putSurface;
136  SDL_Rect area;
137  Uint32 saved_flags;
138  Uint8  saved_alpha;
139
140  putSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
141                               surface->w, surface->h,
142                               32,
143#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
144                               0x000000FF,
145                               0x0000FF00,
146                               0x00FF0000,
147                               0xFF000000
148#else
149                               0xFF000000,
150                               0x00FF0000,
151                               0x0000FF00,
152                               0x000000FF
153#endif
154                               );
155  if ( putSurface == NULL )
156  {
157    this->setSurface(NULL);
158    return false;
159  }
160
161  /* Save the alpha blending attributes */
162  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
163  saved_alpha = surface->format->alpha;
164  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
165    SDL_SetAlpha(surface, 0, 0);
166  }
167
168  /* Copy the surface into the GL texture image */
169  area.x = 0;
170  area.y = 0;
171  area.w = surface->w;
172  area.h = surface->h;
173  SDL_BlitSurface(surface, &area, putSurface, &area);
174
175  /* Restore the alpha blending attributes */
176  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
177  {
178    SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
179    this->bAlpha = true;
180  }
181
182  return (this->setSurface(putSurface));
183}
184
185bool Texture::setSurface(SDL_Surface* newSurface)
186{
187  if (this->image != NULL)
188    SDL_FreeSurface(this->image);
189
190  this->image = newSurface;
191
192  return (this->image != NULL);
193}
194
195
196bool Texture::texturesEnabled = true;
197
198/**
199 * enables, disables textures
200 * @param texturesEnabled true if the textures should be enabled
201 */
202void Texture::setTextureEnableState(bool texturesEnabled)
203{
204  Texture::texturesEnabled = texturesEnabled;
205}
206
207
208//////////////////////////////////////
209// UTILITY FUNCTIONALITY OF TEXTURE //
210//////////////////////////////////////
211/**
212 *  Loads a Texture to the openGL-environment.
213 * @param surface the Image to load to openGL
214 * @returns The ID of the texture.
215 */
216GLuint Texture::loadTexToGL (const SDL_Surface* surface)
217{
218//   if (this->texture != 0 && glIsTexture(this->texture))
219//     glDeleteTextures(1, &this->texture);
220//   this->texture = 0;
221
222  GLuint texture;
223
224  if (surface == NULL)
225    return 0;
226
227  /* Create an OpenGL texture for the image */
228  glGenTextures(1, &texture);
229  glBindTexture(GL_TEXTURE_2D, texture);
230  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
231  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
232  // build the Texture
233  glTexImage2D(GL_TEXTURE_2D,
234               0,
235               GL_RGBA,
236               surface->w, surface->h,
237               0,
238               GL_RGBA,
239               GL_UNSIGNED_BYTE,
240               surface->pixels);
241  // build the MipMaps
242  gluBuild2DMipmaps(GL_TEXTURE_2D,
243                    GL_RGBA,
244                    surface->w,
245                    surface->h,
246                    GL_RGBA,
247                    GL_UNSIGNED_BYTE,
248                    surface->pixels);
249  glBindTexture(GL_TEXTURE_2D, 0);
250  return texture;
251}
Note: See TracBrowser for help on using the repository browser.