]> git.tdb.fi Git - libs/gl.git/blob - source/core/vertexformat.cpp
Rearrange 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 void operator>>(const LexicalConverter &conv, VertexAttribute &a)
109 {
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")
114                 a = 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")
118                 a = COLOR4_UBYTE;
119         else if(str=="TANGENT3")
120                 a = TANGENT3;
121         else if(str=="BINORMAL3")
122                 a = BINORMAL3;
123         else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
124         {
125                 if(str.size()==9)
126                         a = static_cast<VertexAttribute>(TEXCOORD1+(str[8]-'1'));
127                 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='3')
128                         a = make_indexed_attribute(static_cast<VertexAttribute>(TEXCOORD1+(str[8]-'1')), str[10]-'0');
129                 else
130                         throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
131         }
132         else if(str.size()>=10 && !str.compare(0, 6, "GENERIC") && str[7]>='1' && str[7]<='4' && str[8]=='_')
133         {
134                 unsigned n;
135                 try
136                 {
137                         n = lexical_cast<unsigned>(str.substr(9));
138                 }
139                 catch(const lexical_error &)
140                 {
141                         throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
142                 }
143                 a = make_indexed_attribute(static_cast<VertexAttribute>(GENERIC1+(str[7]-'1')), n);
144         }
145         else
146                 throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
147 }
148
149 } // namespace GL
150 } // namespace Msp