From: Mikko Rasa Date: Mon, 4 Apr 2011 16:44:40 +0000 (+0000) Subject: Add a method to get the offset of a single component of a VertexFormat X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=c254bc3ed8df9c4113c975bdd119f127d8360f86 Add a method to get the offset of a single component of a VertexFormat --- diff --git a/source/vertexformat.cpp b/source/vertexformat.cpp index 2b18178b..adb33c2a 100644 --- a/source/vertexformat.cpp +++ b/source/vertexformat.cpp @@ -64,6 +64,30 @@ unsigned VertexFormat::stride() const return s; } +int VertexFormat::offset(VertexComponent comp, unsigned index) const +{ + if((comp0) || (comp=8) || index>=53) + throw InvalidParameterValue("Vertex component index out of range"); + + unsigned type = (comp>>2)+index; + unsigned size = comp&3; + unsigned offs = 0; + for(const unsigned char *i=begin(); i!=end(); ++i) + { + if((*i>>2)==type) + { + if((*i&3)>=size) + return offs; + else + return -1; + } + else + offs += (*i&3)+1; + } + + return -1; +} + VertexFormat operator,(const VertexFormat &f, VertexComponent c) { VertexFormat r = f; diff --git a/source/vertexformat.h b/source/vertexformat.h index 53562ef9..27033200 100644 --- a/source/vertexformat.h +++ b/source/vertexformat.h @@ -46,6 +46,7 @@ struct VertexFormat const unsigned char *begin() const { return data ? data+1 : 0; } const unsigned char *end() const { return data ? data+1+data[0] : 0; } unsigned stride() const; + int offset(VertexComponent, unsigned = 0) const; }; VertexFormat operator,(const VertexFormat &f, VertexComponent c);