Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Material handles references… lets see if this works

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