Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7591 was 7221, checked in by bensch, 19 years ago

orxonox/trunk: merged the std-branche back, it runs on windows and Linux

svn merge https://svn.orxonox.net/orxonox/branches/std . -r7202:HEAD

File size: 7.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"
[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/**
[4836]32 *  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/**
[4836]52 *  Destructor of a Texture
[4662]53
[3344]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/**
[4836]66 *  loads an Image from a file to a Texture
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      {
92        PRINTF(4)("loading Image %s\n", imageName);
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/**
123 * rebuilds the texture.
124 * reloads the Texture from Memory to OpenGL.
125 */
[5755]126bool Texture::rebuild()
[5754]127{
[5860]128  if (this->texture != 0)
[6859]129  {
130    if (glIsTexture(this->texture))
131      glDeleteTextures(1,&this->texture);
132    this->setTexture(0);
133  }
[5754]134
135  if (this->image != NULL)
[6859]136  {
137    PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());
138    this->setTexture(loadTexToGL(this->image));
139  }
[5863]140}
[5768]141
[5863]142
143/**
144 * set the surface this Texture handles
145 * @param newSurface the new Surface to set as the image for this Texture.
146 *
147 * This deletes the old version of the stored Texture,
148 * and sets the newly given Surface as current.
149 */
150bool Texture::setSurface(SDL_Surface* newSurface)
151{
152  if (this->image != NULL)
153    SDL_FreeSurface(this->image);
154
155  this->image = newSurface;
156
157  return (this->image != NULL);
[5754]158}
159
[5863]160
161bool Texture::texturesEnabled = true;
162
[5754]163/**
[5863]164 * enables, disables textures
165 * @param texturesEnabled true if the textures should be enabled
166 */
167void Texture::setTextureEnableState(bool texturesEnabled)
168{
169  Texture::texturesEnabled = texturesEnabled;
170}
171
172
173//////////////////////////////////////
174// UTILITY FUNCTIONALITY OF TEXTURE //
175//////////////////////////////////////
176/**
[5754]177 * converts surface to a new SDL_Surface, that is loadable by openGL
178 * @param surface the Surface to convert
[5859]179 * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false.
[5754]180 * @returns a !!new!! Surface, that is loadable by openGL.
181 */
[6139]182SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) const
[5753]183{
184  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
[5768]185
[5859]186  SDL_Surface* retSurface;
[5753]187  SDL_Rect area;
188  Uint32 saved_flags;
189  Uint8  saved_alpha;
[5768]190
[5859]191  hasAlpha = false;
192  retSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
[5863]193                                    surface->w, surface->h,
194                                    32,
[5753]195#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
[6859]196                                    0x000000FF,
197                                    0x0000FF00,
198                                    0x00FF0000,
199                                    0xFF000000
[5753]200#else
[6859]201                                    0xFF000000,
202                                    0x00FF0000,
203                                    0x0000FF00,
204                                    0x000000FF
[5753]205#endif
[5863]206                                   );
[5859]207  if ( retSurface == NULL )
[5768]208  {
[5859]209    return NULL;
[5768]210  }
211
[5753]212  /* Save the alpha blending attributes */
213  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
214  saved_alpha = surface->format->alpha;
[6859]215  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
216  {
[5753]217    SDL_SetAlpha(surface, 0, 0);
218  }
[5768]219
[5753]220  /* Copy the surface into the GL texture image */
221  area.x = 0;
222  area.y = 0;
223  area.w = surface->w;
224  area.h = surface->h;
[5859]225  SDL_BlitSurface(surface, &area, retSurface, &area);
[5768]226
[5753]227  /* Restore the alpha blending attributes */
[5768]228  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
229  {
230    SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
[5859]231    hasAlpha = true;
[5768]232  }
233
[5859]234  return (retSurface);
[5753]235}
236
237
238/**
239 *  Loads a Texture to the openGL-environment.
240 * @param surface the Image to load to openGL
241 * @returns The ID of the texture.
242 */
[6465]243GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target) const
[5753]244{
[6859]245  //   if (this->texture != 0 && glIsTexture(this->texture))
246  //     glDeleteTextures(1, &this->texture);
247  //   this->texture = 0;
[5768]248
[6139]249  int      errorCode = 0;           //!< the error code for the texture loading functions
250  GLuint   texture;                 //!< the OpenGL texture handle
251  int      mipmapLevel = 0;         //!< the maximum mipmap level for this texture
252  int      mipmapWidth = 0;         //!< the width of the mipmap
253  int      mipmapHight = 0;         //!< the height of the mipmap
[5856]254
[6139]255
[5856]256  if (surface == NULL)
[5768]257    return 0;
258
[5753]259  /* Create an OpenGL texture for the image */
[5856]260  glGenTextures(1, &texture);
[6465]261  glBindTexture(target, texture);
[6139]262
[6634]263  glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
264  glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
265
266  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR/*_MIPMAP_LINEAR*/);
[6465]267  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
[6165]268
[6634]269  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);
[6165]270
[6145]271
[6139]272  /* control the mipmap levels */
[6634]273  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);
274  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
[6139]275
276  /* build the Texture  OpenGL V >= 1.1 */
[6465]277  glTexImage2D(target,
[5768]278               0,
279               GL_RGBA,
[6523]280               surface->w,
281               surface->h,
[5768]282               0,
283               GL_RGBA,
284               GL_UNSIGNED_BYTE,
[5856]285               surface->pixels);
[6871]286  //  printf("%s, w:%d h:%d, 0x%x\n", this->getName(), surface->w, surface->h, target);
[6139]287
288  // build the MipMaps automaticaly
[6465]289  errorCode = gluBuild2DMipmaps(target,
[6139]290                                GL_RGBA,
291                                surface->w,
292                                surface->h,
293                                GL_RGBA,
294                                GL_UNSIGNED_BYTE,
295                                surface->pixels
296                               );
297  if(unlikely(errorCode != 0))
[6165]298    PRINTF(1)("Error while loading texture (mipmap generation), gluBuild2DMipmaps returned %i\n", errorCode);
[6139]299
[5856]300  return texture;
[5753]301}
[6139]302
Note: See TracBrowser for help on using the repository browser.