remove separate depth attribute and use the z-coordinate instead default tip

Thu, 01 May 2025 15:26:01 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 01 May 2025 15:26:01 +0200
changeset 102
0d94fd280c6e
parent 101
febf3dc10011

remove separate depth attribute and use the z-coordinate instead

shader/sprite_vtx.glsl file | annotate | diff | comparison | revisions
src/ascension/scene_node.h file | annotate | diff | comparison | revisions
src/scene.c file | annotate | diff | comparison | revisions
src/scene_node.c file | annotate | diff | comparison | revisions
src/sprite.c file | annotate | diff | comparison | revisions
src/text.c file | annotate | diff | comparison | revisions
--- a/shader/sprite_vtx.glsl	Tue Apr 29 21:51:29 2025 +0200
+++ b/shader/sprite_vtx.glsl	Thu May 01 15:26:01 2025 +0200
@@ -7,12 +7,9 @@
 uniform mat4 projection;
 uniform mat4 view;
 uniform mat4 model;
-uniform float depth;
 
 void main(void) {
-    vec4 pos = projection*view*model*vec4(position.x, position.y, 0, 1.0);
-    // apply depth
-    pos.z = depth / -1024.0;
+    vec4 pos = projection*view*model*vec4(position.xy, 0.0, 1.0);
     gl_Position = pos;
     uvcoord = position;
     texcoord = vec2(model[0].x, model[1].y)*position;
--- a/src/ascension/scene_node.h	Tue Apr 29 21:51:29 2025 +0200
+++ b/src/ascension/scene_node.h	Thu May 01 15:26:01 2025 +0200
@@ -54,7 +54,6 @@
     CxList *behaviors;
     asc_scene_free_func free_func;
     asc_scene_update_func update_func;
-    unsigned depth; // TODO: do we really need this bullshit?
     asc_vec3f position;
     asc_vec3f rotation;
     asc_vec3f scale;
@@ -175,6 +174,12 @@
 void asc_node_update_transform(AscSceneNode *node);
 
 
+/**
+ * This is the z-position a simple 2D element should have to allow
+ * stacking 2D elements with depth-test.
+ */
+#define ASC_SCENE_2D_DEPTH_OFFSET 0.0078125f
+
 __attribute__((__nonnull__)) static inline
 void asc_set_position(AscSceneNode *node, float x, float y, float z) {
     node->position.x = x;
@@ -187,7 +192,6 @@
 void asc_set_position2d(AscSceneNode *node, int x, int y) {
     node->position.x = (float)x;
     node->position.y = (float)y;
-    node->position.z = 0.f;
     asc_node_update_transform(node);
 }
 
@@ -208,7 +212,6 @@
 void asc_set_scale2d(AscSceneNode *node, int width, int height) {
     node->scale.width = (float)width;
     node->scale.height = (float)height;
-    node->scale.depth = 1.f;
     asc_node_update_transform(node);
 }
 
--- a/src/scene.c	Tue Apr 29 21:51:29 2025 +0200
+++ b/src/scene.c	Thu May 01 15:26:01 2025 +0200
@@ -87,8 +87,6 @@
         offsetof(AscSceneNode, next)
     );
     cx_foreach(AscSceneNode*, node, iter) {
-        node->depth = iter.depth;
-
         // skip hidden nodes (and all their children)
         if (asc_test_flag(node->flags, ASC_SCENE_NODE_HIDDEN)) {
             cxTreeVisitorContinue(iter);
--- a/src/scene_node.c	Tue Apr 29 21:51:29 2025 +0200
+++ b/src/scene_node.c	Thu May 01 15:26:01 2025 +0200
@@ -82,6 +82,7 @@
             offsetof(AscSceneNode, prev),
             offsetof(AscSceneNode, next)
     );
+    asc_node_update_transform(node);
 }
 
 void asc_scene_node_unlink(AscSceneNode *node) {
@@ -93,6 +94,7 @@
             offsetof(AscSceneNode, prev),
             offsetof(AscSceneNode, next)
     );
+    asc_node_update_transform(node);
 }
 
 // TODO: rename in asc_node_add_behavior or just asc_add_behavior()
--- a/src/sprite.c	Tue Apr 29 21:51:29 2025 +0200
+++ b/src/sprite.c	Thu May 01 15:26:01 2025 +0200
@@ -54,8 +54,10 @@
 
     node->position.x = (float) args.x;
     node->position.y = (float) args.y;
-    node->scale.x = (float) (args.width == 0 ? args.texture->width : args.width);
-    node->scale.y = (float) (args.height == 0 ? args.texture->height : args.height);
+    node->position.z = ASC_SCENE_2D_DEPTH_OFFSET;
+    node->scale.width = (float) (args.width == 0 ? args.texture->width : args.width);
+    node->scale.height = (float) (args.height == 0 ? args.texture->height : args.height);
+    node->scale.depth = 1.f;
     asc_node_update_transform(node);
 
     return node;
@@ -78,9 +80,6 @@
         asc_texture_bind(node->texture, shader->uv_tex, 1);
     }
 
-    // Apply depth
-    glUniform1f(shader->depth, (float)(node->data.depth));
-
     // Draw mesh
     asc_mesh_draw_triangle_strip(ASC_PRIMITIVE_PLANE);
 }
--- a/src/text.c	Tue Apr 29 21:51:29 2025 +0200
+++ b/src/text.c	Thu May 01 15:26:01 2025 +0200
@@ -89,6 +89,8 @@
     node->flags = args.alignment;
     node->position.x = (float) args.x;
     node->position.y = (float) args.y;
+    node->position.z = ASC_SCENE_2D_DEPTH_OFFSET;
+    node->scale.depth = 1.f;
     text->max_width = args.max_width;
     text->font = asc_active_font;
     text->color = asc_context.ink;

mercurial