X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftexunit.cpp;h=1129a0365617816a58ee1f0ff884cbd2f1e0ef59;hb=126161d1d44ab9503bc747d24a07b7b9d15e527a;hp=1155fa96286be0ab199280e61ea909115545df30;hpb=b617c5d7b5283ad260a77f01e42e6170cabbc03d;p=libs%2Fgl.git diff --git a/source/texunit.cpp b/source/texunit.cpp index 1155fa96..1129a036 100644 --- a/source/texunit.cpp +++ b/source/texunit.cpp @@ -1,23 +1,22 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007-2008 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include "extension.h" +#include +#include +#include #include "gl.h" #include "texunit.h" -#include "version_1_3.h" using namespace std; namespace Msp { namespace GL { +vector TexUnit::units; +TexUnit *TexUnit::cur_unit = 0; + TexUnit::TexUnit(): - texture(0) + texture(0), + texenv(0) { + fill(texgen, texgen+4, static_cast(0)); } bool TexUnit::set_texture(const Texture *tex) @@ -34,17 +33,58 @@ bool TexUnit::set_texenv(const TexEnv *env) return result; } -TexUnit &TexUnit::activate(unsigned n) +bool TexUnit::set_texgen(unsigned i, const TexGen *gen) { - if(units.size()<=n) - units.resize(n+1); + if(i>=4) + throw out_of_range("TexUnit::set_texgen"); + bool result = (texgen[i]!=gen); + texgen[i] = gen; + return result; +} + +const TexGen *TexUnit::get_texgen(unsigned i) +{ + if(i>=4) + throw out_of_range("TexUnit::get_texgen"); + return texgen[i]; +} - if(cur_unit!=&units[n] && (cur_unit || n)) +void TexUnit::bind() +{ + if(cur_unit!=this && (cur_unit || index)) + glActiveTexture(GL_TEXTURE0+index); + cur_unit = this; +} + +unsigned TexUnit::get_n_units() +{ + static int count = -1; + if(count<0) { - static RequireVersion _ver(1, 3); - glActiveTexture(GL_TEXTURE0+n); + if(ARB_vertex_shader) + glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &count); + else if(ARB_multitexture) + glGetIntegerv(GL_MAX_TEXTURE_UNITS, &count); + else + count = 1; + } + return count; +} + +TexUnit &TexUnit::get_unit(unsigned n) +{ + if(n>0) + static Require _req(ARB_multitexture); + if(n>=get_n_units()) + throw out_of_range("TexUnit::get_unit"); + + if(units.size()<=n) + { + unsigned i = units.size(); + units.resize(n+1); + for(; i TexUnit::units; -TexUnit *TexUnit::cur_unit = 0; +TexUnit *TexUnit::find_unit(const Texture *tex) +{ + for(vector::iterator i=units.begin(); i!=units.end(); ++i) + if(i->texture==tex) + return &*i; + return 0; +} + +TexUnit *TexUnit::find_unit(const TexEnv *env) +{ + for(vector::iterator i=units.begin(); i!=units.end(); ++i) + if(i->texenv==env) + return &*i; + return 0; +} } // namespace GL } // namespace Msp