X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftexture.cpp;h=1cb169450fbde2bdb090fa2adadcf562a4148f02;hb=5c59a04e253bf7868796fc0dc8e9768ad1988b33;hp=d12e18e0fa4766efd60bf46ff594376f7796b546;hpb=7adcad3b40a03000a82e32db4523761c218309b8;p=libs%2Fgl.git diff --git a/source/texture.cpp b/source/texture.cpp index d12e18e0..1cb16945 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -1,61 +1,59 @@ +/* $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" 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(); - - int depth; - glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth); - return depth; -} + const Texture *cur=TexUnit::current().get_texture(); + if(!cur) + return; -Texture::~Texture() -{ - glDeleteTextures(1, &id); + glBindTexture(cur->target, 0); + glDisable(cur->target); + TexUnit::current().set_texture(0); } Texture::Texture(): @@ -64,7 +62,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