Sat, 19 Apr 2025 11:42:53 +0200
add generic mesh draw function and move sprite draw function to separate unit
--- a/src/Makefile Fri Apr 18 20:13:01 2025 +0200 +++ b/src/Makefile Sat Apr 19 11:42:53 2025 +0200 @@ -27,8 +27,11 @@ BUILD_DIR=../build/lib -SRC = context.c glcontext.c error.c window.c shader.c font.c text.c \ - texture.c scene.c camera.c primitives.c mesh.c +SRC = context.c glcontext.c error.c window.c shader.c \ + mesh.c primitives.c texture.c \ + sprite.c \ + camera.c scene.c \ + font.c text.c OBJ = $(SRC:%.c=$(BUILD_DIR)/%.o) @@ -48,8 +51,8 @@ $(BUILD_DIR)/context.o: context.c ascension/context.h \ ascension/datatypes.h ascension/window.h ascension/glcontext.h \ ascension/primitives.h ascension/mesh.h ascension/shader.h \ - ascension/scene.h ascension/transform.h ascension/camera.h \ - ascension/texture.h ascension/input.h ascension/ui/font.h \ + ascension/scene.h ascension/scene_node.h ascension/transform.h \ + ascension/camera.h ascension/input.h ascension/ui/font.h \ ascension/error.h ascension/utils.h @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< @@ -57,7 +60,7 @@ $(BUILD_DIR)/error.o: error.c ascension/context.h ascension/datatypes.h \ ascension/window.h ascension/glcontext.h ascension/primitives.h \ ascension/mesh.h ascension/shader.h ascension/scene.h \ - ascension/transform.h ascension/camera.h ascension/texture.h \ + ascension/scene_node.h ascension/transform.h ascension/camera.h \ ascension/input.h ascension/ui/font.h ascension/error.h \ ascension/utils.h @echo "Compiling $<" @@ -66,8 +69,8 @@ $(BUILD_DIR)/font.o: font.c ascension/ui/font.h ascension/context.h \ ascension/datatypes.h ascension/window.h ascension/glcontext.h \ ascension/primitives.h ascension/mesh.h ascension/shader.h \ - ascension/scene.h ascension/transform.h ascension/camera.h \ - ascension/texture.h ascension/input.h ascension/ui/font.h \ + ascension/scene.h ascension/scene_node.h ascension/transform.h \ + ascension/camera.h ascension/input.h ascension/ui/font.h \ ascension/error.h @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< @@ -86,17 +89,18 @@ ascension/mesh.h ascension/error.h ascension/context.h \ ascension/datatypes.h ascension/window.h ascension/glcontext.h \ ascension/primitives.h ascension/shader.h ascension/scene.h \ - ascension/transform.h ascension/camera.h ascension/texture.h \ + ascension/scene_node.h ascension/transform.h ascension/camera.h \ ascension/input.h ascension/ui/font.h @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< $(BUILD_DIR)/scene.o: scene.c ascension/scene.h ascension/datatypes.h \ - ascension/transform.h ascension/camera.h ascension/texture.h \ + ascension/scene_node.h ascension/transform.h ascension/camera.h \ ascension/context.h ascension/window.h ascension/glcontext.h \ ascension/primitives.h ascension/mesh.h ascension/shader.h \ ascension/scene.h ascension/input.h ascension/ui/font.h \ - ascension/utils.h ascension/shader.h + ascension/utils.h ascension/2d.h ascension/2d/sprite.h \ + ascension/2d/../scene_node.h ascension/2d/../texture.h @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< @@ -104,14 +108,25 @@ @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< -$(BUILD_DIR)/text.o: text.c ascension/ui/text.h ascension/ui/font.h \ - ascension/ui/../scene.h ascension/ui/../datatypes.h \ - ascension/ui/../transform.h ascension/ui/../camera.h \ - ascension/ui/../texture.h ascension/ui/../utils.h ascension/context.h \ - ascension/datatypes.h ascension/window.h ascension/glcontext.h \ - ascension/primitives.h ascension/mesh.h ascension/shader.h \ - ascension/scene.h ascension/input.h ascension/ui/font.h \ - ascension/error.h ascension/shader.h +$(BUILD_DIR)/sprite.o: sprite.c ascension/2d/sprite.h \ + ascension/2d/../scene_node.h ascension/2d/../datatypes.h \ + ascension/2d/../transform.h ascension/2d/../texture.h \ + ascension/context.h ascension/datatypes.h ascension/window.h \ + ascension/glcontext.h ascension/primitives.h ascension/mesh.h \ + ascension/shader.h ascension/scene.h ascension/scene_node.h \ + ascension/camera.h ascension/input.h ascension/ui/font.h \ + ascension/glcontext.h + @echo "Compiling $<" + $(CC) -o $@ $(CFLAGS) -c $< + +$(BUILD_DIR)/text.o: text.c ascension/context.h ascension/datatypes.h \ + ascension/window.h ascension/glcontext.h ascension/primitives.h \ + ascension/mesh.h ascension/shader.h ascension/scene.h \ + ascension/scene_node.h ascension/transform.h ascension/camera.h \ + ascension/input.h ascension/ui/font.h ascension/error.h \ + ascension/ui/text.h ascension/ui/font.h ascension/ui/../2d/sprite.h \ + ascension/ui/../2d/../scene_node.h ascension/ui/../2d/../texture.h \ + ascension/ui/../utils.h @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< @@ -121,8 +136,8 @@ $(BUILD_DIR)/window.o: window.c ascension/window.h ascension/datatypes.h \ ascension/glcontext.h ascension/primitives.h ascension/mesh.h \ - ascension/shader.h ascension/scene.h ascension/transform.h \ - ascension/camera.h ascension/texture.h ascension/context.h \ + ascension/shader.h ascension/scene.h ascension/scene_node.h \ + ascension/transform.h ascension/camera.h ascension/context.h \ ascension/window.h ascension/input.h ascension/ui/font.h \ ascension/error.h ascension/utils.h @echo "Compiling $<"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ascension/2d.h Sat Apr 19 11:42:53 2025 +0200 @@ -0,0 +1,34 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2025 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef ASCENSION_2D_H +#define ASCENSION_2D_H + +#include "2d/sprite.h" + +#endif /* ASCENSION_2D_H */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ascension/2d/sprite.h Sat Apr 19 11:42:53 2025 +0200 @@ -0,0 +1,41 @@ +/* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2025 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef ASCENSION_SPRITE_H +#define ASCENSION_SPRITE_H + +#include "../scene_node.h" +#include "../texture.h" + +typedef struct AscSprite { + AscSceneNode data; + AscTexture tex; +} AscSprite; + +void asc_sprite_draw(AscSprite const *sprite); + +#endif //ASCENSION_SPRITE_H
--- a/src/ascension/glcontext.h Fri Apr 18 20:13:01 2025 +0200 +++ b/src/ascension/glcontext.h Sat Apr 19 11:42:53 2025 +0200 @@ -51,6 +51,8 @@ } shader; } AscGLContext; +#define ASC_PRIMITIVES_PLANE (&asc_active_window->glctx.primitives.plane) + #define ASC_SHADER_SPRITE (&asc_active_window->glctx.shader.sprite) __attribute__((__nonnull__, __warn_unused_result__))
--- a/src/ascension/mesh.h Fri Apr 18 20:13:01 2025 +0200 +++ b/src/ascension/mesh.h Sat Apr 19 11:42:53 2025 +0200 @@ -50,4 +50,11 @@ */ void asc_mesh_free_buffers(AscMesh *mesh, unsigned count); +/** + * Draws the mesh as a triangle strip. + * + * @param mesh the mesh to draw + */ +void asc_mesh_draw_triangle_strip(AscMesh *mesh); + #endif //ASCENSION_MESH_H
--- a/src/ascension/primitives.h Fri Apr 18 20:13:01 2025 +0200 +++ b/src/ascension/primitives.h Sat Apr 19 11:42:53 2025 +0200 @@ -34,6 +34,4 @@ void asc_primitives_init_plane(AscMesh *mesh); -void asc_primitives_draw_plane(void); - #endif //ASCENSION_PRIMITIVES_H
--- a/src/ascension/scene.h Fri Apr 18 20:13:01 2025 +0200 +++ b/src/ascension/scene.h Sat Apr 19 11:42:53 2025 +0200 @@ -29,50 +29,8 @@ #define ASCENSION_SCENE_H #include "datatypes.h" -#include "transform.h" +#include "scene_node.h" #include "camera.h" -#include "texture.h" - -#include <cx/list.h> - -typedef struct AscSceneNode AscSceneNode; - -typedef void(*asc_scene_free_func)(AscSceneNode*); -typedef void(*asc_scene_update_func)(AscSceneNode*); - -enum AscRenderGroup { - ASC_RENDER_GROUP_SPRITE_OPAQUE, - ASC_RENDER_GROUP_SPRITE_BLEND, - ASC_RENDER_GROUP_COUNT -}; - -struct AscSceneNode { - AscSceneNode *parent; - AscSceneNode *prev; - AscSceneNode *next; - AscSceneNode *children; - AscSceneNode *last_child; - 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; - asc_transform transform; - asc_transform world_transform; - enum AscRenderGroup render_group; - /** - * Custom flags for this node. - * The #ASC_SCENE_NODE_FLAGS_MASK bits are reserved for general flags. - */ - uint32_t flags; -}; - -typedef struct AscSprite { - AscSceneNode data; - AscTexture tex; -} AscSprite; /** * The reserved bits for general flags.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ascension/scene_node.h Sat Apr 19 11:42:53 2025 +0200 @@ -0,0 +1,70 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2025 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef ASCENSION_SCENE_NODE_H +#define ASCENSION_SCENE_NODE_H + +#include <cx/list.h> + +#include "datatypes.h" +#include "transform.h" + +typedef struct asc_scene_node_s AscSceneNode; + +typedef void(*asc_scene_free_func)(AscSceneNode*); +typedef void(*asc_scene_update_func)(AscSceneNode*); + +enum AscRenderGroup { + ASC_RENDER_GROUP_SPRITE_OPAQUE, + ASC_RENDER_GROUP_SPRITE_BLEND, + ASC_RENDER_GROUP_COUNT +}; + +struct asc_scene_node_s { + AscSceneNode *parent; + AscSceneNode *prev; + AscSceneNode *next; + AscSceneNode *children; + AscSceneNode *last_child; + 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; + asc_transform transform; + asc_transform world_transform; + enum AscRenderGroup render_group; + /** + * Custom flags for this node. + * The #ASC_SCENE_NODE_FLAGS_MASK bits are reserved for general flags. + */ + uint32_t flags; +}; + +#endif
--- a/src/ascension/ui/text.h Fri Apr 18 20:13:01 2025 +0200 +++ b/src/ascension/ui/text.h Sat Apr 19 11:42:53 2025 +0200 @@ -29,7 +29,7 @@ #define ASCENSION_UI_TEXT_H #include "font.h" -#include "../scene.h" +#include "../2d/sprite.h" #include "../utils.h" #include <cx/string.h>
--- a/src/mesh.c Fri Apr 18 20:13:01 2025 +0200 +++ b/src/mesh.c Sat Apr 19 11:42:53 2025 +0200 @@ -62,4 +62,13 @@ glDeleteBuffers(count, buffers); glDeleteVertexArrays(count, arrays); asc_error_catch_all_gl(); -} \ No newline at end of file +} + +void asc_mesh_draw_triangle_strip(AscMesh *mesh) { + glBindVertexArray(mesh->vao); + glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vertices); +#ifndef NDEBUG + // only unbind in debug mode to detect accidental re-use of the wrong VAO + glBindVertexArray(0); +#endif +}
--- a/src/primitives.c Fri Apr 18 20:13:01 2025 +0200 +++ b/src/primitives.c Sat Apr 19 11:42:53 2025 +0200 @@ -52,9 +52,3 @@ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(0); } - -void asc_primitives_draw_plane(void) { - AscMesh const *mesh = &(asc_active_window->glctx.primitives.plane); - glBindVertexArray(mesh->vao); - glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vertices); -} \ No newline at end of file
--- a/src/scene.c Fri Apr 18 20:13:01 2025 +0200 +++ b/src/scene.c Sat Apr 19 11:42:53 2025 +0200 @@ -30,12 +30,13 @@ #include "ascension/context.h" #include "ascension/utils.h" +#include "ascension/2d.h" + #include <cx/linked_list.h> #include <cx/array_list.h> #include <cx/tree.h> #include <cx/utils.h> -#include "ascension/shader.h" #include <GL/glew.h> #include <assert.h> @@ -58,24 +59,6 @@ ); } -static void asc_sprite_draw(AscSprite const *node) { - // Obtain shader - AscShaderSprite *shader = ASC_SHADER_SPRITE; - - // Upload model matrix - glUniformMatrix4fv(shader->program.model, 1, - GL_FALSE, node->data.world_transform); - - // 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];
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sprite.c Sat Apr 19 11:42:53 2025 +0200 @@ -0,0 +1,51 @@ +/* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2025 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ascension/2d/sprite.h" + +#include "ascension/context.h" +#include "ascension/glcontext.h" + +#include <GL/glew.h> + +void asc_sprite_draw(AscSprite const *node) { + // Obtain shader + AscShaderSprite *shader = ASC_SHADER_SPRITE; + + // Upload model matrix + glUniformMatrix4fv(shader->program.model, 1, + GL_FALSE, node->data.world_transform); + + // Bind texture + asc_texture_bind(&node->tex, shader->tex, 0); + + // Apply depth + glUniform1f(shader->depth, (float)(node->data.depth)); + + // Draw mesh + asc_mesh_draw_triangle_strip(ASC_PRIMITIVES_PLANE); +}
--- a/src/text.c Fri Apr 18 20:13:01 2025 +0200 +++ b/src/text.c Sat Apr 19 11:42:53 2025 +0200 @@ -25,10 +25,9 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "ascension/ui/text.h" #include "ascension/context.h" #include "ascension/error.h" -#include "ascension/shader.h" +#include "ascension/ui/text.h" #include <cx/printf.h>
--- a/test/Makefile Fri Apr 18 20:13:01 2025 +0200 +++ b/test/Makefile Sat Apr 19 11:42:53 2025 +0200 @@ -46,12 +46,13 @@ ../src/ascension/datatypes.h ../src/ascension/window.h \ ../src/ascension/glcontext.h ../src/ascension/primitives.h \ ../src/ascension/mesh.h ../src/ascension/shader.h \ - ../src/ascension/scene.h ../src/ascension/transform.h \ - ../src/ascension/camera.h ../src/ascension/texture.h \ + ../src/ascension/scene.h ../src/ascension/scene_node.h \ + ../src/ascension/transform.h ../src/ascension/camera.h \ ../src/ascension/input.h ../src/ascension/ui/font.h \ ../src/ascension/ui.h ../src/ascension/ui/text.h \ - ../src/ascension/ui/font.h ../src/ascension/ui/../scene.h \ - ../src/ascension/ui/../utils.h + ../src/ascension/ui/font.h ../src/ascension/ui/../2d/sprite.h \ + ../src/ascension/ui/../2d/../scene_node.h \ + ../src/ascension/ui/../2d/../texture.h ../src/ascension/ui/../utils.h @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $<