Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: TextureToGL is now more modular

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