summaryrefslogtreecommitdiff
path: root/prge_mesh.c
diff options
context:
space:
mode:
authorpryazha <pryadeiniv@mail.ru>2025-02-20 15:10:11 +0500
committerpryazha <pryadeiniv@mail.ru>2025-02-20 15:10:11 +0500
commit34821e9fefb0d7cbf9e72a2457b2901edbbe03bb (patch)
tree59ec69f7d19aa48091b3d39ad0dfd6b26bacb88b /prge_mesh.c
initial
Diffstat (limited to 'prge_mesh.c')
-rw-r--r--prge_mesh.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/prge_mesh.c b/prge_mesh.c
new file mode 100644
index 0000000..dc2c2da
--- /dev/null
+++ b/prge_mesh.c
@@ -0,0 +1,65 @@
+Vertex vertex(V3 pos)
+{
+ Vertex v = { pos };
+ return v;
+}
+
+void mesh_init_buffers(Mesh *mesh)
+{
+ Assert(mesh->verts);
+ Assert(mesh->nverts > 0);
+
+ glGenVertexArrays(1, &mesh->vao);
+ glBindVertexArray(mesh->vao);
+
+ glGenBuffers(1, &mesh->vbo);
+ glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
+ glBufferData(GL_ARRAY_BUFFER, mesh->nverts*sizeof(Vertex), mesh->verts, GL_STATIC_DRAW);
+
+ if (mesh->indices && (mesh->nindices > 0)) {
+ glGenBuffers(1, &mesh->ebo);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->ebo);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->nindices*sizeof(U32), mesh->indices, GL_STATIC_DRAW);
+ }
+
+ glEnableVertexAttribArray(PRGE_SHADER_POS_LOC);
+ glVertexAttribPointer(PRGE_SHADER_POS_LOC, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
+ glBindVertexArray(0);
+}
+
+Mesh *mesh(Arena *arena, Vertex *verts, U32 nverts, U32 *indices, U32 nindices)
+{
+ Mesh *mesh = arena_push(arena, sizeof(Mesh));
+ mesh->verts = verts;
+ mesh->nverts = nverts;
+ mesh->indices = indices;
+ mesh->nindices = nindices;
+ mesh_init_buffers(mesh);
+ return mesh;
+}
+
+void mesh_clear(Mesh *mesh)
+{
+ glBindVertexArray(mesh->vao);
+ glDisableVertexAttribArray(PRGE_SHADER_POS_LOC);
+
+ glBindVertexArray(0);
+ glDeleteVertexArrays(1, &mesh->vao);
+
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ glDeleteBuffers(1, &mesh->vbo);
+
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+ glDeleteBuffers(1, &mesh->ebo);
+}
+
+void mesh_draw(Mesh mesh)
+{
+ Assert(mesh.vao);
+ glBindVertexArray(mesh.vao);
+ if (mesh.ebo)
+ glDrawElements(GL_TRIANGLES, mesh.nindices, GL_UNSIGNED_INT, 0);
+ else
+ glDrawArrays(GL_TRIANGLES, 0, mesh.nverts);
+ glBindVertexArray(0);
+}