Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: now Textures are maped as they should
before this textures where switched upside-down, now this is done in the corresponding model's textureCoordinate

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