src/mesh.c

Sun, 20 Apr 2025 15:41:16 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 20 Apr 2025 15:41:16 +0200
changeset 88
6234b7ea48f3
parent 82
4e1e698f4b0d
child 89
e1f682a8a145
permissions
-rw-r--r--

add support for 2d textures in sprite shader - fixes #386

/*
* 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/mesh.h"
#include "ascension/error.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) {
    asc_dprintf("Free mesh buffers for %u meshes.", count);
    if (count > 32) {
        asc_error("Trying to free more than 32 mesh buffers at once.");
        count = 32;
    }
    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(AscMesh *mesh) {
    glBindVertexArray(mesh->vao);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vertices);
#ifndef NDEBUG
    // only unbind in debug mode to detect accidental re-use of the wrong VAO
    glBindVertexArray(0);
#endif
}

mercurial