1 #include "arb_vertex_program.h"
5 #include "version_1_2.h"
6 #include "version_1_3.h"
7 #include "vertexarray.h"
14 VertexArray::ArrayMask VertexArray::enabled_arrays;
16 VertexArray::VertexArray(const VertexFormat &f):
23 VertexArray::~VertexArray()
26 void VertexArray::use_vertex_buffer()
31 vbuf = new Buffer(ARRAY_BUFFER);
36 void VertexArray::use_vertex_buffer(Buffer *b)
44 void VertexArray::reserve(unsigned n)
46 data.reserve(n*stride);
49 void VertexArray::clear()
54 void VertexArray::reset(const VertexFormat &f)
58 stride = get_stride(format);
60 bool has_multitex = false;
61 bool has_gen_attrs = false;
62 for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
64 if(*c>=TEXCOORD1+4 && *c<ATTRIB1)
70 static RequireVersion _ver(1, 3);
72 static RequireExtension _ext("GL_ARB_vertex_program");
75 void VertexArray::apply() const
78 throw InvalidState("Trying to apply a vertex array with no data");
82 vbuf->bind_to(ARRAY_BUFFER);
85 vbuf->data(data.size()*sizeof(float), &data[0]);
90 const float *base = (vbuf ? 0 : &data[0]);
93 unsigned bpv = stride*sizeof(float);
94 unsigned active_tex = 0;
95 for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
97 unsigned sz = (*c&3)+1;
99 bool en = enabled_arrays.is_set(t);
103 glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
105 glEnableClientState(GL_VERTEX_ARRAY);
108 glNormalPointer(GL_FLOAT, bpv, base+offset);
110 glEnableClientState(GL_NORMAL_ARRAY);
114 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
116 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
118 glEnableClientState(GL_COLOR_ARRAY);
123 if(t>3 || active_tex)
125 glClientActiveTexture(GL_TEXTURE0+(t-3));
128 glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
130 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
134 glVertexAttribPointerARB(t-11, sz, GL_FLOAT, false, bpv, base+offset);
136 glEnableVertexAttribArrayARB(t-11);
144 for(unsigned i=0; i<64; ++i)
145 if(enabled_arrays.is_set(i) && !found.is_set(i))
148 glDisableClientState(GL_VERTEX_ARRAY);
150 glDisableClientState(GL_NORMAL_ARRAY);
152 glDisableClientState(GL_COLOR_ARRAY);
153 else if(i>=3 && i<11)
155 if(i>3 || active_tex)
156 glClientActiveTexture(GL_TEXTURE0+(i-3));
157 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
161 glDisableVertexAttribArrayARB(i-11);
164 enabled_arrays = found;
167 glClientActiveTexture(GL_TEXTURE0);
170 Buffer::unbind_from(ARRAY_BUFFER);
173 float *VertexArray::append()
175 data.insert(data.end(), stride, 0.0f);
177 return &*(data.end()-stride);
180 float *VertexArray::modify(unsigned i)
183 return &data[0]+i*stride;
186 void VertexArray::set_dirty()
191 vbuf = new Buffer(ARRAY_BUFFER);
197 VertexArray::ArrayMask::ArrayMask()
199 for(unsigned i=0; i<N; ++i)
203 void VertexArray::ArrayMask::set(unsigned bit)
205 mask[bit/B] |= 1<<(bit%B);
208 bool VertexArray::ArrayMask::is_set(unsigned bit) const
210 return mask[bit/B]&(1<<(bit%B));
214 VertexArray::Loader::Loader(VertexArray &a):
215 VertexArrayBuilder(a)
217 add("vertex2", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
218 add("vertex3", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
219 add("vertex4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
220 add("normal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
221 add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
222 add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
223 add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
224 add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
225 add("multitexcoord1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::multitexcoord));
226 add("multitexcoord2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::multitexcoord));
227 add("multitexcoord3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::multitexcoord));
228 add("multitexcoord4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::multitexcoord));
229 add("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
230 add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));
231 add("attrib1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::attrib));
232 add("attrib2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::attrib));
233 add("attrib3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::attrib));
234 add("attrib4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::attrib));
238 void array_element(int i)
243 void draw_arrays(PrimitiveType mode, int first, unsigned count)
245 glDrawArrays(mode, first, count);
248 void draw_elements(PrimitiveType mode, unsigned count, DataType type, const void *indices)
250 glDrawElements(mode, count, type, indices);
253 void draw_range_elements(PrimitiveType mode, unsigned low, unsigned high, unsigned count, DataType type, const void *indices)
255 static RequireVersion _ver(1, 2);
256 glDrawRangeElements(mode, low, high, count, type, indices);