Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged branches/particleEngine into the trunk, because of the new vector class
merged with command:
svn merge -r 3922:HEAD particleEngine/ ../trunk/

not merged src/story_entities/world.cc. will do this at a later time (do not forget)

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