X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fcore%2Fvertexformat.cpp;h=e529f4eb2632770e91ae68b3bd221cc913ca0382;hp=05364be81ae8075c55102c29c2d0c892fb9094c3;hb=86721a55699193e63c76e8a0a7b0ced0416c1cce;hpb=ea7832c7c1ffab00cc1168bc8c41375fdd0eae86 diff --git a/source/core/vertexformat.cpp b/source/core/vertexformat.cpp index 05364be8..e529f4eb 100644 --- a/source/core/vertexformat.cpp +++ b/source/core/vertexformat.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -82,27 +83,57 @@ int VertexFormat::offset(VertexAttribute attr) const VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index) { + unsigned base = attr; if(attr>=TEXCOORD1 && attr<=TEXCOORD4) { if(index>=4) throw out_of_range("make_indexed_attribute"); } - else if(attr>=ATTRIB1 && attr<=ATTRIB4) + else if(attr>=RAW_ATTRIB1 && attr<=RAW_ATTRIB4) + base &= 7; + else if(attrGENERIC4) + throw invalid_argument("make_indexed_attribute"); + + if(static_cast((base>>3)+index)>=31) + throw out_of_range("make_indexed_attribute"); + + return static_cast(base+index*8); +} + + +bool convert_attribute(const string &str, const char *name, int min_size, int max_size, VertexAttribute &attr, VertexAttribute base_attr) +{ + string::const_iterator i = str.begin(); + for(; *name; ++name, ++i) + if(*i!=*name) + return false; + if(i==str.end() || *i<'0'+min_size || *i>'0'+max_size) + return false; + VertexAttribute result = static_cast(base_attr+(*i-'0'-min_size)); + + if(++i!=str.end()) { - if(index>=24) - throw out_of_range("make_indexed_attribute"); + if(*i!='_' || ++i==str.end()) + return false; + try + { + result = make_indexed_attribute(result, lexical_cast(string(i, str.end()))); + } + catch(...) + { + // The operator>> will throw a more appropriate exception + return false; + } } - else - throw invalid_argument("make_indexed_attribute"); - return static_cast(attr+index*4); + + attr = result; + return true; } void operator>>(const LexicalConverter &conv, VertexAttribute &a) { const string &str = conv.get(); - if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4') - a = static_cast(VERTEX2+(str[6]-'2')); - else if(str=="NORMAL3") + if(str=="NORMAL3") a = NORMAL3; else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT")) a = static_cast(COLOR3_FLOAT+(str[5]-'3')); @@ -111,30 +142,15 @@ void operator>>(const LexicalConverter &conv, VertexAttribute &a) else if(str=="TANGENT3") a = TANGENT3; else if(str=="BINORMAL3") - a = BINORMAL3; - else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4') - { - if(str.size()==9) - a = static_cast(TEXCOORD1+(str[8]-'1')); - else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7') - a = static_cast(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4); - else - throw lexical_error(format("conversion of '%s' to VertexAttribute", str)); - } - else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_') { - unsigned n; - try - { - n = lexical_cast(str.substr(8)); - } - catch(const lexical_error &) - { - throw lexical_error(format("conversion of '%s' to VertexAttribute", str)); - } - a = static_cast(ATTRIB1+(str[6]-'1')+n*4); + IO::print(IO::cerr, "BINORMAL3 attribute is deprecated\n"); + a = make_indexed_attribute(GENERIC3, 5); } - else + else if(!convert_attribute(str, "VERTEX", 2, 4, a, VERTEX2) && + !convert_attribute(str, "GROUP", 1, 4, a, GROUP1) && + !convert_attribute(str, "WEIGHT", 1, 4, a, WEIGHT1) && + !convert_attribute(str, "TEXCOORD", 1, 4, a, TEXCOORD1) && + !convert_attribute(str, "GENERIC", 1, 4, a, GENERIC1)) throw lexical_error(format("conversion of '%s' to VertexAttribute", str)); }