Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

206 rindas
4.4 KiB

/* libsegl - Sebas Extended GL Library
* Collection of Opengl/3D-Math helpers
*
* Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
* http://www.seba-geek.de
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "gltexture.h"
namespace segl {
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);
filename = fname;
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;
// Entweder tex == image, wurde schon gel<65>scht oder wir m<>ssen es nich l<>schen
}
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;
loaded = false;
texconverted = false;
if(keepsurface&&tex!=0) {
unloadTexture();
return loadLocalSurface();
} else if(filename!="") {
//std::cout << "Filename: " << filename << std::endl;
return loadImage(filename);
} else {
// keine datei, kein surface
unloadTexture();
std::cerr << "Couldn't reload GLTexture " << this << "- No surface, no file!" << std::endl;
return false;
}
//std::cout << "Texture reloaded" << std::endl;
}
int GLTexture::getW() {
return width;
}
int GLTexture::getH() {
return height;
}
bool GLTexture::isLoaded() {
return loaded;
}
void GLTexture::reloadAll() {
for(unsigned int i=0; i<alltextures.size(); i++) {
alltextures[i]->setParameter();
}
}
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;
} // namespace segl