Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: rescale of the screen-size should reload Models too on windows

File size: 5.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#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)
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              loadTexToGL();
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)
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->loadTexToGL();
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
196/**
197 *  Loads a Texture to the openGL-environment.
198 * @param surface the Image to load to openGL
199 * @returns The ID of the texture.
200 */
201GLuint Texture::loadTexToGL ()
202{
203  if (this->texture != 0 && glIsTexture(this->texture))
204    glDeleteTextures(1, &this->texture);
205  this->texture = 0;
206
207  if (this->image == NULL)
208    return 0;
209
210  /* Create an OpenGL texture for the image */
211  glGenTextures(1, &this->texture);
212  glBindTexture(GL_TEXTURE_2D, this->texture);
213  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
214  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
215  // build the Texture
216  glTexImage2D(GL_TEXTURE_2D,
217               0,
218               GL_RGBA,
219               this->image->w, this->image->h,
220               0,
221               GL_RGBA,
222               GL_UNSIGNED_BYTE,
223               this->image->pixels);
224  // build the MipMaps
225  gluBuild2DMipmaps(GL_TEXTURE_2D,
226                    GL_RGBA,
227                    this->image->w,
228                    this->image->h,
229                    GL_RGBA,
230                    GL_UNSIGNED_BYTE,
231                    this->image->pixels);
232  glBindTexture(GL_TEXTURE_2D, 0);
233  return this->texture;
234}
Note: See TracBrowser for help on using the repository browser.