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, 18 years ago

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

File size: 8.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// INCLUDING SDL_Image
24#ifdef HAVE_SDL_IMAGE_H
25#include <SDL_image.h>
26#else
27#include <SDL/SDL_image.h>
28#endif
29
30
31TextureData::TextureData()
32{
33  this->bAlpha = false;
34  this->texture = 0;
35  this->image = NULL;
36}
37
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
105Texture::Texture(const Texture& texture)
106  : data(texture.data)
107{
108  this->setClassID(CL_TEXTURE, "Texture");
109  this->priority = 0.5;
110}
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
128  if (!imageName.empty())
129  {
130    this->setName(imageName);
131    this->loadImage(imageName, target);
132  }
133}
134
135
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
156/**
157 * @brief Destructor of a Texture
158 *
159 * Frees Data, and deletes the textures from GL
160 */
161Texture::~Texture()
162{
163}
164
165
166/**
167 * @brief loads an Image from a file to a Texture
168 * @param imageName The image to load
169*/
170bool Texture::loadImage(const std::string& imageName, GLenum target)
171{
172  if (Texture::texturesEnabled)
173  {
174    if (!imageName.empty())
175    {
176      SDL_Surface* tmpSurf;
177
178      // load the new Image to memory
179      tmpSurf = IMG_Load(imageName.c_str());
180      if(tmpSurf != NULL)
181      {
182        this->data->loadSurface(tmpSurf, target);
183        SDL_FreeSurface(tmpSurf);
184        return true;
185      }
186      else
187      {
188        PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
189        this->setTexture(0);
190        return false;
191      }
192    }
193    else
194    {
195      PRINTF(2)("Image-Name not specified\n");
196      return false;
197    }
198  }
199  return false;
200}
201
202/**
203 * @brief rebuilds the texture.
204 *
205 * reloads the Texture from Memory to OpenGL.
206 */
207bool Texture::rebuild()
208{
209  this->data->setTexture(0);
210
211  if (this->data->getStoredImage() != NULL)
212  {
213    PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());
214    this->setTexture(Texture::loadTexToGL(this->data->getStoredImage()));
215  }
216}
217
218bool Texture::texturesEnabled = true;
219
220/**
221 * @brief enables, disables textures
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/**
234 * @brief converts surface to a new SDL_Surface, that is loadable by openGL
235 * @param surface the Surface to convert
236 * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false.
237 * @returns a !!new!! Surface, that is loadable by openGL.
238 */
239SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha)
240{
241  assert(surface != NULL);
242  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
243
244  SDL_Surface* retSurface;
245  SDL_Rect area;
246  Uint32 saved_flags;
247  Uint8  saved_alpha;
248
249  hasAlpha = false;
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,
263                                    surface->w, surface->h,
264                                    pixelDepth,
265#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
266                                    0x000000FF,
267                                    0x0000FF00,
268                                    0x00FF0000,
269                                    0xFF000000
270#else
271                                    0xFF000000,
272                                    0x00FF0000,
273                                    0x0000FF00,
274                                    0x000000FF
275#endif
276                                   );
277  if ( retSurface == NULL )
278    return NULL;
279
280  /* Copy the surface into the GL texture this->data->getStoredImage() */
281  area.x = 0;
282  area.y = 0;
283  area.w = surface->w;
284  area.h = surface->h;
285  SDL_BlitSurface(surface, &area, retSurface, &area);
286
287  /* Restore the alpha blending attributes */
288  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
289  {
290    SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
291    hasAlpha = true;
292  }
293
294  return (retSurface);
295}
296
297
298/**
299 * @brief Loads a Texture to the openGL-environment.
300 * @param surface the Image to load to openGL
301 * @returns The ID of the texture.
302 */
303GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target)
304{
305  //   if (this->data->getTexture() != 0 && glIsTexture(this->data->getTexture()))
306  //     glDeleteTextures(1, &this->data->getTexture());
307  //   this->data->getTexture() = 0;
308  assert(surface != NULL);
309
310  int      errorCode = 0;           //!< the error code for the texture loading functions
311  GLuint   texture = 0;             //!< the OpenGL texture handle
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
315  GLenum   format = GL_RGB;
316  if (surface->format->BitsPerPixel == 32)
317  {
318    format = GL_RGBA;
319    assert(surface->format->BitsPerPixel == 32);
320  }
321  else
322  {
323    assert(surface->format->BitsPerPixel == 24);
324  }
325
326  /* Create an OpenGL texture for the this->data->getStoredImage() */
327  Texture::generateTexture(texture, target);
328
329//   glTexImage2D(target,  0,  format,
330//                surface->w,  surface->h,
331//                0, format,  GL_UNSIGNED_BYTE,
332//                surface->pixels);
333
334///  glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
335///  glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
336
337  /// TODO CHECK THIS BACK in
338  //glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);
339
340  /* build the Texture  OpenGL V >= 1.1 */
341
342  //  printf("%s, w:%d h:%d, 0x%x\n", this->getName(), surface->w, surface->h, target);
343
344  // build the MipMaps automaticaly
345  errorCode = gluBuild2DMipmaps(target, format,
346                                surface->w,  surface->h,
347                                format,  GL_UNSIGNED_BYTE,
348                                surface->pixels
349                               );
350  if(unlikely(errorCode != 0))
351    PRINTF(1)("Error while loading texture (mipmap generation), gluBuild2DMipmaps returned %i\n", errorCode);
352
353  return texture;
354}
355
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.