X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftexturing.cpp;fp=source%2Ftexturing.cpp;h=69c2b3ca55cdae1ff6df19a9227b7d4ddbfa24bf;hb=abc16c5ab0fff0945d724febe9d5d3889fb8a6ce;hp=0000000000000000000000000000000000000000;hpb=6a4907898281b738111e0c6527cc46f8810dc123;p=libs%2Fgl.git diff --git a/source/texturing.cpp b/source/texturing.cpp new file mode 100644 index 00000000..69c2b3ca --- /dev/null +++ b/source/texturing.cpp @@ -0,0 +1,105 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2010 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include "texenv.h" +#include "texture.h" +#include "texturing.h" +#include "texunit.h" + +namespace Msp { +namespace GL { + +Texturing::~Texturing() +{ + if(current()==this) + unbind(); +} + +void Texturing::attach(unsigned attch, const Texture &tex) +{ + set_attachment(attch, &tex, 0); +} + +void Texturing::attach(unsigned attch, const Texture &tex, const TexEnv &env) +{ + set_attachment(attch, &tex, &env); +} + +void Texturing::detach(unsigned attch) +{ + set_attachment(attch, 0, 0); +} + +void Texturing::set_attachment(unsigned attch, const Texture *tex, const TexEnv *env) +{ + if(attch>=TexUnit::get_n_units()) + throw InvalidParameterValue("Invalid texture attachment"); + + if(attachments.size()<=attch) + attachments.resize(attch+1); + + attachments[attch].tex = tex; + attachments[attch].env = env; + + if(current()==this) + bind_attachment(attch); +} + +void Texturing::bind_attachment(unsigned i) const +{ + const Attachment &attch = attachments[i]; + TexUnit::activate(i); + if(attch.tex) + attch.tex->bind(); + else + Texture::unbind(); + if(attch.env) + attch.env->bind(); + else + TexEnv::unbind(); +} + +void Texturing::unbind_attachment(unsigned i) +{ + TexUnit::activate(i); + Texture::unbind(); + TexEnv::unbind(); +} + +void Texturing::bind() const +{ + const Texturing *old = current(); + if(set_current(this)) + { + for(unsigned i=0; iattachments.size(); ++i) + unbind_attachment(i); + } + } +} + +void Texturing::unbind() +{ + const Texturing *old = current(); + if(set_current(0)) + { + for(unsigned i=old->attachments.size(); i--;) + unbind_attachment(i); + } +} + + +Texturing::Attachment::Attachment(): + tex(0), + env(0) +{ } + +} // namespace GL +} // namespace Msp;