Sat, 10 May 2025 18:51:45 +0200
refactor rendering 1/3 - create new mesh structs
/* * 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_plane_2d(AscMesh *mesh) { if (mesh->vbo == 0) { asc_mesh_allocate_buffers(mesh, 1); } free(mesh->vtx_data); 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}}, } , // bottom left { .pos = {{0.0f, 1.0f}}, .uv = {{0.0f, 1.0f}}, }, // top left { .pos = {{1.0f, 0.0f}}, .uv = {{1.0f, 0.0f}}, }, // bottom right { .pos = {{1.0f, 1.0f}}, .uv = {{1.0f, 1.0f}}, } // 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); 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); }