1 #include "arb_multitexture.h"
2 #include "arb_vertex_shader.h"
6 #include "vertexarray.h"
13 VertexArray::VertexArray(const VertexFormat &f):
20 VertexArray::~VertexArray()
23 void VertexArray::reset(const VertexFormat &f)
27 stride = get_stride(format);
32 for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
34 unsigned slot = get_array_slot(*c);
35 if(slot>=arrays.size())
36 arrays.resize(slot+1);
38 Array &arr = arrays[slot];
42 offset += get_component_size(*c);
46 unsigned VertexArray::get_array_slot(unsigned char comp)
48 unsigned t = get_component_type(comp);
49 if(t==get_component_type(VERTEX3))
51 else if(t==get_component_type(NORMAL3))
53 else if(t==get_component_type(COLOR4_FLOAT))
55 else if(comp>=TEXCOORD1 && comp<=TEXCOORD4+12)
57 t -= get_component_type(TEXCOORD1);
59 static Require _req(ARB_multitexture);
64 static Require _req(ARB_vertex_shader);
66 t -= get_component_type(ATTRIB1);
71 void VertexArray::use_vertex_buffer()
76 vbuf = new Buffer(ARRAY_BUFFER);
81 void VertexArray::use_vertex_buffer(Buffer *b)
89 void VertexArray::clear()
94 void VertexArray::reserve(unsigned n)
96 data.reserve(n*stride);
99 float *VertexArray::append()
101 data.insert(data.end(), stride, 0.0f);
103 return &*(data.end()-stride);
106 float *VertexArray::modify(unsigned i)
109 return &data[0]+i*stride;
112 void VertexArray::set_dirty()
117 vbuf = new Buffer(ARRAY_BUFFER);
122 void VertexArray::apply() const
125 throw invalid_operation("VertexArray::apply");
127 const VertexArray *old = current();
128 if(!set_current(this))
133 vbuf->bind_to(ARRAY_BUFFER);
136 vbuf->data(data.size()*sizeof(float), &data[0]);
141 const float *base = (vbuf ? 0 : &data[0]);
142 unsigned stride_bytes = stride*sizeof(float);
143 unsigned active_tex = 0;
144 unsigned n_arrays = arrays.size();
146 n_arrays = max(n_arrays, old->arrays.size());
147 for(unsigned i=0; i<n_arrays; ++i)
149 const Array *arr = ((i<arrays.size() && arrays[i].component) ? &arrays[i] : 0);
150 const Array *old_arr = ((old && i<old->arrays.size() && old->arrays[i].component) ? &old->arrays[i] : 0);
154 unsigned char comp = (arr ? arr->component : old_arr->component);
155 unsigned sz = get_component_size(comp);
156 unsigned t = get_component_type(comp);
157 GLenum array_type = 0;
158 if(t==get_component_type(VERTEX3))
161 glVertexPointer(sz, GL_FLOAT, stride_bytes, base+arr->offset);
162 array_type = GL_VERTEX_ARRAY;
164 else if(t==get_component_type(NORMAL3))
167 glNormalPointer(GL_FLOAT, stride_bytes, base+arr->offset);
168 array_type = GL_NORMAL_ARRAY;
170 else if(t==get_component_type(COLOR4_FLOAT))
175 glColorPointer(4, GL_UNSIGNED_BYTE, stride_bytes, base+arr->offset);
177 glColorPointer(sz, GL_FLOAT, stride_bytes, base+arr->offset);
179 array_type = GL_COLOR_ARRAY;
181 else if(comp>=TEXCOORD1 && comp<=TEXCOORD4+12)
183 t -= get_component_type(TEXCOORD1);
184 if(t>0 || active_tex)
186 glClientActiveTexture(GL_TEXTURE0+t);
190 glTexCoordPointer(sz, GL_FLOAT, stride_bytes, base+arr->offset);
191 array_type = GL_TEXTURE_COORD_ARRAY;
195 if(t>=get_component_type(ATTRIB1))
196 t -= get_component_type(ATTRIB1);
198 glVertexAttribPointer(t, sz, GL_FLOAT, false, stride_bytes, base+arr->offset);
201 // Only change enable state if needed
205 glEnableClientState(array_type);
207 glEnableVertexAttribArray(t);
209 else if(old_arr && !arr)
212 glDisableClientState(array_type);
214 glDisableVertexAttribArray(t);
219 glClientActiveTexture(GL_TEXTURE0);
222 Buffer::unbind_from(ARRAY_BUFFER);
226 VertexArray::Array::Array():
232 VertexArray::Loader::Loader(VertexArray &a):
233 VertexArrayBuilder(a)
235 add("vertex2", static_cast<void (Loader::*)(float, float)>(&Loader::vertex));
236 add("vertex3", static_cast<void (Loader::*)(float, float, float)>(&Loader::vertex));
237 add("vertex4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::vertex));
238 add("normal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::normal));
239 add("texcoord1", static_cast<void (Loader::*)(float)>(&Loader::texcoord));
240 add("texcoord2", static_cast<void (Loader::*)(float, float)>(&Loader::texcoord));
241 add("texcoord3", static_cast<void (Loader::*)(float, float, float)>(&Loader::texcoord));
242 add("texcoord4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::texcoord));
243 add("multitexcoord1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::multitexcoord));
244 add("multitexcoord2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::multitexcoord));
245 add("multitexcoord3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::multitexcoord));
246 add("multitexcoord4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::multitexcoord));
247 add("color3", static_cast<void (Loader::*)(float, float, float)>(&Loader::color));
248 add("color4", static_cast<void (Loader::*)(float, float, float, float)>(&Loader::color));
249 add("attrib1", static_cast<void (Loader::*)(unsigned, float)>(&Loader::attrib));
250 add("attrib2", static_cast<void (Loader::*)(unsigned, float, float)>(&Loader::attrib));
251 add("attrib3", static_cast<void (Loader::*)(unsigned, float, float, float)>(&Loader::attrib));
252 add("attrib4", static_cast<void (Loader::*)(unsigned, float, float, float, float)>(&Loader::attrib));
253 add("tangent3", static_cast<void (Loader::*)(float, float, float)>(&Loader::tangent));
254 add("binormal3", static_cast<void (Loader::*)(float, float, float)>(&Loader::binormal));