Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/water/src/lib/graphics/importer/texture.cc @ 7749

Last change on this file since 7749 was 7700, checked in by stefalie, 19 years ago

branches/water: first attempt to realise reflection

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