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<=TEXCOORD4+12)
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 = get_component_size(*c);
120 unsigned t = get_component_type(*c);
121 bool en = enabled_arrays.is_set(t);
122 if(t==get_component_type(VERTEX3))
124 glVertexPointer(sz, GL_FLOAT, bpv, base+offset);
126 glEnableClientState(GL_VERTEX_ARRAY);
128 else if(t==get_component_type(NORMAL3))
130 glNormalPointer(GL_FLOAT, bpv, base+offset);
132 glEnableClientState(GL_NORMAL_ARRAY);
134 else if(t==get_component_type(COLOR4_FLOAT))
137 glColorPointer(4, GL_UNSIGNED_BYTE, bpv, base+offset);
139 glColorPointer(sz, GL_FLOAT, bpv, base+offset);
141 glEnableClientState(GL_COLOR_ARRAY);
143 else if(*c>=TEXCOORD1 && *c<=TEXCOORD4+12)
145 t -= get_component_type(TEXCOORD1);
146 if(t>0 || active_tex)
148 glClientActiveTexture(GL_TEXTURE0+t);
151 glTexCoordPointer(sz, GL_FLOAT, bpv, base+offset);
153 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
157 if(t>=get_component_type(ATTRIB1))
158 t -= get_component_type(ATTRIB1);
159 glVertexAttribPointer(t, sz, GL_FLOAT, false, bpv, base+offset);
161 glEnableVertexAttribArray(t);
167 for(unsigned i=0; i<64; ++i)
168 if(enabled_arrays.is_set(i) && !found.is_set(i))
170 if(i==get_component_type(VERTEX3))
171 glDisableClientState(GL_VERTEX_ARRAY);
172 else if(i==get_component_type(NORMAL3))
173 glDisableClientState(GL_NORMAL_ARRAY);
174 else if(i==get_component_type(COLOR4_FLOAT))
175 glDisableClientState(GL_COLOR_ARRAY);
176 else if(i>=get_component_type(TEXCOORD1) && i<=get_component_type(TEXCOORD1)+3)
178 unsigned j = i-get_component_type(TEXCOORD1);
179 if(j>0 || active_tex)
180 glClientActiveTexture(GL_TEXTURE0+j);
181 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
184 else if(i<get_component_type(ATTRIB1))
185 glDisableVertexAttribArray(i);
187 glDisableVertexAttribArray(i-get_component_type(ATTRIB1));
190 enabled_arrays = found;
193 glClientActiveTexture(GL_TEXTURE0);
196 Buffer::unbind_from(ARRAY_BUFFER);
200 VertexArray::ArrayMask::ArrayMask()
202 for(unsigned i=0; i<N; ++i)
206 void VertexArray::ArrayMask::set(unsigned bit)
208 mask[bit/B] |= 1<<(bit%B);
211 bool VertexArray::ArrayMask::is_set(unsigned bit) const
213 return mask[bit/B]&(1<<(bit%B));
217 VertexArray::Loader::Loader(VertexArray &a):
218 VertexArrayBuilder(a)
220 add("vertex2", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
221 add("vertex3", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
222 add("vertex4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
223 add("normal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
224 add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
225 add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
226 add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
227 add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
228 add("multitexcoord1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::multitexcoord));
229 add("multitexcoord2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::multitexcoord));
230 add("multitexcoord3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::multitexcoord));
231 add("multitexcoord4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::multitexcoord));
232 add("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
233 add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));
234 add("attrib1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::attrib));
235 add("attrib2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::attrib));
236 add("attrib3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::attrib));
237 add("attrib4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::attrib));