Thu, 21 Mar 2024 20:34:33 +0100
minor improvements
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_graphics_update) {
106 assert(node->update_func != NULL);
107 node->need_graphics_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 glEnable(GL_DEPTH_TEST);
154 glDisable(GL_BLEND);
155 asc_scene_draw_render_group(scene->rg_none);
157 // draw the FONTS group
158 glDisable(GL_DEPTH_TEST);
159 glEnable(GL_BLEND);
160 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
161 // TODO: see if we can really always ignore the view matrix
162 // TODO: compute render order for alpha blending to work correctly
163 glUseProgram(ASC_SHADER_FONT.base.id);
164 glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1,
165 GL_FALSE, camera->projection);
166 asc_scene_draw_render_group(scene->rg_fonts);
167 }
168 }
170 AscSceneNode *asc_scene_node_empty(void) {
171 AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
172 node->free_func = (asc_scene_free_func) free;
173 asc_transform_identity(node->local_transform);
174 asc_transform_identity(node->world_transform);
175 asc_transform_identity(node->final_transform);
176 return node;
177 }
179 void asc_scene_node_free(AscSceneNode *node) {
180 if (node == NULL) return;
182 // remove this node from its parent
183 asc_scene_node_unlink(node);
185 // free the entire subtree
186 CxTreeIterator iter = asc_scene_node_iterator(node, true);
187 cx_foreach(AscSceneNode*, child, iter) {
188 if (!iter.exiting) continue;
189 if (child->free_func != NULL) {
190 child->free_func(child);
191 } else {
192 free(child);
193 }
194 }
195 }
197 void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) {
198 cx_tree_link(
199 parent, node,
200 offsetof(AscSceneNode, parent),
201 offsetof(AscSceneNode, children),
202 offsetof(AscSceneNode, prev),
203 offsetof(AscSceneNode, next)
204 );
205 }
207 void asc_scene_node_unlink(AscSceneNode *node) {
208 cx_tree_unlink(
209 node,
210 offsetof(AscSceneNode, parent),
211 offsetof(AscSceneNode, children),
212 offsetof(AscSceneNode, prev),
213 offsetof(AscSceneNode, next)
214 );
215 }
217 AscBehaviorNode *asc_scene_add_behavior(AscSceneNode *node, asc_scene_update_func behavior) {
218 AscBehaviorNode *behavior_node = calloc(1, sizeof(AscBehaviorNode));
219 behavior_node->func = behavior;
220 cx_tree_link(
221 node,
222 behavior_node,
223 offsetof(AscBehaviorNode, parent),
224 offsetof(AscSceneNode, behaviors),
225 offsetof(AscBehaviorNode, prev),
226 offsetof(AscBehaviorNode, next)
227 );
228 return behavior_node;
229 }
231 void asc_scene_remove_behavior(AscBehaviorNode *node) {
232 cx_tree_unlink(
233 node,
234 offsetof(AscBehaviorNode, parent),
235 offsetof(AscSceneNode, behaviors),
236 offsetof(AscBehaviorNode, prev),
237 offsetof(AscBehaviorNode, next)
238 );
239 }
241 void asc_node_update_transform(AscSceneNode *node) {
242 CxTreeIterator iter = asc_scene_node_iterator(node, false);
243 cx_foreach(AscSceneNode*, n, iter) {
244 n->need_transform_update = true;
245 }
246 }