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