src/scene.c

Sun, 06 Oct 2024 19:43:08 +0200

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

update to recent snapshot of ucx 3.1

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

mercurial