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