Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/graphics/importer/texture.cc @ 6129

Last change on this file since 6129 was 6129, checked in by patrick, 18 years ago

network: read some stuff about textures, extending the texture class. not yet finished

File size: 7.0 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
22// INCLUDING SDL_Image
23#ifdef HAVE_SDL_IMAGE_H
24#include <SDL_image.h>
25#else
26#include <SDL/SDL_image.h>
27#endif
28
29/**
30 *  Constructor for a Texture
31*/
32Texture::Texture(const char* imageName)
33{
34  this->setClassID(CL_TEXTURE, "Texture");
35
36  this->bAlpha = false;
37  this->texture = 0;
38  this->image = NULL;
39  this->priority = 0.5;
40
41  if (imageName != NULL)
42  {
43    this->setName(imageName);
44    this->loadImage(imageName);
45  }
46}
47
48
49/**
50 *  Destructor of a Texture
51
52   Frees Data, and deletes the textures from GL
53*/
54Texture::~Texture()
55{
56  if (this->texture != 0)
57    glDeleteTextures(1, &this->texture);
58  if (this->image != NULL)
59    SDL_FreeSurface(this->image);
60}
61
62
63/**
64 *  loads an Image from a file to a Texture
65 * @param imageName The image to load
66*/
67bool Texture::loadImage(const char* imageName)
68{
69  if (Texture::texturesEnabled)
70    {
71      if (this->image != NULL)
72        {
73          SDL_FreeSurface(this->image);
74          this->image = NULL;
75        }
76      if (this->texture != 0)
77        {
78          glDeleteTextures(1, &this->texture);
79          this->texture = 0;
80        }
81      if (imageName != NULL)
82        {
83          SDL_Surface* tmpSurf;
84          if (this->texture != 0 && glIsTexture(this->texture))
85            glDeleteTextures(1, &this->texture);
86          // load the new Image to memory
87          tmpSurf = IMG_Load(imageName);
88          if(tmpSurf != NULL)
89          {
90            PRINTF(4)("loading Image %s\n", imageName);
91            bool hasAlpha;
92            SDL_Surface* newSurf = this->prepareSurface(tmpSurf, hasAlpha);
93            if (newSurf != NULL)
94            {
95              this->setSurface(newSurf);
96              this->setAlpha(hasAlpha);
97              this->setTexture(Texture::loadTexToGL(newSurf));
98            }
99
100            SDL_FreeSurface(tmpSurf);
101            return true;
102          }
103          else
104            {
105              PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
106              this->texture = 0;
107              return false;
108            }
109        }
110      else
111        {
112          PRINTF(2)("Image-Name not specified\n");
113          return false;
114        }
115    }
116  return false;
117}
118
119
120/**
121 * rebuilds the texture.
122 * reloads the Texture from Memory to OpenGL.
123 */
124bool Texture::rebuild()
125{
126  if (this->texture != 0)
127    {
128      if (glIsTexture(this->texture))
129        glDeleteTextures(1,&this->texture);
130      this->setTexture(0);
131    }
132
133  if (this->image != NULL)
134    {
135      PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());
136      this->setTexture(loadTexToGL(this->image));
137    }
138}
139
140
141/**
142 * set the surface this Texture handles
143 * @param newSurface the new Surface to set as the image for this Texture.
144 *
145 * This deletes the old version of the stored Texture,
146 * and sets the newly given Surface as current.
147 */
148bool Texture::setSurface(SDL_Surface* newSurface)
149{
150  if (this->image != NULL)
151    SDL_FreeSurface(this->image);
152
153  this->image = newSurface;
154
155  return (this->image != NULL);
156}
157
158
159bool Texture::texturesEnabled = true;
160
161/**
162 * enables, disables textures
163 * @param texturesEnabled true if the textures should be enabled
164 */
165void Texture::setTextureEnableState(bool texturesEnabled)
166{
167  Texture::texturesEnabled = texturesEnabled;
168}
169
170
171//////////////////////////////////////
172// UTILITY FUNCTIONALITY OF TEXTURE //
173//////////////////////////////////////
174/**
175 * converts surface to a new SDL_Surface, that is loadable by openGL
176 * @param surface the Surface to convert
177 * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false.
178 * @returns a !!new!! Surface, that is loadable by openGL.
179 */
180SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha)
181{
182  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
183
184  SDL_Surface* retSurface;
185  SDL_Rect area;
186  Uint32 saved_flags;
187  Uint8  saved_alpha;
188
189  hasAlpha = false;
190  retSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
191                                    surface->w, surface->h,
192                                    32,
193#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
194                               0x000000FF,
195                               0x0000FF00,
196                               0x00FF0000,
197                               0xFF000000
198#else
199                                   0xFF000000,
200                               0x00FF0000,
201                               0x0000FF00,
202                               0x000000FF
203#endif
204                                   );
205  if ( retSurface == NULL )
206  {
207    return NULL;
208  }
209
210  /* Save the alpha blending attributes */
211  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
212  saved_alpha = surface->format->alpha;
213  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
214    SDL_SetAlpha(surface, 0, 0);
215  }
216
217  /* Copy the surface into the GL texture image */
218  area.x = 0;
219  area.y = 0;
220  area.w = surface->w;
221  area.h = surface->h;
222  SDL_BlitSurface(surface, &area, retSurface, &area);
223
224  /* Restore the alpha blending attributes */
225  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
226  {
227    SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
228    hasAlpha = true;
229  }
230
231  return (retSurface);
232}
233
234
235/**
236 *  Loads a Texture to the openGL-environment.
237 * @param surface the Image to load to openGL
238 * @returns The ID of the texture.
239 */
240GLuint Texture::loadTexToGL (const SDL_Surface* surface)
241{
242//   if (this->texture != 0 && glIsTexture(this->texture))
243//     glDeleteTextures(1, &this->texture);
244//   this->texture = 0;
245
246  GLuint texture;
247
248  if (surface == NULL)
249    return 0;
250
251  /* Create an OpenGL texture for the image */
252  glGenTextures(1, &texture);
253  glBindTexture(GL_TEXTURE_2D, texture);
254
255  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, /*GL_LINEAR*/ GL_LINEAR_MIPMAP_LINEAR);
256  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, /*GL_LINEAR*/ GL_LINEAR);
257  /* control the mipmap levels */
258  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, -100);
259  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
260  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);
261
262  // build the Texture
263  glTexImage2D(GL_TEXTURE_2D,
264               0,
265               GL_RGBA,
266               surface->w, surface->h,
267               0,
268               GL_RGBA,
269               GL_UNSIGNED_BYTE,
270               surface->pixels);
271
272  // build the MipMaps automaticaly
273  gluBuild2DMipmaps(GL_TEXTURE_2D,
274                    GL_RGBA,
275                    surface->w,
276                    surface->h,
277                    GL_RGBA,
278                    GL_UNSIGNED_BYTE,
279                    surface->pixels);
280  glBindTexture(GL_TEXTURE_2D, 0);
281  return texture;
282}
283
Note: See TracBrowser for help on using the repository browser.