Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ResourceManage-fixes

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