]> git.tdb.fi Git - libs/gl.git/blob - source/vertexformat.cpp
Add the specializations for non-square matrix uniforms
[libs/gl.git] / source / 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(VertexComponent c):
18         count(1)
19 {
20         components[0] = c;
21 }
22
23 VertexFormat VertexFormat::operator,(VertexComponent c) const
24 {
25         if(count>=MAX_COMPONENTS)
26                 throw invalid_operation("VertexFormat::operator,");
27
28         VertexFormat r = *this;
29         r.components[r.count++] = c;
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 *c = &r.components[r.count-1];
41         *c = make_indexed_component(static_cast<VertexComponent>(*c), i);
42
43         return r;
44 }
45
46 unsigned VertexFormat::stride() const
47 {
48         unsigned s = 0;
49         for(const unsigned char *i=begin(); i!=end(); ++i)
50                 s += get_component_size(*i);
51         return s;
52 }
53
54 int VertexFormat::offset(VertexComponent comp) const
55 {
56         unsigned type = get_component_type(comp);
57         unsigned size = get_component_size(comp);
58         unsigned offs = 0;
59         for(const unsigned char *i=begin(); i!=end(); ++i)
60         {
61                 if(get_component_type(*i)==type)
62                 {
63                         if(get_component_size(*i)>=size)
64                                 return offs;
65                         else
66                                 return -1;
67                 }
68                 else
69                         offs += get_component_size(*i);
70         }
71
72         return -1;
73 }
74
75 VertexComponent make_indexed_component(VertexComponent comp, unsigned index)
76 {
77         if(comp>=TEXCOORD1 && comp<=TEXCOORD4)
78         {
79                 if(index>=4)
80                         throw out_of_range("make_indexed_component");
81         }
82         else if(comp>=ATTRIB1 && comp<=ATTRIB4)
83         {
84                 if(index>=24)
85                         throw out_of_range("make_indexed_component");
86         }
87         else
88                 throw invalid_argument("make_indexed_component");
89         return static_cast<VertexComponent>(comp+index*4);
90 }
91
92 void operator>>(const LexicalConverter &conv, VertexComponent &c)
93 {
94         const string &str = conv.get();
95         if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
96                 c = static_cast<VertexComponent>(VERTEX2+(str[6]-'2'));
97         else if(str=="NORMAL3")
98                 c = NORMAL3;
99         else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
100                 c = static_cast<VertexComponent>(COLOR3_FLOAT+(str[5]-'3'));
101         else if(str=="COLOR4_UBYTE")
102                 c = COLOR4_UBYTE;
103         else if(str=="TANGENT3")
104                 c = TANGENT3;
105         else if(str=="BINORMAL3")
106                 c = BINORMAL3;
107         else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
108         {
109                 if(str.size()==9)
110                         c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1'));
111                 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
112                         c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
113                 else
114                         throw lexical_error(format("conversion of '%s' to VertexComponent", str));
115         }
116         else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
117         {
118                 unsigned n;
119                 try
120                 {
121                         n = lexical_cast<unsigned>(str.substr(8));
122                 }
123                 catch(const lexical_error &)
124                 {
125                         throw lexical_error(format("conversion of '%s' to VertexComponent", str));
126                 }
127                 c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
128         }
129         else
130                 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
131 }
132
133 } // namespace GL
134 } // namespace Msp