]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texunit.cpp
Allow tagging objects in a scene file for retrieval after loading
[libs/gl.git] / source / texunit.cpp
index 91a33ecc378d966644e11aaf90c11524ffbc1bd0..9277c27704ebd8347c3c6a782fc1b6ff05fc8658 100644 (file)
@@ -1,13 +1,9 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#define GL_GLEXT_PROTOTYPES
-#include <GL/gl.h>
-#include <GL/glext.h>
+#include <stdexcept>
+#include <msp/gl/extensions/arb_multitexture.h>
+#include <msp/gl/extensions/arb_vertex_shader.h>
+#include <msp/gl/extensions/msp_legacy_features.h>
+#include "gl.h"
+#include "misc.h"
 #include "texunit.h"
 
 using namespace std;
@@ -15,24 +11,77 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
+vector<TexUnit> TexUnit::units;
+TexUnit *TexUnit::cur_unit = 0;
+
 TexUnit::TexUnit():
-       texture(0)
+       legacy(false),
+       texture(0),
+       tex_legacy(false)
 { }
 
-bool TexUnit::set_texture(const Texture *tex)
+bool TexUnit::set_texture(const Texture *tex, bool lgc)
 {
-       bool result=(tex!=texture);
-       texture=tex;
+       lgc = (lgc && legacy && tex);
+       bool result = (tex!=texture || lgc!=tex_legacy);
+       texture = tex;
+       tex_legacy = lgc;
        return result;
 }
 
-TexUnit &TexUnit::activate(unsigned n)
+void TexUnit::bind()
 {
-       if(units.size()<=n)
-               units.resize(n+1);
+       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)
+       {
+               if(ARB_vertex_shader)
+                       count = get_i(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS);
+               else if(ARB_multitexture)
+                       count = get_i(GL_MAX_TEXTURE_UNITS);
+               else
+                       count = 1;
+       }
+       return count;
+}
 
-       glActiveTextureARB(GL_TEXTURE0+n);
-       cur_unit=&units[n];
+unsigned TexUnit::get_n_legacy_units()
+{
+       static int count = -1;
+       if(count<0)
+       {
+               if(MSP_legacy_features)
+                       count = get_i(GL_MAX_TEXTURE_UNITS);
+               else
+                       count = 0;
+       }
+       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();
+               unsigned n_legacy = get_n_legacy_units();
+               units.resize(n+1, TexUnit());
+               for(; i<units.size(); ++i)
+               {
+                       units[i].index = i;
+                       units[i].legacy = (i<n_legacy);
+               }
+       }
 
        return units[n];
 }
@@ -40,12 +89,17 @@ TexUnit &TexUnit::activate(unsigned n)
 TexUnit &TexUnit::current()
 {
        if(!cur_unit)
-               return activate(0);
+               get_unit(0).bind();
        return *cur_unit;
 }
 
-vector<TexUnit> TexUnit::units;
-TexUnit *TexUnit::cur_unit=0;
+TexUnit *TexUnit::find_unit(const Texture *tex)
+{
+       for(vector<TexUnit>::iterator i=units.begin(); i!=units.end(); ++i)
+               if(i->texture==tex)
+                       return &*i;
+       return 0;
+}
 
 } // namespace GL
 } // namespace Msp