src/scene.c

Thu, 21 Mar 2024 20:34:33 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 21 Mar 2024 20:34:33 +0100
changeset 39
7cf310cc47cb
parent 38
6e5629ea4c5c
child 40
6c438be1a1fd
permissions
-rw-r--r--

minor improvements

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
21
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 void asc_scene_init(AscScene *scene) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 if (scene->root != NULL) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 asc_error("Scene is already initialized.");
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 return;
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 }
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
57
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
58 // zero everything, first
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
59 memset(scene, 0, sizeof(AscScene));
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
60
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
61 // 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
62 scene->viewport.size = asc_context.active_window->dimensions;
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
63
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
64 // create the root node
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
65 scene->root = asc_scene_node_empty();
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
66
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
67 // initialize the render groups
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
68 cx_array_initialize(scene->rg_none, 8);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
69 cx_array_initialize(scene->rg_fonts, 8);
21
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 void asc_scene_destroy(AscScene *scene) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 asc_scene_node_free(scene->root);
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
76 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
77 asc_scene_node_link(scene->root, node);
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
78 asc_node_update(node);
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
79 }
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
80
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
81 #define asc_scene_draw_render_group(rg) \
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
82 cx_for_n(i, rg##_size) { \
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
83 rg[i].draw(rg[i].node); \
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
84 } (void)0
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
85
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
86 void asc_scene_draw(AscScene *scene) {
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
87 // reset render groups
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
88 scene->rg_none_size = 0;
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
89 scene->rg_fonts_size = 0;
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
90
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
91 // skip the root node deliberately, we know it's just the container
38
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
92 CxTreeIterator iter = asc_scene_node_iterator(scene->root, false);
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
93 cxIteratorNext(iter);
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
94
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
95 // 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
96 cx_foreach(AscSceneNode*, node, iter) {
33
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
97 // execute behaviors, first
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
98 AscBehaviorNode *behavior = node->behaviors;
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
99 while (behavior) {
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
100 behavior->func(node);
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
101 behavior = behavior->next;
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
102 }
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
103
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
104 // check if geometry needs update
39
7cf310cc47cb minor improvements
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
105 if (node->need_graphics_update) {
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
106 assert(node->update_func != NULL);
39
7cf310cc47cb minor improvements
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
107 node->need_graphics_update = false;
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
108 node->update_func(node);
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
109 }
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
110 if (node->need_transform_update) {
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
111 assert(node->transform_update_func != NULL);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
112 node->need_transform_update = false;
38
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
113 asc_transform_identity(node->local_transform);
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
114 asc_transform_copy(node->world_transform, node->parent->world_transform);
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
115 node->transform_update_func(node);
38
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
116 asc_mat4f_mulst(node->final_transform, node->local_transform, node->world_transform);
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
117 }
33
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
118
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
119 // add to render group
30
fceda550ebcb apply new CxTreeIterator from ucx 3.1 preview
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
120 if (node->draw_func != NULL) {
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
121 struct asc_render_group_entry entry = {
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
122 node->draw_func, node
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
123 };
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
124 switch (node->render_group) {
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
125 case ASC_RENDER_GROUP_NONE:
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
126 cx_array_simple_add(scene->rg_none, entry);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
127 break;
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
128 case ASC_RENDER_GROUP_FONTS:
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
129 cx_array_simple_add(scene->rg_fonts, entry);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
130 break;
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
131 }
30
fceda550ebcb apply new CxTreeIterator from ucx 3.1 preview
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
132 }
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
133 }
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
134
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
135 // 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
136 glViewport(
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
137 scene->viewport.pos.x,
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
138 -scene->viewport.pos.y,
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
139 scene->viewport.size.width,
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
140 scene->viewport.size.height
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
141 );
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
142
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
143 // -----------------------------------------
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
144 // process the render groups for each camera
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 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
147 // update camera parameters, first
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
148 AscCamera *camera = &scene->cameras[cam_id];
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
149 if (camera->update == NULL) continue;
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
150 camera->update(camera);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
151
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
152 // for the NONE group, the draw func is expected to do everything
39
7cf310cc47cb minor improvements
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
153 glEnable(GL_DEPTH_TEST);
7cf310cc47cb minor improvements
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
154 glDisable(GL_BLEND);
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
155 asc_scene_draw_render_group(scene->rg_none);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
156
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
157 // draw the FONTS group
39
7cf310cc47cb minor improvements
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
158 glDisable(GL_DEPTH_TEST);
7cf310cc47cb minor improvements
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
159 glEnable(GL_BLEND);
7cf310cc47cb minor improvements
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
160 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
161 // TODO: see if we can really always ignore the view matrix
39
7cf310cc47cb minor improvements
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
162 // TODO: compute render order for alpha blending to work correctly
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
163 glUseProgram(ASC_SHADER_FONT.base.id);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
164 glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1,
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
165 GL_FALSE, camera->projection);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
166 asc_scene_draw_render_group(scene->rg_fonts);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
167 }
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
168 }
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
169
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
170 AscSceneNode *asc_scene_node_empty(void) {
27
4ccf4703999e improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
171 AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
172 node->free_func = (asc_scene_free_func) free;
38
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
173 asc_transform_identity(node->local_transform);
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
174 asc_transform_identity(node->world_transform);
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
175 asc_transform_identity(node->final_transform);
21
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 return node;
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 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
180 if (node == NULL) return;
27
4ccf4703999e improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
181
4ccf4703999e improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
182 // remove this node from its parent
4ccf4703999e improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
183 asc_scene_node_unlink(node);
4ccf4703999e improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
184
31
8324037e0148 use tree iterator to free scene nodes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
185 // 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
186 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
187 cx_foreach(AscSceneNode*, child, iter) {
8324037e0148 use tree iterator to free scene nodes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
188 if (!iter.exiting) continue;
8324037e0148 use tree iterator to free scene nodes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
189 if (child->free_func != NULL) {
8324037e0148 use tree iterator to free scene nodes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
190 child->free_func(child);
8324037e0148 use tree iterator to free scene nodes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
191 } else {
8324037e0148 use tree iterator to free scene nodes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
192 free(child);
8324037e0148 use tree iterator to free scene nodes
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
193 }
21
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 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
197 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
198 cx_tree_link(
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
199 parent, node,
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
200 offsetof(AscSceneNode, parent),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
201 offsetof(AscSceneNode, children),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
202 offsetof(AscSceneNode, prev),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
203 offsetof(AscSceneNode, next)
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
204 );
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
205 }
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
206
21
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 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
208 cx_tree_unlink(
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
209 node,
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
210 offsetof(AscSceneNode, parent),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
211 offsetof(AscSceneNode, children),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
212 offsetof(AscSceneNode, prev),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
213 offsetof(AscSceneNode, next)
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
214 );
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
215 }
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
216
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
217 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
218 AscBehaviorNode *behavior_node = calloc(1, sizeof(AscBehaviorNode));
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
219 behavior_node->func = behavior;
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
220 cx_tree_link(
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
221 node,
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
222 behavior_node,
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
223 offsetof(AscBehaviorNode, parent),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
224 offsetof(AscSceneNode, behaviors),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
225 offsetof(AscBehaviorNode, prev),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
226 offsetof(AscBehaviorNode, next)
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
227 );
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
228 return behavior_node;
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
229 }
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
230
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
231 void asc_scene_remove_behavior(AscBehaviorNode *node) {
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
232 cx_tree_unlink(
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
233 node,
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
234 offsetof(AscBehaviorNode, parent),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
235 offsetof(AscSceneNode, behaviors),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
236 offsetof(AscBehaviorNode, prev),
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
237 offsetof(AscBehaviorNode, next)
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
238 );
e7ddb52facd3 add behavior nodes + restructure test program
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
239 }
38
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
240
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
241 void asc_node_update_transform(AscSceneNode *node) {
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
242 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
243 cx_foreach(AscSceneNode*, n, iter) {
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
244 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
245 }
6e5629ea4c5c implement that nodes inherit the world transform of their parent
Mike Becker <universe@uap-core.de>
parents: 37
diff changeset
246 }

mercurial