src/scene.c

Sun, 06 Oct 2024 20:49:43 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 06 Oct 2024 20:49:43 +0200
changeset 75
0ce353485509
parent 73
cfa1d05754ac
permissions
-rw-r--r--

revert introduction of high level ucx trees and stick to the low level API

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
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
30 #include "ascension/context.h"
61
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
31 #include "ascension/utils.h"
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
32
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
33 #include <cx/linked_list.h>
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
34 #include <cx/array_list.h>
75
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
35 #include <cx/tree.h>
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
36 #include <cx/utils.h>
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
38 #include "ascension/shader.h"
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
39 #include <GL/glew.h>
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
40
21
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 #include <assert.h>
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42
75
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
43 static CxTreeIterator asc_scene_node_iterator(
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
44 AscSceneNode *node,
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
45 bool visit_on_exit
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
46 ) {
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
47 return cx_tree_iterator(
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
48 node, visit_on_exit,
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
49 offsetof(AscSceneNode, children),
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
50 offsetof(AscSceneNode, next)
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
51 );
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
52 }
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
53
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
54 static CxTreeVisitor asc_scene_node_visitor(AscSceneNode *node) {
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
55 return cx_tree_visitor(node,
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
56 offsetof(AscSceneNode, children),
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
57 offsetof(AscSceneNode, next)
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
58 );
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
59 }
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
60
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
61 static void asc_sprite_draw(AscSprite const *node) {
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
62 // Obtain shader
72
84472fb3adbd define macro for predefined shader
Mike Becker <universe@uap-core.de>
parents: 71
diff changeset
63 AscShaderSprite *shader = ASC_SHADER_SPRITE;
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
64
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
65 // Upload model matrix
72
84472fb3adbd define macro for predefined shader
Mike Becker <universe@uap-core.de>
parents: 71
diff changeset
66 glUniformMatrix4fv(shader->program.model, 1,
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
67 GL_FALSE, node->data.world_transform);
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
68
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
69 // Bind texture
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
70 asc_texture_bind(&node->tex, shader->tex, 0);
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
72 // Apply depth
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
73 glUniform1f(shader->depth, (float)(node->data.depth));
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
74
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
75 // Draw mesh
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
76 asc_primitives_draw_plane();
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
77 }
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
78
75
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
79 void asc_scene_draw(AscSceneNode *root, asc_recti viewport, AscCamera *camera) {
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
80 // create render groups
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
81 CxList *render_group[ASC_RENDER_GROUP_COUNT];
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
82 cx_for_n(i, ASC_RENDER_GROUP_COUNT) {
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
83 render_group[i] = cxArrayListCreateSimple(CX_STORE_POINTERS, 32);
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
84 }
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
85
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
86 // skip the root node deliberately, we know it's just the container
75
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
87 CxTreeVisitor iter = asc_scene_node_visitor(root);
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
88 cxIteratorNext(iter);
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
89
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
90 // 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
91 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
92 node->depth = iter.depth;
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
93
55
6b61b4899587 make hidden flag a general attribute of nodes
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
94 // skip hidden nodes (and all their children)
61
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
95 if (asc_test_flag(node->flags, ASC_SCENE_NODE_HIDDEN)) {
55
6b61b4899587 make hidden flag a general attribute of nodes
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
96 cxTreeVisitorContinue(iter);
6b61b4899587 make hidden flag a general attribute of nodes
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
97 }
6b61b4899587 make hidden flag a general attribute of nodes
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
98
33
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
99 // execute behaviors, first
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
100 if (node->behaviors != NULL) {
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
101 CxIterator behavior_iter = cxListIterator(node->behaviors);
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
102 cx_foreach(asc_scene_update_func, behavior, behavior_iter) {
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
103 behavior(node);
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
104 }
33
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
105 }
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
106
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
107 // TODO: implement culling
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
108
33
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
109 // check if geometry needs update
69
86d545f490e4 fix that update_transform flag was never cleared
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
110 asc_clear_flag(node->flags,
86d545f490e4 fix that update_transform flag was never cleared
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
111 ASC_SCENE_NODE_GRAPHICS_UPDATED
86d545f490e4 fix that update_transform flag was never cleared
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
112 | ASC_SCENE_NODE_TRANSFORM_UPDATED);
61
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
113 if (asc_test_flag(node->flags, ASC_SCENE_NODE_UPDATE_GRAPHICS)) {
69
86d545f490e4 fix that update_transform flag was never cleared
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
114 asc_set_flag(node->flags, ASC_SCENE_NODE_GRAPHICS_UPDATED);
86d545f490e4 fix that update_transform flag was never cleared
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
115 asc_clear_flag(node->flags, ASC_SCENE_NODE_UPDATE_GRAPHICS);
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
116 assert(node->update_func != NULL);
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
117 node->update_func(node);
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
118 }
61
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
119 if (asc_test_flag(node->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM)) {
69
86d545f490e4 fix that update_transform flag was never cleared
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
120 asc_set_flag(node->flags, ASC_SCENE_NODE_TRANSFORM_UPDATED);
86d545f490e4 fix that update_transform flag was never cleared
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
121 asc_clear_flag(node->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM);
45
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
122 asc_transform_from_parts(
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
123 node->transform,
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
124 node->position,
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
125 node->scale,
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
126 node->rotation
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
127 );
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
128 asc_mat4f_mulst(
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
129 node->world_transform,
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
130 node->transform,
75
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
131 node->parent->world_transform
45
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
132 );
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
133 }
33
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
134
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
135 // add to render group
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
136 cxListAdd(render_group[node->render_group], node);
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
137 }
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
138
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
139 // 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
140 glViewport(
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
141 viewport.pos.x,
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
142 -viewport.pos.y,
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
143 viewport.size.width,
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
144 viewport.size.height
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
145 );
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
146
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
147 // -------------------------
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
148 // process the render groups
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
149 // -------------------------
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
150 AscShaderProgram *shader;
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
151 CxIterator render_iter;
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
152
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
153 // 2D Elements
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
154 // ===========
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
155 glEnable(GL_DEPTH_TEST);
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
156 glClear(GL_DEPTH_BUFFER_BIT);
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
157
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
158 // Sprites
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
159 // -------
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
160 // TODO: implement view matrix for 2D worlds
72
84472fb3adbd define macro for predefined shader
Mike Becker <universe@uap-core.de>
parents: 71
diff changeset
161 shader = &asc_active_window->glctx.shader.sprite.program;
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
162 glUseProgram(shader->id);
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
163 glUniformMatrix4fv(shader->projection, 1,
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
164 GL_FALSE, camera->projection);
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
165
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
166 // render opaque sprites from front to back
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
167 glDisable(GL_BLEND);
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
168 render_iter = cxListBackwardsIterator(render_group[ASC_RENDER_GROUP_SPRITE_OPAQUE]);
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
169 cx_foreach(AscSprite const *, node, render_iter) {
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
170 asc_sprite_draw(node);
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
171 }
41
df81d493716e add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
172
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
173 // render sprites with alpha value from back to front
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
174 glEnable(GL_BLEND);
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
175 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
176 render_iter = cxListIterator(render_group[ASC_RENDER_GROUP_SPRITE_BLEND]);
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
177 cx_foreach(AscSprite const *, node, render_iter) {
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
178 asc_sprite_draw(node);
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
179 }
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
180
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
181 // destroy render groups
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
182 cx_for_n(i, ASC_RENDER_GROUP_COUNT) {
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
183 cxListDestroy(render_group[i]);
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
184 }
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
185 }
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
186
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
187 AscSceneNode *asc_scene_node_empty(void) {
27
4ccf4703999e improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
188 AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
75
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
189 node->free_func = (asc_scene_free_func) free;
51
a656496594f9 fix scale not initialized to 1
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
190 node->scale.x = node->scale.y = node->scale.z = 1;
45
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
191 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
192 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
193 return node;
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195
75
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
196 void asc_scene_node_free(AscSceneNode *node) {
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
197 if (node == NULL) return;
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
198
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
199 // remove this node from its parent
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
200 asc_scene_node_unlink(node);
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
201
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
202 // free the entire subtree
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
203 CxTreeIterator iter = asc_scene_node_iterator(node, true);
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
204 cx_foreach(AscSceneNode*, child, iter) {
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
205 if (!iter.exiting) continue;
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
206 if (child->behaviors != NULL) {
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
207 cxListDestroy(child->behaviors);
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
208 }
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
209 if (child->free_func != NULL) {
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
210 child->free_func(child);
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
211 } else {
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
212 free(child);
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
213 }
21
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
215 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216
75
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
217 void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) {
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
218 cx_tree_link(
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
219 parent, node,
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
220 offsetof(AscSceneNode, parent),
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
221 offsetof(AscSceneNode, children),
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
222 offsetof(AscSceneNode, last_child),
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
223 offsetof(AscSceneNode, prev),
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
224 offsetof(AscSceneNode, next)
33
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
225 );
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
226 }
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
227
75
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
228 void asc_scene_node_unlink(AscSceneNode *node) {
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
229 cx_tree_unlink(
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
230 node,
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
231 offsetof(AscSceneNode, parent),
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
232 offsetof(AscSceneNode, children),
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
233 offsetof(AscSceneNode, last_child),
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
234 offsetof(AscSceneNode, prev),
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
235 offsetof(AscSceneNode, next)
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
236 );
33
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
237 }
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
238
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
239 void asc_scene_add_behavior(
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
240 AscSceneNode *node,
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
241 asc_scene_update_func behavior
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
242 ) {
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
243 if (node->behaviors == NULL) {
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
244 node->behaviors = cxLinkedListCreateSimple(CX_STORE_POINTERS);
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
245 }
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
246 cxListAdd(node->behaviors, behavior);
33
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
247 }
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
248
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
249 void asc_scene_remove_behavior(
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
250 AscSceneNode *node,
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
251 asc_scene_update_func behavior
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
252 ) {
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
253 if (node->behaviors != NULL) {
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
254 cxListFindRemove(node->behaviors, behavior);
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
255 }
33
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
256 }
38
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
257
61
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
258 void asc_node_update(AscSceneNode *node) {
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
259 asc_set_flag(node->flags, ASC_SCENE_NODE_UPDATE_GRAPHICS);
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
260 }
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
261
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
262 void asc_node_update_transform(AscSceneNode *node) {
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
263 // fast skip if node is already marked
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
264 if (asc_test_flag(node->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM)) {
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
265 return;
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
266 }
45
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
267
75
0ce353485509 revert introduction of high level ucx trees and stick to the low level API
Mike Becker <universe@uap-core.de>
parents: 73
diff changeset
268 CxTreeIterator iter = asc_scene_node_iterator(node, false);
38
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
269 cx_foreach(AscSceneNode*, n, iter) {
61
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
270 if (asc_test_flag(n->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM)) {
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
271 cxTreeIteratorContinue(iter);
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
272 }
b7954818a6b7 merge different bools of AscSceneNode into flags
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
273 asc_set_flag(n->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM);
38
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
274 }
45
18de2af03531 simplify how transforms work
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
275 }

mercurial