1 #include "arb_multitexture.h"
2 #include "arb_vertex_shader.h"
6 #include "vertexarray.h"
13 VertexArray::ArrayMask VertexArray::enabled_arrays;
15 VertexArray::VertexArray(const VertexFormat &f):
22 VertexArray::~VertexArray()
25 void VertexArray::reset(const VertexFormat &f)
29 stride = get_stride(format);
31 bool has_multitex = false;
32 bool has_gen_attrs = false;
33 for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
35 if(*c>=TEXCOORD1+4 && *c<ATTRIB1)
41 static Require _req(ARB_multitexture);
43 static Require _req(ARB_vertex_shader);
46 void VertexArray::use_vertex_buffer()
51 vbuf = new Buffer(ARRAY_BUFFER);
56 void VertexArray::use_vertex_buffer(Buffer *b)
64 void VertexArray::clear()
69 void VertexArray::reserve(unsigned n)
71 data.reserve(n*stride);
74 float *VertexArray::append()
76 data.insert(data.end(), stride, 0.0f);
78 return &*(data.end()-stride);
81 float *VertexArray::modify(unsigned i)
84 return &data[0]+i*stride;
87 void VertexArray::set_dirty()
92 vbuf = new Buffer(ARRAY_BUFFER);
97 void VertexArray::apply() const
100 throw invalid_operation("VertexArray::apply");
104 vbuf->bind_to(ARRAY_BUFFER);
107 vbuf->data(data.size()*sizeof(float), &data[0]);
112 const float *base = (vbuf ? 0 : &data[0]);
115 unsigned bpv = stride*sizeof(float);
116 unsigned active_tex = 0;
117 for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
119 unsigned sz = (*c&3)+1;
121 bool en = enabled_arrays.is_set(t);
125 glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
127 glEnableClientState(GL_VERTEX_ARRAY);
130 glNormalPointer(GL_FLOAT, bpv, base+offset);
132 glEnableClientState(GL_NORMAL_ARRAY);
136 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
138 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
140 glEnableClientState(GL_COLOR_ARRAY);
145 if(t>3 || active_tex)
147 glClientActiveTexture(GL_TEXTURE0+(t-3));
150 glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
152 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
156 glVertexAttribPointer(t-11, sz, GL_FLOAT, false, bpv, base+offset);
158 glEnableVertexAttribArray(t-11);
166 for(unsigned i=0; i<64; ++i)
167 if(enabled_arrays.is_set(i) && !found.is_set(i))
170 glDisableClientState(GL_VERTEX_ARRAY);
172 glDisableClientState(GL_NORMAL_ARRAY);
174 glDisableClientState(GL_COLOR_ARRAY);
175 else if(i>=3 && i<11)
177 if(i>3 || active_tex)
178 glClientActiveTexture(GL_TEXTURE0+(i-3));
179 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
183 glDisableVertexAttribArray(i-11);
186 enabled_arrays = found;
189 glClientActiveTexture(GL_TEXTURE0);
192 Buffer::unbind_from(ARRAY_BUFFER);
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("multitexcoord1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::multitexcoord));
225 add("multitexcoord2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::multitexcoord));
226 add("multitexcoord3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::multitexcoord));
227 add("multitexcoord4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::multitexcoord));
228 add("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
229 add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));
230 add("attrib1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::attrib));
231 add("attrib2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::attrib));
232 add("attrib3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::attrib));
233 add("attrib4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::attrib));