src/scene.c

Tue, 26 Mar 2024 20:37:21 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 26 Mar 2024 20:37:21 +0100
changeset 44
b3da4096c607
parent 43
5a8c31904e44
child 45
18de2af03531
permissions
-rw-r--r--

create own compilation unit for GL context - fixes shader not being created per context

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  * Copyright 2023 Mike Becker. All rights reserved.
     4  *
     5  * Redistribution and use in source and binary forms, with or without
     6  * modification, are permitted provided that the following conditions are met:
     7  *
     8  *   1. Redistributions of source code must retain the above copyright
     9  *      notice, this list of conditions and the following disclaimer.
    10  *
    11  *   2. Redistributions in binary form must reproduce the above copyright
    12  *      notice, this list of conditions and the following disclaimer in the
    13  *      documentation and/or other materials provided with the distribution.
    14  *
    15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    25  * POSSIBILITY OF SUCH DAMAGE.
    26  */
    28 #include "ascension/scene.h"
    29 #include "ascension/error.h"
    31 #include "ascension/context.h"
    33 #include <cx/tree.h>
    34 #include <cx/utils.h>
    36 #include "ascension/shader.h"
    37 #include <GL/glew.h>
    39 #include <assert.h>
    41 static CxTreeIterator asc_scene_node_iterator(
    42         AscSceneNode *node,
    43         bool visit_on_exit
    44 ) {
    45     return cx_tree_iterator(
    46             node, visit_on_exit,
    47             offsetof(AscSceneNode, children),
    48             offsetof(AscSceneNode, next)
    49     );
    50 }
    52 static CxTreeVisitor asc_scene_node_visitor(AscSceneNode *node) {
    53     return cx_tree_visitor(node,
    54             offsetof(AscSceneNode, children),
    55             offsetof(AscSceneNode, next)
    56     );
    57 }
    59 void asc_scene_init(AscScene *scene) {
    60     if (scene->root != NULL) {
    61         asc_error("Scene is already initialized.");
    62         return;
    63     }
    65     // zero everything, first
    66     memset(scene, 0, sizeof(AscScene));
    68     // default viewport is the entire viewport of the active window
    69     scene->viewport.size = asc_context.active_window->dimensions;
    71     // create the root node
    72     scene->root = asc_scene_node_empty();
    74     // initialize the render groups
    75     cx_array_initialize(scene->rg_sprites_opaque, 8);
    76     cx_array_initialize(scene->rg_sprites_blended, 8);
    77 }
    79 void asc_scene_destroy(AscScene *scene) {
    80     asc_scene_node_free(scene->root);
    81 }
    83 void asc_scene_add(AscScene *scene, AscSceneNode *node) {
    84     asc_scene_node_link(scene->root, node);
    85     asc_node_update(node);
    86 }
    88 #define asc_scene_draw_render_group(rg) \
    89     cx_for_n(i, rg##_size) { \
    90         rg[i].draw(rg[i].node); \
    91     } (void)0
    93 #define asc_scene_draw_render_group_reversed(rg) \
    94     for(size_t i = rg##_size ; i > 0 ; i--) { \
    95         rg[i-1].draw(rg[i-1].node); \
    96     } (void)0
    98 void asc_scene_draw(AscScene *scene) {
    99     // reset render groups
   100     // TODO: avoid recalculating the groups, if possible
   101     scene->rg_sprites_opaque_size = 0;
   102     scene->rg_sprites_blended_size = 0;
   104     // skip the root node deliberately, we know it's just the container
   105     CxTreeVisitor iter = asc_scene_node_visitor(scene->root);
   106     cxIteratorNext(iter);
   108     // update the children and add them to the render groups
   109     cx_foreach(AscSceneNode*, node, iter) {
   110         node->depth = iter.depth;
   112         // execute behaviors, first
   113         AscBehaviorNode *behavior = node->behaviors;
   114         while (behavior) {
   115             behavior->func(node);
   116             behavior = behavior->next;
   117         }
   119         // check if geometry needs update
   120         if (node->need_graphics_update) {
   121             assert(node->update_func != NULL);
   122             node->need_graphics_update = false;
   123             node->update_func(node);
   124         }
   125         if (node->need_transform_update) {
   126             assert(node->transform_update_func != NULL);
   127             node->need_transform_update = false;
   128             asc_transform_identity(node->local_transform);
   129             asc_transform_copy(node->world_transform, node->parent->world_transform);
   130             node->transform_update_func(node);
   131             asc_mat4f_mulst(node->final_transform, node->local_transform, node->world_transform);
   132         }
   134         // add to render group
   135         if (node->draw_func != NULL) {
   136             struct asc_render_group_entry entry = {
   137                     node->draw_func, node
   138             };
   139             switch (node->render_group) {
   140                 case ASC_RENDER_GROUP_SPRITE_OPAQUE:
   141                     cx_array_simple_add(scene->rg_sprites_opaque, entry);
   142                     break;
   143                 case ASC_RENDER_GROUP_SPRITE_BLEND:
   144                     cx_array_simple_add(scene->rg_sprites_blended, entry);
   145                     break;
   146             }
   147         }
   148     }
   150     // set the viewport (in OpenGL we need to invert the Y axis)
   151     glViewport(
   152             scene->viewport.pos.x,
   153             -scene->viewport.pos.y,
   154             scene->viewport.size.width,
   155             scene->viewport.size.height
   156     );
   157     glClear(GL_COLOR_BUFFER_BIT);
   159     // -----------------------------------------
   160     // process the render groups for each camera
   161     // -----------------------------------------
   162     AscShaderProgram *shader;
   163     cx_for_n(cam_id, ASC_SCENE_CAMERAS_MAX) {
   164         // update camera parameters, first
   165         AscCamera *camera = &scene->cameras[cam_id];
   166         if (camera->update == NULL) continue;
   167         camera->update(camera);
   169         // 2D Elements
   170         // ===========
   171         glEnable(GL_DEPTH_TEST);
   172         glClear(GL_DEPTH_BUFFER_BIT);
   174         // Sprites
   175         // -------
   176         // TODO: see if we can really always ignore the view matrix
   177         shader = &asc_context.active_window->glctx.shader.sprite;
   178         glUseProgram(shader->id);
   179         glUniformMatrix4fv(shader->projection, 1,
   180                            GL_FALSE, camera->projection);
   182         // render opaque sprites from front to back
   183         glDisable(GL_BLEND);
   184         asc_scene_draw_render_group_reversed(scene->rg_sprites_opaque);
   185         // render sprites with alpha value from back to front
   186         glEnable(GL_BLEND);
   187         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   188         asc_scene_draw_render_group(scene->rg_sprites_blended);
   189     }
   190 }
   192 AscSceneNode *asc_scene_node_empty(void) {
   193     AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
   194     node->free_func = (asc_scene_free_func) free;
   195     asc_transform_identity(node->local_transform);
   196     asc_transform_identity(node->world_transform);
   197     asc_transform_identity(node->final_transform);
   198     return node;
   199 }
   201 void asc_scene_node_free(AscSceneNode *node) {
   202     if (node == NULL) return;
   204     // remove this node from its parent
   205     asc_scene_node_unlink(node);
   207     // free the entire subtree
   208     CxTreeIterator iter = asc_scene_node_iterator(node, true);
   209     cx_foreach(AscSceneNode*, child, iter) {
   210         if (!iter.exiting) continue;
   211         if (child->free_func != NULL) {
   212             child->free_func(child);
   213         } else {
   214             free(child);
   215         }
   216     }
   217 }
   219 void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) {
   220     cx_tree_link(
   221             parent, node,
   222             offsetof(AscSceneNode, parent),
   223             offsetof(AscSceneNode, children),
   224             offsetof(AscSceneNode, prev),
   225             offsetof(AscSceneNode, next)
   226     );
   227 }
   229 void asc_scene_node_unlink(AscSceneNode *node) {
   230     cx_tree_unlink(
   231             node,
   232             offsetof(AscSceneNode, parent),
   233             offsetof(AscSceneNode, children),
   234             offsetof(AscSceneNode, prev),
   235             offsetof(AscSceneNode, next)
   236     );
   237 }
   239 AscBehaviorNode *asc_scene_add_behavior(AscSceneNode *node, asc_scene_update_func behavior) {
   240     AscBehaviorNode *behavior_node = calloc(1, sizeof(AscBehaviorNode));
   241     behavior_node->func = behavior;
   242     cx_tree_link(
   243             node,
   244             behavior_node,
   245             offsetof(AscBehaviorNode, parent),
   246             offsetof(AscSceneNode, behaviors),
   247             offsetof(AscBehaviorNode, prev),
   248             offsetof(AscBehaviorNode, next)
   249     );
   250     return behavior_node;
   251 }
   253 void asc_scene_remove_behavior(AscBehaviorNode *node) {
   254     cx_tree_unlink(
   255             node,
   256             offsetof(AscBehaviorNode, parent),
   257             offsetof(AscSceneNode, behaviors),
   258             offsetof(AscBehaviorNode, prev),
   259             offsetof(AscBehaviorNode, next)
   260     );
   261 }
   263 void asc_node_update_transform(AscSceneNode *node) {
   264     CxTreeIterator iter = asc_scene_node_iterator(node, false);
   265     cx_foreach(AscSceneNode*, n, iter) {
   266         n->need_transform_update = true;
   267     }
   268 }

mercurial