1 #ifndef BLOBS_GRAPHICS_VAO_HPP_
2 #define BLOBS_GRAPHICS_VAO_HPP_
5 #include "gl_traits.hpp"
13 /// Simple vertex array object based on indexed draw calls with attributes
14 /// interleaved in a single buffer.
15 template<class Attributes, class Element>
22 glGenVertexArrays(1, &vao);
23 glGenBuffers(2, buffers);
25 ~SimpleVAO() noexcept {
26 glDeleteBuffers(2, buffers);
27 glDeleteVertexArrays(1, &vao);
30 SimpleVAO(const SimpleVAO &) = delete;
31 SimpleVAO &operator =(const SimpleVAO &) = delete;
34 void Bind() const noexcept {
35 glBindVertexArray(vao);
37 void Unbind() const noexcept {
40 void BindAttributes() const noexcept {
41 glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
43 void BindElements() const noexcept {
44 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
46 void EnableAttribute(GLuint index) noexcept {
47 glEnableVertexAttribArray(index);
49 void DisableAttribute(GLuint index) noexcept {
50 glDisableVertexAttribArray(index);
52 template<class Attribute>
53 void AttributePointer(GLuint index, bool normalized, std::size_t offset) noexcept {
54 glVertexAttribPointer(
56 gl_traits<Attribute>::size,
57 gl_traits<Attribute>::type,
60 reinterpret_cast<const void *>(offset)
63 void ReserveAttributes(std::size_t size, GLenum usage) noexcept {
64 glBufferData(GL_ARRAY_BUFFER, size * sizeof(Attributes), nullptr, usage);
66 MappedBuffer<Attributes> MapAttributes(GLenum access) {
67 return MappedBuffer<Attributes>(GL_ARRAY_BUFFER, access);
69 void ReserveElements(std::size_t size, GLenum usage) noexcept {
70 glBufferData(GL_ELEMENT_ARRAY_BUFFER, size * sizeof(Element), nullptr, usage);
72 MappedBuffer<Element> MapElements(GLenum access) {
73 return MappedBuffer<Element>(GL_ELEMENT_ARRAY_BUFFER, access);
75 void Draw(GLenum mode, std::size_t size, std::size_t offset = 0) const noexcept {
76 glDrawElements(mode, size, gl_traits<Element>::type, ((Element *) nullptr) + offset);
78 void DrawTriangles(std::size_t size, std::size_t offset = 0) const noexcept {
79 Draw(GL_TRIANGLES, size, offset);
81 void DrawTriangleStrip(std::size_t size, std::size_t offset = 0) const noexcept {
82 Draw(GL_TRIANGLE_STRIP, size, offset);
84 void DrawLineLoop(std::size_t size, std::size_t offset = 0) const noexcept {
85 Draw(GL_LINE_LOOP, size, offset);