Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/particleEngine/src/lib/graphics/importer/texture.cc @ 3947

Last change on this file since 3947 was 3947, checked in by bensch, 19 years ago

orxonox/branches/particleEngine: ok, all the materials get rendered again
it was a little error in the MipMaps-creation

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