src/scene.c

Sun, 06 Oct 2024 19:43:08 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 06 Oct 2024 19:43:08 +0200
changeset 73
cfa1d05754ac
parent 72
84472fb3adbd
child 75
0ce353485509
permissions
-rw-r--r--

update to recent snapshot of ucx 3.1

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright 2023 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/scene.h"

#include "ascension/context.h"
#include "ascension/utils.h"

#include <cx/linked_list.h>
#include <cx/array_list.h>
#include <cx/utils.h>

#include "ascension/shader.h"
#include <GL/glew.h>

#include <assert.h>

// TODO: move to a sprite.c
static void asc_sprite_draw(AscSprite const *node) {
    // Obtain shader
    AscShaderSprite *shader = ASC_SHADER_SPRITE;

    // Upload model matrix
    glUniformMatrix4fv(shader->program.model, 1,
                       GL_FALSE, node->data.world_transform);

    // Bind texture
    asc_texture_bind(&node->tex, shader->tex, 0);

    // Apply depth
    glUniform1f(shader->depth, (float)(node->data.depth));

    // Draw mesh
    asc_primitives_draw_plane();
}

void asc_scene_draw(AscScene scene, asc_recti viewport, AscCamera *camera) {
    // create render groups
    CxList *render_group[ASC_RENDER_GROUP_COUNT];
    cx_for_n(i, ASC_RENDER_GROUP_COUNT) {
        render_group[i] = cxArrayListCreateSimple(CX_STORE_POINTERS, 32);
    }

    // skip the root node deliberately, we know it's just the container
    CxTreeVisitor iter = cxTreeVisit(scene);
    cxIteratorNext(iter);

    // update the children and add them to the render groups
    cx_foreach(AscSceneNode*, node, iter) {
        node->depth = iter.depth;

        // skip hidden nodes (and all their children)
        if (asc_test_flag(node->flags, ASC_SCENE_NODE_HIDDEN)) {
            cxTreeVisitorContinue(iter);
        }

        // execute behaviors, first
        if (node->behaviors != NULL) {
            CxIterator behavior_iter = cxListIterator(node->behaviors);
            cx_foreach(asc_scene_update_func, behavior, behavior_iter) {
                behavior(node);
            }
        }

        // TODO: implement culling

        // check if geometry needs update
        asc_clear_flag(node->flags,
                       ASC_SCENE_NODE_GRAPHICS_UPDATED
                       | ASC_SCENE_NODE_TRANSFORM_UPDATED);
        if (asc_test_flag(node->flags, ASC_SCENE_NODE_UPDATE_GRAPHICS)) {
            asc_set_flag(node->flags, ASC_SCENE_NODE_GRAPHICS_UPDATED);
            asc_clear_flag(node->flags, ASC_SCENE_NODE_UPDATE_GRAPHICS);
            assert(node->update_func != NULL);
            node->update_func(node);
        }
        if (asc_test_flag(node->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM)) {
            asc_set_flag(node->flags, ASC_SCENE_NODE_TRANSFORM_UPDATED);
            asc_clear_flag(node->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM);
            asc_transform_from_parts(
                    node->transform,
                    node->position,
                    node->scale,
                    node->rotation
            );
            asc_mat4f_mulst(
                    node->world_transform,
                    node->transform,
                    asc_scene_node_parent(node)->world_transform
            );
        }

        // add to render group
        cxListAdd(render_group[node->render_group], node);
    }

    // set the viewport (in OpenGL we need to invert the Y axis)
    glViewport(
            viewport.pos.x,
            -viewport.pos.y,
            viewport.size.width,
            viewport.size.height
    );

    // -------------------------
    // process the render groups
    // -------------------------
    AscShaderProgram *shader;
    CxIterator render_iter;

    // 2D Elements
    // ===========
    glEnable(GL_DEPTH_TEST);
    glClear(GL_DEPTH_BUFFER_BIT);

    // Sprites
    // -------
    // TODO: implement view matrix for 2D worlds
    shader = &asc_active_window->glctx.shader.sprite.program;
    glUseProgram(shader->id);
    glUniformMatrix4fv(shader->projection, 1,
                       GL_FALSE, camera->projection);

    // render opaque sprites from front to back
    glDisable(GL_BLEND);
    render_iter = cxListBackwardsIterator(render_group[ASC_RENDER_GROUP_SPRITE_OPAQUE]);
    cx_foreach(AscSprite const *, node, render_iter) {
        asc_sprite_draw(node);
    }

    // render sprites with alpha value from back to front
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    render_iter = cxListIterator(render_group[ASC_RENDER_GROUP_SPRITE_BLEND]);
    cx_foreach(AscSprite const *, node, render_iter) {
        asc_sprite_draw(node);
    }

    // destroy render groups
    cx_for_n(i, ASC_RENDER_GROUP_COUNT) {
        cxListDestroy(render_group[i]);
    }
}

AscSceneNode *asc_scene_node_empty(void) {
    AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
    node->scale.x = node->scale.y = node->scale.z = 1;
    asc_transform_identity(node->transform);
    asc_transform_identity(node->world_transform);
    return node;
}

void asc_scene_node_destructor(AscSceneNode *node) {
    if (node->behaviors != NULL) {
        cxListDestroy(node->behaviors);
    }
    if (node->free_func != NULL) {
        node->free_func(node);
    } else {
        free(node);
    }
}

AscScene asc_scene_create(void) {
    AscSceneNode *root = asc_scene_node_empty();
    AscScene scene = cxTreeCreateWrapped(
            cxDefaultAllocator, root,
            cx_tree_node_base_layout
    );
    root->scene = scene;
    scene->simple_destructor = (cx_destructor_func) asc_scene_node_destructor;
    return scene;
}

void asc_scene_destroy(AscScene scene) {
    cxTreeDestroy(scene);
}

void asc_scene_node_add(AscSceneNode * restrict parent, AscSceneNode * restrict node) {
    assert(node->scene == NULL || node->scene == parent->scene);
    cxTreeSetParent(parent->scene, parent, node);
    CxTreeVisitor visitor = cxTreeVisitSubtree(parent->scene, node);
    cx_foreach(AscSceneNode *, n, visitor) {
        n->scene = parent->scene;
    }
    asc_node_update_transform(node);
}

void asc_scene_node_remove(AscSceneNode *node) {
    if (node->scene == NULL) return;
    cxTreeRemoveSubtree(node->scene, node);
    CxTreeVisitor visitor = cxTreeVisitSubtree(node->scene, node);
    cx_foreach(AscSceneNode *, n, visitor) {
        n->scene = NULL;
    }
}

void asc_scene_add_behavior(
        AscSceneNode *node,
        asc_scene_update_func behavior
) {
    if (node->behaviors == NULL) {
        node->behaviors = cxLinkedListCreateSimple(CX_STORE_POINTERS);
    }
    cxListAdd(node->behaviors, behavior);
}

void asc_scene_remove_behavior(
        AscSceneNode *node,
        asc_scene_update_func behavior
) {
    if (node->behaviors != NULL) {
        cxListFindRemove(node->behaviors, behavior);
    }
}

void asc_node_update(AscSceneNode *node) {
    asc_set_flag(node->flags, ASC_SCENE_NODE_UPDATE_GRAPHICS);
}

void asc_node_update_transform(AscSceneNode *node) {
    // fast skip if node is already marked
    if (asc_test_flag(node->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM)) {
        return;
    }

    // if node is not part of a scene, just mark it and return
    if (node->scene == NULL) {
        asc_set_flag(node->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM);
        return;
    }

    // creating the iterator this way saves us from needing scene as parameter
    CxTreeIterator iter = cxTreeIterateSubtree(node->scene, node, false);
    cx_foreach(AscSceneNode*, n, iter) {
        if (asc_test_flag(n->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM)) {
            cxTreeIteratorContinue(iter);
        }
        asc_set_flag(n->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM);
    }
}

mercurial