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::use_vertex_buffer()
30 vbuf = new Buffer(ARRAY_BUFFER);
35 void VertexArray::use_vertex_buffer(Buffer *b)
43 void VertexArray::reserve(unsigned n)
45 data.reserve(n*stride);
48 void VertexArray::clear()
53 void VertexArray::reset(const VertexFormat &f)
57 stride = get_stride(format);
59 bool has_multitex = false;
60 bool has_gen_attrs = false;
61 for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
63 if(*c>=TEXCOORD1+4 && *c<ATTRIB1)
69 static Require _req(ARB_multitexture);
71 static Require _req(ARB_vertex_shader);
74 void VertexArray::apply() const
77 throw invalid_operation("VertexArray::apply");
81 vbuf->bind_to(ARRAY_BUFFER);
84 vbuf->data(data.size()*sizeof(float), &data[0]);
89 const float *base = (vbuf ? 0 : &data[0]);
92 unsigned bpv = stride*sizeof(float);
93 unsigned active_tex = 0;
94 for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
96 unsigned sz = (*c&3)+1;
98 bool en = enabled_arrays.is_set(t);
102 glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
104 glEnableClientState(GL_VERTEX_ARRAY);
107 glNormalPointer(GL_FLOAT, bpv, base+offset);
109 glEnableClientState(GL_NORMAL_ARRAY);
113 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
115 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
117 glEnableClientState(GL_COLOR_ARRAY);
122 if(t>3 || active_tex)
124 glClientActiveTexture(GL_TEXTURE0+(t-3));
127 glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
129 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
133 glVertexAttribPointer(t-11, sz, GL_FLOAT, false, bpv, base+offset);
135 glEnableVertexAttribArray(t-11);
143 for(unsigned i=0; i<64; ++i)
144 if(enabled_arrays.is_set(i) && !found.is_set(i))
147 glDisableClientState(GL_VERTEX_ARRAY);
149 glDisableClientState(GL_NORMAL_ARRAY);
151 glDisableClientState(GL_COLOR_ARRAY);
152 else if(i>=3 && i<11)
154 if(i>3 || active_tex)
155 glClientActiveTexture(GL_TEXTURE0+(i-3));
156 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
160 glDisableVertexAttribArray(i-11);
163 enabled_arrays = found;
166 glClientActiveTexture(GL_TEXTURE0);
169 Buffer::unbind_from(ARRAY_BUFFER);
172 float *VertexArray::append()
174 data.insert(data.end(), stride, 0.0f);
176 return &*(data.end()-stride);
179 float *VertexArray::modify(unsigned i)
182 return &data[0]+i*stride;
185 void VertexArray::set_dirty()
190 vbuf = new Buffer(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));