Thu, 21 Mar 2024 22:23:00 +0100
add correct interleaving of opaque and transparent sprites
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_sprites_opaque, 8);
69 cx_array_initialize(scene->rg_sprites_blended, 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 #define asc_scene_draw_render_group_reversed(rg) \
87 for(size_t i = rg##_size ; i > 0 ; i--) { \
88 rg[i-1].draw(rg[i-1].node); \
89 } (void)0
91 void asc_scene_draw(AscScene *scene) {
92 // reset render groups
93 // TODO: avoid recalculating the groups, if possible
94 scene->rg_sprites_opaque_size = 0;
95 scene->rg_sprites_blended_size = 0;
97 // skip the root node deliberately, we know it's just the container
98 CxTreeIterator iter = asc_scene_node_iterator(scene->root, false);
99 cxIteratorNext(iter);
101 // update the children and add them to the render groups
102 cx_foreach(AscSceneNode*, node, iter) {
103 node->depth = iter.depth;
105 // execute behaviors, first
106 AscBehaviorNode *behavior = node->behaviors;
107 while (behavior) {
108 behavior->func(node);
109 behavior = behavior->next;
110 }
112 // check if geometry needs update
113 if (node->need_graphics_update) {
114 assert(node->update_func != NULL);
115 node->need_graphics_update = false;
116 node->update_func(node);
117 }
118 if (node->need_transform_update) {
119 assert(node->transform_update_func != NULL);
120 node->need_transform_update = false;
121 asc_transform_identity(node->local_transform);
122 asc_transform_copy(node->world_transform, node->parent->world_transform);
123 node->transform_update_func(node);
124 asc_mat4f_mulst(node->final_transform, node->local_transform, node->world_transform);
125 }
127 // add to render group
128 if (node->draw_func != NULL) {
129 struct asc_render_group_entry entry = {
130 node->draw_func, node
131 };
132 switch (node->render_group) {
133 case ASC_RENDER_GROUP_SPRITE_OPAQUE:
134 cx_array_simple_add(scene->rg_sprites_opaque, entry);
135 break;
136 case ASC_RENDER_GROUP_SPRITE_BLEND:
137 cx_array_simple_add(scene->rg_sprites_blended, entry);
138 break;
139 }
140 }
141 }
143 // set the viewport (in OpenGL we need to invert the Y axis)
144 glViewport(
145 scene->viewport.pos.x,
146 -scene->viewport.pos.y,
147 scene->viewport.size.width,
148 scene->viewport.size.height
149 );
150 glClear(GL_COLOR_BUFFER_BIT);
152 // -----------------------------------------
153 // process the render groups for each camera
154 // -----------------------------------------
155 cx_for_n(cam_id, ASC_SCENE_CAMERAS_MAX) {
156 // update camera parameters, first
157 AscCamera *camera = &scene->cameras[cam_id];
158 if (camera->update == NULL) continue;
159 camera->update(camera);
161 // 2D Elements
162 // ===========
163 glEnable(GL_DEPTH_TEST);
164 glClear(GL_DEPTH_BUFFER_BIT);
166 // Sprites
167 // -------
168 // TODO: see if we can really always ignore the view matrix
169 glUseProgram(ASC_SHADER_SPRITE.base.id);
170 glUniformMatrix4fv(ASC_SHADER_SPRITE.base.projection, 1,
171 GL_FALSE, camera->projection);
173 // render opaque sprites from front to back
174 glDisable(GL_BLEND);
175 asc_scene_draw_render_group_reversed(scene->rg_sprites_opaque);
176 // render sprites with alpha value from back to front
177 glEnable(GL_BLEND);
178 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
179 asc_scene_draw_render_group(scene->rg_sprites_blended);
180 }
181 }
183 AscSceneNode *asc_scene_node_empty(void) {
184 AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
185 node->free_func = (asc_scene_free_func) free;
186 asc_transform_identity(node->local_transform);
187 asc_transform_identity(node->world_transform);
188 asc_transform_identity(node->final_transform);
189 return node;
190 }
192 void asc_scene_node_free(AscSceneNode *node) {
193 if (node == NULL) return;
195 // remove this node from its parent
196 asc_scene_node_unlink(node);
198 // free the entire subtree
199 CxTreeIterator iter = asc_scene_node_iterator(node, true);
200 cx_foreach(AscSceneNode*, child, iter) {
201 if (!iter.exiting) continue;
202 if (child->free_func != NULL) {
203 child->free_func(child);
204 } else {
205 free(child);
206 }
207 }
208 }
210 void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) {
211 cx_tree_link(
212 parent, node,
213 offsetof(AscSceneNode, parent),
214 offsetof(AscSceneNode, children),
215 offsetof(AscSceneNode, prev),
216 offsetof(AscSceneNode, next)
217 );
218 }
220 void asc_scene_node_unlink(AscSceneNode *node) {
221 cx_tree_unlink(
222 node,
223 offsetof(AscSceneNode, parent),
224 offsetof(AscSceneNode, children),
225 offsetof(AscSceneNode, prev),
226 offsetof(AscSceneNode, next)
227 );
228 }
230 AscBehaviorNode *asc_scene_add_behavior(AscSceneNode *node, asc_scene_update_func behavior) {
231 AscBehaviorNode *behavior_node = calloc(1, sizeof(AscBehaviorNode));
232 behavior_node->func = behavior;
233 cx_tree_link(
234 node,
235 behavior_node,
236 offsetof(AscBehaviorNode, parent),
237 offsetof(AscSceneNode, behaviors),
238 offsetof(AscBehaviorNode, prev),
239 offsetof(AscBehaviorNode, next)
240 );
241 return behavior_node;
242 }
244 void asc_scene_remove_behavior(AscBehaviorNode *node) {
245 cx_tree_unlink(
246 node,
247 offsetof(AscBehaviorNode, parent),
248 offsetof(AscSceneNode, behaviors),
249 offsetof(AscBehaviorNode, prev),
250 offsetof(AscBehaviorNode, next)
251 );
252 }
254 void asc_node_update_transform(AscSceneNode *node) {
255 CxTreeIterator iter = asc_scene_node_iterator(node, false);
256 cx_foreach(AscSceneNode*, n, iter) {
257 n->need_transform_update = true;
258 }
259 }