Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: textures are rebuild on resize of the winodow (on winodws)

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