src/2d.c

Tue, 17 Jun 2025 20:11:53 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 17 Jun 2025 20:11:53 +0200
changeset 158
f650994ec543
parent 151
42960d0c879b
permissions
-rw-r--r--

implement rounded corners

for the time being this should be enough to close issue #384
later we add anti-aliasing, glow effects, etc.

/*
 * 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/2d.h"

#include "ascension/constants.h"
#include "ascension/context.h"
#include "ascension/error.h"
#include "ascension/shader.h"

#include <GL/glew.h>

typedef struct asc_rectangle_shader_s {
    AscShaderProgram program;
    GLint color;
    GLint size;
    GLint thickness;
    GLint radius;
} AscRectangleShader;

#define ASC_RECTANGLE_SHADER_FLAG_FILL  1
#define ASC_RECTANGLE_SHADER_FLAG_ROUND 2

static AscShaderProgram *asc_rectangle_shader_create(int flags) {
    AscShaderCodes codes;
    const char * const defines[] = {
        "",
        "#define FILL",
        "#define ROUNDED_CORNERS",
        "#define FILL\n#define ROUNDED_CORNERS",
    };
    if (asc_shader_load_code_files((AscShaderCodeInfo){
        .files.vtx = "sprite_vtx.glsl",
        .files.frag = "rectangle_frag.glsl",
        .defines.frag = defines[flags],
    }, &codes)) {
        asc_error("Loading sprite shader failed.");
        return NULL;
    }
    AscRectangleShader *shader = asc_shader_create(codes, sizeof(*shader));
    if (asc_has_error()) {
        asc_shader_free_codes(codes);
        return NULL;
    }
    shader->color = glGetUniformLocation(shader->program.gl_id, "color");
    shader->size = glGetUniformLocation(shader->program.gl_id, "size");
    if (asc_test_flag(flags, ASC_RECTANGLE_SHADER_FLAG_FILL)) {
        shader->thickness = -1;
    } else {
        shader->thickness = glGetUniformLocation(shader->program.gl_id, "thickness");
    }
    if (asc_test_flag(flags, ASC_RECTANGLE_SHADER_FLAG_ROUND)) {
        shader->radius = glGetUniformLocation(shader->program.gl_id, "radius");
    } else {
        shader->radius = -1;
    }
    asc_shader_free_codes(codes);

    asc_error_catch_all_gl();

    return (AscShaderProgram*) shader;
}

static void asc_rectangle_destroy(AscSceneNode *node) {
    asc_ptr_cast(AscRectangle, rectangle, node);
    asc_mesh_destroy(&rectangle->mesh);
}

static void asc_rectangle_update(AscSceneNode *node) {
    asc_ptr_cast(AscRectangle, rectangle, node);
    asc_vec2f size = asc_vec2f_new(rectangle->width, rectangle->height);
    asc_mesh_plane_2d(&rectangle->mesh, .size = size, .uv_scale = size);
}

static void asc_rectangle_draw(const AscCamera *camera, const AscSceneNode *node) {
    asc_ptr_cast(AscRectangle, rectangle, node);
    const bool filled = asc_test_flag(rectangle->flags, ASC_RECTANGLE_FILLED);
    const bool round = rectangle->radius > 0;

    // Compute shader flags
    int shader_flags = 0;
    if (filled) shader_flags |= ASC_RECTANGLE_SHADER_FLAG_FILL;
    if (round) shader_flags |= ASC_RECTANGLE_SHADER_FLAG_ROUND;

    // Compute shader ID
    const int shader_ids[] = {
        ASC_SHADER_RECTANGLE_DRAW,
        ASC_SHADER_RECTANGLE_FILL,
        ASC_SHADER_RECTANGLE_DRAW_ROUND,
        ASC_SHADER_RECTANGLE_FILL_ROUND,
    };

    // Look up and activate shader
    const AscRectangleShader *shader = asc_shader_lookup_or_create(
        shader_ids[shader_flags], asc_rectangle_shader_create, shader_flags);
    asc_shader_use(&shader->program, camera);

    // Upload uniforms
    // TODO: uploading model matrix could be a helper function
    glUniformMatrix4fv(shader->program.model, 1,
                       GL_FALSE, node->world_transform);

    glUniform4f(shader->color,
        rectangle->color.red,
        rectangle->color.green,
        rectangle->color.blue,
        rectangle->color.alpha
    );
    glUniform2f(shader->size, rectangle->width, rectangle->height);

    if (!filled) {
        glUniform1f(shader->thickness, rectangle->thickness);
    }
    if (round) {
        glUniform1f(shader->radius, rectangle->radius);
    }

    // Draw mesh
    asc_mesh_draw_triangle_strip(&rectangle->mesh);
}

AscSceneNode *asc_rectangle_create(struct asc_rectangle_create_args args) {
    AscRectangle *rectangle = cxZallocDefault(sizeof(AscRectangle));

    if (args.bounds.size.width + args.bounds.size.height > 0) {
        rectangle->node.position.x = (float) args.bounds.pos.x;
        rectangle->node.position.y = (float) args.bounds.pos.y;
        rectangle->width = (float) args.bounds.size.width;
        rectangle->height = (float) args.bounds.size.height;
    } else {
        rectangle->node.position.x = (float) args.x;
        rectangle->node.position.y = (float) args.y;
        rectangle->width = (float) args.width;
        rectangle->height = (float) args.height;
    }

    rectangle->thickness = ASC_NONZERO_OR(1.f, args.thickness);
    rectangle->radius = (float)args.radius;
    rectangle->color = asc_col_itof(asc_context.ink);

    if (args.filled) {
        asc_set_flag(rectangle->flags, ASC_RECTANGLE_FILLED);
    }

    AscSceneNode *node = &rectangle->node;
    node->position.z = ASC_SCENE_2D_DEPTH_OFFSET;
    node->scale = asc_vec3f_one;
    node->render_group = asc_context.ink.alpha < 255
                             ? ASC_RENDER_GROUP_2D_BLEND
                             : ASC_RENDER_GROUP_2D_OPAQUE;
    node->update_func = asc_rectangle_update;
    node->destroy_func = asc_rectangle_destroy;
    node->draw_func = asc_rectangle_draw;
    asc_node_update(node);
    return node;
}

mercurial