1 #include "arb_vertex_program.h"
6 #include "version_1_2.h"
7 #include "version_1_3.h"
8 #include "vertexarray.h"
15 VertexArray::ArrayMask VertexArray::enabled_arrays;
17 VertexArray::VertexArray(const VertexFormat &f):
24 VertexArray::~VertexArray()
27 void VertexArray::use_vertex_buffer()
32 vbuf = new Buffer(ARRAY_BUFFER);
37 void VertexArray::use_vertex_buffer(Buffer *b)
45 void VertexArray::reserve(unsigned n)
47 data.reserve(n*stride);
50 void VertexArray::clear()
55 void VertexArray::reset(const VertexFormat &f)
59 stride = get_stride(format);
61 bool has_multitex = false;
62 bool has_gen_attrs = false;
63 for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
65 if(*c>=TEXCOORD1+4 && *c<ATTRIB1)
71 static RequireVersion _ver(1, 3);
73 static RequireExtension _ext("GL_ARB_vertex_program");
76 void VertexArray::apply() const
79 throw invalid_operation("VertexArray::apply");
83 vbuf->bind_to(ARRAY_BUFFER);
86 vbuf->data(data.size()*sizeof(float), &data[0]);
91 const float *base = (vbuf ? 0 : &data[0]);
94 unsigned bpv = stride*sizeof(float);
95 unsigned active_tex = 0;
96 for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
98 unsigned sz = (*c&3)+1;
100 bool en = enabled_arrays.is_set(t);
104 glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
106 glEnableClientState(GL_VERTEX_ARRAY);
109 glNormalPointer(GL_FLOAT, bpv, base+offset);
111 glEnableClientState(GL_NORMAL_ARRAY);
115 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
117 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
119 glEnableClientState(GL_COLOR_ARRAY);
124 if(t>3 || active_tex)
126 glClientActiveTexture(GL_TEXTURE0+(t-3));
129 glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
131 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
135 glVertexAttribPointerARB(t-11, sz, GL_FLOAT, false, bpv, base+offset);
137 glEnableVertexAttribArrayARB(t-11);
145 for(unsigned i=0; i<64; ++i)
146 if(enabled_arrays.is_set(i) && !found.is_set(i))
149 glDisableClientState(GL_VERTEX_ARRAY);
151 glDisableClientState(GL_NORMAL_ARRAY);
153 glDisableClientState(GL_COLOR_ARRAY);
154 else if(i>=3 && i<11)
156 if(i>3 || active_tex)
157 glClientActiveTexture(GL_TEXTURE0+(i-3));
158 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
162 glDisableVertexAttribArrayARB(i-11);
165 enabled_arrays = found;
168 glClientActiveTexture(GL_TEXTURE0);
171 Buffer::unbind_from(ARRAY_BUFFER);
174 float *VertexArray::append()
176 data.insert(data.end(), stride, 0.0f);
178 return &*(data.end()-stride);
181 float *VertexArray::modify(unsigned i)
184 return &data[0]+i*stride;
187 void VertexArray::set_dirty()
192 vbuf = new Buffer(ARRAY_BUFFER);
198 VertexArray::ArrayMask::ArrayMask()
200 for(unsigned i=0; i<N; ++i)
204 void VertexArray::ArrayMask::set(unsigned bit)
206 mask[bit/B] |= 1<<(bit%B);
209 bool VertexArray::ArrayMask::is_set(unsigned bit) const
211 return mask[bit/B]&(1<<(bit%B));
215 VertexArray::Loader::Loader(VertexArray &a):
216 VertexArrayBuilder(a)
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));
239 void array_element(int i)
244 void draw_arrays(PrimitiveType mode, int first, unsigned count)
246 glDrawArrays(mode, first, count);
249 void draw_elements(PrimitiveType mode, unsigned count, DataType type, const void *indices)
251 glDrawElements(mode, count, type, indices);
254 void draw_range_elements(PrimitiveType mode, unsigned low, unsigned high, unsigned count, DataType type, const void *indices)
256 static RequireVersion _ver(1, 2);
257 glDrawRangeElements(mode, low, high, count, type, indices);