-void VertexArray::apply() const
-{
- if(format.empty())
- throw invalid_operation("VertexArray::apply");
- // Don't mess up the vertex array object of a mesh
- if(Mesh::current())
- throw invalid_operation("VertexArray::apply");
-
- static Require _req(ARB_vertex_shader);
-
- const VertexArray *old = current();
- /* If the array has been modified, apply it even if it was the last one to
- be applied. This is necessary to get the data updated to vertex buffer, and
- to resync things after a format change. Radeon drivers also have some
- problems with modifying vertex arrays without re-setting the pointers. */
- if(!set_current(this) && !dirty)
- return;
-
- const Buffer *vbuf = get_buffer();
- Bind _bind_vbuf(vbuf, ARRAY_BUFFER);
- if(vbuf && dirty)
- update_buffer();
-
- const float *base = (vbuf ? reinterpret_cast<float *>(get_offset()) : &data[0]);
- unsigned stride_bytes = stride*sizeof(float);
- apply_arrays(&arrays, (old ? &old->arrays : 0), base, stride_bytes);
-}
-
-void VertexArray::apply_arrays(const vector<Array> *arrays, const vector<Array> *old_arrays, const float *base, unsigned stride_bytes)
-{
- unsigned n_arrays = arrays ? arrays->size() : 0;
- if(old_arrays)
- n_arrays = max<unsigned>(n_arrays, old_arrays->size());
- for(unsigned i=0; i<n_arrays; ++i)
- {
- const Array *arr = ((arrays && i<arrays->size() && (*arrays)[i].component) ? &(*arrays)[i] : 0);
- const Array *old_arr = ((old_arrays && i<old_arrays->size() && (*old_arrays)[i].component) ? &(*old_arrays)[i] : 0);
- if(!arr && !old_arr)
- continue;
-
- unsigned char comp = (arr ? arr->component : old_arr->component);
- unsigned sz = get_component_size(comp);
- unsigned t = get_component_type(comp);
-
- if(t>=get_component_type(ATTRIB1))
- t -= get_component_type(ATTRIB1);
- if(arr)
- {
- if(arr->component==COLOR4_UBYTE)
- glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride_bytes, base+arr->offset);
- else
- glVertexAttribPointer(t, sz, GL_FLOAT, false, stride_bytes, base+arr->offset);
- }
-
- // Only change enable state if needed
- if(arr && !old_arr)
- glEnableVertexAttribArray(t);
- else if(old_arr && !arr)
- glDisableVertexAttribArray(t);
- }
-}
-
-void VertexArray::unbind()
-{
- const VertexArray *old = current();
- if(set_current(0))
- apply_arrays(0, &old->arrays, 0, 0);
-}
-
-
-VertexArray::Array::Array():
- component(0),
- offset(0)
-{ }
-