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