X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Ftexture.cpp;h=c367397fb76bb19d2392b17971b915b1a31ea9a8;hp=d12e18e0fa4766efd60bf46ff594376f7796b546;hb=84bc56b96c21c831104a22e0cbd0f3b72ab5d8c3;hpb=7adcad3b40a03000a82e32db4523761c218309b8 diff --git a/source/texture.cpp b/source/texture.cpp index d12e18e0..c367397f 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -1,61 +1,52 @@ +#include #include "texture.h" +#include "texunit.h" namespace Msp { namespace GL { void Texture::bind() const { - if(!target) return; - - glEnable(target); + 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 -{ - if(bound!=this) bind(); - - int width; - glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width); - return width; -} - -sizei Texture::get_height(int level) const +Texture::~Texture() { - if(bound!=this) bind(); - - int height; - glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height); - return height; + glDeleteTextures(1, &id); } -sizei Texture::get_depth(int level) const +void Texture::unbind() { - if(bound!=this) bind(); + const Texture *cur=TexUnit::current().get_texture(); + if(!cur) + return; - int depth; - glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth); - return depth; -} - -Texture::~Texture() -{ - glDeleteTextures(1, &id); + glBindTexture(cur->target, 0); + glDisable(cur->target); + TexUnit::current().set_texture(0); } Texture::Texture(): @@ -64,7 +55,11 @@ Texture::Texture(): glGenTextures(1, &id); } -const Texture *Texture::bound=0; +void Texture::maybe_bind() const +{ + if(TexUnit::current().get_texture()!=this) + bind(); +} } // namespace GL } // namespace Msp