src/mesh.c

changeset 220
6b266e907f89
parent 208
658bccb1bf73
--- a/src/mesh.c	Tue Jul 22 20:57:13 2025 +0200
+++ b/src/mesh.c	Tue Jul 22 21:38:02 2025 +0200
@@ -32,7 +32,7 @@
 
 #include <GL/glew.h>
 
-void asc_mesh_allocate_buffers(AscMesh *mesh, unsigned count) {
+int asc_mesh_allocate_buffers(AscMesh *mesh, unsigned count) {
     asc_dprintf("Allocate mesh buffers for %u meshes.", count);
     GLuint buffers[count];
     GLuint arrays[count];
@@ -42,7 +42,7 @@
         mesh[i].vbo = buffers[i];
         mesh[i].vao = arrays[i];
     }
-    asc_error_catch_all_gl();
+    return asc_error_catch_all_gl();
 }
 
 void asc_mesh_free_buffers(AscMesh *mesh, unsigned count) {
@@ -79,8 +79,18 @@
 }
 
 void asc_mesh_init_plane_2d(AscMesh *mesh, struct asc_mesh_init_plane_2d_args args) {
-    if (mesh->vbo == 0) {
-        asc_mesh_allocate_buffers(mesh, 1);
+    if (mesh->vao == 0) {
+        if (asc_mesh_allocate_buffers(mesh, 1)) return;
+        // bind the buffer and configure the vertex attributes
+        glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
+        glBindVertexArray(mesh->vao);
+        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(asc_vertex2d), (void*)offsetof(asc_vertex2d, pos));
+        glEnableVertexAttribArray(0);
+        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(asc_vertex2d), (void*)offsetof(asc_vertex2d, uv));
+        glEnableVertexAttribArray(1);
+    } else {
+        // only bind the buffer for updating the data
+        glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
     }
 
     unsigned required_memory = 4 * sizeof(asc_vertex2d);
@@ -121,14 +131,8 @@
     // top right
     data[3].pos = ASC_VEC2F(args.size.x, args.size.y);
     data[3].uv = ASC_VEC2F(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y + args.uv_scale.y);
-    glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
     glBufferData(GL_ARRAY_BUFFER, mesh->vtx_data_size, mesh->vtx_data, GL_STATIC_DRAW);
-    // TODO: this should not be repeated for every adjustment - but it will be moved to the batch renderer anyway
-    glBindVertexArray(mesh->vao);
-    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(asc_vertex2d), (void*)offsetof(asc_vertex2d, pos));
-    glEnableVertexAttribArray(0);
-    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(asc_vertex2d), (void*)offsetof(asc_vertex2d, uv));
-    glEnableVertexAttribArray(1);
 
+    // TODO: replace with specific error handling for setting the buffer
     asc_error_catch_all_gl();
 }

mercurial