]> git.tdb.fi Git - libs/gl.git/blob - source/core/vertexformat.cpp
Rename VertexComponent to VertexAttribute
[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         if(attr>=TEXCOORD1 && attr<=TEXCOORD4)
86         {
87                 if(index>=4)
88                         throw out_of_range("make_indexed_attribute");
89         }
90         else if(attr>=ATTRIB1 && attr<=ATTRIB4)
91         {
92                 if(index>=24)
93                         throw out_of_range("make_indexed_attribute");
94         }
95         else
96                 throw invalid_argument("make_indexed_attribute");
97         return static_cast<VertexAttribute>(attr+index*4);
98 }
99
100 void operator>>(const LexicalConverter &conv, VertexAttribute &a)
101 {
102         const string &str = conv.get();
103         if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
104                 a = static_cast<VertexAttribute>(VERTEX2+(str[6]-'2'));
105         else if(str=="NORMAL3")
106                 a = NORMAL3;
107         else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
108                 a = static_cast<VertexAttribute>(COLOR3_FLOAT+(str[5]-'3'));
109         else if(str=="COLOR4_UBYTE")
110                 a = COLOR4_UBYTE;
111         else if(str=="TANGENT3")
112                 a = TANGENT3;
113         else if(str=="BINORMAL3")
114                 a = BINORMAL3;
115         else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
116         {
117                 if(str.size()==9)
118                         a = static_cast<VertexAttribute>(TEXCOORD1+(str[8]-'1'));
119                 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
120                         a = static_cast<VertexAttribute>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
121                 else
122                         throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
123         }
124         else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
125         {
126                 unsigned n;
127                 try
128                 {
129                         n = lexical_cast<unsigned>(str.substr(8));
130                 }
131                 catch(const lexical_error &)
132                 {
133                         throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
134                 }
135                 a = static_cast<VertexAttribute>(ATTRIB1+(str[6]-'1')+n*4);
136         }
137         else
138                 throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
139 }
140
141 } // namespace GL
142 } // namespace Msp