Mon, 01 Apr 2024 18:54:19 +0200
remove unnecessary scene container
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/linked_list.h>
34 #include <cx/array_list.h>
35 #include <cx/tree.h>
36 #include <cx/utils.h>
38 #include "ascension/shader.h"
39 #include <GL/glew.h>
41 #include <assert.h>
43 static CxTreeIterator asc_scene_node_iterator(
44 AscSceneNode *node,
45 bool visit_on_exit
46 ) {
47 return cx_tree_iterator(
48 node, visit_on_exit,
49 offsetof(AscSceneNode, children),
50 offsetof(AscSceneNode, next)
51 );
52 }
54 static CxTreeVisitor asc_scene_node_visitor(AscSceneNode *node) {
55 return cx_tree_visitor(node,
56 offsetof(AscSceneNode, children),
57 offsetof(AscSceneNode, next)
58 );
59 }
61 struct asc_render_group_entry {
62 asc_scene_draw_func draw;
63 AscSceneNode const *node;
64 };
66 #define asc_draw_render_group(iter) \
67 cx_foreach(struct asc_render_group_entry*, entry, iter) { \
68 entry->draw(entry->node); \
69 }
71 void asc_scene_draw(AscSceneNode *root, asc_recti viewport, AscCamera *camera) {
72 // create render groups
73 CxList *render_group[ASC_RENDER_GROUP_COUNT];
74 cx_for_n(i, ASC_RENDER_GROUP_COUNT) {
75 render_group[i] = cxArrayListCreateSimple(
76 sizeof(struct asc_render_group_entry), 32);
77 }
79 // skip the root node deliberately, we know it's just the container
80 CxTreeVisitor iter = asc_scene_node_visitor(root);
81 cxIteratorNext(iter);
83 // update the children and add them to the render groups
84 cx_foreach(AscSceneNode*, node, iter) {
85 node->depth = iter.depth;
87 // execute behaviors, first
88 if (node->behaviors != NULL) {
89 CxIterator behavior_iter = cxListIterator(node->behaviors);
90 cx_foreach(asc_scene_update_func, behavior, behavior_iter) {
91 behavior(node);
92 }
93 }
95 // TODO: implement culling
96 // TODO: implement a hidden flag (requires UCX tree-continue function)
98 // check if geometry needs update
99 if (node->need_graphics_update) {
100 assert(node->update_func != NULL);
101 node->need_graphics_update = false;
102 node->update_func(node);
103 }
104 if (node->need_transform_update) {
105 node->need_transform_update = false;
106 asc_transform_from_parts(
107 node->transform,
108 node->position,
109 node->scale,
110 node->rotation
111 );
112 asc_mat4f_mulst(
113 node->world_transform,
114 node->transform,
115 node->parent->world_transform
116 );
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 cxListAdd(render_group[node->render_group], &entry);
125 }
126 }
128 // set the viewport (in OpenGL we need to invert the Y axis)
129 glViewport(
130 viewport.pos.x,
131 -viewport.pos.y,
132 viewport.size.width,
133 viewport.size.height
134 );
136 // -------------------------
137 // process the render groups
138 // -------------------------
139 AscShaderProgram *shader;
140 CxIterator render_iter;
142 // 2D Elements
143 // ===========
144 glEnable(GL_DEPTH_TEST);
145 glClear(GL_DEPTH_BUFFER_BIT);
147 // Sprites
148 // -------
149 // TODO: implement view matrix for 2D worlds
150 shader = &asc_context.active_window->glctx.shader.sprite.base;
151 glUseProgram(shader->id);
152 glUniformMatrix4fv(shader->projection, 1,
153 GL_FALSE, camera->projection);
155 // render opaque sprites from front to back
156 glDisable(GL_BLEND);
157 render_iter = cxListBackwardsIterator(render_group[ASC_RENDER_GROUP_SPRITE_OPAQUE]);
158 asc_draw_render_group(render_iter);
160 // render sprites with alpha value from back to front
161 glEnable(GL_BLEND);
162 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
163 render_iter = cxListIterator(render_group[ASC_RENDER_GROUP_SPRITE_BLEND]);
164 asc_draw_render_group(render_iter);
166 // destroy render groups
167 cx_for_n(i, ASC_RENDER_GROUP_COUNT) {
168 cxListDestroy(render_group[i]);
169 }
170 }
172 AscSceneNode *asc_scene_node_empty(void) {
173 AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
174 node->free_func = (asc_scene_free_func) free;
175 asc_transform_identity(node->transform);
176 asc_transform_identity(node->world_transform);
177 return node;
178 }
180 void asc_scene_node_free(AscSceneNode *node) {
181 if (node == NULL) return;
183 // remove this node from its parent
184 asc_scene_node_unlink(node);
186 // free the entire subtree
187 CxTreeIterator iter = asc_scene_node_iterator(node, true);
188 cx_foreach(AscSceneNode*, child, iter) {
189 if (!iter.exiting) continue;
190 if (child->behaviors != NULL) {
191 cxListDestroy(child->behaviors);
192 }
193 if (child->free_func != NULL) {
194 child->free_func(child);
195 } else {
196 free(child);
197 }
198 }
199 }
201 void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) {
202 cx_tree_link(
203 parent, node,
204 offsetof(AscSceneNode, parent),
205 offsetof(AscSceneNode, children),
206 offsetof(AscSceneNode, prev),
207 offsetof(AscSceneNode, next)
208 );
209 }
211 void asc_scene_node_unlink(AscSceneNode *node) {
212 cx_tree_unlink(
213 node,
214 offsetof(AscSceneNode, parent),
215 offsetof(AscSceneNode, children),
216 offsetof(AscSceneNode, prev),
217 offsetof(AscSceneNode, next)
218 );
219 }
221 void asc_scene_add_behavior(
222 AscSceneNode *node,
223 asc_scene_update_func behavior
224 ) {
225 if (node->behaviors == NULL) {
226 node->behaviors = cxLinkedListCreateSimple(CX_STORE_POINTERS);
227 }
228 cxListAdd(node->behaviors, behavior);
229 }
231 void asc_scene_remove_behavior(
232 AscSceneNode *node,
233 asc_scene_update_func behavior
234 ) {
235 if (node->behaviors != NULL) {
236 cxListFindRemove(node->behaviors, behavior);
237 }
238 }
240 void asc_update_transform(AscSceneNode *node) {
241 if (node->need_transform_update) return;
243 CxTreeIterator iter = asc_scene_node_iterator(node, false);
244 cx_foreach(AscSceneNode*, n, iter) {
245 // TODO: break/continue when subtree is already marked for update
246 n->need_transform_update = true;
247 }
248 }