Mon, 12 May 2025 21:14:22 +0200
fix texture scaling
/* * 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); } unsigned required_memory = 4 * sizeof(AscMeshVertex2d); // free any previous data if (mesh->vtx_data && mesh->vtx_data_size < required_memory) { free(mesh->vtx_data); mesh->vtx_data = NULL; } // allocate memory AscMeshVertex2d *data; if (mesh->vtx_data == NULL) { asc_dprintf("Create plane in VBO %u and VAO %u", mesh->vbo, mesh->vao); mesh->vtx_data_size = required_memory; data = malloc(mesh->vtx_data_size); mesh->vtx_data = (float*) data; } else { data = (AscMeshVertex2d*) mesh->vtx_data; } mesh->vtx_count = 4; // default values args.size.x = ASC_NONZERO_OR(1.f, args.size.x); args.size.y = ASC_NONZERO_OR(1.f, args.size.y); args.uv_scale.x = ASC_NONZERO_OR(1.f, args.uv_scale.x); args.uv_scale.y = ASC_NONZERO_OR(1.f, args.uv_scale.y); // bottom left data[0].pos = asc_vec2f_new(0.0f, 0.0f); data[0].uv = asc_vec2f_new(args.uv_offset.x, args.uv_offset.y); // top left data[1].pos = asc_vec2f_new(0.0f, args.size.y); data[1].uv = asc_vec2f_new(args.uv_offset.x, args.uv_offset.y + args.uv_scale.y); // bottom right data[2].pos = asc_vec2f_new(args.size.x, 0.0f); data[2].uv = asc_vec2f_new(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y); // top right data[3].pos = asc_vec2f_new(args.size.x, args.size.y); data[3].uv = asc_vec2f_new(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(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, pos)); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, uv)); glEnableVertexAttribArray(1); }