Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6080 was 5863, checked in by bensch, 19 years ago

orxonox/trunk: TextureSequence finished for testing

File size: 6.7 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
[5858]22// INCLUDING SDL_Image
[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
[5863]47
[3655]48/**
[4836]49 *  Destructor of a Texture
[4662]50
[3344]51   Frees Data, and deletes the textures from GL
52*/
[4746]53Texture::~Texture()
[3344]54{
[5211]55  if (this->texture != 0)
[3344]56    glDeleteTextures(1, &this->texture);
[5753]57  if (this->image != NULL)
58    SDL_FreeSurface(this->image);
[3344]59}
60
[5863]61
[3863]62/**
[4836]63 *  loads an Image from a file to a Texture
64 * @param imageName The image to load
[3863]65*/
[3655]66bool Texture::loadImage(const char* imageName)
[3341]67{
[5858]68  if (Texture::texturesEnabled)
[3341]69    {
[5754]70      if (this->image != NULL)
[5768]71        {
72          SDL_FreeSurface(this->image);
73          this->image = NULL;
74        }
[5754]75      if (this->texture != 0)
[5768]76        {
77          glDeleteTextures(1, &this->texture);
78          this->texture = 0;
79        }
[5305]80      if (imageName != NULL)
[4662]81        {
82          SDL_Surface* tmpSurf;
[5856]83          if (this->texture != 0 && glIsTexture(this->texture))
[4662]84            glDeleteTextures(1, &this->texture);
85          // load the new Image to memory
86          tmpSurf = IMG_Load(imageName);
[5753]87          if(tmpSurf != NULL)
88          {
[5754]89            PRINTF(4)("loading Image %s\n", imageName);
[5859]90            bool hasAlpha;
91            SDL_Surface* newSurf = this->prepareSurface(tmpSurf, hasAlpha);
92            if (newSurf != NULL)
93            {
94              this->setSurface(newSurf);
95              this->setAlpha(hasAlpha);
96              this->setTexture(Texture::loadTexToGL(newSurf));
97            }
[5856]98
[5753]99            SDL_FreeSurface(tmpSurf);
100            return true;
101          }
102          else
[4662]103            {
104              PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
[5305]105              this->texture = 0;
[4662]106              return false;
107            }
108        }
[3622]109      else
[4662]110        {
[5768]111          PRINTF(2)("Image-Name not specified\n");
[4662]112          return false;
113        }
[3341]114    }
[5754]115  return false;
[3341]116}
[5753]117
[5863]118
119/**
120 * rebuilds the texture.
121 * reloads the Texture from Memory to OpenGL.
122 */
[5755]123bool Texture::rebuild()
[5754]124{
[5860]125  if (this->texture != 0)
[5754]126    {
[5860]127      if (glIsTexture(this->texture))
128        glDeleteTextures(1,&this->texture);
[5859]129      this->setTexture(0);
[5754]130    }
131
132  if (this->image != NULL)
[5755]133    {
[5790]134      PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());
[5859]135      this->setTexture(loadTexToGL(this->image));
[5755]136    }
[5863]137}
[5768]138
[5863]139
140/**
141 * set the surface this Texture handles
142 * @param newSurface the new Surface to set as the image for this Texture.
143 *
144 * This deletes the old version of the stored Texture,
145 * and sets the newly given Surface as current.
146 */
147bool Texture::setSurface(SDL_Surface* newSurface)
148{
149  if (this->image != NULL)
150    SDL_FreeSurface(this->image);
151
152  this->image = newSurface;
153
154  return (this->image != NULL);
[5754]155}
156
[5863]157
158bool Texture::texturesEnabled = true;
159
[5754]160/**
[5863]161 * enables, disables textures
162 * @param texturesEnabled true if the textures should be enabled
163 */
164void Texture::setTextureEnableState(bool texturesEnabled)
165{
166  Texture::texturesEnabled = texturesEnabled;
167}
168
169
170//////////////////////////////////////
171// UTILITY FUNCTIONALITY OF TEXTURE //
172//////////////////////////////////////
173/**
[5754]174 * converts surface to a new SDL_Surface, that is loadable by openGL
175 * @param surface the Surface to convert
[5859]176 * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false.
[5754]177 * @returns a !!new!! Surface, that is loadable by openGL.
178 */
[5859]179SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha)
[5753]180{
181  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
[5768]182
[5859]183  SDL_Surface* retSurface;
[5753]184  SDL_Rect area;
185  Uint32 saved_flags;
186  Uint8  saved_alpha;
[5768]187
[5859]188  hasAlpha = false;
189  retSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
[5863]190                                    surface->w, surface->h,
191                                    32,
[5753]192#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
[5768]193                               0x000000FF,
194                               0x0000FF00,
195                               0x00FF0000,
196                               0xFF000000
[5753]197#else
[5863]198                                   0xFF000000,
[5768]199                               0x00FF0000,
200                               0x0000FF00,
201                               0x000000FF
[5753]202#endif
[5863]203                                   );
[5859]204  if ( retSurface == NULL )
[5768]205  {
[5859]206    return NULL;
[5768]207  }
208
[5753]209  /* Save the alpha blending attributes */
210  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
211  saved_alpha = surface->format->alpha;
212  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
213    SDL_SetAlpha(surface, 0, 0);
214  }
[5768]215
[5753]216  /* Copy the surface into the GL texture image */
217  area.x = 0;
218  area.y = 0;
219  area.w = surface->w;
220  area.h = surface->h;
[5859]221  SDL_BlitSurface(surface, &area, retSurface, &area);
[5768]222
[5753]223  /* Restore the alpha blending attributes */
[5768]224  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
225  {
226    SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
[5859]227    hasAlpha = true;
[5768]228  }
229
[5859]230  return (retSurface);
[5753]231}
232
233
234/**
235 *  Loads a Texture to the openGL-environment.
236 * @param surface the Image to load to openGL
237 * @returns The ID of the texture.
238 */
[5856]239GLuint Texture::loadTexToGL (const SDL_Surface* surface)
[5753]240{
[5856]241//   if (this->texture != 0 && glIsTexture(this->texture))
242//     glDeleteTextures(1, &this->texture);
243//   this->texture = 0;
[5768]244
[5856]245  GLuint texture;
246
247  if (surface == NULL)
[5768]248    return 0;
249
[5753]250  /* Create an OpenGL texture for the image */
[5856]251  glGenTextures(1, &texture);
252  glBindTexture(GL_TEXTURE_2D, texture);
[5753]253  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
254  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
255  // build the Texture
256  glTexImage2D(GL_TEXTURE_2D,
[5768]257               0,
258               GL_RGBA,
[5856]259               surface->w, surface->h,
[5768]260               0,
261               GL_RGBA,
262               GL_UNSIGNED_BYTE,
[5856]263               surface->pixels);
[5753]264  // build the MipMaps
265  gluBuild2DMipmaps(GL_TEXTURE_2D,
[5768]266                    GL_RGBA,
[5856]267                    surface->w,
268                    surface->h,
[5768]269                    GL_RGBA,
270                    GL_UNSIGNED_BYTE,
[5856]271                    surface->pixels);
[5753]272  glBindTexture(GL_TEXTURE_2D, 0);
[5856]273  return texture;
[5753]274}
Note: See TracBrowser for help on using the repository browser.