2 #include <msp/io/print.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/lexicalcast.h>
5 #include <msp/strings/utils.h>
7 #include "vertexformat.h"
14 VertexFormat::VertexFormat():
18 VertexFormat::VertexFormat(VertexAttribute a):
24 VertexFormat VertexFormat::operator,(VertexAttribute a) const
26 if(count>=MAX_ATTRIBUTES)
27 throw invalid_operation("VertexFormat::operator,");
29 VertexFormat r = *this;
30 r.attributes[r.count++] = a;
35 VertexFormat VertexFormat::operator,(unsigned i) const
38 throw invalid_operation("VertexFormat::operator,");
40 VertexFormat r = *this;
41 unsigned char &a = r.attributes[r.count-1];
42 a = make_indexed_attribute(static_cast<VertexAttribute>(a), i);
47 bool VertexFormat::operator==(const VertexFormat &other) const
49 if(count!=other.count)
51 return equal(attributes, attributes+count, other.attributes);
54 unsigned VertexFormat::stride() const
57 for(const unsigned char *i=begin(); i!=end(); ++i)
58 s += get_attribute_size(*i);
62 int VertexFormat::offset(VertexAttribute attr) const
64 unsigned sem = get_attribute_semantic(attr);
65 unsigned sz = get_attribute_size(attr);
67 for(const unsigned char *i=begin(); i!=end(); ++i)
69 if(get_attribute_semantic(*i)==sem)
71 if(get_attribute_size(*i)>=sz)
77 offs += get_attribute_size(*i);
84 VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
87 if(attr>=TEXCOORD1 && attr<=TEXCOORD4)
90 throw out_of_range("make_indexed_attribute");
92 else if(attr>=RAW_ATTRIB1 && attr<=RAW_ATTRIB4)
94 else if(attr<GENERIC1 || attr>GENERIC4)
95 throw invalid_argument("make_indexed_attribute");
97 if(static_cast<int>((base>>3)+index)>=31)
98 throw out_of_range("make_indexed_attribute");
100 return static_cast<VertexAttribute>(base+index*8);
104 bool convert_attribute(const string &str, const char *name, int min_size, int max_size, VertexAttribute &attr, VertexAttribute base_attr)
106 string::const_iterator i = str.begin();
107 for(; *name; ++name, ++i)
110 if(i==str.end() || *i<'0'+min_size || *i>'0'+max_size)
112 VertexAttribute result = static_cast<VertexAttribute>(base_attr+(*i-'0'-min_size));
116 if(*i!='_' || ++i==str.end())
120 result = make_indexed_attribute(result, lexical_cast<unsigned>(string(i, str.end())));
124 // The operator>> will throw a more appropriate exception
133 void operator>>(const LexicalConverter &conv, VertexAttribute &a)
135 const string &str = conv.get();
138 else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
139 a = static_cast<VertexAttribute>(COLOR3_FLOAT+(str[5]-'3'));
140 else if(str=="COLOR4_UBYTE")
142 else if(str=="TANGENT3")
144 else if(str=="BINORMAL3")
146 IO::print(IO::cerr, "BINORMAL3 attribute is deprecated\n");
147 a = make_indexed_attribute(GENERIC3, 5);
149 else if(!convert_attribute(str, "VERTEX", 2, 4, a, VERTEX2) &&
150 !convert_attribute(str, "GROUP", 1, 4, a, GROUP1) &&
151 !convert_attribute(str, "WEIGHT", 1, 4, a, WEIGHT1) &&
152 !convert_attribute(str, "TEXCOORD", 1, 4, a, TEXCOORD1) &&
153 !convert_attribute(str, "GENERIC", 1, 4, a, GENERIC1))
154 throw lexical_error(format("conversion of '%s' to VertexAttribute", str));