2008-02-09 13:43:23 +01:00
|
|
|
|
#include "gltexture.h"
|
|
|
|
|
|
2008-08-10 17:14:54 +02:00
|
|
|
|
namespace segl {
|
|
|
|
|
|
2008-02-09 13:43:23 +01:00
|
|
|
|
GLTexture::GLTexture() {
|
|
|
|
|
init();
|
|
|
|
|
alltextures.push_back(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GLTexture::GLTexture(std::string fname, GLint _minfilter, GLint _magfilter, GLint _wraps, GLint _wrapt) {
|
|
|
|
|
init();
|
|
|
|
|
setParameter(_minfilter, _magfilter, _wraps, _wrapt);
|
|
|
|
|
loadImage(fname);
|
2008-03-23 18:00:55 +01:00
|
|
|
|
filename = fname;
|
2008-02-09 13:43:23 +01:00
|
|
|
|
|
|
|
|
|
alltextures.push_back(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GLTexture::GLTexture(SDL_Surface *srfc) {
|
|
|
|
|
init();
|
|
|
|
|
loadSurface(srfc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GLTexture::init() {
|
|
|
|
|
loaded = false;
|
|
|
|
|
texconverted = false;
|
|
|
|
|
keepsurface = false;
|
|
|
|
|
donotreload = false;
|
|
|
|
|
|
|
|
|
|
minfilter = GL_LINEAR;
|
|
|
|
|
magfilter = GL_LINEAR;
|
|
|
|
|
|
|
|
|
|
wraps = GL_REPEAT;
|
|
|
|
|
wrapt = GL_REPEAT;
|
|
|
|
|
|
|
|
|
|
tex = 0;
|
|
|
|
|
width = height = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GLTexture::loadLocalSurface() {
|
|
|
|
|
if(!texconverted)
|
|
|
|
|
convertLocalSurface();
|
|
|
|
|
|
|
|
|
|
width = tex->w;
|
|
|
|
|
height = tex->h;
|
|
|
|
|
|
|
|
|
|
glGenTextures(1, &texint);
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texint);
|
|
|
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wraps);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapt);
|
|
|
|
|
|
|
|
|
|
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, width, height, GL_BGRA, GL_UNSIGNED_BYTE, tex->pixels);
|
|
|
|
|
|
|
|
|
|
loaded = true;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GLTexture::convertLocalSurface() {
|
|
|
|
|
tex = SDL_DisplayFormatAlpha(tex);
|
|
|
|
|
mirrorSurfaceMiddleX(tex);
|
|
|
|
|
|
|
|
|
|
texconverted = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GLTexture::loadImage(std::string fname) {
|
|
|
|
|
if(isLoaded())
|
|
|
|
|
unloadTexture();
|
|
|
|
|
filename = fname;
|
|
|
|
|
tex = IMG_Load(filename.c_str());
|
|
|
|
|
|
|
|
|
|
if(!tex) {
|
|
|
|
|
std::cerr << "Textur konnte nicht geladen werden: " << filename << std::endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ret = loadLocalSurface();
|
|
|
|
|
keepsurface = false;
|
|
|
|
|
SDL_FreeSurface(tex);
|
|
|
|
|
tex = 0;
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GLTexture::loadSurface(SDL_Surface *srfc, bool noreloading) {
|
|
|
|
|
if(isLoaded())
|
|
|
|
|
unloadTexture();
|
|
|
|
|
if(filename!="")
|
|
|
|
|
filename = "";
|
|
|
|
|
|
|
|
|
|
tex = srfc;
|
|
|
|
|
|
|
|
|
|
keepsurface = true;
|
|
|
|
|
donotreload = noreloading;
|
|
|
|
|
|
|
|
|
|
return loadLocalSurface();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GLTexture::selectTexture() {
|
|
|
|
|
if(!loaded) {
|
|
|
|
|
std::cerr << "Textur wurde nicht geladen: " << filename << std::endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texint);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GLTexture::unloadTexture() {
|
|
|
|
|
glDeleteTextures(1, &texint);
|
|
|
|
|
|
|
|
|
|
loaded = false;
|
2008-08-10 17:14:54 +02:00
|
|
|
|
// Entweder tex == image, wurde schon gel<65>scht oder wir m<>ssen es nich l<>schen
|
2008-02-09 13:43:23 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GLTexture::setParameter(GLint _minfilter, GLint _magfilter, GLint _wraps, GLint _wrapt) {
|
|
|
|
|
if(_minfilter!=0)
|
|
|
|
|
minfilter = _minfilter;
|
|
|
|
|
if(_magfilter!=0)
|
|
|
|
|
magfilter = _magfilter;
|
|
|
|
|
if(_wraps!=0)
|
|
|
|
|
wraps = _wraps;
|
|
|
|
|
if(_wrapt!=0)
|
|
|
|
|
wrapt = _wrapt;
|
|
|
|
|
|
|
|
|
|
if(!loaded)
|
|
|
|
|
return true;
|
|
|
|
|
|
2008-03-23 18:00:55 +01:00
|
|
|
|
loaded = false;
|
|
|
|
|
texconverted = false;
|
|
|
|
|
|
2008-02-09 13:43:23 +01:00
|
|
|
|
if(keepsurface&&tex!=0) {
|
|
|
|
|
unloadTexture();
|
|
|
|
|
return loadLocalSurface();
|
|
|
|
|
} else if(filename!="") {
|
2008-04-05 01:27:46 +02:00
|
|
|
|
//std::cout << "Filename: " << filename << std::endl;
|
2008-03-23 18:00:55 +01:00
|
|
|
|
return loadImage(filename);
|
2008-02-09 13:43:23 +01:00
|
|
|
|
} else {
|
|
|
|
|
// keine datei, kein surface
|
|
|
|
|
unloadTexture();
|
|
|
|
|
std::cerr << "Couldn't reload GLTexture " << this << "- No surface, no file!" << std::endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2008-04-05 01:27:46 +02:00
|
|
|
|
//std::cout << "Texture reloaded" << std::endl;
|
2008-02-09 13:43:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GLTexture::getW() {
|
|
|
|
|
return width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GLTexture::getH() {
|
|
|
|
|
return height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GLTexture::isLoaded() {
|
|
|
|
|
return loaded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GLTexture::reloadAll() {
|
2008-03-23 18:00:55 +01:00
|
|
|
|
for(unsigned int i=0; i<alltextures.size(); i++) {
|
2008-02-09 13:43:23 +01:00
|
|
|
|
alltextures[i]->setParameter();
|
2008-03-23 18:00:55 +01:00
|
|
|
|
}
|
2008-02-09 13:43:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GLTexture::~GLTexture() {
|
|
|
|
|
if(isLoaded())
|
|
|
|
|
unloadTexture();
|
|
|
|
|
|
|
|
|
|
for(unsigned int i=0; i<alltextures.size(); i++) {
|
|
|
|
|
if(alltextures[i]==this) {
|
|
|
|
|
alltextures.erase(alltextures.begin()+i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<GLTexture*> GLTexture::alltextures;
|
2008-08-10 17:14:54 +02:00
|
|
|
|
|
|
|
|
|
} // namespace segl
|