Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: splitted Texture and TextureData into two files.
Also fixed the Creator-Function for Textures with empty textures with size

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