Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5753 was 5753, checked in by bensch, 18 years ago

orxonox/trunk: cleaner Texture Loading

File size: 4.1 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 "graphics_engine.h"
22
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  this->setName(imageName);
36
37  this->bAlpha = false;
38  this->texture = 0;
39  this->image = NULL;
40  if (imageName != NULL)
41    this->loadImage(imageName);
42}
43
44/**
45 *  Destructor of a Texture
46
47   Frees Data, and deletes the textures from GL
48*/
49Texture::~Texture()
50{
51  if (this->texture != 0)
52    glDeleteTextures(1, &this->texture);
53  if (this->image != NULL)
54    SDL_FreeSurface(this->image);
55}
56
57
58/**
59 *  loads an Image from a file to a Texture
60 * @param imageName The image to load
61*/
62bool Texture::loadImage(const char* imageName)
63{
64  if (GraphicsEngine::texturesEnabled)
65    {
66      if (imageName != NULL)
67        {
68          SDL_Surface* tmpSurf;
69          if (this->texture)
70            glDeleteTextures(1, &this->texture);
71          // load the new Image to memory
72          tmpSurf = IMG_Load(imageName);
73          if(tmpSurf != NULL)
74          {
75            PRINTF(3)("loading Image %s\n", imageName);
76            this->image = this->prepareSurface(tmpSurf);
77            this->texture = loadTexToGL(this->image);
78            SDL_FreeSurface(tmpSurf);
79            return true;
80          }
81          else
82            {
83              PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
84              this->texture = 0;
85              return false;
86            }
87        }
88      else
89        {
90          return false;
91        }
92    }
93    return false;
94}
95
96SDL_Surface* Texture::prepareSurface(SDL_Surface* surface)
97{
98  PRINTF(4)("Loading texture to OpenGL-Environment.\n");
99 
100  SDL_Surface *image;
101  SDL_Rect area;
102  Uint32 saved_flags;
103  Uint8  saved_alpha;
104 
105  image = SDL_CreateRGBSurface(SDL_SWSURFACE,
106                               surface->w, surface->h,
107                               32,
108#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
109                               0x000000FF,
110                               0x0000FF00,
111                               0x00FF0000,
112                               0xFF000000
113#else
114                               0xFF000000,
115                               0x00FF0000,
116                               0x0000FF00,
117                               0x000000FF
118#endif
119                               );
120  if ( image == NULL ) {
121    return 0;
122  }
123 
124  /* Save the alpha blending attributes */
125  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
126  saved_alpha = surface->format->alpha;
127  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
128    SDL_SetAlpha(surface, 0, 0);
129  }
130 
131  /* Copy the surface into the GL texture image */
132  area.x = 0;
133  area.y = 0;
134  area.w = surface->w;
135  area.h = surface->h;
136  SDL_BlitSurface(surface, &area, image, &area);
137 
138  /* Restore the alpha blending attributes */
139  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) 
140    {
141      SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
142      this->bAlpha = true;
143    }
144  return image;
145}
146
147
148/**
149 *  Loads a Texture to the openGL-environment.
150 * @param surface the Image to load to openGL
151 * @returns The ID of the texture.
152 */
153GLuint Texture::loadTexToGL (SDL_Surface* surface) const
154{
155  GLuint texture = 0;
156  /* Create an OpenGL texture for the image */
157  glGenTextures(1, &texture);
158  glBindTexture(GL_TEXTURE_2D, texture);
159  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
160  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
161  // build the Texture
162  glTexImage2D(GL_TEXTURE_2D,
163               0,
164               GL_RGBA,
165               surface->w, surface->h,
166               0,
167               GL_RGBA,
168               GL_UNSIGNED_BYTE,
169               surface->pixels);
170  // build the MipMaps
171  gluBuild2DMipmaps(GL_TEXTURE_2D,
172                    GL_RGBA,
173                    surface->w,
174                    surface->h,
175                    GL_RGBA,
176                    GL_UNSIGNED_BYTE,
177                    surface->pixels);
178  glBindTexture(GL_TEXTURE_2D, 0);
179  return texture;
180}
Note: See TracBrowser for help on using the repository browser.