Tue, 23 Jan 2024 21:34:12 +0100
bring first scene graph to live
21
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * Copyright 2023 Mike Becker. All rights reserved. |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
4 | * |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * Redistribution and use in source and binary forms, with or without |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | * modification, are permitted provided that the following conditions are met: |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | * |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | * 1. Redistributions of source code must retain the above copyright |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | * notice, this list of conditions and the following disclaimer. |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | * |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | * notice, this list of conditions and the following disclaimer in the |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | * documentation and/or other materials provided with the distribution. |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | * |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
25 | * POSSIBILITY OF SUCH DAMAGE. |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
26 | */ |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | #include "ascension/scene.h" |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
29 | #include "ascension/error.h" |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
31 | #include <cx/tree.h> |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
32 | |
21
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | #include <assert.h> |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
34 | |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
35 | #define asc_scene_node_layout \ |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
36 | offsetof(AscSceneNode, parent), offsetof(AscSceneNode, children), \ |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
37 | offsetof(AscSceneNode, prev), offsetof(AscSceneNode, next) |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
38 | |
21
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
39 | void asc_scene_init(AscScene *scene) { |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | if (scene->root != NULL) { |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | asc_error("Scene is already initialized."); |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | return; |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | } |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
44 | scene->root = asc_scene_node_empty(); |
21
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | } |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | void asc_scene_destroy(AscScene *scene) { |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | asc_scene_node_free(scene->root); |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | } |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
51 | void asc_scene_add(AscScene *scene, AscSceneNode *node) { |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
52 | asc_scene_node_link(scene->root, node); |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
53 | } |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
54 | |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
55 | static void asc_scene_draw_node(AscSceneNode *node) { |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
56 | if (node->draw_func != NULL) { |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
57 | node->draw_func(node); |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
58 | } |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
59 | if (node->children != NULL) { |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
60 | asc_scene_draw_node(node->children); |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
61 | } |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
62 | if (node->next != NULL) { |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
63 | asc_scene_draw_node(node->next); |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
64 | } |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
65 | } |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
66 | |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
67 | void asc_scene_draw(AscScene const *scene) { |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
68 | // TODO: replace with UCX tree visitor |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
69 | // TODO: don't visit the tree, visit the render groups |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
70 | // TODO: avoid recursion |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
71 | asc_scene_draw_node(scene->root); |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
72 | } |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
73 | |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
74 | AscSceneNode *asc_scene_node_empty(void) { |
27
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
75 | // TODO: check if this can remain a calloc or if it's too expensive |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
76 | AscSceneNode *node = calloc(1, sizeof(AscSceneNode)); |
21
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | assert(node != NULL); |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
78 | node->free_func = (asc_scene_free_func) free; |
21
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | return node; |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | } |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | void asc_scene_node_free(AscSceneNode *node) { |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | if (node == NULL) return; |
27
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
84 | |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
85 | // TODO: replace with UCX tree visitor |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
86 | // TODO: avoid recursion |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
87 | |
27
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
88 | // free the children recursively |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
89 | while (node->children != NULL) { |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
90 | asc_scene_node_free(node->children); |
21
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
91 | } |
27
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
92 | |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
93 | // remove this node from its parent |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
94 | asc_scene_node_unlink(node); |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
95 | |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
96 | // free the node |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
97 | if (node->free_func != NULL) { |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
98 | node->free_func(node); |
21
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
99 | } |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
100 | } |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
101 | |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
102 | void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) { |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
103 | cx_tree_link(parent, node, asc_scene_node_layout); |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
104 | } |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
105 | |
21
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 | void asc_scene_node_unlink(AscSceneNode *node) { |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
107 | cx_tree_unlink(node, asc_scene_node_layout); |
21
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
108 | } |