3 This file is part of libmspgl
4 Copyright © 2007-2009 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_gen_attrs=false;
71 for(const unsigned char *c=format.begin(); (!has_gen_attrs && c!=format.end()); ++c)
72 has_gen_attrs=(*c>=ATTRIB1);
74 static RequireExtension _ext("GL_ARB_vertex_program");
77 void VertexArray::apply() const
80 throw InvalidState("Trying to apply a vertex array with no data");
85 const float *base=vbuf?0:&data[0];
88 uint bpv=stride*sizeof(float);
89 for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
96 glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
99 glNormalPointer(GL_FLOAT, bpv, base+offset);
102 glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
106 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
108 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
111 glVertexAttribPointerARB(t-4, sz, GL_FLOAT, false, bpv, base+offset);
118 set_array(GL_VERTEX_ARRAY, found&1, 1);
119 set_array(GL_NORMAL_ARRAY, found&2, 2);
120 set_array(GL_TEXTURE_COORD_ARRAY, found&4, 4);
121 set_array(GL_COLOR_ARRAY, found&8, 8);
122 for(unsigned i=4; i<32; ++i)
123 set_array(i-4, (found>>i)&1, 1<<i);
130 Updates the VertexArray data to the VertexBuffer tied to the array, if any.
132 void VertexArray::update_data()
136 vbuf->data(data.size()*sizeof(float), &data[0]);
141 float *VertexArray::append()
143 data.insert(data.end(), stride, 0.0f);
144 return &*data.end()-stride;
147 void VertexArray::set_array(unsigned array, bool en, unsigned mask) const
149 if((enabled_arrays&mask) && !en)
152 glDisableClientState(array);
154 glDisableVertexAttribArrayARB(array);
155 enabled_arrays&=~mask;
157 else if(!(enabled_arrays&mask) && en)
160 glEnableClientState(array);
162 glEnableVertexAttribArrayARB(array);
163 enabled_arrays|=mask;
167 unsigned VertexArray::enabled_arrays=0;
170 VertexArray::Loader::Loader(VertexArray &a):
171 VertexArrayBuilder(a)
173 add("vertex2", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
174 add("vertex3", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
175 add("vertex4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
176 add("normal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
177 add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
178 add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
179 add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
180 add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
181 add("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
182 add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));
183 add("attrib1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::attrib));
184 add("attrib2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::attrib));
185 add("attrib3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::attrib));
186 add("attrib4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::attrib));
190 void array_element(int i)
195 void draw_arrays(PrimitiveType mode, int first, sizei count)
197 glDrawArrays(mode, first, count);
200 void draw_elements(PrimitiveType mode, sizei count, DataType type, const void *indices)
202 glDrawElements(mode, count, type, indices);
205 void draw_range_elements(PrimitiveType mode, uint low, uint high, sizei count, DataType type, const void *indices)
207 static RequireVersion _ver(1, 2);
208 glDrawRangeElements(mode, low, high, count, type, indices);