]> git.tdb.fi Git - libs/gl.git/commitdiff
Helper functions for extracting vertex component type and size
authorMikko Rasa <tdb@tdb.fi>
Sun, 2 Sep 2012 09:16:12 +0000 (12:16 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 2 Sep 2012 09:16:12 +0000 (12:16 +0300)
Magic numbers are bad.  This makes the code more robust against future
changes.

source/vertexarray.cpp
source/vertexarraybuilder.cpp
source/vertexformat.cpp
source/vertexformat.h

index 6e39930ea8db95792b0d9a5ae865d94d91c377a1..6c037f9fd030de3f382335a16dc8c8d2cc69acff 100644 (file)
@@ -116,48 +116,48 @@ void VertexArray::apply() const
        unsigned active_tex = 0;
        for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
        {
-               unsigned sz = (*c&3)+1;
-               unsigned t = *c>>2;
+               unsigned sz = get_component_size(*c);
+               unsigned t = get_component_type(*c);
                bool en = enabled_arrays.is_set(t);
-               switch(t)
+               if(t==get_component_type(VERTEX3))
                {
-               case 0:
                        glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
                        if(!en)
                                glEnableClientState(GL_VERTEX_ARRAY);
-                       break;
-               case 1:
+               }
+               else if(t==get_component_type(NORMAL3))
+               {
                        glNormalPointer(GL_FLOAT, bpv, base+offset);
                        if(!en)
                                glEnableClientState(GL_NORMAL_ARRAY);
-                       break;
-               case 2:
+               }
+               else if(t==get_component_type(COLOR4_FLOAT))
+               {
                        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:
-                       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
+               }
+               else if(*c>=TEXCOORD1 && *c<=TEXCOORD4+28)
+               {
+                       t -= get_component_type(TEXCOORD1);
+                       if(t>0 || active_tex)
                        {
-                               glVertexAttribPointer(t-11, sz, GL_FLOAT, false, bpv, base+offset);
-                               if(!en)
-                                       glEnableVertexAttribArray(t-11);
+                               glClientActiveTexture(GL_TEXTURE0+t);
+                               active_tex = t;
                        }
-                       break;
+                       glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
+                       if(!en)
+                               glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+               }
+               else
+               {
+                       t -= get_component_type(ATTRIB1);
+                       glVertexAttribPointer(t, sz, GL_FLOAT, false, bpv, base+offset);
+                       if(!en)
+                               glEnableVertexAttribArray(t);
                }
                found.set(t);
                offset += sz;
@@ -166,21 +166,22 @@ void VertexArray::apply() const
        for(unsigned i=0; i<64; ++i)
                if(enabled_arrays.is_set(i) && !found.is_set(i))
                {
-                       if(i==0)
+                       if(i==get_component_type(VERTEX3))
                                glDisableClientState(GL_VERTEX_ARRAY);
-                       else if(i==1)
+                       else if(i==get_component_type(NORMAL3))
                                glDisableClientState(GL_NORMAL_ARRAY);
-                       else if(i==2)
+                       else if(i==get_component_type(COLOR4_FLOAT))
                                glDisableClientState(GL_COLOR_ARRAY);
-                       else if(i>=3 && i<11)
+                       else if(i>=get_component_type(TEXCOORD1) && i<=get_component_type(TEXCOORD1)+7)
                        {
-                               if(i>3 || active_tex)
-                                       glClientActiveTexture(GL_TEXTURE0+(i-3));
+                               unsigned j = i-get_component_type(TEXCOORD1);
+                               if(j>0 || active_tex)
+                                       glClientActiveTexture(GL_TEXTURE0+j);
                                glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-                               active_tex = i-3;
+                               active_tex = j;
                        }
                        else
-                               glDisableVertexAttribArray(i-11);
+                               glDisableVertexAttribArray(i-get_component_type(ATTRIB1));
                }
 
        enabled_arrays = found;
index 9b8d3b4284ce383df67c6131bb2f164d7efe53ee..a30c6e21183808a36a5c191b42953e5968180d68 100644 (file)
@@ -8,51 +8,48 @@ VertexArrayBuilder::VertexArrayBuilder(VertexArray &a):
        array(a)
 { }
 
-void VertexArrayBuilder::vertex_(const Vector4 &v)
+void VertexArrayBuilder::vertex_(const Vector4 &ver)
 {
        float *ptr = array.append();
        for(const unsigned char *c=array.get_format().begin(); c!=array.get_format().end(); ++c)
        {
-               unsigned sz = (*c&3)+1;
-               unsigned t = *c>>2;
-               switch(t)
+               unsigned sz = get_component_size(*c);
+               unsigned t = get_component_type(*c);
+               if(*c==COLOR4_UBYTE)
+               {
+                       union { unsigned char c[4]; float f; } u;
+                       u.c[0] = static_cast<unsigned char>(col.r*255);
+                       u.c[1] = static_cast<unsigned char>(col.g*255);
+                       u.c[2] = static_cast<unsigned char>(col.b*255);
+                       u.c[3] = static_cast<unsigned char>(col.a*255);
+                       *ptr++ = u.f;
+               }
+               else if(*c==NORMAL3)
                {
-               case 0:
-                       *ptr++ = v.x;
-                       *ptr++ = v.y;
-                       if(sz>=3) *ptr++ = v.z;
-                       if(sz>=4) *ptr++ = v.w;
-                       break;
-               case 1:
                        *ptr++ = nor.x;
                        *ptr++ = nor.y;
                        *ptr++ = nor.z;
-                       break;
-               case 2:
-                       if(sz==1)
-                       {
-                               union { unsigned char c[4]; float f; } u;
-                               u.c[0] = static_cast<unsigned char>(col.r*255);
-                               u.c[1] = static_cast<unsigned char>(col.g*255);
-                               u.c[2] = static_cast<unsigned char>(col.b*255);
-                               u.c[3] = static_cast<unsigned char>(col.a*255);
-                               *ptr++ = u.f;
-                       }
-                       else
-                       {
-                               *ptr++ = col.r;
-                               *ptr++ = col.g;
-                               *ptr++ = col.b;
-                               if(sz>=4) *ptr++ = col.a;
-                       }
-                       break;
-               default:
-                       const Vector4 &a = (t<11 ? texc[t-3] : attr[t-11]);
-                       *ptr++ = a.x;
-                       if(sz>=2) *ptr++ = a.y;
-                       if(sz>=3) *ptr++ = a.z;
-                       if(sz>=4) *ptr++ = a.w;
-                       break;
+               }
+               else if(t==get_component_type(COLOR4_FLOAT))
+               {
+                       *ptr++ = col.r;
+                       *ptr++ = col.g;
+                       *ptr++ = col.b;
+                       if(sz>=4) *ptr++ = col.a;
+               }
+               else
+               {
+                       const Vector4 *v = 0;
+                       if(t==get_component_type(VERTEX3))
+                               v = &ver;
+                       else if(*c>=TEXCOORD1 && *c<=TEXCOORD4+28)
+                               v = &texc[t-get_component_type(TEXCOORD1)];
+                       else if(*c>=ATTRIB1)
+                               v = &attr[t-get_component_type(ATTRIB1)];
+                       *ptr++ = v->x;
+                       if(sz>=2) *ptr++ = v->y;
+                       if(sz>=3) *ptr++ = v->z;
+                       if(sz>=4) *ptr++ = v->w;
                }
        }
 }
index e3f8bf97a5c49e5fe245975d09308de0e57295c9..02410857bff6e8873570055499b4be71800a6c02 100644 (file)
@@ -54,7 +54,7 @@ unsigned VertexFormat::stride() const
 {
        unsigned s = 0;
        for(const unsigned char *i=begin(); i!=end(); ++i)
-               s += (*i&3)+1;
+               s += get_component_size(*i);
        return s;
 }
 
@@ -63,20 +63,20 @@ int VertexFormat::offset(VertexComponent comp, unsigned index) const
        if((comp<TEXCOORD1 && index>0) || (comp<ATTRIB1 && index>=8) || index>=53)
                throw out_of_range("VertexFormat::offset");
 
-       unsigned type = (comp>>2)+index;
-       unsigned size = comp&3;
+       unsigned type = get_component_type(comp)+index;
+       unsigned size = get_component_size(comp);
        unsigned offs = 0;
        for(const unsigned char *i=begin(); i!=end(); ++i)
        {
-               if(static_cast<unsigned>(*i>>2)==type)
+               if(get_component_type(*i)==type)
                {
-                       if((*i&3)>=size)
+                       if(get_component_size(*i)>=size)
                                return offs;
                        else
                                return -1;
                }
                else
-                       offs += (*i&3)+1;
+                       offs += get_component_size(*i);
        }
 
        return -1;
index edea62d80262910893cd0355bd188853359f0e2d..35b5de3d15d9910da9ffda9bb15d521c112d1f91 100644 (file)
@@ -50,6 +50,12 @@ VertexFormat operator,(const VertexFormat &f, unsigned i);
 inline VertexFormat operator,(VertexComponent c, unsigned i)
 { return (VertexFormat(c), i); }
 
+inline unsigned get_component_type(unsigned char c)
+{ return c>>2; }
+
+inline unsigned get_component_size(unsigned char c)
+{ return (c&3)+1; }
+
 inline unsigned get_stride(const VertexFormat &f)
 { return f.stride(); }