src/scene.c

Fri, 29 Mar 2024 00:03:25 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 29 Mar 2024 00:03:25 +0100
changeset 46
d3285aed65b3
parent 45
18de2af03531
child 47
44457f6cb0a2
permissions
-rw-r--r--

make the timer have nanoseconds precision

/*
 * 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)
    );
}

static CxTreeVisitor asc_scene_node_visitor(AscSceneNode *node) {
    return cx_tree_visitor(node,
            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_sprites_opaque, 8);
    cx_array_initialize(scene->rg_sprites_blended, 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

#define asc_scene_draw_render_group_reversed(rg) \
    for(size_t i = rg##_size ; i > 0 ; i--) { \
        rg[i-1].draw(rg[i-1].node); \
    } (void)0

void asc_scene_draw(AscScene *scene) {
    // reset render groups
    // TODO: avoid recalculating the groups, if possible
    scene->rg_sprites_opaque_size = 0;
    scene->rg_sprites_blended_size = 0;

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

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

        // 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) {
            node->need_transform_update = false;
            asc_transform_from_parts(
                    node->transform,
                    node->position,
                    node->scale,
                    node->rotation
            );
            asc_mat4f_mulst(
                    node->world_transform,
                    node->transform,
                    node->parent->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_SPRITE_OPAQUE:
                    cx_array_simple_add(scene->rg_sprites_opaque, entry);
                    break;
                case ASC_RENDER_GROUP_SPRITE_BLEND:
                    cx_array_simple_add(scene->rg_sprites_blended, 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
    );
    glClear(GL_COLOR_BUFFER_BIT);

    // -----------------------------------------
    // process the render groups for each camera
    // -----------------------------------------
    AscShaderProgram *shader;
    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);

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

        // Sprites
        // -------
        // TODO: see if we can really always ignore the view matrix
        shader = &asc_context.active_window->glctx.shader.sprite.base;
        glUseProgram(shader->id);
        glUniformMatrix4fv(shader->projection, 1,
                           GL_FALSE, camera->projection);

        // render opaque sprites from front to back
        glDisable(GL_BLEND);
        asc_scene_draw_render_group_reversed(scene->rg_sprites_opaque);
        // render sprites with alpha value from back to front
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        asc_scene_draw_render_group(scene->rg_sprites_blended);
    }
}

AscSceneNode *asc_scene_node_empty(void) {
    AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
    node->free_func = (asc_scene_free_func) free;
    asc_transform_identity(node->transform);
    asc_transform_identity(node->world_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_update_transform(AscSceneNode *node) {
    if (node->need_transform_update) return;

    CxTreeIterator iter = asc_scene_node_iterator(node, false);
    cx_foreach(AscSceneNode*, n, iter) {
        // TODO: break/continue when subtree is already marked for update
        n->need_transform_update = true;
    }
}

mercurial