3 #include "client/mesh.h"
5 // upload data to GPU (only done once)
6 void mesh_upload(Mesh *mesh)
8 glGenVertexArrays(1, &mesh->vao);
9 glGenBuffers(1, &mesh->vbo);
11 glBindVertexArray(mesh->vao);
12 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
14 glBufferData(GL_ARRAY_BUFFER, mesh->count * mesh->layout->size,
15 mesh->data, GL_STATIC_DRAW);
18 for (GLuint i = 0; i < mesh->layout->count; i++) {
19 VertexAttribute *attrib = &mesh->layout->attributes[i];
21 glVertexAttribPointer(i, attrib->length, attrib->type, GL_FALSE,
22 mesh->layout->size, (GLvoid *) offset);
23 glEnableVertexAttribArray(i);
25 offset += attrib->size;
28 glBindBuffer(GL_ARRAY_BUFFER, 0);
37 void mesh_render(Mesh *mesh)
43 glBindVertexArray(mesh->vao);
44 glDrawArrays(GL_TRIANGLES, 0, mesh->count);
47 void mesh_destroy(Mesh *mesh)
49 if (mesh->data && mesh->free_data)
53 glDeleteVertexArrays(1, &mesh->vao);
56 glDeleteBuffers(1, &mesh->vbo);
58 mesh->vao = mesh->vbo = 0;