]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarray.cpp
Shuffle the members of VertexArray into a sensible order
[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::reset(const VertexFormat &f)
26 {
27         clear();
28         format = f;
29         stride = get_stride(format);
30
31         bool has_multitex = false;
32         bool has_gen_attrs = false;
33         for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
34         {
35                 if(*c>=TEXCOORD1+4 && *c<ATTRIB1)
36                         has_multitex = true;
37                 if(*c>=ATTRIB1)
38                         has_gen_attrs = true;
39         }
40         if(has_multitex)
41                 static Require _req(ARB_multitexture);
42         if(has_gen_attrs)
43                 static Require _req(ARB_vertex_shader);
44 }
45
46 void VertexArray::use_vertex_buffer()
47 {
48         if(vbuf)
49                 return;
50
51         vbuf = new Buffer(ARRAY_BUFFER);
52         defer_vbuf = false;
53         dirty = true;
54 }
55
56 void VertexArray::use_vertex_buffer(Buffer *b)
57 {
58         vbuf = b;
59         vbuf.keep();
60         defer_vbuf = false;
61         dirty = true;
62 }
63
64 void VertexArray::clear()
65 {
66         data.clear();
67 }
68
69 void VertexArray::reserve(unsigned n)
70 {
71         data.reserve(n*stride);
72 }
73
74 float *VertexArray::append()
75 {
76         data.insert(data.end(), stride, 0.0f);
77         set_dirty();
78         return &*(data.end()-stride);
79 }
80
81 float *VertexArray::modify(unsigned i)
82 {
83         set_dirty();
84         return &data[0]+i*stride;
85 }
86
87 void VertexArray::set_dirty()
88 {
89         dirty = true;
90         if(defer_vbuf)
91         {
92                 vbuf = new Buffer(ARRAY_BUFFER);
93                 defer_vbuf = false;
94         }
95 }
96
97 void VertexArray::apply() const
98 {
99         if(format.empty())
100                 throw invalid_operation("VertexArray::apply");
101
102         if(vbuf)
103         {
104                 vbuf->bind_to(ARRAY_BUFFER);
105                 if(dirty)
106                 {
107                         vbuf->data(data.size()*sizeof(float), &data[0]);
108                         dirty = false;
109                 }
110         }
111
112         const float *base = (vbuf ? 0 : &data[0]);
113         unsigned offset = 0;
114         ArrayMask found;
115         unsigned bpv = stride*sizeof(float);
116         unsigned active_tex = 0;
117         for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
118         {
119                 unsigned sz = (*c&3)+1;
120                 unsigned t = *c>>2;
121                 bool en = enabled_arrays.is_set(t);
122                 switch(t)
123                 {
124                 case 0:
125                         glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
126                         if(!en)
127                                 glEnableClientState(GL_VERTEX_ARRAY);
128                         break;
129                 case 1:
130                         glNormalPointer(GL_FLOAT, bpv, base+offset);
131                         if(!en)
132                                 glEnableClientState(GL_NORMAL_ARRAY);
133                         break;
134                 case 2:
135                         if(sz==1)
136                                 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
137                         else
138                                 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
139                         if(!en)
140                                 glEnableClientState(GL_COLOR_ARRAY);
141                         break;
142                 default:
143                         if(t<11)
144                         {
145                                 if(t>3 || active_tex)
146                                 {
147                                         glClientActiveTexture(GL_TEXTURE0+(t-3));
148                                         active_tex = t-3;
149                                 }
150                                 glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
151                                 if(!en)
152                                         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
153                         }
154                         else
155                         {
156                                 glVertexAttribPointer(t-11, sz, GL_FLOAT, false, bpv, base+offset);
157                                 if(!en)
158                                         glEnableVertexAttribArray(t-11);
159                         }
160                         break;
161                 }
162                 found.set(t);
163                 offset += sz;
164         }
165
166         for(unsigned i=0; i<64; ++i)
167                 if(enabled_arrays.is_set(i) && !found.is_set(i))
168                 {
169                         if(i==0)
170                                 glDisableClientState(GL_VERTEX_ARRAY);
171                         else if(i==1)
172                                 glDisableClientState(GL_NORMAL_ARRAY);
173                         else if(i==2)
174                                 glDisableClientState(GL_COLOR_ARRAY);
175                         else if(i>=3 && i<11)
176                         {
177                                 if(i>3 || active_tex)
178                                         glClientActiveTexture(GL_TEXTURE0+(i-3));
179                                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
180                                 active_tex = i-3;
181                         }
182                         else
183                                 glDisableVertexAttribArray(i-11);
184                 }
185
186         enabled_arrays = found;
187
188         if(active_tex)
189                 glClientActiveTexture(GL_TEXTURE0);
190
191         if(vbuf)
192                 Buffer::unbind_from(ARRAY_BUFFER);
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