]> git.tdb.fi Git - libs/gl.git/blob - source/vertexformat.cpp
Move filter heuristic for mipmap levels to Texture::Loader
[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 bool VertexFormat::operator==(const VertexFormat &other) const
47 {
48         if(count!=other.count)
49                 return false;
50         return equal(components, components+count, other.components);
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_component_size(*i);
58         return s;
59 }
60
61 int VertexFormat::offset(VertexComponent comp) const
62 {
63         unsigned type = get_component_type(comp);
64         unsigned size = get_component_size(comp);
65         unsigned offs = 0;
66         for(const unsigned char *i=begin(); i!=end(); ++i)
67         {
68                 if(get_component_type(*i)==type)
69                 {
70                         if(get_component_size(*i)>=size)
71                                 return offs;
72                         else
73                                 return -1;
74                 }
75                 else
76                         offs += get_component_size(*i);
77         }
78
79         return -1;
80 }
81
82 VertexComponent make_indexed_component(VertexComponent comp, unsigned index)
83 {
84         if(comp>=TEXCOORD1 && comp<=TEXCOORD4)
85         {
86                 if(index>=4)
87                         throw out_of_range("make_indexed_component");
88         }
89         else if(comp>=ATTRIB1 && comp<=ATTRIB4)
90         {
91                 if(index>=24)
92                         throw out_of_range("make_indexed_component");
93         }
94         else
95                 throw invalid_argument("make_indexed_component");
96         return static_cast<VertexComponent>(comp+index*4);
97 }
98
99 void operator>>(const LexicalConverter &conv, VertexComponent &c)
100 {
101         const string &str = conv.get();
102         if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
103                 c = static_cast<VertexComponent>(VERTEX2+(str[6]-'2'));
104         else if(str=="NORMAL3")
105                 c = NORMAL3;
106         else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
107                 c = static_cast<VertexComponent>(COLOR3_FLOAT+(str[5]-'3'));
108         else if(str=="COLOR4_UBYTE")
109                 c = COLOR4_UBYTE;
110         else if(str=="TANGENT3")
111                 c = TANGENT3;
112         else if(str=="BINORMAL3")
113                 c = BINORMAL3;
114         else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
115         {
116                 if(str.size()==9)
117                         c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1'));
118                 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
119                         c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
120                 else
121                         throw lexical_error(format("conversion of '%s' to VertexComponent", str));
122         }
123         else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
124         {
125                 unsigned n;
126                 try
127                 {
128                         n = lexical_cast<unsigned>(str.substr(8));
129                 }
130                 catch(const lexical_error &)
131                 {
132                         throw lexical_error(format("conversion of '%s' to VertexComponent", str));
133                 }
134                 c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
135         }
136         else
137                 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
138 }
139
140 } // namespace GL
141 } // namespace Msp