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