Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.old/src/lib/graphics/importer/texture.cc @ 9337

Last change on this file since 9337 was 8950, checked in by ponder, 18 years ago

Minor changes to the terrain

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