-#include <stdlib.h>
#include <stddef.h>
+#include <stdlib.h>
#include "client/mesh.h"
-Mesh *mesh_create()
-{
- Mesh *mesh = malloc(sizeof(Mesh));
- mesh->VAO = mesh->VBO = 0;
- mesh->free_textures = false;
- mesh->free_vertices = false;
-
- return mesh;
-}
-
-void mesh_delete(Mesh *mesh)
+// upload data to GPU (only done once)
+void mesh_upload(Mesh *mesh)
{
- if (mesh->textures && mesh->free_textures)
- free(mesh->textures);
-
- if (mesh->vertices && mesh->free_vertices)
- free(mesh->vertices);
-
- if (mesh->VAO)
- glDeleteVertexArrays(1, &mesh->VAO);
+ glGenVertexArrays(1, &mesh->vao);
+ glGenBuffers(1, &mesh->vbo);
- if (mesh->VBO)
- glDeleteBuffers(1, &mesh->VAO);
+ glBindVertexArray(mesh->vao);
+ glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
- free(mesh);
-}
-
-void mesh_configure(Mesh *mesh)
-{
- glGenVertexArrays(1, &mesh->VAO);
- glGenBuffers(1, &mesh->VBO);
+ glBufferData(GL_ARRAY_BUFFER, mesh->count * mesh->layout->size,
+ mesh->data, GL_STATIC_DRAW);
- glBindVertexArray(mesh->VAO);
- glBindBuffer(GL_ARRAY_BUFFER, mesh->VBO);
+ size_t offset = 0;
+ for (GLuint i = 0; i < mesh->layout->count; i++) {
+ VertexAttribute *attrib = &mesh->layout->attributes[i];
- glBufferData(GL_ARRAY_BUFFER, mesh->vertices_count * mesh->layout->size, mesh->vertices, GL_STATIC_DRAW);
+ glVertexAttribPointer(i, attrib->length, attrib->type, GL_FALSE,
+ mesh->layout->size, (GLvoid *) offset);
+ glEnableVertexAttribArray(i);
- vertex_layout_configure(mesh->layout);
+ offset += attrib->size;
+ }
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
- if (mesh->free_vertices)
- free(mesh->vertices);
+ if (mesh->free_data)
+ free(mesh->data);
- mesh->vertices = NULL;
+ mesh->data = NULL;
}
void mesh_render(Mesh *mesh)
{
- if (mesh->vertices)
- mesh_configure(mesh);
+ if (mesh->data)
+ mesh_upload(mesh);
+
+ // render
+ glBindVertexArray(mesh->vao);
+ glDrawArrays(GL_TRIANGLES, 0, mesh->count);
+}
+
+void mesh_destroy(Mesh *mesh)
+{
+ if (mesh->data && mesh->free_data)
+ free(mesh->data);
+
+ if (mesh->vao)
+ glDeleteVertexArrays(1, &mesh->vao);
- for (GLuint i = 0; i < mesh->textures_count; i++)
- glBindTextureUnit(i, mesh->textures[i]);
+ if (mesh->vbo)
+ glDeleteBuffers(1, &mesh->vbo);
- glBindVertexArray(mesh->VAO);
- glDrawArrays(GL_TRIANGLES, 0, mesh->vertices_count);
+ mesh->vao = mesh->vbo = 0;
}