]> git.tdb.fi Git - libs/gl.git/blobdiff - source/vertexarray.cpp
Support multiple sets of texture coordinates in VertexArrays
[libs/gl.git] / source / vertexarray.cpp
index 8381d8adb1d9663b742db5a88af857f988b6bfd0..6d6699020334655edf2f7a899d3636572ee6071e 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspgl
-Copyright © 2007-2009  Mikko Rasa, Mikkosoft Productions
+Copyright © 2007-2010  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
@@ -67,9 +67,17 @@ void VertexArray::reset(const VertexFormat &f)
        format = f;
        stride = get_stride(format);
 
+       bool has_multitex = false;
        bool has_gen_attrs = false;
-       for(const unsigned char *c=format.begin(); (!has_gen_attrs && c!=format.end()); ++c)
-               has_gen_attrs = (*c>=ATTRIB1);
+       for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
+       {
+               if(*c>=TEXCOORD1+4 && *c<ATTRIB1)
+                       has_multitex = true;
+               if(*c>=ATTRIB1)
+                       has_gen_attrs = true;
+       }
+       if(has_multitex)
+               static RequireVersion _ver(1, 3);
        if(has_gen_attrs)
                static RequireExtension _ext("GL_ARB_vertex_program");
 }
@@ -84,43 +92,81 @@ void VertexArray::apply() const
 
        const float *base = vbuf?0:&data[0];
        unsigned offset = 0;
-       unsigned found = 0;
+       ArrayMask found;
        unsigned bpv = stride*sizeof(float);
+       unsigned active_tex = 0;
        for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
        {
                unsigned sz = (*c&3)+1;
                unsigned t = *c>>2;
+               bool en = enabled_arrays.is_set(t);
                switch(t)
                {
                case 0:
                        glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
+                       if(!en)
+                               glEnableClientState(GL_VERTEX_ARRAY);
                        break;
                case 1:
                        glNormalPointer(GL_FLOAT, bpv, base+offset);
+                       if(!en)
+                               glEnableClientState(GL_NORMAL_ARRAY);
                        break;
                case 2:
-                       glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
-                       break;
-               case 3:
                        if(sz==1)
                                glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
                        else
                                glColorPointer(sz, GL_FLOAT, bpv, base+offset);
+                       if(!en)
+                               glEnableClientState(GL_COLOR_ARRAY);
                        break;
                default:
-                       glVertexAttribPointerARB(t-4, sz, GL_FLOAT, false, bpv, base+offset);
+                       if(t<11)
+                       {
+                               if(t>3 || active_tex)
+                               {
+                                       glClientActiveTexture(GL_TEXTURE0+(t-3));
+                                       active_tex = t-3;
+                               }
+                               glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
+                               if(!en)
+                                       glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+                       }
+                       else
+                       {
+                               glVertexAttribPointerARB(t-11, sz, GL_FLOAT, false, bpv, base+offset);
+                               if(!en)
+                                       glEnableVertexAttribArrayARB(t-11);
+                       }
                        break;
                }
-               found |= 1<<t;
+               found.set(t);
                offset += sz;
        }
 
-       set_array(GL_VERTEX_ARRAY, found&1, 1);
-       set_array(GL_NORMAL_ARRAY, found&2, 2);
-       set_array(GL_TEXTURE_COORD_ARRAY, found&4, 4);
-       set_array(GL_COLOR_ARRAY, found&8, 8);
-       for(unsigned i=4; i<32; ++i)
-               set_array(i-4, (found>>i)&1, 1<<i);
+       for(unsigned i=0; i<64; ++i)
+               if(enabled_arrays.is_set(i) && !found.is_set(i))
+               {
+                       if(i==0)
+                               glDisableClientState(GL_VERTEX_ARRAY);
+                       else if(i==1)
+                               glDisableClientState(GL_NORMAL_ARRAY);
+                       else if(i==2)
+                               glDisableClientState(GL_COLOR_ARRAY);
+                       else if(i>=3 && i<11)
+                       {
+                               if(i>3 || active_tex)
+                                       glClientActiveTexture(GL_TEXTURE0+(i-3));
+                               glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+                       }
+                       else
+                               glDisableVertexAttribArrayARB(i-11);
+               }
+
+       enabled_arrays = found;
+
+       if(active_tex)
+               glClientActiveTexture(GL_TEXTURE0);
 
        if(vbuf)
                vbuf->unbind();
@@ -144,27 +190,24 @@ float *VertexArray::append()
        return &*data.end()-stride;
 }
 
-void VertexArray::set_array(unsigned array, bool en, unsigned mask) const
+VertexArray::ArrayMask VertexArray::enabled_arrays;
+
+
+VertexArray::ArrayMask::ArrayMask()
 {
-       if((enabled_arrays&mask) && !en)
-       {
-               if(mask<16)
-                       glDisableClientState(array);
-               else
-                       glDisableVertexAttribArrayARB(array);
-               enabled_arrays &= ~mask;
-       }
-       else if(!(enabled_arrays&mask) && en)
-       {
-               if(mask<16)
-                       glEnableClientState(array);
-               else
-                       glEnableVertexAttribArrayARB(array);
-               enabled_arrays |= mask;
-       }
+       for(unsigned i=0; i<N; ++i)
+               mask[i] = 0;
 }
 
-unsigned VertexArray::enabled_arrays = 0;
+void VertexArray::ArrayMask::set(unsigned bit)
+{
+       mask[bit/B] |= 1<<(bit%B);
+}
+
+bool VertexArray::ArrayMask::is_set(unsigned bit) const
+{
+       return mask[bit/B]&(1<<(bit%B));
+}
 
 
 VertexArray::Loader::Loader(VertexArray &a):