+#include <msp/core/error.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();
+ 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():
glGenTextures(1, &id);
}
-const Texture *Texture::bound=0;
+void Texture::maybe_bind() const
+{
+ if(TexUnit::current().get_texture()!=this)
+ bind();
+}
} // namespace GL
} // namespace Msp