src/mesh.c

changeset 116
bfb2a7d62047
parent 115
e5f8c99b0987
--- a/src/mesh.c	Sat May 10 18:51:45 2025 +0200
+++ b/src/mesh.c	Sun May 11 14:51:00 2025 +0200
@@ -75,35 +75,45 @@
     free(mesh->vtx_data);
 }
 
-void asc_mesh_plane_2d(AscMesh *mesh) {
+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);
     }
+
+    // free any previous data
     free(mesh->vtx_data);
+
+    // default values
+    if (args.size.x == 0.0f) args.size.x = 1.0f;
+    if (args.size.y == 0.0f) args.size.y = 1.0f;
+    if (args.uv_scale.x == 0.0f) args.uv_scale.x = 1.0f;
+    if (args.uv_scale.y == 0.0f) args.uv_scale.y = 1.0f;
+
     asc_dprintf("Create plane in VBO %u and VAO %u", mesh->vbo, mesh->vao);
     mesh->vtx_count = 4;
     AscMeshVertex2d data[4] = {
         {
             .pos = {{0.0f, 0.0f}},
-            .uv = {{0.0f, 0.0f}},
+            .uv = {{args.uv_offset.x, args.uv_offset.y}},
         } , // bottom left
         {
-            .pos = {{0.0f, 1.0f}},
-            .uv = {{0.0f, 1.0f}},
+            .pos = {{0.0f, args.size.y}},
+            .uv = {{args.uv_offset.x, args.uv_offset.y + args.uv_scale.y}},
         }, // top left
         {
-            .pos = {{1.0f, 0.0f}},
-            .uv = {{1.0f, 0.0f}},
+            .pos = {{args.size.x, 0.0f}},
+            .uv = {{args.uv_offset.x + args.uv_scale.x, args.uv_offset.y}},
         }, // bottom right
         {
-            .pos = {{1.0f, 1.0f}},
-            .uv = {{1.0f, 1.0f}},
+            .pos = {{args.size.x, args.size.y}},
+            .uv = {{args.uv_offset.x + args.uv_scale.x, args.uv_offset.y + args.uv_scale.y}},
         } // top right
     };
     mesh->vtx_data = malloc(sizeof(data));
     memcpy(mesh->vtx_data, data, sizeof(data));
     glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
     glBufferData(GL_ARRAY_BUFFER, sizeof(data), 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(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, pos));
     glEnableVertexAttribArray(0);

mercurial