summaryrefslogtreecommitdiff
path: root/src/mesh/mesh.c
blob: 1889bd70b78e4449ad7a8e1ca7e4f1c1256de7e3 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "mesh.h"

void honey_setup_mesh(lua_State* L)
{
    honey_lua_element mesh_elements[] = {
        { "new", HONEY_FUNCTION, { .function = honey_mesh_new } },
        { "draw", HONEY_FUNCTION, { .function = honey_mesh_draw } },
        { "delete", HONEY_FUNCTION, { .function = honey_mesh_delete } },
    };

    honey_lua_create_table(L, mesh_elements, 3);
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

int honey_mesh_new(lua_State* L)
{
    float* vertices;
    unsigned int* indices;
    int n_vertices, n_indices;
    honey_lua_parse_arguments(L, 5,
                              HONEY_USERDATA, &vertices,
                              HONEY_USERDATA, &indices,
                              HONEY_TABLE,
                              HONEY_INTEGER, &n_vertices,
                              HONEY_INTEGER, &n_indices);

    size_t n_attributes = lua_objlen(L, 3);
    unsigned int* attribute_sizes = malloc(n_attributes * sizeof(unsigned int));
    if (attribute_sizes == NULL) {
        lua_pushstring(L, "failed to allocate memory for vertex attributes");
        lua_error(L);
    }

    unsigned int vertex_size = 0;
    for (int i=0; i<n_attributes; i++) {
        lua_rawgeti(L, 2, i+1);
        attribute_sizes[i] = lua_tointeger(L, -1);
        vertex_size += attribute_sizes[i];
    }
    lua_pop(L, n_attributes);

    honey_mesh* mesh = lua_newuserdata(L, sizeof(honey_mesh));
    mesh->n_indices = n_indices;

    glGenVertexArrays(1, &mesh->vertex_array);
    glGenBuffers(1, &mesh->vertex_buffer);
    glGenBuffers(1, &mesh->element_buffer);

    glBindVertexArray(mesh->vertex_array);

    glBindBuffer(GL_ARRAY_BUFFER, mesh->vertex_buffer);
    glBufferData(GL_ARRAY_BUFFER,
                 vertex_size * n_vertices * sizeof(float),
                 vertices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->element_buffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                 n_indices * sizeof(unsigned int),
                 indices, GL_STATIC_DRAW);

    unsigned int offset = 0;
    for (int i=0; i<n_attributes; i++) {
        glEnableVertexAttribArray(i);
        glVertexAttribPointer(i,
                              attribute_sizes[i],
                              GL_FLOAT, GL_FALSE,
                              vertex_size * sizeof(float),
                              (void*) (offset * sizeof(float)));
        offset += attribute_sizes[i];
    }

    glBindVertexArray(0);

    free(attribute_sizes);
    
    return 1;
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

int honey_mesh_draw(lua_State* L)
{
    honey_mesh* mesh;
    int shader;
    honey_lua_parse_arguments(L, 2,
                              HONEY_USERDATA, &mesh,
                              HONEY_INTEGER, &shader);
    
    glUseProgram(shader);
  
    glBindVertexArray(mesh->vertex_array);
    glDrawElements(GL_TRIANGLES, mesh->n_indices, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

int honey_mesh_delete(lua_State* L)
{
    honey_mesh* mesh;
    honey_lua_parse_arguments(L, 1, HONEY_USERDATA, &mesh);
    glDeleteVertexArrays(1, &(mesh->vertex_array));
    glDeleteBuffers(1, &(mesh->vertex_buffer));
    glDeleteBuffers(1, &(mesh->element_buffer));
}