src/scene.c

Thu, 21 Mar 2024 20:34:33 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 21 Mar 2024 20:34:33 +0100
changeset 39
7cf310cc47cb
parent 38
6e5629ea4c5c
child 40
6c438be1a1fd
permissions
-rw-r--r--

minor improvements

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

#include "ascension/context.h"

#include <cx/tree.h>
#include <cx/utils.h>

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

#include <assert.h>

static CxTreeIterator asc_scene_node_iterator(
        AscSceneNode *node,
        bool visit_on_exit
) {
    return cx_tree_iterator(
            node, visit_on_exit,
            offsetof(AscSceneNode, children),
            offsetof(AscSceneNode, next)
    );
}

void asc_scene_init(AscScene *scene) {
    if (scene->root != NULL) {
        asc_error("Scene is already initialized.");
        return;
    }

    // zero everything, first
    memset(scene, 0, sizeof(AscScene));

    // default viewport is the entire viewport of the active window
    scene->viewport.size = asc_context.active_window->dimensions;

    // create the root node
    scene->root = asc_scene_node_empty();

    // initialize the render groups
    cx_array_initialize(scene->rg_none, 8);
    cx_array_initialize(scene->rg_fonts, 8);
}

void asc_scene_destroy(AscScene *scene) {
    asc_scene_node_free(scene->root);
}

void asc_scene_add(AscScene *scene, AscSceneNode *node) {
    asc_scene_node_link(scene->root, node);
    asc_node_update(node);
}

#define asc_scene_draw_render_group(rg) \
    cx_for_n(i, rg##_size) { \
        rg[i].draw(rg[i].node); \
    } (void)0

void asc_scene_draw(AscScene *scene) {
    // reset render groups
    scene->rg_none_size = 0;
    scene->rg_fonts_size = 0;

    // skip the root node deliberately, we know it's just the container
    CxTreeIterator iter = asc_scene_node_iterator(scene->root, false);
    cxIteratorNext(iter);

    // update the children and add them to the render groups
    cx_foreach(AscSceneNode*, node, iter) {
        // execute behaviors, first
        AscBehaviorNode *behavior = node->behaviors;
        while (behavior) {
            behavior->func(node);
            behavior = behavior->next;
        }

        // check if geometry needs update
        if (node->need_graphics_update) {
            assert(node->update_func != NULL);
            node->need_graphics_update = false;
            node->update_func(node);
        }
        if (node->need_transform_update) {
            assert(node->transform_update_func != NULL);
            node->need_transform_update = false;
            asc_transform_identity(node->local_transform);
            asc_transform_copy(node->world_transform, node->parent->world_transform);
            node->transform_update_func(node);
            asc_mat4f_mulst(node->final_transform, node->local_transform, node->world_transform);
        }

        // add to render group
        if (node->draw_func != NULL) {
            struct asc_render_group_entry entry = {
                    node->draw_func, node
            };
            switch (node->render_group) {
                case ASC_RENDER_GROUP_NONE:
                    cx_array_simple_add(scene->rg_none, entry);
                    break;
                case ASC_RENDER_GROUP_FONTS:
                    cx_array_simple_add(scene->rg_fonts, entry);
                    break;
            }
        }
    }

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

    // -----------------------------------------
    // process the render groups for each camera
    // -----------------------------------------
    cx_for_n(cam_id, ASC_SCENE_CAMERAS_MAX) {
        // update camera parameters, first
        AscCamera *camera = &scene->cameras[cam_id];
        if (camera->update == NULL) continue;
        camera->update(camera);

        // for the NONE group, the draw func is expected to do everything
        glEnable(GL_DEPTH_TEST);
        glDisable(GL_BLEND);
        asc_scene_draw_render_group(scene->rg_none);

        // draw the FONTS group
        glDisable(GL_DEPTH_TEST);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        // TODO: see if we can really always ignore the view matrix
        // TODO: compute render order for alpha blending to work correctly
        glUseProgram(ASC_SHADER_FONT.base.id);
        glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1,
                           GL_FALSE, camera->projection);
        asc_scene_draw_render_group(scene->rg_fonts);
    }
}

AscSceneNode *asc_scene_node_empty(void) {
    AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
    node->free_func = (asc_scene_free_func) free;
    asc_transform_identity(node->local_transform);
    asc_transform_identity(node->world_transform);
    asc_transform_identity(node->final_transform);
    return node;
}

void asc_scene_node_free(AscSceneNode *node) {
    if (node == NULL) return;

    // remove this node from its parent
    asc_scene_node_unlink(node);

    // free the entire subtree
    CxTreeIterator iter = asc_scene_node_iterator(node, true);
    cx_foreach(AscSceneNode*, child, iter) {
        if (!iter.exiting) continue;
        if (child->free_func != NULL) {
            child->free_func(child);
        } else {
            free(child);
        }
    }
}

void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) {
    cx_tree_link(
            parent, node,
            offsetof(AscSceneNode, parent),
            offsetof(AscSceneNode, children),
            offsetof(AscSceneNode, prev),
            offsetof(AscSceneNode, next)
    );
}

void asc_scene_node_unlink(AscSceneNode *node) {
    cx_tree_unlink(
            node,
            offsetof(AscSceneNode, parent),
            offsetof(AscSceneNode, children),
            offsetof(AscSceneNode, prev),
            offsetof(AscSceneNode, next)
    );
}

AscBehaviorNode *asc_scene_add_behavior(AscSceneNode *node, asc_scene_update_func behavior) {
    AscBehaviorNode *behavior_node = calloc(1, sizeof(AscBehaviorNode));
    behavior_node->func = behavior;
    cx_tree_link(
            node,
            behavior_node,
            offsetof(AscBehaviorNode, parent),
            offsetof(AscSceneNode, behaviors),
            offsetof(AscBehaviorNode, prev),
            offsetof(AscBehaviorNode, next)
    );
    return behavior_node;
}

void asc_scene_remove_behavior(AscBehaviorNode *node) {
    cx_tree_unlink(
            node,
            offsetof(AscBehaviorNode, parent),
            offsetof(AscSceneNode, behaviors),
            offsetof(AscBehaviorNode, prev),
            offsetof(AscBehaviorNode, next)
    );
}

void asc_node_update_transform(AscSceneNode *node) {
    CxTreeIterator iter = asc_scene_node_iterator(node, false);
    cx_foreach(AscSceneNode*, n, iter) {
        n->need_transform_update = true;
    }
}

mercurial