Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7783 was 7751, checked in by bensch, 19 years ago

orxonox/trunk: all Textures are shown again.

File size: 7.9 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"
[6139]21#include "compiler.h"
22#include <math.h>
[3622]23
[5858]24// INCLUDING SDL_Image
[4662]25#ifdef HAVE_SDL_IMAGE_H
[4357]26#include <SDL_image.h>
[4662]27#else
28#include <SDL/SDL_image.h>
29#endif
[4357]30
[3341]31/**
[7727]32 * @brief Constructor for a Texture
[3344]33*/
[7221]34Texture::Texture(const std::string& imageName, GLenum target)
[3655]35{
[5304]36  this->setClassID(CL_TEXTURE, "Texture");
37
[5212]38  this->bAlpha = false;
[3655]39  this->texture = 0;
[5753]40  this->image = NULL;
[6139]41  this->priority = 0.5;
[5754]42
[7221]43  if (!imageName.empty())
[5769]44  {
45    this->setName(imageName);
[6465]46    this->loadImage(imageName, target);
[5769]47  }
[4662]48}
[3655]49
[5863]50
[3655]51/**
[7727]52 * @brief Destructor of a Texture
53 *
54 * Frees Data, and deletes the textures from GL
55 */
[4746]56Texture::~Texture()
[3344]57{
[5211]58  if (this->texture != 0)
[3344]59    glDeleteTextures(1, &this->texture);
[5753]60  if (this->image != NULL)
61    SDL_FreeSurface(this->image);
[3344]62}
63
[5863]64
[3863]65/**
[7727]66 * @brief loads an Image from a file to a Texture
[4836]67 * @param imageName The image to load
[3863]68*/
[7221]69bool Texture::loadImage(const std::string& imageName, GLenum target)
[3341]70{
[5858]71  if (Texture::texturesEnabled)
[6859]72  {
73    if (this->image != NULL)
[3341]74    {
[6859]75      SDL_FreeSurface(this->image);
76      this->image = NULL;
77    }
78    if (this->texture != 0)
79    {
80      glDeleteTextures(1, &this->texture);
81      this->texture = 0;
82    }
[7221]83    if (!imageName.empty())
[6859]84    {
85      SDL_Surface* tmpSurf;
86      if (this->texture != 0 && glIsTexture(this->texture))
87        glDeleteTextures(1, &this->texture);
88      // load the new Image to memory
[7221]89      tmpSurf = IMG_Load(imageName.c_str());
[6859]90      if(tmpSurf != NULL)
91      {
[7676]92        PRINTF(4)("loading Image %s\n", imageName.c_str());
[6859]93        bool hasAlpha;
94        SDL_Surface* newSurf = this->prepareSurface(tmpSurf, hasAlpha);
95        if (newSurf != NULL)
[5768]96        {
[6859]97          this->setSurface(newSurf);
98          this->setAlpha(hasAlpha);
99          this->setTexture(Texture::loadTexToGL(newSurf, target));
[5768]100        }
[5856]101
[6859]102        SDL_FreeSurface(tmpSurf);
103        return true;
104      }
[3622]105      else
[6859]106      {
107        PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
108        this->texture = 0;
109        return false;
110      }
[3341]111    }
[6859]112    else
113    {
114      PRINTF(2)("Image-Name not specified\n");
115      return false;
116    }
117  }
[5754]118  return false;
[3341]119}
[5753]120
[5863]121
122/**
[7727]123 * @brief rebuilds the texture.
124 *
[5863]125 * reloads the Texture from Memory to OpenGL.
126 */
[5755]127bool Texture::rebuild()
[5754]128{
[5860]129  if (this->texture != 0)
[6859]130  {
131    if (glIsTexture(this->texture))
132      glDeleteTextures(1,&this->texture);
133    this->setTexture(0);
134  }
[5754]135
136  if (this->image != NULL)
[6859]137  {
138    PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());
139    this->setTexture(loadTexToGL(this->image));
140  }
[5863]141}
[5768]142
[5863]143
144/**
[7727]145 * @brief set the surface this Texture handles
[5863]146 * @param newSurface the new Surface to set as the image for this Texture.
147 *
148 * This deletes the old version of the stored Texture,
149 * and sets the newly given Surface as current.
150 */
151bool Texture::setSurface(SDL_Surface* newSurface)
152{
153  if (this->image != NULL)
154    SDL_FreeSurface(this->image);
155
156  this->image = newSurface;
157
158  return (this->image != NULL);
[5754]159}
160
[5863]161
162bool Texture::texturesEnabled = true;
163
[5754]164/**
[7727]165 * @brief enables, disables textures
[5863]166 * @param texturesEnabled true if the textures should be enabled
167 */
168void Texture::setTextureEnableState(bool texturesEnabled)
169{
170  Texture::texturesEnabled = texturesEnabled;
171}
172
173
174//////////////////////////////////////
175// UTILITY FUNCTIONALITY OF TEXTURE //
176//////////////////////////////////////
177/**
[7727]178 * @brief converts surface to a new SDL_Surface, that is loadable by openGL
[5754]179 * @param surface the Surface to convert
[5859]180 * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false.
[5754]181 * @returns a !!new!! Surface, that is loadable by openGL.
182 */
[6139]183SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) const
[5753]184{
[7727]185  assert(surface != NULL);
[5753]186  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
[5768]187
[5859]188  SDL_Surface* retSurface;
[5753]189  SDL_Rect area;
190  Uint32 saved_flags;
191  Uint8  saved_alpha;
[5768]192
[5859]193  hasAlpha = false;
[7727]194  int pixelDepth = 24;
195
196  /* Save the alpha blending attributes */
197  saved_flags = surface->flags&(SDL_SRCALPHA | SDL_RLEACCELOK);
198  saved_alpha = surface->format->alpha;
199  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
200  {
201    SDL_SetAlpha(surface, 0, 0);
202    hasAlpha = true;
203    pixelDepth = 32;
204  }
205
206  retSurface = SDL_CreateRGBSurface(SDL_HWSURFACE,
[5863]207                                    surface->w, surface->h,
[7727]208                                    pixelDepth,
[5753]209#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
[6859]210                                    0x000000FF,
211                                    0x0000FF00,
212                                    0x00FF0000,
213                                    0xFF000000
[5753]214#else
[6859]215                                    0xFF000000,
216                                    0x00FF0000,
217                                    0x0000FF00,
218                                    0x000000FF
[5753]219#endif
[5863]220                                   );
[5859]221  if ( retSurface == NULL )
222    return NULL;
[5768]223
[5753]224  /* Copy the surface into the GL texture image */
225  area.x = 0;
226  area.y = 0;
227  area.w = surface->w;
228  area.h = surface->h;
[5859]229  SDL_BlitSurface(surface, &area, retSurface, &area);
[5768]230
[5753]231  /* Restore the alpha blending attributes */
[5768]232  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
233  {
234    SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
[5859]235    hasAlpha = true;
[5768]236  }
237
[5859]238  return (retSurface);
[5753]239}
240
241
242/**
[7727]243 * @brief Loads a Texture to the openGL-environment.
[5753]244 * @param surface the Image to load to openGL
245 * @returns The ID of the texture.
246 */
[6465]247GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target) const
[5753]248{
[6859]249  //   if (this->texture != 0 && glIsTexture(this->texture))
250  //     glDeleteTextures(1, &this->texture);
251  //   this->texture = 0;
[7727]252  assert(surface != NULL);
[5768]253
[6139]254  int      errorCode = 0;           //!< the error code for the texture loading functions
255  GLuint   texture;                 //!< the OpenGL texture handle
256  int      mipmapLevel = 0;         //!< the maximum mipmap level for this texture
257  int      mipmapWidth = 0;         //!< the width of the mipmap
258  int      mipmapHight = 0;         //!< the height of the mipmap
[7727]259  GLenum   format = GL_RGB;
260  if (this->bAlpha)
261  {
262    format = GL_RGBA;
263    assert(surface->format->BitsPerPixel == 32);
264  }
265  else
266  {
267    assert(surface->format->BitsPerPixel == 24);
268  }
[5856]269
[5753]270  /* Create an OpenGL texture for the image */
[5856]271  glGenTextures(1, &texture);
[6465]272  glBindTexture(target, texture);
[6139]273
[7751]274//   glTexImage2D(target,  0,  format,
275//                surface->w,  surface->h,
276//                0, format,  GL_UNSIGNED_BYTE,
277//                surface->pixels);
[7727]278
[6634]279  glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
280  glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
281
282  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR/*_MIPMAP_LINEAR*/);
[6465]283  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
[6165]284
[6634]285  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);
[6165]286
[6145]287
[6139]288  /* control the mipmap levels */
[6634]289  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);
290  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
[6139]291
292  /* build the Texture  OpenGL V >= 1.1 */
[7727]293
[6871]294  //  printf("%s, w:%d h:%d, 0x%x\n", this->getName(), surface->w, surface->h, target);
[6139]295
296  // build the MipMaps automaticaly
[7727]297  errorCode = gluBuild2DMipmaps(target, format,
298                                surface->w,  surface->h,
299                                format,  GL_UNSIGNED_BYTE,
[6139]300                                surface->pixels
301                               );
302  if(unlikely(errorCode != 0))
[6165]303    PRINTF(1)("Error while loading texture (mipmap generation), gluBuild2DMipmaps returned %i\n", errorCode);
[6139]304
[5856]305  return texture;
[5753]306}
[6139]307
Note: See TracBrowser for help on using the repository browser.