Brenta Engine 1.2
Loading...
Searching...
No Matches
vao.cpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#include <brenta/renderer/opengl/vao.hpp>
7#include <brenta/logger.hpp>
8
9using namespace brenta;
10
11Vao::~Vao()
12{
13 this->destroy();
14 return;
15}
16
17void Vao::init()
18{
19 glGenVertexArrays(1, &this->id);
20 EVENT(Logger::Event::Lifetime, "Vao: initialized {}", this->id);
21 return;
22}
23
24void Vao::destroy()
25{
26 if (this->get_id() == 0) return;
27
28 glDeleteVertexArrays(1, &this->id);
29
30 EVENT(Logger::Event::Lifetime, "Vao: destroyed {}", this->id);
31 this->id = 0;
32 return;
33}
34
35void Vao::bind() const
36{
37 if (this->get_id() == 0)
38 {
39 ERROR("Vao::bind: not initialized");
40 return;
41 }
42 glBindVertexArray(this->get_id());
43 return;
44}
45
46void Vao::unbind() const
47{
48 glBindVertexArray(0);
49 return;
50}
51
52unsigned int Vao::get_id() const
53{
54 if (id == 0)
55 return 0;
56 return id;
57}
58
59void Vao::link_buffer(const Buffer &vbo,
60 unsigned int layout_index,
61 GLint components,
62 GLenum type,
63 GLboolean normalized,
64 GLsizei stride,
65 const void *offset)
66{
67 // Save current state
68 GLint old_vao, old_vbo;
69 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &old_vao);
70 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &old_vbo);
71
72 this->bind();
73 vbo.bind();
74 glVertexAttribPointer(layout_index, components, type,
75 normalized, stride, offset);
76 glEnableVertexAttribArray(layout_index);
77
78 // Restore state
79 glBindBuffer(GL_ARRAY_BUFFER, old_vbo);
80 glBindVertexArray(old_vao);
81 return;
82}
83