2 #include <msp/strings/format.h>
3 #include <msp/strings/lexicalcast.h>
4 #include <msp/strings/utils.h>
7 #include "vertexformat.h"
8 #include <msp/gl/extensions/arb_vertex_shader.h>
15 VertexFormat::VertexFormat():
19 VertexFormat::VertexFormat(VertexAttribute a):
25 VertexFormat VertexFormat::operator,(VertexAttribute a) const
27 if(count>=MAX_ATTRIBUTES)
28 throw invalid_operation("VertexFormat::operator,");
30 VertexFormat r = *this;
31 r.attributes[r.count++] = a;
36 VertexFormat VertexFormat::operator,(unsigned i) const
39 throw invalid_operation("VertexFormat::operator,");
41 VertexFormat r = *this;
42 unsigned char &a = r.attributes[r.count-1];
43 a = make_indexed_attribute(static_cast<VertexAttribute>(a), i);
48 bool VertexFormat::operator==(const VertexFormat &other) const
50 if(count!=other.count)
52 return equal(attributes, attributes+count, other.attributes);
55 unsigned VertexFormat::stride() const
58 for(const unsigned char *i=begin(); i!=end(); ++i)
59 s += get_attribute_size(*i);
63 int VertexFormat::offset(VertexAttribute attr) const
65 unsigned sem = get_attribute_semantic(attr);
66 unsigned sz = get_attribute_size(attr);
68 for(const unsigned char *i=begin(); i!=end(); ++i)
70 if(get_attribute_semantic(*i)==sem)
72 if(get_attribute_size(*i)>=sz)
78 offs += get_attribute_size(*i);
85 VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
88 if(attr>=TEXCOORD1 && attr<=TEXCOORD4)
91 throw out_of_range("make_indexed_attribute");
93 else if(attr>=RAW_ATTRIB1 && attr<=RAW_ATTRIB4)
95 else if(attr<GENERIC1 || attr>GENERIC4)
96 throw invalid_argument("make_indexed_attribute");
98 static int max_attribs = -1;
100 max_attribs = get_i(GL_MAX_VERTEX_ATTRIBS);
102 if(static_cast<int>((base>>3)+index)>=max_attribs)
103 throw out_of_range("make_indexed_attribute");
105 return static_cast<VertexAttribute>(base+index*8);
109 bool convert_attribute(const string &str, const char *name, int min_size, int max_size, VertexAttribute &attr, VertexAttribute base_attr)
111 string::const_iterator i = str.begin();
112 for(; *name; ++name, ++i)
115 if(i==str.end() || *i<'0'+min_size || *i>'0'+max_size)
117 VertexAttribute result = static_cast<VertexAttribute>(base_attr+(*i-'0'-min_size));
121 if(*i!='_' || ++i==str.end())
125 result = make_indexed_attribute(result, lexical_cast<unsigned>(string(i, str.end())));
129 // The operator>> will throw a more appropriate exception
138 void operator>>(const LexicalConverter &conv, VertexAttribute &a)
140 const string &str = conv.get();
143 else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
144 a = static_cast<VertexAttribute>(COLOR3_FLOAT+(str[5]-'3'));
145 else if(str=="COLOR4_UBYTE")
147 else if(str=="TANGENT3")
149 else if(str=="BINORMAL3")
151 else if(!convert_attribute(str, "VERTEX", 2, 4, a, VERTEX2) &&
152 !convert_attribute(str, "GROUP", 1, 4, a, GROUP1) &&
153 !convert_attribute(str, "WEIGHT", 1, 4, a, WEIGHT1) &&
154 !convert_attribute(str, "TEXCOORD", 1, 4, a, TEXCOORD1) &&
155 !convert_attribute(str, "GENERIC", 1, 4, a, GENERIC1))
156 throw lexical_error(format("conversion of '%s' to VertexAttribute", str));