]> git.localhorst.tv Git - tacos.git/blob - src/graphics/vao.hpp
isolate some GL stuff
[tacos.git] / src / graphics / vao.hpp
1 #ifndef TACOS_GRAPHICS_VAO_HPP_
2 #define TACOS_GRAPHICS_VAO_HPP_
3
4 #include "buffer.hpp"
5 #include "gl_traits.hpp"
6
7 #include <GL/glew.h>
8
9
10 namespace tacos {
11
12 /// Simple vertex array object based on indexed draw calls with attributes
13 /// interleaved in a single buffer.
14 template<class Attributes, class Element>
15 class SimpleVAO {
16
17 public:
18         SimpleVAO()
19         : vao(0)
20         , buffers{0} {
21                 glGenVertexArrays(1, &vao);
22                 glGenBuffers(2, buffers);
23         }
24         ~SimpleVAO() noexcept {
25                 glDeleteBuffers(2, buffers);
26                 glDeleteVertexArrays(1, &vao);
27         }
28
29         SimpleVAO(const SimpleVAO &) = delete;
30         SimpleVAO &operator =(const SimpleVAO &) = delete;
31
32 public:
33         void Bind() const noexcept {
34                 glBindVertexArray(vao);
35         }
36         void Unbind() const noexcept {
37                 glBindVertexArray(0);
38         }
39         void BindAttributes() const noexcept {
40                 glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
41         }
42         void BindElements() const noexcept {
43                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
44         }
45         void EnableAttribute(GLuint index) noexcept {
46                 glEnableVertexAttribArray(index);
47         }
48         void DisableAttribute(GLuint index) noexcept {
49                 glDisableVertexAttribArray(index);
50         }
51         template<class Attribute>
52         void AttributePointer(GLuint index, bool normalized, std::size_t offset) noexcept {
53                 glVertexAttribPointer(
54                         index,
55                         gl_traits<Attribute>::size,
56                         gl_traits<Attribute>::type,
57                         normalized,
58                         sizeof(Attributes),
59                         reinterpret_cast<const void *>(offset)
60                 );
61         }
62         void ReserveAttributes(std::size_t size, GLenum usage) noexcept {
63                 glBufferData(GL_ARRAY_BUFFER, size * sizeof(Attributes), nullptr, usage);
64         }
65         MappedBuffer<Attributes> MapAttributes(GLenum access) {
66                 return MappedBuffer<Attributes>(GL_ARRAY_BUFFER, access);
67         }
68         void ReserveElements(std::size_t size, GLenum usage) noexcept {
69                 glBufferData(GL_ELEMENT_ARRAY_BUFFER, size * sizeof(Element), nullptr, usage);
70         }
71         MappedBuffer<Element> MapElements(GLenum access) {
72                 return MappedBuffer<Element>(GL_ELEMENT_ARRAY_BUFFER, access);
73         }
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);
76         }
77
78 private:
79         GLuint vao;
80         GLuint buffers[2];
81
82 };
83
84 }
85
86 #endif