]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texturing.cpp
Add a texunit statement which automatically determines the unit number
[libs/gl.git] / source / texturing.cpp
index d8d178b385c05e54da6d588df0e4866e6da1a460..c30216f139ebbf91e4a7fdcb53021e1fa6cbc39f 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/hash.h>
 #include "texture.h"
 #include "texturing.h"
 #include "texunit.h"
@@ -13,6 +14,23 @@ Texturing::~Texturing()
                unbind();
 }
 
+int Texturing::find_free_unit(const string &name_hint) const
+{
+       unsigned max_unit = TexUnit::get_n_units();
+       // Leave some space for effect textures
+       max_unit -= min(max_unit/4, 8U);
+       unsigned initial_unit = (name_hint.empty() ? 0 : hash32(name_hint)%max_unit);
+       unsigned unit = initial_unit;
+       while(get_attached_texture(unit))
+       {
+               unit = (unit+1)%max_unit;
+               if(unit==initial_unit)
+                       return -1;
+       }
+
+       return unit;
+}
+
 void Texturing::attach(unsigned attch, const Texture &tex)
 {
        set_attachment(attch, &tex);