summary refs log tree commit diff
path: root/src/Binder.cpp
blob: 409c4efaf9857cad8b0beba66b87968e002fb1dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <GL/glew.h>
#include "Binder.hpp"
#include "Mesh.hpp"

namespace MC {

BindableMesh Binder::load(Mesh& mesh) {
    auto vao = create_vao();
    store_indices(mesh.raw_indices(), mesh.indices_size());
    store_in_attribute_list(0, 3, mesh.raw(), mesh.size() * 3);
    unbind_vao();

    return {vao, mesh.indices_size()};
}

uint32_t Binder::create_vao() {
    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    return static_cast<uint32_t>(vao);
}

uint32_t Binder::unbind_vao() {
    glBindVertexArray(0);
}

void Binder::store_indices(uint32_t* indices, size_t indices_size) {
    GLuint ebo;
    glGenBuffers(1, &ebo);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_size * sizeof(float), indices, GL_STATIC_DRAW);
}

void Binder::store_in_attribute_list(uint32_t attribute, size_t size, float* data, size_t data_size) {
    GLuint vbo;
    glGenBuffers(1, &vbo);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, data_size * sizeof(float), data, GL_STATIC_DRAW);
    glVertexAttribPointer(attribute, size, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);
}

void BindableMesh::bind() const {
    glBindVertexArray(m_vao);
    glEnableVertexAttribArray(0);
}

void BindableMesh::unbind() {
    glBindVertexArray(0);
    glDisableVertexAttribArray(0);
}

size_t BindableMesh::size() const {
    return m_vertex_count;
}

}