Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/graphics/importer/texture.cc @ 6129

Last change on this file since 6129 was 6129, checked in by patrick, 18 years ago

network: read some stuff about textures, extending the texture class. not yet finished

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