3 This file is part of libmspgl
4 Copyright © 2007-2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include "arb_vertex_program.h"
11 #include "version_1_2.h"
12 #include "vertexarray.h"
13 #include "vertexbuffer.h"
20 VertexArray::VertexArray(const VertexFormat &f):
27 VertexArray::~VertexArray()
33 void VertexArray::use_vertex_buffer()
38 vbuf = new Buffer(ARRAY_BUFFER);
44 void VertexArray::use_vertex_buffer(Buffer *b)
54 void VertexArray::reserve(unsigned n)
56 data.reserve(n*stride);
59 void VertexArray::clear()
64 void VertexArray::reset(const VertexFormat &f)
68 stride = get_stride(format);
70 bool has_multitex = false;
71 bool has_gen_attrs = false;
72 for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
74 if(*c>=TEXCOORD1+4 && *c<ATTRIB1)
80 static RequireVersion _ver(1, 3);
82 static RequireExtension _ext("GL_ARB_vertex_program");
85 void VertexArray::apply() const
88 throw InvalidState("Trying to apply a vertex array with no data");
93 const float *base = vbuf?0:&data[0];
96 unsigned bpv = stride*sizeof(float);
97 unsigned active_tex = 0;
98 for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
100 unsigned sz = (*c&3)+1;
102 bool en = enabled_arrays.is_set(t);
106 glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
108 glEnableClientState(GL_VERTEX_ARRAY);
111 glNormalPointer(GL_FLOAT, bpv, base+offset);
113 glEnableClientState(GL_NORMAL_ARRAY);
117 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
119 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
121 glEnableClientState(GL_COLOR_ARRAY);
126 if(t>3 || active_tex)
128 glClientActiveTexture(GL_TEXTURE0+(t-3));
131 glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
133 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
137 glVertexAttribPointerARB(t-11, sz, GL_FLOAT, false, bpv, base+offset);
139 glEnableVertexAttribArrayARB(t-11);
147 for(unsigned i=0; i<64; ++i)
148 if(enabled_arrays.is_set(i) && !found.is_set(i))
151 glDisableClientState(GL_VERTEX_ARRAY);
153 glDisableClientState(GL_NORMAL_ARRAY);
155 glDisableClientState(GL_COLOR_ARRAY);
156 else if(i>=3 && i<11)
158 if(i>3 || active_tex)
159 glClientActiveTexture(GL_TEXTURE0+(i-3));
160 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
163 glDisableVertexAttribArrayARB(i-11);
166 enabled_arrays = found;
169 glClientActiveTexture(GL_TEXTURE0);
176 Updates the VertexArray data to the VertexBuffer tied to the array, if any.
178 void VertexArray::update_data()
182 vbuf->data(data.size()*sizeof(float), &data[0]);
187 float *VertexArray::append()
189 data.insert(data.end(), stride, 0.0f);
190 return &*data.end()-stride;
193 VertexArray::ArrayMask VertexArray::enabled_arrays;
196 VertexArray::ArrayMask::ArrayMask()
198 for(unsigned i=0; i<N; ++i)
202 void VertexArray::ArrayMask::set(unsigned bit)
204 mask[bit/B] |= 1<<(bit%B);
207 bool VertexArray::ArrayMask::is_set(unsigned bit) const
209 return mask[bit/B]&(1<<(bit%B));
213 VertexArray::Loader::Loader(VertexArray &a):
214 VertexArrayBuilder(a)
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("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
225 add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));
226 add("attrib1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::attrib));
227 add("attrib2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::attrib));
228 add("attrib3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::attrib));
229 add("attrib4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::attrib));
233 void array_element(int i)
238 void draw_arrays(PrimitiveType mode, int first, unsigned count)
240 glDrawArrays(mode, first, count);
243 void draw_elements(PrimitiveType mode, unsigned count, DataType type, const void *indices)
245 glDrawElements(mode, count, type, indices);
248 void draw_range_elements(PrimitiveType mode, unsigned low, unsigned high, unsigned count, DataType type, const void *indices)
250 static RequireVersion _ver(1, 2);
251 glDrawRangeElements(mode, low, high, count, type, indices);