Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: renamed all the \param → @param and so on in Doxygen tags.
Thanks a lot to the kDevelop team. this took since the last commit :)

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