src/scene.c

changeset 71
baa73a0be3ce
parent 69
86d545f490e4
child 72
84472fb3adbd
--- a/src/scene.c	Sun Aug 11 16:29:09 2024 +0200
+++ b/src/scene.c	Wed Aug 14 21:09:52 2024 +0200
@@ -58,22 +58,29 @@
     );
 }
 
-struct asc_render_group_entry {
-    asc_scene_draw_func draw;
-    AscSceneNode const *node;
-};
+static void asc_sprite_draw(AscSprite const *node) {
+    // Obtain shader
+    AscShaderSprite *shader = &asc_active_window->glctx.shader.sprite;
+
+    // Upload model matrix
+    glUniformMatrix4fv(shader->base.model, 1,
+                       GL_FALSE, node->data.world_transform);
 
-#define asc_draw_render_group(iter) \
-    cx_foreach(struct asc_render_group_entry*, entry, iter) { \
-        entry->draw(entry->node); \
-    }
+    // Bind texture
+    asc_texture_bind(&node->tex, shader->tex, 0);
+
+    // Apply depth
+    glUniform1f(shader->depth, (float)(node->data.depth));
+
+    // Draw mesh
+    asc_primitives_draw_plane();
+}
 
 void asc_scene_draw(AscSceneNode *root, asc_recti viewport, AscCamera *camera) {
     // create render groups
     CxList *render_group[ASC_RENDER_GROUP_COUNT];
     cx_for_n(i, ASC_RENDER_GROUP_COUNT) {
-        render_group[i] = cxArrayListCreateSimple(
-                sizeof(struct asc_render_group_entry), 32);
+        render_group[i] = cxArrayListCreateSimple(CX_STORE_POINTERS, 32);
     }
 
     // skip the root node deliberately, we know it's just the container
@@ -126,12 +133,7 @@
         }
 
         // add to render group
-        if (node->draw_func != NULL) {
-            struct asc_render_group_entry entry = {
-                    node->draw_func, node
-            };
-            cxListAdd(render_group[node->render_group], &entry);
-        }
+        cxListAdd(render_group[node->render_group], node);
     }
 
     // set the viewport (in OpenGL we need to invert the Y axis)
@@ -164,13 +166,17 @@
     // render opaque sprites from front to back
     glDisable(GL_BLEND);
     render_iter = cxListBackwardsIterator(render_group[ASC_RENDER_GROUP_SPRITE_OPAQUE]);
-    asc_draw_render_group(render_iter);
+    cx_foreach(AscSprite const *, node, render_iter) {
+        asc_sprite_draw(node);
+    }
 
     // render sprites with alpha value from back to front
     glEnable(GL_BLEND);
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     render_iter = cxListIterator(render_group[ASC_RENDER_GROUP_SPRITE_BLEND]);
-    asc_draw_render_group(render_iter);
+    cx_foreach(AscSprite const *, node, render_iter) {
+        asc_sprite_draw(node);
+    }
 
     // destroy render groups
     cx_for_n(i, ASC_RENDER_GROUP_COUNT) {

mercurial