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);
108 void operator>>(const LexicalConverter &conv, VertexAttribute &a)
110 const string &str = conv.get();
111 if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
112 a = static_cast<VertexAttribute>(VERTEX2+(str[6]-'2'));
113 else if(str=="NORMAL3")
115 else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
116 a = static_cast<VertexAttribute>(COLOR3_FLOAT+(str[5]-'3'));
117 else if(str=="COLOR4_UBYTE")
119 else if(str=="TANGENT3")
121 else if(str=="BINORMAL3")
123 else if(str.size()==6 && !str.compare(0, 5, "GROUP") && str[5]>='1' && str[5]<='4')
124 a = static_cast<VertexAttribute>(GROUP1+(str[5]-'1'));
125 else if(str.size()==7 && !str.compare(0, 6, "WEIGHT") && str[6]>='1' && str[6]<='4')
126 a = static_cast<VertexAttribute>(WEIGHT1+(str[6]-'1'));
127 else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
130 a = static_cast<VertexAttribute>(TEXCOORD1+(str[8]-'1'));
131 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='3')
132 a = make_indexed_attribute(static_cast<VertexAttribute>(TEXCOORD1+(str[8]-'1')), str[10]-'0');
134 throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
136 else if(str.size()>=10 && !str.compare(0, 6, "GENERIC") && str[7]>='1' && str[7]<='4' && str[8]=='_')
141 n = lexical_cast<unsigned>(str.substr(9));
143 catch(const lexical_error &)
145 throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
147 a = make_indexed_attribute(static_cast<VertexAttribute>(GENERIC1+(str[7]-'1')), n);
150 throw lexical_error(format("conversion of '%s' to VertexAttribute", str));