]> git.tdb.fi Git - libs/gl.git/blob - source/vertexformat.cpp
Add a helper function for making indexed vertex components
[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         data(0)
15 { }
16
17 VertexFormat::VertexFormat(VertexComponent c):
18         data(new unsigned char[8])
19 {
20         data[0] = 1;
21         data[1] = c;
22 }
23
24 VertexFormat::VertexFormat(const VertexFormat &f):
25         data(0)
26 {
27         *this = f;
28 }
29
30 VertexFormat &VertexFormat::operator=(const VertexFormat &f)
31 {
32         delete[] data;
33         if(f.data)
34         {
35                 data = new unsigned char[(f.data[0]&~7)+8];
36                 copy(f.data, f.data+f.data[0]+1, data);
37         }
38         else
39                 data = 0;
40
41         return *this;
42 }
43
44 VertexFormat::~VertexFormat()
45 {
46         delete[] data;
47 }
48
49 VertexFormat VertexFormat::operator,(VertexComponent c) const
50 {
51         VertexFormat r = *this;
52         if(r.data)
53         {
54                 const unsigned char n = ++r.data[0];
55                 if((n&7)==0)
56                 {
57                         unsigned char *newdt = new unsigned char[n+8];
58                         copy(r.data, r.data+n, newdt);
59                         delete r.data;
60                         r.data = newdt;
61                 }
62                 r.data[n] = c;
63         }
64         else
65         {
66                 r.data = new unsigned char[8];
67                 r.data[0] = 1;
68                 r.data[1] = c;
69         }
70
71         return r;
72 }
73
74 VertexFormat VertexFormat::operator,(unsigned i) const
75 {
76         if(!data)
77                 throw invalid_operation("VertexFormat::operator,");
78         VertexFormat r = *this;
79         unsigned char *c = r.data+r.data[0];
80         *c = make_indexed_component(static_cast<VertexComponent>(*c), i);
81
82         return r;
83 }
84
85 unsigned VertexFormat::stride() const
86 {
87         unsigned s = 0;
88         for(const unsigned char *i=begin(); i!=end(); ++i)
89                 s += get_component_size(*i);
90         return s;
91 }
92
93 int VertexFormat::offset(VertexComponent comp) const
94 {
95         unsigned type = get_component_type(comp);
96         unsigned size = get_component_size(comp);
97         unsigned offs = 0;
98         for(const unsigned char *i=begin(); i!=end(); ++i)
99         {
100                 if(get_component_type(*i)==type)
101                 {
102                         if(get_component_size(*i)>=size)
103                                 return offs;
104                         else
105                                 return -1;
106                 }
107                 else
108                         offs += get_component_size(*i);
109         }
110
111         return -1;
112 }
113
114 VertexComponent make_indexed_component(VertexComponent comp, unsigned index)
115 {
116         if(comp>=TEXCOORD1 && comp<=TEXCOORD4)
117         {
118                 if(index>=4)
119                         throw out_of_range("make_indexed_component");
120         }
121         else if(comp>=ATTRIB1 && comp<=ATTRIB4)
122         {
123                 if(index>=53)
124                         throw out_of_range("make_indexed_component");
125         }
126         else
127                 throw invalid_argument("make_indexed_component");
128         return static_cast<VertexComponent>(comp+index*4);
129 }
130
131 void operator>>(const LexicalConverter &conv, VertexComponent &c)
132 {
133         const string &str = conv.get();
134         if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
135                 c = static_cast<VertexComponent>(VERTEX2+(str[6]-'2'));
136         else if(str=="NORMAL3")
137                 c = NORMAL3;
138         else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
139                 c = static_cast<VertexComponent>(COLOR3_FLOAT+(str[5]-'3'));
140         else if(str=="COLOR4_UBYTE")
141                 c = COLOR4_UBYTE;
142         else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
143         {
144                 if(str.size()==9)
145                         c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1'));
146                 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
147                         c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
148                 else
149                         throw lexical_error(format("conversion of '%s' to VertexComponent", str));
150         }
151         else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
152         {
153                 unsigned n;
154                 try
155                 {
156                         n = lexical_cast<unsigned>(str.substr(8));
157                 }
158                 catch(const lexical_error &)
159                 {
160                         throw lexical_error(format("conversion of '%s' to VertexComponent", str));
161                 }
162                 c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
163         }
164         else
165                 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
166 }
167
168 // XXX This will go away eventually
169 void operator>>(const LexicalConverter &conv, VertexFormat &f)
170 {
171         vector<string> parts = split(conv.get(), '_');
172         for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
173         {
174                 if(*i=="COLOR4UB")
175                         *i = "COLOR4_UBYTE";
176                 else if(i->size()==7 && !i->compare(0, 5, "COLOR") && (*i)[6]=='F')
177                         *i = i->substr(0, 6)+"_FLOAT";
178                 else if(i->size()>=10 && !i->compare(0, 8, "TEXCOORD"))
179                         *i = i->substr(0, 9)+"_"+i->substr(9);
180                 else if(i->size()>=8 && !i->compare(0, 6, "ATTRIB"))
181                         *i = i->substr(0, 7)+"_"+i->substr(7);
182         }
183
184         for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
185                 f = (f, lexical_cast<VertexComponent>(*i));
186 }
187
188 } // namespace GL
189 } // namespace Msp