From: Mikko Rasa Date: Sun, 2 Sep 2012 09:16:12 +0000 (+0300) Subject: Helper functions for extracting vertex component type and size X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=e06ee659c66c92065dbd767475ac9bf9f0f14846;p=libs%2Fgl.git Helper functions for extracting vertex component type and size Magic numbers are bad. This makes the code more robust against future changes. --- diff --git a/source/vertexarray.cpp b/source/vertexarray.cpp index 6e39930e..6c037f9f 100644 --- a/source/vertexarray.cpp +++ b/source/vertexarray.cpp @@ -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; diff --git a/source/vertexarraybuilder.cpp b/source/vertexarraybuilder.cpp index 9b8d3b42..a30c6e21 100644 --- a/source/vertexarraybuilder.cpp +++ b/source/vertexarraybuilder.cpp @@ -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(col.r*255); + u.c[1] = static_cast(col.g*255); + u.c[2] = static_cast(col.b*255); + u.c[3] = static_cast(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(col.r*255); - u.c[1] = static_cast(col.g*255); - u.c[2] = static_cast(col.b*255); - u.c[3] = static_cast(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; } } } diff --git a/source/vertexformat.cpp b/source/vertexformat.cpp index e3f8bf97..02410857 100644 --- a/source/vertexformat.cpp +++ b/source/vertexformat.cpp @@ -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((comp0) || (comp=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(*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; diff --git a/source/vertexformat.h b/source/vertexformat.h index edea62d8..35b5de3d 100644 --- a/source/vertexformat.h +++ b/source/vertexformat.h @@ -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(); }