X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftexture.cpp;h=7ce039acb6df2278ff8c2df8c289e63cfd290820;hb=a80b074c70ec991f27114efd13686038cf42c493;hp=d12e18e0fa4766efd60bf46ff594376f7796b546;hpb=7adcad3b40a03000a82e32db4523761c218309b8;p=libs%2Fgl.git diff --git a/source/texture.cpp b/source/texture.cpp index d12e18e0..7ce039ac 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -1,70 +1,122 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include "except.h" #include "texture.h" +#include "texunit.h" + +using namespace std; namespace Msp { namespace GL { -void Texture::bind() const +istream &operator>>(istream &in, TextureFilter &tf) { - if(!target) return; + string str; + in>>str; + + if(str=="NEAREST") + tf=NEAREST; + else if(str=="LINEAR") + tf=LINEAR; + else if(str=="NEAREST_MIPMAP_NEAREST") + tf=NEAREST_MIPMAP_NEAREST; + else if(str=="NEAREST_MIPMAP_LINEAR") + tf=NEAREST_MIPMAP_LINEAR; + else if(str=="LINEAR_MIPMAP_NEAREST") + tf=LINEAR_MIPMAP_NEAREST; + else if(str=="LINEAR_MIPMAP_LINEAR") + tf=LINEAR_MIPMAP_LINEAR; + else + in.setstate(ios_base::failbit); + + return in; +} + - glEnable(target); +void Texture::bind() const +{ + if(!target) + throw InvalidState("Attempt to bind a texture without target"); + + const Texture *cur=TexUnit::current().get_texture(); + if(cur && cur->target!=target) + glDisable(cur->target); + if(!cur || cur->target!=target) + glEnable(target); glBindTexture(target, id); - bound=this; + TexUnit::current().set_texture(this); } void Texture::parameter(GLenum param, int value) { - if(bound!=this) bind(); + maybe_bind(); glTexParameteri(target, param, value); } void Texture::parameter(GLenum param, float value) { - if(bound!=this) bind(); + maybe_bind(); glTexParameterf(target, param, value); } -sizei Texture::get_width(int level) const +Texture::~Texture() { - if(bound!=this) bind(); - - int width; - glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width); - return width; + glDeleteTextures(1, &id); } -sizei Texture::get_height(int level) const +void Texture::unbind() { - if(bound!=this) bind(); + const Texture *cur=TexUnit::current().get_texture(); + if(!cur) + return; - int height; - glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height); - return height; + glBindTexture(cur->target, 0); + glDisable(cur->target); + TexUnit::current().set_texture(0); } -sizei Texture::get_depth(int level) const +Texture::Texture(): + target(0) { - if(bound!=this) bind(); + glGenTextures(1, &id); +} - int depth; - glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth); - return depth; +void Texture::maybe_bind() const +{ + if(TexUnit::current().get_texture()!=this) + bind(); } -Texture::~Texture() + +Texture::Loader::Loader(Texture &t): + tex(t) { - glDeleteTextures(1, &id); + add("min_filter", &Loader::min_filter); + add("mag_filter", &Loader::mag_filter); + add("generate_mipmap", &Loader::generate_mipmap); } -Texture::Texture(): - target(0) +void Texture::Loader::min_filter(TextureFilter f) { - glGenTextures(1, &id); + tex.set_min_filter(f); +} + +void Texture::Loader::mag_filter(TextureFilter f) +{ + tex.set_mag_filter(f); } -const Texture *Texture::bound=0; +void Texture::Loader::generate_mipmap(bool gm) +{ + tex.parameter(GL_GENERATE_MIPMAP_SGIS, gm); +} } // namespace GL } // namespace Msp