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"
10 #include "extension.h"
12 #include "version_1_2.h"
13 #include "version_1_3.h"
14 #include "vertexarray.h"
21 VertexArray::ArrayMask VertexArray::enabled_arrays;
23 VertexArray::VertexArray(const VertexFormat &f):
30 VertexArray::~VertexArray()
33 void VertexArray::use_vertex_buffer()
38 vbuf = new Buffer(ARRAY_BUFFER);
43 void VertexArray::use_vertex_buffer(Buffer *b)
51 void VertexArray::reserve(unsigned n)
53 data.reserve(n*stride);
56 void VertexArray::clear()
61 void VertexArray::reset(const VertexFormat &f)
65 stride = get_stride(format);
67 bool has_multitex = false;
68 bool has_gen_attrs = false;
69 for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
71 if(*c>=TEXCOORD1+4 && *c<ATTRIB1)
77 static RequireVersion _ver(1, 3);
79 static RequireExtension _ext("GL_ARB_vertex_program");
82 void VertexArray::apply() const
85 throw InvalidState("Trying to apply a vertex array with no data");
89 vbuf->bind_to(ARRAY_BUFFER);
92 vbuf->data(data.size()*sizeof(float), &data[0]);
97 const float *base = (vbuf ? 0 : &data[0]);
100 unsigned bpv = stride*sizeof(float);
101 unsigned active_tex = 0;
102 for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
104 unsigned sz = (*c&3)+1;
106 bool en = enabled_arrays.is_set(t);
110 glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
112 glEnableClientState(GL_VERTEX_ARRAY);
115 glNormalPointer(GL_FLOAT, bpv, base+offset);
117 glEnableClientState(GL_NORMAL_ARRAY);
121 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
123 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
125 glEnableClientState(GL_COLOR_ARRAY);
130 if(t>3 || active_tex)
132 glClientActiveTexture(GL_TEXTURE0+(t-3));
135 glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
137 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
141 glVertexAttribPointerARB(t-11, sz, GL_FLOAT, false, bpv, base+offset);
143 glEnableVertexAttribArrayARB(t-11);
151 for(unsigned i=0; i<64; ++i)
152 if(enabled_arrays.is_set(i) && !found.is_set(i))
155 glDisableClientState(GL_VERTEX_ARRAY);
157 glDisableClientState(GL_NORMAL_ARRAY);
159 glDisableClientState(GL_COLOR_ARRAY);
160 else if(i>=3 && i<11)
162 if(i>3 || active_tex)
163 glClientActiveTexture(GL_TEXTURE0+(i-3));
164 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
167 glDisableVertexAttribArrayARB(i-11);
170 enabled_arrays = found;
173 glClientActiveTexture(GL_TEXTURE0);
176 Buffer::unbind_from(ARRAY_BUFFER);
179 float *VertexArray::append()
181 data.insert(data.end(), stride, 0.0f);
183 return &*(data.end()-stride);
186 float *VertexArray::modify(unsigned i)
189 return &data[0]+i*stride;
192 void VertexArray::set_dirty()
197 vbuf = new Buffer(ARRAY_BUFFER);
203 VertexArray::ArrayMask::ArrayMask()
205 for(unsigned i=0; i<N; ++i)
209 void VertexArray::ArrayMask::set(unsigned bit)
211 mask[bit/B] |= 1<<(bit%B);
214 bool VertexArray::ArrayMask::is_set(unsigned bit) const
216 return mask[bit/B]&(1<<(bit%B));
220 VertexArray::Loader::Loader(VertexArray &a):
221 VertexArrayBuilder(a)
223 add("vertex2", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
224 add("vertex3", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
225 add("vertex4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
226 add("normal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
227 add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
228 add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
229 add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
230 add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
231 add("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
232 add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));
233 add("attrib1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::attrib));
234 add("attrib2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::attrib));
235 add("attrib3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::attrib));
236 add("attrib4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::attrib));
240 void array_element(int i)
245 void draw_arrays(PrimitiveType mode, int first, unsigned count)
247 glDrawArrays(mode, first, count);
250 void draw_elements(PrimitiveType mode, unsigned count, DataType type, const void *indices)
252 glDrawElements(mode, count, type, indices);
255 void draw_range_elements(PrimitiveType mode, unsigned low, unsigned high, unsigned count, DataType type, const void *indices)
257 static RequireVersion _ver(1, 2);
258 glDrawRangeElements(mode, low, high, count, type, indices);