]> git.tdb.fi Git - libs/gl.git/blob - source/core/vertexformat.cpp
Refactor lexical conversion of vertex attributes
[libs/gl.git] / source / core / vertexformat.cpp
1 #include <algorithm>
2 #include <msp/strings/format.h>
3 #include <msp/strings/lexicalcast.h>
4 #include <msp/strings/utils.h>
5 #include "error.h"
6 #include "misc.h"
7 #include "vertexformat.h"
8 #include <msp/gl/extensions/arb_vertex_shader.h>
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 VertexFormat::VertexFormat():
16         count(0)
17 { }
18
19 VertexFormat::VertexFormat(VertexAttribute a):
20         count(1)
21 {
22         attributes[0] = a;
23 }
24
25 VertexFormat VertexFormat::operator,(VertexAttribute a) const
26 {
27         if(count>=MAX_ATTRIBUTES)
28                 throw invalid_operation("VertexFormat::operator,");
29
30         VertexFormat r = *this;
31         r.attributes[r.count++] = a;
32
33         return r;
34 }
35
36 VertexFormat VertexFormat::operator,(unsigned i) const
37 {
38         if(!count)
39                 throw invalid_operation("VertexFormat::operator,");
40
41         VertexFormat r = *this;
42         unsigned char &a = r.attributes[r.count-1];
43         a = make_indexed_attribute(static_cast<VertexAttribute>(a), i);
44
45         return r;
46 }
47
48 bool VertexFormat::operator==(const VertexFormat &other) const
49 {
50         if(count!=other.count)
51                 return false;
52         return equal(attributes, attributes+count, other.attributes);
53 }
54
55 unsigned VertexFormat::stride() const
56 {
57         unsigned s = 0;
58         for(const unsigned char *i=begin(); i!=end(); ++i)
59                 s += get_attribute_size(*i);
60         return s;
61 }
62
63 int VertexFormat::offset(VertexAttribute attr) const
64 {
65         unsigned sem = get_attribute_semantic(attr);
66         unsigned sz = get_attribute_size(attr);
67         unsigned offs = 0;
68         for(const unsigned char *i=begin(); i!=end(); ++i)
69         {
70                 if(get_attribute_semantic(*i)==sem)
71                 {
72                         if(get_attribute_size(*i)>=sz)
73                                 return offs;
74                         else
75                                 return -1;
76                 }
77                 else
78                         offs += get_attribute_size(*i);
79         }
80
81         return -1;
82 }
83
84
85 VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
86 {
87         unsigned base = attr;
88         if(attr>=TEXCOORD1 && attr<=TEXCOORD4)
89         {
90                 if(index>=4)
91                         throw out_of_range("make_indexed_attribute");
92         }
93         else if(attr>=RAW_ATTRIB1 && attr<=RAW_ATTRIB4)
94                 base &= 7;
95         else if(attr<GENERIC1 || attr>GENERIC4)
96                 throw invalid_argument("make_indexed_attribute");
97
98         static int max_attribs = -1;
99         if(max_attribs<0)
100                 max_attribs = get_i(GL_MAX_VERTEX_ATTRIBS);
101
102         if(static_cast<int>((base>>3)+index)>=max_attribs)
103                 throw out_of_range("make_indexed_attribute");
104
105         return static_cast<VertexAttribute>(base+index*8);
106 }
107
108
109 bool convert_attribute(const string &str, const char *name, int min_size, int max_size, VertexAttribute &attr, VertexAttribute base_attr)
110 {
111         string::const_iterator i = str.begin();
112         for(; *name; ++name, ++i)
113                 if(*i!=*name)
114                         return false;
115         if(i==str.end() || *i<'0'+min_size || *i>'0'+max_size)
116                 return false;
117         VertexAttribute result = static_cast<VertexAttribute>(base_attr+(*i-'0'-min_size));
118
119         if(++i!=str.end())
120         {
121                 if(*i!='_' || ++i==str.end())
122                         return false;
123                 try
124                 {
125                         result = make_indexed_attribute(result, lexical_cast<unsigned>(string(i, str.end())));
126                 }
127                 catch(...)
128                 {
129                         // The operator>> will throw a more appropriate exception
130                         return false;
131                 }
132         }
133
134         attr = result;
135         return true;
136 }
137
138 void operator>>(const LexicalConverter &conv, VertexAttribute &a)
139 {
140         const string &str = conv.get();
141         if(str=="NORMAL3")
142                 a = NORMAL3;
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")
146                 a = COLOR4_UBYTE;
147         else if(str=="TANGENT3")
148                 a = TANGENT3;
149         else if(str=="BINORMAL3")
150                 a = 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));
157 }
158
159 } // namespace GL
160 } // namespace Msp