Thu, 21 Mar 2024 20:24:31 +0100
implement that nodes inherit the world transform of their parent
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 void asc_scene_init(AscScene *scene) {
53 if (scene->root != NULL) {
54 asc_error("Scene is already initialized.");
55 return;
56 }
58 // zero everything, first
59 memset(scene, 0, sizeof(AscScene));
61 // default viewport is the entire viewport of the active window
62 scene->viewport.size = asc_context.active_window->dimensions;
64 // create the root node
65 scene->root = asc_scene_node_empty();
67 // initialize the render groups
68 cx_array_initialize(scene->rg_none, 8);
69 cx_array_initialize(scene->rg_fonts, 8);
70 }
72 void asc_scene_destroy(AscScene *scene) {
73 asc_scene_node_free(scene->root);
74 }
76 void asc_scene_add(AscScene *scene, AscSceneNode *node) {
77 asc_scene_node_link(scene->root, node);
78 asc_node_update(node);
79 }
81 #define asc_scene_draw_render_group(rg) \
82 cx_for_n(i, rg##_size) { \
83 rg[i].draw(rg[i].node); \
84 } (void)0
86 void asc_scene_draw(AscScene *scene) {
87 // reset render groups
88 scene->rg_none_size = 0;
89 scene->rg_fonts_size = 0;
91 // skip the root node deliberately, we know it's just the container
92 CxTreeIterator iter = asc_scene_node_iterator(scene->root, false);
93 cxIteratorNext(iter);
95 // update the children and add them to the render groups
96 cx_foreach(AscSceneNode*, node, iter) {
97 // execute behaviors, first
98 AscBehaviorNode *behavior = node->behaviors;
99 while (behavior) {
100 behavior->func(node);
101 behavior = behavior->next;
102 }
104 // check if geometry needs update
105 if (node->need_full_update) {
106 assert(node->update_func != NULL);
107 node->need_full_update = false;
108 node->update_func(node);
109 }
110 if (node->need_transform_update) {
111 assert(node->transform_update_func != NULL);
112 node->need_transform_update = false;
113 asc_transform_identity(node->local_transform);
114 asc_transform_copy(node->world_transform, node->parent->world_transform);
115 node->transform_update_func(node);
116 asc_mat4f_mulst(node->final_transform, node->local_transform, node->world_transform);
117 }
119 // add to render group
120 if (node->draw_func != NULL) {
121 struct asc_render_group_entry entry = {
122 node->draw_func, node
123 };
124 switch (node->render_group) {
125 case ASC_RENDER_GROUP_NONE:
126 cx_array_simple_add(scene->rg_none, entry);
127 break;
128 case ASC_RENDER_GROUP_FONTS:
129 cx_array_simple_add(scene->rg_fonts, entry);
130 break;
131 }
132 }
133 }
135 // set the viewport (in OpenGL we need to invert the Y axis)
136 glViewport(
137 scene->viewport.pos.x,
138 -scene->viewport.pos.y,
139 scene->viewport.size.width,
140 scene->viewport.size.height
141 );
143 // -----------------------------------------
144 // process the render groups for each camera
145 // -----------------------------------------
146 cx_for_n(cam_id, ASC_SCENE_CAMERAS_MAX) {
147 // update camera parameters, first
148 AscCamera *camera = &scene->cameras[cam_id];
149 if (camera->update == NULL) continue;
150 camera->update(camera);
152 // for the NONE group, the draw func is expected to do everything
153 asc_scene_draw_render_group(scene->rg_none);
155 // draw the FONTS group
156 // TODO: see if we can really always ignore the view matrix
157 glUseProgram(ASC_SHADER_FONT.base.id);
158 glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1,
159 GL_FALSE, camera->projection);
160 asc_scene_draw_render_group(scene->rg_fonts);
161 }
162 }
164 AscSceneNode *asc_scene_node_empty(void) {
165 AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
166 node->free_func = (asc_scene_free_func) free;
167 asc_transform_identity(node->local_transform);
168 asc_transform_identity(node->world_transform);
169 asc_transform_identity(node->final_transform);
170 return node;
171 }
173 void asc_scene_node_free(AscSceneNode *node) {
174 if (node == NULL) return;
176 // remove this node from its parent
177 asc_scene_node_unlink(node);
179 // free the entire subtree
180 CxTreeIterator iter = asc_scene_node_iterator(node, true);
181 cx_foreach(AscSceneNode*, child, iter) {
182 if (!iter.exiting) continue;
183 if (child->free_func != NULL) {
184 child->free_func(child);
185 } else {
186 free(child);
187 }
188 }
189 }
191 void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) {
192 cx_tree_link(
193 parent, node,
194 offsetof(AscSceneNode, parent),
195 offsetof(AscSceneNode, children),
196 offsetof(AscSceneNode, prev),
197 offsetof(AscSceneNode, next)
198 );
199 }
201 void asc_scene_node_unlink(AscSceneNode *node) {
202 cx_tree_unlink(
203 node,
204 offsetof(AscSceneNode, parent),
205 offsetof(AscSceneNode, children),
206 offsetof(AscSceneNode, prev),
207 offsetof(AscSceneNode, next)
208 );
209 }
211 AscBehaviorNode *asc_scene_add_behavior(AscSceneNode *node, asc_scene_update_func behavior) {
212 AscBehaviorNode *behavior_node = calloc(1, sizeof(AscBehaviorNode));
213 behavior_node->func = behavior;
214 cx_tree_link(
215 node,
216 behavior_node,
217 offsetof(AscBehaviorNode, parent),
218 offsetof(AscSceneNode, behaviors),
219 offsetof(AscBehaviorNode, prev),
220 offsetof(AscBehaviorNode, next)
221 );
222 return behavior_node;
223 }
225 void asc_scene_remove_behavior(AscBehaviorNode *node) {
226 cx_tree_unlink(
227 node,
228 offsetof(AscBehaviorNode, parent),
229 offsetof(AscSceneNode, behaviors),
230 offsetof(AscBehaviorNode, prev),
231 offsetof(AscBehaviorNode, next)
232 );
233 }
235 void asc_node_update_transform(AscSceneNode *node) {
236 CxTreeIterator iter = asc_scene_node_iterator(node, false);
237 cx_foreach(AscSceneNode*, n, iter) {
238 n->need_transform_update = true;
239 }
240 }