1 #include <msp/core/raii.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_vertex_array_object.h>
4 #include <msp/gl/extensions/arb_vertex_attrib_binding.h>
5 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
6 #include <msp/gl/extensions/arb_vertex_shader.h>
9 #include "vertexarray.h"
10 #include "vertexsetup.h"
15 VertexSetup::VertexSetup():
20 static Require req(ARB_vertex_array_object);
21 if(ARB_direct_state_access)
22 glCreateVertexArrays(1, &id);
24 glGenVertexArrays(1, &id);
27 VertexSetup::~VertexSetup()
31 glDeleteVertexArrays(1, &id);
34 void VertexSetup::set_vertex_array(const VertexArray &a)
40 void VertexSetup::set_index_buffer(const Buffer &ibuf)
46 void VertexSetup::update(unsigned mask) const
48 static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
49 if(!direct && current()!=this)
56 update_vertex_array(direct);
61 glVertexArrayElementBuffer(id, index_buffer->get_id());
63 glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
67 void VertexSetup::update_vertex_array(bool direct) const
69 Conditional<Bind> bind_vbuf(!direct, array->get_buffer(), ARRAY_BUFFER);
71 const VertexFormat &fmt = array->get_format();
72 unsigned stride = get_stride(fmt)*sizeof(float);
74 glVertexArrayVertexBuffer(id, 0, array->get_buffer()->get_id(), 0, stride);
77 for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
79 unsigned t = get_component_type(*c);
80 if(t>=get_component_type(ATTRIB1))
81 t -= get_component_type(ATTRIB1);
82 unsigned sz = get_component_size(*c);
86 glVertexArrayAttribFormat(id, t, 4, GL_UNSIGNED_BYTE, true, offset);
88 glVertexArrayAttribFormat(id, t, sz, GL_FLOAT, false, offset);
89 glVertexArrayAttribBinding(id, t, 0);
90 glEnableVertexArrayAttrib(id, t);
95 glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
97 glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
98 glEnableVertexAttribArray(t);
100 offset += sz*sizeof(float);
104 void VertexSetup::bind() const
106 if(set_current(this))
108 glBindVertexArray(id);
117 void VertexSetup::unbind()
120 glBindVertexArray(0);