Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8252 was 8145, checked in by bensch, 18 years ago

trunk: merged the gui back
merged with command:
svn merge -r8114:HEAD https://svn.orxonox.net/orxonox/branches/gui .
→ no conflicts

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