Sun, 11 May 2025 14:51:00 +0200
replace scaling with correct mesh sizes
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * Copyright 2025 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "ascension/error.h" #include "ascension/mesh.h" #include <GL/glew.h> void asc_mesh_allocate_buffers(AscMesh *mesh, unsigned count) { asc_dprintf("Allocate mesh buffers for %u meshes.", count); GLuint buffers[count]; GLuint arrays[count]; glGenBuffers(count, buffers); glGenVertexArrays(count, arrays); for (unsigned i = 0; i < count; i++) { mesh[i].vbo = buffers[i]; mesh[i].vao = arrays[i]; } asc_error_catch_all_gl(); } void asc_mesh_free_buffers(AscMesh *mesh, unsigned count) { if (count == 1 && mesh->vbo == 0) return; // hack to skip this function until we remove it asc_dprintf("Free mesh buffers for %u meshes.", count); GLuint buffers[count]; GLuint arrays[count]; for (unsigned i = 0; i < count; i++) { buffers[i] = mesh[i].vbo; arrays[i] = mesh[i].vao; mesh[i].vbo = 0; mesh[i].vao = 0; } glDeleteBuffers(count, buffers); glDeleteVertexArrays(count, arrays); asc_error_catch_all_gl(); } void asc_mesh_draw_triangle_strip(const AscMesh *mesh) { glBindVertexArray(mesh->vao); glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vtx_count); #ifndef NDEBUG // only unbind in debug mode to detect accidental re-use of the wrong VAO glBindVertexArray(0); #endif } void asc_mesh_destroy(AscMesh *mesh) { asc_mesh_free_buffers(mesh, 1); free(mesh->vtx_data); } 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 = {{args.uv_offset.x, args.uv_offset.y}}, } , // bottom left { .pos = {{0.0f, args.size.y}}, .uv = {{args.uv_offset.x, args.uv_offset.y + args.uv_scale.y}}, }, // top left { .pos = {{args.size.x, 0.0f}}, .uv = {{args.uv_offset.x + args.uv_scale.x, args.uv_offset.y}}, }, // bottom right { .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); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, uv)); glEnableVertexAttribArray(1); }