1 #ifndef TACOS_GRAPHICS_VAO_HPP_
2 #define TACOS_GRAPHICS_VAO_HPP_
5 #include "gl_traits.hpp"
12 /// Simple vertex array object based on indexed draw calls with attributes
13 /// interleaved in a single buffer.
14 template<class Attributes, class Element>
21 glGenVertexArrays(1, &vao);
22 glGenBuffers(2, buffers);
24 ~SimpleVAO() noexcept {
25 glDeleteBuffers(2, buffers);
26 glDeleteVertexArrays(1, &vao);
29 SimpleVAO(const SimpleVAO &) = delete;
30 SimpleVAO &operator =(const SimpleVAO &) = delete;
33 void Bind() const noexcept {
34 glBindVertexArray(vao);
36 void Unbind() const noexcept {
39 void BindAttributes() const noexcept {
40 glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
42 void BindElements() const noexcept {
43 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
45 void EnableAttribute(GLuint index) noexcept {
46 glEnableVertexAttribArray(index);
48 void DisableAttribute(GLuint index) noexcept {
49 glDisableVertexAttribArray(index);
51 template<class Attribute>
52 void AttributePointer(GLuint index, bool normalized, std::size_t offset) noexcept {
53 glVertexAttribPointer(
55 gl_traits<Attribute>::size,
56 gl_traits<Attribute>::type,
59 reinterpret_cast<const void *>(offset)
62 void ReserveAttributes(std::size_t size, GLenum usage) noexcept {
63 glBufferData(GL_ARRAY_BUFFER, size * sizeof(Attributes), nullptr, usage);
65 MappedBuffer<Attributes> MapAttributes(GLenum access) {
66 return MappedBuffer<Attributes>(GL_ARRAY_BUFFER, access);
68 void ReserveElements(std::size_t size, GLenum usage) noexcept {
69 glBufferData(GL_ELEMENT_ARRAY_BUFFER, size * sizeof(Element), nullptr, usage);
71 MappedBuffer<Element> MapElements(GLenum access) {
72 return MappedBuffer<Element>(GL_ELEMENT_ARRAY_BUFFER, access);
74 void DrawTriangles(std::size_t size, std::size_t offset = 0) const noexcept {
75 glDrawElements(GL_TRIANGLES, size, gl_traits<Element>::type, ((Element *) nullptr) + offset);