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