Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5768 was 5768, checked in by bensch, 20 years ago

orxonox/trunk: font is a Texture now (this is a procedural texture)

File size: 5.5 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"
[3622]21#include "graphics_engine.h"
22
[4662]23#ifdef HAVE_SDL_IMAGE_H
[4357]24#include <SDL_image.h>
[4662]25#else
26#include <SDL/SDL_image.h>
27#endif
[4357]28
[3341]29/**
[4836]30 *  Constructor for a Texture
[3344]31*/
[3655]32Texture::Texture(const char* imageName)
33{
[5304]34  this->setClassID(CL_TEXTURE, "Texture");
[5306]35  this->setName(imageName);
[5304]36
[5212]37  this->bAlpha = false;
[3655]38  this->texture = 0;
[5753]39  this->image = NULL;
[5754]40
[5305]41  if (imageName != NULL)
[3905]42    this->loadImage(imageName);
[4662]43}
[3655]44
45/**
[4836]46 *  Destructor of a Texture
[4662]47
[3344]48   Frees Data, and deletes the textures from GL
49*/
[4746]50Texture::~Texture()
[3344]51{
[5211]52  if (this->texture != 0)
[3344]53    glDeleteTextures(1, &this->texture);
[5753]54  if (this->image != NULL)
55    SDL_FreeSurface(this->image);
[3344]56}
57
[3905]58
[3863]59/**
[4836]60 *  loads an Image from a file to a Texture
61 * @param imageName The image to load
[3863]62*/
[3655]63bool Texture::loadImage(const char* imageName)
[3341]64{
[3622]65  if (GraphicsEngine::texturesEnabled)
[3341]66    {
[5754]67      if (this->image != NULL)
[5768]68        {
69          SDL_FreeSurface(this->image);
70          this->image = NULL;
71        }
[5754]72      if (this->texture != 0)
[5768]73        {
74          glDeleteTextures(1, &this->texture);
75          this->texture = 0;
76        }
[5305]77      if (imageName != NULL)
[4662]78        {
79          SDL_Surface* tmpSurf;
80          if (this->texture)
81            glDeleteTextures(1, &this->texture);
82          // load the new Image to memory
83          tmpSurf = IMG_Load(imageName);
[5753]84          if(tmpSurf != NULL)
85          {
[5754]86            PRINTF(4)("loading Image %s\n", imageName);
[5768]87            if (this->prepareSurface(tmpSurf))
88              loadTexToGL();
[5753]89            SDL_FreeSurface(tmpSurf);
90            return true;
91          }
92          else
[4662]93            {
94              PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
[5305]95              this->texture = 0;
[4662]96              return false;
97            }
98        }
[3622]99      else
[4662]100        {
[5768]101          PRINTF(2)("Image-Name not specified\n");
[4662]102          return false;
103        }
[3341]104    }
[5754]105  return false;
[3341]106}
[5753]107
[5755]108bool Texture::rebuild()
[5754]109{
110  if (this->texture != 0)
111    {
112      glDeleteTextures(1,&this->texture);
113      this->texture = 0;
114    }
115
116  if (this->image != NULL)
[5755]117    {
118      PRINTF(4)("Reloading Texture %s\n", this->getName());
[5768]119      this->loadTexToGL();
[5755]120    }
[5768]121
[5754]122}
123
124/**
125 * converts surface to a new SDL_Surface, that is loadable by openGL
126 * @param surface the Surface to convert
127 * @returns a !!new!! Surface, that is loadable by openGL.
128 */
[5768]129bool Texture::prepareSurface(SDL_Surface* surface)
[5753]130{
131  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
[5768]132
133  SDL_Surface* putSurface;
[5753]134  SDL_Rect area;
135  Uint32 saved_flags;
136  Uint8  saved_alpha;
[5768]137
138  putSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
139                               surface->w, surface->h,
140                               32,
[5753]141#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
[5768]142                               0x000000FF,
143                               0x0000FF00,
144                               0x00FF0000,
145                               0xFF000000
[5753]146#else
[5768]147                               0xFF000000,
148                               0x00FF0000,
149                               0x0000FF00,
150                               0x000000FF
[5753]151#endif
[5768]152                               );
153  if ( putSurface == NULL )
154  {
155    this->setSurface(NULL);
156    return false;
157  }
158
[5753]159  /* Save the alpha blending attributes */
160  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
161  saved_alpha = surface->format->alpha;
162  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
163    SDL_SetAlpha(surface, 0, 0);
164  }
[5768]165
[5753]166  /* Copy the surface into the GL texture image */
167  area.x = 0;
168  area.y = 0;
169  area.w = surface->w;
170  area.h = surface->h;
[5768]171  SDL_BlitSurface(surface, &area, putSurface, &area);
172
[5753]173  /* Restore the alpha blending attributes */
[5768]174  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
175  {
176    SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
177    this->bAlpha = true;
178  }
179
180  return (this->setSurface(putSurface));
[5753]181}
182
[5768]183bool Texture::setSurface(SDL_Surface* newSurface)
184{
185  if (this->image != NULL)
186    SDL_FreeSurface(this->image);
[5753]187
[5768]188  this->image = newSurface;
189
190  return (this->image != NULL);
191}
192
193
[5753]194/**
195 *  Loads a Texture to the openGL-environment.
196 * @param surface the Image to load to openGL
197 * @returns The ID of the texture.
198 */
[5768]199GLuint Texture::loadTexToGL ()
[5753]200{
[5768]201  if (this->texture != 0 && glIsTexture(this->texture))
202    glDeleteTextures(1, &this->texture);
203  this->texture = 0;
204
205  if (this->image == NULL)
206    return 0;
207
[5753]208  /* Create an OpenGL texture for the image */
[5768]209  glGenTextures(1, &this->texture);
210  glBindTexture(GL_TEXTURE_2D, this->texture);
[5753]211  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
212  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
213  // build the Texture
214  glTexImage2D(GL_TEXTURE_2D,
[5768]215               0,
216               GL_RGBA,
217               this->image->w, this->image->h,
218               0,
219               GL_RGBA,
220               GL_UNSIGNED_BYTE,
221               this->image->pixels);
[5753]222  // build the MipMaps
223  gluBuild2DMipmaps(GL_TEXTURE_2D,
[5768]224                    GL_RGBA,
225                    this->image->w,
226                    this->image->h,
227                    GL_RGBA,
228                    GL_UNSIGNED_BYTE,
229                    this->image->pixels);
[5753]230  glBindTexture(GL_TEXTURE_2D, 0);
[5768]231  return this->texture;
[5753]232}
Note: See TracBrowser for help on using the repository browser.