Fri, 15 Mar 2024 00:06:59 +0100
add camera and render groups
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 void asc_scene_init(AscScene *scene) {
42 if (scene->root != NULL) {
43 asc_error("Scene is already initialized.");
44 return;
45 }
47 // zero everything, first
48 memset(scene, 0, sizeof(AscScene));
50 // default viewport is the entire viewport of the active window
51 scene->viewport.size = asc_context.active_window->dimensions;
53 // create the root node
54 scene->root = asc_scene_node_empty();
56 // initialize the render groups
57 cx_array_initialize(scene->rg_none, 8);
58 cx_array_initialize(scene->rg_fonts, 8);
59 }
61 void asc_scene_destroy(AscScene *scene) {
62 asc_scene_node_free(scene->root);
63 }
65 void asc_scene_add(AscScene *scene, AscSceneNode *node) {
66 asc_scene_node_link(scene->root, node);
67 asc_node_update(node);
68 }
70 #define asc_scene_draw_render_group(rg) \
71 cx_for_n(i, rg##_size) { \
72 rg[i].draw(rg[i].node); \
73 } (void)0
75 void asc_scene_draw(AscScene *scene) {
76 // reset render groups
77 scene->rg_none_size = 0;
78 scene->rg_fonts_size = 0;
80 // skip the root node deliberately, we know it's just the container
81 CxTreeIterator iter = cx_tree_iterator(
82 scene->root, false,
83 offsetof(AscSceneNode, children),
84 offsetof(AscSceneNode, next)
85 );
86 cxIteratorNext(iter);
88 // update the children and add them to the render groups
89 cx_foreach(AscSceneNode*, node, iter) {
90 // execute behaviors, first
91 AscBehaviorNode *behavior = node->behaviors;
92 while (behavior) {
93 behavior->func(node);
94 behavior = behavior->next;
95 }
97 // check if geometry needs update
98 if (node->need_full_update) {
99 assert(node->update_func != NULL);
100 node->need_full_update = false;
101 node->update_func(node);
102 }
103 if (node->need_transform_update) {
104 assert(node->transform_update_func != NULL);
105 node->need_transform_update = false;
106 asc_transform_identity(node->transform);
107 node->transform_update_func(node);
108 }
110 // add to render group
111 if (node->draw_func != NULL) {
112 struct asc_render_group_entry entry = {
113 node->draw_func, node
114 };
115 switch (node->render_group) {
116 case ASC_RENDER_GROUP_NONE:
117 cx_array_simple_add(scene->rg_none, entry);
118 break;
119 case ASC_RENDER_GROUP_FONTS:
120 cx_array_simple_add(scene->rg_fonts, entry);
121 break;
122 }
123 }
124 }
126 // set the viewport (in OpenGL we need to invert the Y axis)
127 glViewport(
128 scene->viewport.pos.x,
129 -scene->viewport.pos.y,
130 scene->viewport.size.width,
131 scene->viewport.size.height
132 );
134 // -----------------------------------------
135 // process the render groups for each camera
136 // -----------------------------------------
137 cx_for_n(cam_id, ASC_SCENE_CAMERAS_MAX) {
138 // update camera parameters, first
139 AscCamera *camera = &scene->cameras[cam_id];
140 if (camera->update == NULL) continue;
141 camera->update(camera);
143 // for the NONE group, the draw func is expected to do everything
144 asc_scene_draw_render_group(scene->rg_none);
146 // draw the FONTS group
147 // TODO: see if we can really always ignore the view matrix
148 glUseProgram(ASC_SHADER_FONT.base.id);
149 glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1,
150 GL_FALSE, camera->projection);
151 asc_scene_draw_render_group(scene->rg_fonts);
152 }
153 }
155 AscSceneNode *asc_scene_node_empty(void) {
156 AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
157 node->free_func = (asc_scene_free_func) free;
158 asc_transform_identity(node->transform);
159 return node;
160 }
162 void asc_scene_node_free(AscSceneNode *node) {
163 if (node == NULL) return;
165 // remove this node from its parent
166 asc_scene_node_unlink(node);
168 // free the entire subtree
169 CxTreeIterator iter = cx_tree_iterator(
170 node, true,
171 offsetof(AscSceneNode, children),
172 offsetof(AscSceneNode, next)
173 );
174 cx_foreach(AscSceneNode*, child, iter) {
175 if (!iter.exiting) continue;
176 if (child->free_func != NULL) {
177 child->free_func(child);
178 } else {
179 free(child);
180 }
181 }
182 }
184 void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) {
185 cx_tree_link(
186 parent, node,
187 offsetof(AscSceneNode, parent),
188 offsetof(AscSceneNode, children),
189 offsetof(AscSceneNode, prev),
190 offsetof(AscSceneNode, next)
191 );
192 }
194 void asc_scene_node_unlink(AscSceneNode *node) {
195 cx_tree_unlink(
196 node,
197 offsetof(AscSceneNode, parent),
198 offsetof(AscSceneNode, children),
199 offsetof(AscSceneNode, prev),
200 offsetof(AscSceneNode, next)
201 );
202 }
204 AscBehaviorNode *asc_scene_add_behavior(AscSceneNode *node, asc_scene_update_func behavior) {
205 AscBehaviorNode *behavior_node = calloc(1, sizeof(AscBehaviorNode));
206 behavior_node->func = behavior;
207 cx_tree_link(
208 node,
209 behavior_node,
210 offsetof(AscBehaviorNode, parent),
211 offsetof(AscSceneNode, behaviors),
212 offsetof(AscBehaviorNode, prev),
213 offsetof(AscBehaviorNode, next)
214 );
215 return behavior_node;
216 }
218 void asc_scene_remove_behavior(AscBehaviorNode *node) {
219 cx_tree_unlink(
220 node,
221 offsetof(AscBehaviorNode, parent),
222 offsetof(AscSceneNode, behaviors),
223 offsetof(AscBehaviorNode, prev),
224 offsetof(AscBehaviorNode, next)
225 );
226 }