src/scene.c

Fri, 29 Mar 2024 00:03:25 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 29 Mar 2024 00:03:25 +0100
changeset 46
d3285aed65b3
parent 45
18de2af03531
child 47
44457f6cb0a2
permissions
-rw-r--r--

make the timer have nanoseconds precision

21
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 * Copyright 2023 Mike Becker. All rights reserved.
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 *
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * modification, are permitted provided that the following conditions are met:
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 *
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 *
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 * 2. Redistributions in binary form must reproduce the above copyright
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * notice, this list of conditions and the following disclaimer in the
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * documentation and/or other materials provided with the distribution.
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 *
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * POSSIBILITY OF SUCH DAMAGE.
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 */
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 #include "ascension/scene.h"
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "ascension/error.h"
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
31 #include "ascension/context.h"
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
32
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
33 #include <cx/tree.h>
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
34 #include <cx/utils.h>
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
35
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
36 #include "ascension/shader.h"
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
37 #include <GL/glew.h>
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
38
21
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 #include <assert.h>
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40
38
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
41 static CxTreeIterator asc_scene_node_iterator(
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
42 AscSceneNode *node,
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
43 bool visit_on_exit
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
44 ) {
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
45 return cx_tree_iterator(
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
46 node, visit_on_exit,
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
47 offsetof(AscSceneNode, children),
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
48 offsetof(AscSceneNode, next)
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
49 );
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
50 }
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
51
43
5a8c31904e44 fix iteration order when drawing nodes
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
52 static CxTreeVisitor asc_scene_node_visitor(AscSceneNode *node) {
5a8c31904e44 fix iteration order when drawing nodes
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
53 return cx_tree_visitor(node,
5a8c31904e44 fix iteration order when drawing nodes
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
54 offsetof(AscSceneNode, children),
5a8c31904e44 fix iteration order when drawing nodes
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
55 offsetof(AscSceneNode, next)
5a8c31904e44 fix iteration order when drawing nodes
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
56 );
5a8c31904e44 fix iteration order when drawing nodes
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
57 }
5a8c31904e44 fix iteration order when drawing nodes
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
58
21
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 void asc_scene_init(AscScene *scene) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 if (scene->root != NULL) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 asc_error("Scene is already initialized.");
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 return;
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 }
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
64
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
65 // zero everything, first
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
66 memset(scene, 0, sizeof(AscScene));
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
67
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
68 // default viewport is the entire viewport of the active window
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
69 scene->viewport.size = asc_context.active_window->dimensions;
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
70
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
71 // create the root node
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
72 scene->root = asc_scene_node_empty();
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
73
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
74 // initialize the render groups
41
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
75 cx_array_initialize(scene->rg_sprites_opaque, 8);
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
76 cx_array_initialize(scene->rg_sprites_blended, 8);
21
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 void asc_scene_destroy(AscScene *scene) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 asc_scene_node_free(scene->root);
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
83 void asc_scene_add(AscScene *scene, AscSceneNode *node) {
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
84 asc_scene_node_link(scene->root, node);
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
85 asc_node_update(node);
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
86 }
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
87
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
88 #define asc_scene_draw_render_group(rg) \
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
89 cx_for_n(i, rg##_size) { \
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
90 rg[i].draw(rg[i].node); \
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
91 } (void)0
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
92
41
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
93 #define asc_scene_draw_render_group_reversed(rg) \
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
94 for(size_t i = rg##_size ; i > 0 ; i--) { \
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
95 rg[i-1].draw(rg[i-1].node); \
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
96 } (void)0
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
97
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
98 void asc_scene_draw(AscScene *scene) {
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
99 // reset render groups
41
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
100 // TODO: avoid recalculating the groups, if possible
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
101 scene->rg_sprites_opaque_size = 0;
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
102 scene->rg_sprites_blended_size = 0;
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
103
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
104 // skip the root node deliberately, we know it's just the container
43
5a8c31904e44 fix iteration order when drawing nodes
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
105 CxTreeVisitor iter = asc_scene_node_visitor(scene->root);
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
106 cxIteratorNext(iter);
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
107
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
108 // update the children and add them to the render groups
30
fceda550ebcb apply new CxTreeIterator from ucx 3.1 preview
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
109 cx_foreach(AscSceneNode*, node, iter) {
41
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
110 node->depth = iter.depth;
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
111
33
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
112 // execute behaviors, first
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
113 AscBehaviorNode *behavior = node->behaviors;
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
114 while (behavior) {
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
115 behavior->func(node);
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
116 behavior = behavior->next;
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
117 }
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
118
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
119 // check if geometry needs update
39
7cf310cc47cb minor improvements
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
120 if (node->need_graphics_update) {
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
121 assert(node->update_func != NULL);
39
7cf310cc47cb minor improvements
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
122 node->need_graphics_update = false;
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
123 node->update_func(node);
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
124 }
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
125 if (node->need_transform_update) {
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
126 node->need_transform_update = false;
45
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
127 asc_transform_from_parts(
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
128 node->transform,
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
129 node->position,
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
130 node->scale,
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
131 node->rotation
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
132 );
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
133 asc_mat4f_mulst(
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
134 node->world_transform,
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
135 node->transform,
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
136 node->parent->world_transform
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
137 );
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
138 }
33
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
139
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
140 // add to render group
30
fceda550ebcb apply new CxTreeIterator from ucx 3.1 preview
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
141 if (node->draw_func != NULL) {
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
142 struct asc_render_group_entry entry = {
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
143 node->draw_func, node
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
144 };
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
145 switch (node->render_group) {
41
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
146 case ASC_RENDER_GROUP_SPRITE_OPAQUE:
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
147 cx_array_simple_add(scene->rg_sprites_opaque, entry);
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
148 break;
41
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
149 case ASC_RENDER_GROUP_SPRITE_BLEND:
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
150 cx_array_simple_add(scene->rg_sprites_blended, entry);
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
151 break;
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
152 }
30
fceda550ebcb apply new CxTreeIterator from ucx 3.1 preview
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
153 }
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
154 }
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
155
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
156 // set the viewport (in OpenGL we need to invert the Y axis)
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
157 glViewport(
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
158 scene->viewport.pos.x,
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
159 -scene->viewport.pos.y,
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
160 scene->viewport.size.width,
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
161 scene->viewport.size.height
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
162 );
41
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
163 glClear(GL_COLOR_BUFFER_BIT);
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
164
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
165 // -----------------------------------------
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
166 // process the render groups for each camera
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
167 // -----------------------------------------
44
b3da4096c607 create own compilation unit for GL context - fixes shader not being created per context
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
168 AscShaderProgram *shader;
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
169 cx_for_n(cam_id, ASC_SCENE_CAMERAS_MAX) {
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
170 // update camera parameters, first
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
171 AscCamera *camera = &scene->cameras[cam_id];
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
172 if (camera->update == NULL) continue;
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
173 camera->update(camera);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
174
41
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
175 // 2D Elements
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
176 // ===========
39
7cf310cc47cb minor improvements
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
177 glEnable(GL_DEPTH_TEST);
41
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
178 glClear(GL_DEPTH_BUFFER_BIT);
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
179
41
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
180 // Sprites
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
181 // -------
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
182 // TODO: see if we can really always ignore the view matrix
45
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
183 shader = &asc_context.active_window->glctx.shader.sprite.base;
44
b3da4096c607 create own compilation unit for GL context - fixes shader not being created per context
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
184 glUseProgram(shader->id);
b3da4096c607 create own compilation unit for GL context - fixes shader not being created per context
Mike Becker <universe@uap-core.de>
parents: 43
diff changeset
185 glUniformMatrix4fv(shader->projection, 1,
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
186 GL_FALSE, camera->projection);
41
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
187
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
188 // render opaque sprites from front to back
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
189 glDisable(GL_BLEND);
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
190 asc_scene_draw_render_group_reversed(scene->rg_sprites_opaque);
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
191 // render sprites with alpha value from back to front
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
192 glEnable(GL_BLEND);
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
193 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
194 asc_scene_draw_render_group(scene->rg_sprites_blended);
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
195 }
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
196 }
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
197
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
198 AscSceneNode *asc_scene_node_empty(void) {
27
4ccf4703999e improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
199 AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
200 node->free_func = (asc_scene_free_func) free;
45
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
201 asc_transform_identity(node->transform);
38
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
202 asc_transform_identity(node->world_transform);
21
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 return node;
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 void asc_scene_node_free(AscSceneNode *node) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 if (node == NULL) return;
27
4ccf4703999e improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
208
4ccf4703999e improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
209 // remove this node from its parent
4ccf4703999e improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
210 asc_scene_node_unlink(node);
4ccf4703999e improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
211
31
8324037e0148 use tree iterator to free scene nodes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
212 // free the entire subtree
38
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
213 CxTreeIterator iter = asc_scene_node_iterator(node, true);
31
8324037e0148 use tree iterator to free scene nodes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
214 cx_foreach(AscSceneNode*, child, iter) {
8324037e0148 use tree iterator to free scene nodes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
215 if (!iter.exiting) continue;
8324037e0148 use tree iterator to free scene nodes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
216 if (child->free_func != NULL) {
8324037e0148 use tree iterator to free scene nodes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
217 child->free_func(child);
8324037e0148 use tree iterator to free scene nodes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
218 } else {
8324037e0148 use tree iterator to free scene nodes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
219 free(child);
8324037e0148 use tree iterator to free scene nodes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
220 }
21
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
224 void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) {
33
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
225 cx_tree_link(
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
226 parent, node,
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
227 offsetof(AscSceneNode, parent),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
228 offsetof(AscSceneNode, children),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
229 offsetof(AscSceneNode, prev),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
230 offsetof(AscSceneNode, next)
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
231 );
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
232 }
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
233
21
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234 void asc_scene_node_unlink(AscSceneNode *node) {
33
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
235 cx_tree_unlink(
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
236 node,
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
237 offsetof(AscSceneNode, parent),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
238 offsetof(AscSceneNode, children),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
239 offsetof(AscSceneNode, prev),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
240 offsetof(AscSceneNode, next)
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
241 );
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
242 }
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
243
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
244 AscBehaviorNode *asc_scene_add_behavior(AscSceneNode *node, asc_scene_update_func behavior) {
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
245 AscBehaviorNode *behavior_node = calloc(1, sizeof(AscBehaviorNode));
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
246 behavior_node->func = behavior;
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
247 cx_tree_link(
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
248 node,
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
249 behavior_node,
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
250 offsetof(AscBehaviorNode, parent),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
251 offsetof(AscSceneNode, behaviors),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
252 offsetof(AscBehaviorNode, prev),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
253 offsetof(AscBehaviorNode, next)
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
254 );
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
255 return behavior_node;
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
256 }
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
257
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
258 void asc_scene_remove_behavior(AscBehaviorNode *node) {
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
259 cx_tree_unlink(
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
260 node,
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
261 offsetof(AscBehaviorNode, parent),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
262 offsetof(AscSceneNode, behaviors),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
263 offsetof(AscBehaviorNode, prev),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
264 offsetof(AscBehaviorNode, next)
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
265 );
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
266 }
38
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
267
45
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
268 void asc_update_transform(AscSceneNode *node) {
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
269 if (node->need_transform_update) return;
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
270
38
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
271 CxTreeIterator iter = asc_scene_node_iterator(node, false);
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
272 cx_foreach(AscSceneNode*, n, iter) {
45
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
273 // TODO: break/continue when subtree is already marked for update
38
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
274 n->need_transform_update = true;
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
275 }
45
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
276 }

mercurial