X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftexture.cpp;h=40f18d45eff1f0c8d10d2363d8ea2a6123bbc650;hb=b617c5d7b5283ad260a77f01e42e6170cabbc03d;hp=c367397fb76bb19d2392b17971b915b1a31ea9a8;hpb=84bc56b96c21c831104a22e0cbd0f3b72ab5d8c3;p=libs%2Fgl.git diff --git a/source/texture.cpp b/source/texture.cpp index c367397f..40f18d45 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -1,22 +1,64 @@ -#include +/* $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 { +istream &operator>>(istream &in, TextureFilter &tf) +{ + 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; +} + + 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); - TexUnit::current().set_texture(this); + const Texture *cur = TexUnit::current().get_texture(); + if(cur!=this) + { + if(cur && cur->target!=target) + glDisable(cur->target); + if(!cur || cur->target!=target) + glEnable(target); + glBindTexture(target, id); + TexUnit::current().set_texture(this); + } +} + +void Texture::bind_to(unsigned i) const +{ + TexUnit::activate(i); + bind(); } void Texture::parameter(GLenum param, int value) @@ -40,7 +82,7 @@ Texture::~Texture() void Texture::unbind() { - const Texture *cur=TexUnit::current().get_texture(); + const Texture *cur = TexUnit::current().get_texture(); if(!cur) return; @@ -49,6 +91,12 @@ void Texture::unbind() TexUnit::current().set_texture(0); } +void Texture::unbind_from(unsigned i) +{ + TexUnit::activate(i); + unbind(); +} + Texture::Texture(): target(0) { @@ -61,5 +109,29 @@ void Texture::maybe_bind() const bind(); } + +Texture::Loader::Loader(Texture &t): + DataFile::ObjectLoader(t) +{ + add("min_filter", &Loader::min_filter); + add("mag_filter", &Loader::mag_filter); + add("generate_mipmap", &Loader::generate_mipmap); +} + +void Texture::Loader::min_filter(TextureFilter f) +{ + obj.set_min_filter(f); +} + +void Texture::Loader::mag_filter(TextureFilter f) +{ + obj.set_mag_filter(f); +} + +void Texture::Loader::generate_mipmap(bool gm) +{ + obj.parameter(GL_GENERATE_MIPMAP_SGIS, gm); +} + } // namespace GL } // namespace Msp