]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarray.cpp
Complete rewrite of extension handling
[libs/gl.git] / source / vertexarray.cpp
1 #include "arb_multitexture.h"
2 #include "arb_vertex_shader.h"
3 #include "buffer.h"
4 #include "error.h"
5 #include "gl.h"
6 #include "vertexarray.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 VertexArray::ArrayMask VertexArray::enabled_arrays;
14
15 VertexArray::VertexArray(const VertexFormat &f):
16         defer_vbuf(true),
17         dirty(false)
18 {
19         reset(f);
20 }
21
22 VertexArray::~VertexArray()
23 { }
24
25 void VertexArray::use_vertex_buffer()
26 {
27         if(vbuf)
28                 return;
29
30         vbuf = new Buffer(ARRAY_BUFFER);
31         defer_vbuf = false;
32         dirty = true;
33 }
34
35 void VertexArray::use_vertex_buffer(Buffer *b)
36 {
37         vbuf = b;
38         vbuf.keep();
39         defer_vbuf = false;
40         dirty = true;
41 }
42
43 void VertexArray::reserve(unsigned n)
44 {
45         data.reserve(n*stride);
46 }
47
48 void VertexArray::clear()
49 {
50         data.clear();
51 }
52
53 void VertexArray::reset(const VertexFormat &f)
54 {
55         clear();
56         format = f;
57         stride = get_stride(format);
58
59         bool has_multitex = false;
60         bool has_gen_attrs = false;
61         for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
62         {
63                 if(*c>=TEXCOORD1+4 && *c<ATTRIB1)
64                         has_multitex = true;
65                 if(*c>=ATTRIB1)
66                         has_gen_attrs = true;
67         }
68         if(has_multitex)
69                 static Require _req(ARB_multitexture);
70         if(has_gen_attrs)
71                 static Require _req(ARB_vertex_shader);
72 }
73
74 void VertexArray::apply() const
75 {
76         if(format.empty())
77                 throw invalid_operation("VertexArray::apply");
78
79         if(vbuf)
80         {
81                 vbuf->bind_to(ARRAY_BUFFER);
82                 if(dirty)
83                 {
84                         vbuf->data(data.size()*sizeof(float), &data[0]);
85                         dirty = false;
86                 }
87         }
88
89         const float *base = (vbuf ? 0 : &data[0]);
90         unsigned offset = 0;
91         ArrayMask found;
92         unsigned bpv = stride*sizeof(float);
93         unsigned active_tex = 0;
94         for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
95         {
96                 unsigned sz = (*c&3)+1;
97                 unsigned t = *c>>2;
98                 bool en = enabled_arrays.is_set(t);
99                 switch(t)
100                 {
101                 case 0:
102                         glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
103                         if(!en)
104                                 glEnableClientState(GL_VERTEX_ARRAY);
105                         break;
106                 case 1:
107                         glNormalPointer(GL_FLOAT, bpv, base+offset);
108                         if(!en)
109                                 glEnableClientState(GL_NORMAL_ARRAY);
110                         break;
111                 case 2:
112                         if(sz==1)
113                                 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
114                         else
115                                 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
116                         if(!en)
117                                 glEnableClientState(GL_COLOR_ARRAY);
118                         break;
119                 default:
120                         if(t<11)
121                         {
122                                 if(t>3 || active_tex)
123                                 {
124                                         glClientActiveTexture(GL_TEXTURE0+(t-3));
125                                         active_tex = t-3;
126                                 }
127                                 glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
128                                 if(!en)
129                                         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
130                         }
131                         else
132                         {
133                                 glVertexAttribPointer(t-11, sz, GL_FLOAT, false, bpv, base+offset);
134                                 if(!en)
135                                         glEnableVertexAttribArray(t-11);
136                         }
137                         break;
138                 }
139                 found.set(t);
140                 offset += sz;
141         }
142
143         for(unsigned i=0; i<64; ++i)
144                 if(enabled_arrays.is_set(i) && !found.is_set(i))
145                 {
146                         if(i==0)
147                                 glDisableClientState(GL_VERTEX_ARRAY);
148                         else if(i==1)
149                                 glDisableClientState(GL_NORMAL_ARRAY);
150                         else if(i==2)
151                                 glDisableClientState(GL_COLOR_ARRAY);
152                         else if(i>=3 && i<11)
153                         {
154                                 if(i>3 || active_tex)
155                                         glClientActiveTexture(GL_TEXTURE0+(i-3));
156                                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
157                                 active_tex = i-3;
158                         }
159                         else
160                                 glDisableVertexAttribArray(i-11);
161                 }
162
163         enabled_arrays = found;
164
165         if(active_tex)
166                 glClientActiveTexture(GL_TEXTURE0);
167
168         if(vbuf)
169                 Buffer::unbind_from(ARRAY_BUFFER);
170 }
171
172 float *VertexArray::append()
173 {
174         data.insert(data.end(), stride, 0.0f);
175         set_dirty();
176         return &*(data.end()-stride);
177 }
178
179 float *VertexArray::modify(unsigned i)
180 {
181         set_dirty();
182         return &data[0]+i*stride;
183 }
184
185 void VertexArray::set_dirty()
186 {
187         dirty = true;
188         if(defer_vbuf)
189         {
190                 vbuf = new Buffer(ARRAY_BUFFER);
191                 defer_vbuf = false;
192         }
193 }
194
195
196 VertexArray::ArrayMask::ArrayMask()
197 {
198         for(unsigned i=0; i<N; ++i)
199                 mask[i] = 0;
200 }
201
202 void VertexArray::ArrayMask::set(unsigned bit)
203 {
204         mask[bit/B] |= 1<<(bit%B);
205 }
206
207 bool VertexArray::ArrayMask::is_set(unsigned bit) const
208 {
209         return mask[bit/B]&(1<<(bit%B));
210 }
211
212
213 VertexArray::Loader::Loader(VertexArray &a):
214         VertexArrayBuilder(a)
215 {
216         add("vertex2",   static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
217         add("vertex3",   static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
218         add("vertex4",   static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
219         add("normal3",   static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
220         add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
221         add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
222         add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
223         add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
224         add("multitexcoord1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::multitexcoord));
225         add("multitexcoord2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::multitexcoord));
226         add("multitexcoord3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::multitexcoord));
227         add("multitexcoord4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::multitexcoord));
228         add("color3",    static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
229         add("color4",    static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));
230         add("attrib1",   static_cast<void (Loader::*)(unsigned, float)>(&Loader::attrib));
231         add("attrib2",   static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::attrib));
232         add("attrib3",   static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::attrib));
233         add("attrib4",   static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::attrib));
234 }
235
236 } // namespace GL
237 } // namespace Msp