Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

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

File size: 5.6 KB
RevLine 
[4662]1/*
[3341]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
[3590]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER
17
[3341]18#include "texture.h"
19
[4357]20#include "debug.h"
[3622]21#include "graphics_engine.h"
22
[4662]23#ifdef HAVE_SDL_IMAGE_H
[4357]24#include <SDL_image.h>
[4662]25#else
26#include <SDL/SDL_image.h>
27#endif
[4357]28
[3341]29/**
[4836]30 *  Constructor for a Texture
[3344]31*/
[3655]32Texture::Texture(const char* imageName)
33{
[5304]34  this->setClassID(CL_TEXTURE, "Texture");
35
[5212]36  this->bAlpha = false;
[3655]37  this->texture = 0;
[5753]38  this->image = NULL;
[5754]39
[5305]40  if (imageName != NULL)
[5769]41  {
42    this->setName(imageName);
[3905]43    this->loadImage(imageName);
[5769]44  }
[4662]45}
[3655]46
47/**
[4836]48 *  Destructor of a Texture
[4662]49
[3344]50   Frees Data, and deletes the textures from GL
51*/
[4746]52Texture::~Texture()
[3344]53{
[5211]54  if (this->texture != 0)
[3344]55    glDeleteTextures(1, &this->texture);
[5753]56  if (this->image != NULL)
57    SDL_FreeSurface(this->image);
[3344]58}
59
[3905]60
[3863]61/**
[4836]62 *  loads an Image from a file to a Texture
63 * @param imageName The image to load
[3863]64*/
[3655]65bool Texture::loadImage(const char* imageName)
[3341]66{
[3622]67  if (GraphicsEngine::texturesEnabled)
[3341]68    {
[5754]69      if (this->image != NULL)
[5768]70        {
71          SDL_FreeSurface(this->image);
72          this->image = NULL;
73        }
[5754]74      if (this->texture != 0)
[5768]75        {
76          glDeleteTextures(1, &this->texture);
77          this->texture = 0;
78        }
[5305]79      if (imageName != NULL)
[4662]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);
[5753]86          if(tmpSurf != NULL)
87          {
[5754]88            PRINTF(4)("loading Image %s\n", imageName);
[5768]89            if (this->prepareSurface(tmpSurf))
90              loadTexToGL();
[5753]91            SDL_FreeSurface(tmpSurf);
92            return true;
93          }
94          else
[4662]95            {
96              PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
[5305]97              this->texture = 0;
[4662]98              return false;
99            }
100        }
[3622]101      else
[4662]102        {
[5768]103          PRINTF(2)("Image-Name not specified\n");
[4662]104          return false;
105        }
[3341]106    }
[5754]107  return false;
[3341]108}
[5753]109
[5755]110bool Texture::rebuild()
[5754]111{
112  if (this->texture != 0)
113    {
114      glDeleteTextures(1,&this->texture);
115      this->texture = 0;
116    }
117
118  if (this->image != NULL)
[5755]119    {
[5790]120      PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());
[5768]121      this->loadTexToGL();
[5755]122    }
[5768]123
[5754]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 */
[5768]131bool Texture::prepareSurface(SDL_Surface* surface)
[5753]132{
133  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
[5768]134
135  SDL_Surface* putSurface;
[5753]136  SDL_Rect area;
137  Uint32 saved_flags;
138  Uint8  saved_alpha;
[5768]139
140  putSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
141                               surface->w, surface->h,
142                               32,
[5753]143#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
[5768]144                               0x000000FF,
145                               0x0000FF00,
146                               0x00FF0000,
147                               0xFF000000
[5753]148#else
[5768]149                               0xFF000000,
150                               0x00FF0000,
151                               0x0000FF00,
152                               0x000000FF
[5753]153#endif
[5768]154                               );
155  if ( putSurface == NULL )
156  {
157    this->setSurface(NULL);
158    return false;
159  }
160
[5753]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  }
[5768]167
[5753]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;
[5768]173  SDL_BlitSurface(surface, &area, putSurface, &area);
174
[5753]175  /* Restore the alpha blending attributes */
[5768]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));
[5753]183}
184
[5768]185bool Texture::setSurface(SDL_Surface* newSurface)
186{
187  if (this->image != NULL)
188    SDL_FreeSurface(this->image);
[5753]189
[5768]190  this->image = newSurface;
191
192  return (this->image != NULL);
193}
194
195
[5753]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 */
[5768]201GLuint Texture::loadTexToGL ()
[5753]202{
[5768]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
[5753]210  /* Create an OpenGL texture for the image */
[5768]211  glGenTextures(1, &this->texture);
212  glBindTexture(GL_TEXTURE_2D, this->texture);
[5753]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,
[5768]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);
[5753]224  // build the MipMaps
225  gluBuild2DMipmaps(GL_TEXTURE_2D,
[5768]226                    GL_RGBA,
227                    this->image->w,
228                    this->image->h,
229                    GL_RGBA,
230                    GL_UNSIGNED_BYTE,
231                    this->image->pixels);
[5753]232  glBindTexture(GL_TEXTURE_2D, 0);
[5768]233  return this->texture;
[5753]234}
Note: See TracBrowser for help on using the repository browser.