summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-08-11 23:00:26 +1000
committeromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-08-11 23:00:26 +1000
commitb03bde3d412148cd573f5f14012cdd270f309947 (patch)
tree848af582079a60787cc5a5f8138e7ca6d508f2ee /src
parent48a703e52490cb52fd32e54e3e37f7e70462a267 (diff)
starting work on immediate mode drawing
Diffstat (limited to 'src')
-rw-r--r--src/animation.h6
-rw-r--r--src/collision.h20
-rw-r--r--src/core/core.c12
-rw-r--r--src/core/core.h1
-rw-r--r--src/maths/geometry.h12
-rw-r--r--src/maths/primitives.c22
-rw-r--r--src/physics.h (renamed from src/physics/physics.h)0
-rw-r--r--src/physics/broadphase.h10
-rw-r--r--src/physics/collision.h30
-rw-r--r--src/physics/narrowphase.h10
-rw-r--r--src/physics/physics.c1
-rw-r--r--src/platform/mutex.c9
-rw-r--r--src/platform/mutex.h28
-rw-r--r--src/platform/path.c35
-rw-r--r--src/platform/path.h16
-rw-r--r--src/platform/platform.h37
-rw-r--r--src/platform/platform_unix.c16
-rw-r--r--src/platform/platform_windows.c22
-rw-r--r--src/platform/thread.c0
-rw-r--r--src/platform/thread.h17
-rw-r--r--src/ral/backends/opengl/backend_opengl.c5
-rw-r--r--src/ral/backends/opengl/opengl_helpers.h4
-rw-r--r--src/render/immdraw.c94
-rw-r--r--src/render/immdraw.h29
-rw-r--r--src/render/render.c21
-rw-r--r--src/render/render.h2
-rw-r--r--src/render/render_types.h2
-rw-r--r--src/resources/gltf.c14
-rw-r--r--src/resources/obj.c3
-rw-r--r--src/scene.c57
-rw-r--r--src/scene.h54
-rw-r--r--src/std/containers/darray.h6
-rw-r--r--src/std/str.h2
-rw-r--r--src/ui/ui.h13
34 files changed, 261 insertions, 349 deletions
diff --git a/src/animation.h b/src/animation.h
index 66277e9..343746a 100644
--- a/src/animation.h
+++ b/src/animation.h
@@ -4,7 +4,11 @@
#include "defines.h"
#include "maths_types.h"
-typedef enum Interpolation { INTERPOLATION_LINEAR, INTERPOLATION_COUNT } Interpolation;
+typedef enum Interpolation {
+ INTERPOLATION_STEP,
+ INTERPOLATION_LINEAR,
+ INTERPOLATION_CUBIC, /** @brief Cubic spline interpolation */
+ INTERPOLATION_COUNT } Interpolation;
typedef enum KeyframeKind {
KEYFRAME_ROTATION,
diff --git a/src/collision.h b/src/collision.h
new file mode 100644
index 0000000..4ac9ec3
--- /dev/null
+++ b/src/collision.h
@@ -0,0 +1,20 @@
+#pragma once
+#include "geometry.h"
+
+enum ColliderType {
+ CuboidCollider,
+ SphereCollider,
+};
+
+/** @brief generic collider structure */
+typedef struct Collider {
+ u64 id; // ? Replace with handle?
+ enum ColliderType shape;
+ union collider_data {
+ Cuboid cuboid;
+ Sphere sphere;
+ } geometry;
+ Transform transform;
+ u8 layer;
+ bool on_ground;
+} Collider;
diff --git a/src/core/core.c b/src/core/core.c
index 7d209d5..64f59f3 100644
--- a/src/core/core.c
+++ b/src/core/core.c
@@ -9,7 +9,6 @@
#include "mem.h"
#include "render.h"
#include "render_types.h"
-#include "scene.h"
// These are only the initial window dimensions
#define SCR_WIDTH 1000
@@ -30,7 +29,9 @@ void Core_Bringup(const char* window_name, struct GLFWwindow* optional_window) {
.clear_colour = (Vec3){ .08, .08, .1 } };
g_core.renderer = malloc(Renderer_GetMemReqs());
- // initialise all subsystems
+
+ // Initialise all subsystems
+
// renderer config, renderer ptr, ptr to store a window, and optional preexisting glfw window
if (!Renderer_Init(conf, g_core.renderer, &g_core.window, optional_window)) {
// FATAL("Failed to start renderer");
@@ -52,9 +53,6 @@ void Core_Bringup(const char* window_name, struct GLFWwindow* optional_window) {
Model_pool model_pool = Model_pool_create(&model_arena, 256, sizeof(Model));
g_core.models = model_pool;
INFO("Created model pool allocator");
-
- // INFO("Creating default scene");
- // scene_init(&g_core.default_scene);
}
void Core_Shutdown() {
@@ -78,8 +76,6 @@ Core* get_global_core() { return &g_core; }
GLFWwindow* Core_GetGlfwWindowPtr(Core* core) { return g_core.window; }
-struct Renderer* Core_GetRenderer(Core* core) {
- return core->renderer;
-}
+struct Renderer* Core_GetRenderer(Core* core) { return core->renderer; }
Model* Model_Get(ModelHandle h) { return Model_pool_get(&g_core.models, h); }
diff --git a/src/core/core.h b/src/core/core.h
index 4e7ff03..be44b8c 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -3,7 +3,6 @@
#include "input.h"
#include "mem.h"
#include "render_types.h"
-#include "scene.h"
#include "screenspace.h"
#include "terrain.h"
#include "text.h"
diff --git a/src/maths/geometry.h b/src/maths/geometry.h
index 0df80fc..532651c 100644
--- a/src/maths/geometry.h
+++ b/src/maths/geometry.h
@@ -19,13 +19,13 @@
// vec3 normal;
// } plane;
-// typedef struct cuboid {
-// vec3 half_extents;
-// } cuboid;
+typedef struct Cuboid {
+ Vec3 half_extents;
+} Cuboid;
-// typedef struct sphere {
-// f32 radius;
-// } sphere;
+typedef struct Sphere {
+ f32 radius;
+} Sphere;
// typedef struct cylinder {
// f32 radius;
diff --git a/src/maths/primitives.c b/src/maths/primitives.c
index 4a4d55f..d353d33 100644
--- a/src/maths/primitives.c
+++ b/src/maths/primitives.c
@@ -35,22 +35,25 @@ Geometry Geo_CreatePlane(f32x2 extents) {
vert_pos[i].x *= extents.x;
vert_pos[i].z *= extents.y;
}
- VERT_3D(vertices, vert_pos[0], VEC3_Y, vec2(0, 0));
- VERT_3D(vertices, vert_pos[1], VEC3_Y, vec2(1, 0));
- VERT_3D(vertices, vert_pos[2], VEC3_Y, vec2(0, 1));
- VERT_3D(vertices, vert_pos[3], VEC3_Y, vec2(1, 1));
+ VERT_3D(vertices, vert_pos[0], VEC3_Y, vec2(0, 0)); // back left
+ VERT_3D(vertices, vert_pos[1], VEC3_Y, vec2(1, 0)); // back right
+ VERT_3D(vertices, vert_pos[2], VEC3_Y, vec2(0, 1)); // front left
+ VERT_3D(vertices, vert_pos[3], VEC3_Y, vec2(1, 1)); // front right
- /* push_triangle(indices, 0, 1, 2); */
- /* push_triangle(indices, 2, 1, 3); */
- push_triangle(indices, 2, 1, 0);
- push_triangle(indices, 3, 1, 2);
+ push_triangle(indices, 0, 1, 2);
+ push_triangle(indices, 2, 1, 3);
+ // push_triangle(indices, 2, 1, 0);
+ // push_triangle(indices, 3, 2, 1);
+
+ for (int i = 0; i < 4; i++) {
+ printf("Vertex %d: (%f, %f, %f)\n", i, vert_pos[i].x, vert_pos[i].y, vert_pos[i].z);
+ }
Geometry geo = { .format = VERTEX_STATIC_3D,
.vertices = vertices,
.has_indices = true,
.index_count = indices->len,
.indices = indices };
- // .colour = (rgba){ 0, 0, 0, 1 } };
return geo;
}
@@ -239,5 +242,6 @@ Geometry Geo_CreateUVsphere(f32 radius, u32 north_south_lines, u32 east_west_lin
.index_count = indices->len,
.indices = indices,
};
+
return geo;
}
diff --git a/src/physics/physics.h b/src/physics.h
index e0e3b89..e0e3b89 100644
--- a/src/physics/physics.h
+++ b/src/physics.h
diff --git a/src/physics/broadphase.h b/src/physics/broadphase.h
deleted file mode 100644
index 8b49716..0000000
--- a/src/physics/broadphase.h
+++ /dev/null
@@ -1,10 +0,0 @@
-/**
- * @file broadphase.h
- * @author your name (you@domain.com)
- * @brief
- * @version 0.1
- * @date 2024-05-12
- *
- * @copyright Copyright (c) 2024
- *
- */ \ No newline at end of file
diff --git a/src/physics/collision.h b/src/physics/collision.h
deleted file mode 100644
index cca6042..0000000
--- a/src/physics/collision.h
+++ /dev/null
@@ -1,30 +0,0 @@
-/**
- * @file collision.h
- * @author your name (you@domain.com)
- * @brief
- * @version 0.1
- * @date 2024-05-12
- *
- * @copyright Copyright (c) 2024
- *
- */
-#pragma once
-#include "geometry.h"
-
-enum collider_type {
- cuboid_collider,
- sphere_collider,
-};
-
-/** @brief generic collider structure */
-typedef struct physics_collider {
- u64 id; // ? Replace with handle?
- enum collider_type shape;
- union collider_data {
- cuboid cuboid;
- sphere sphere;
- } geometry;
- transform transform;
- u8 layer;
- bool on_ground;
-} physics_collider; \ No newline at end of file
diff --git a/src/physics/narrowphase.h b/src/physics/narrowphase.h
deleted file mode 100644
index 2368c49..0000000
--- a/src/physics/narrowphase.h
+++ /dev/null
@@ -1,10 +0,0 @@
-/**
- * @file narrowphase.h
- * @author your name (you@domain.com)
- * @brief
- * @version 0.1
- * @date 2024-05-12
- *
- * @copyright Copyright (c) 2024
- *
- */ \ No newline at end of file
diff --git a/src/physics/physics.c b/src/physics/physics.c
deleted file mode 100644
index 299c0c1..0000000
--- a/src/physics/physics.c
+++ /dev/null
@@ -1 +0,0 @@
-#include "physics.h" \ No newline at end of file
diff --git a/src/platform/mutex.c b/src/platform/mutex.c
deleted file mode 100644
index 2aeb825..0000000
--- a/src/platform/mutex.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include "mutex.h"
-
-#if defined(CEL_PLATFORM_LINUX) || defined(CEL_PLATFORM_MAC)
-// TODO: implement in terms of pthreads
-#endif
-
-#if defined(CEL_PLATFORM_WINDOWS)
-// TODO: implement using win32 api
-#endif \ No newline at end of file
diff --git a/src/platform/mutex.h b/src/platform/mutex.h
deleted file mode 100644
index a0a4208..0000000
--- a/src/platform/mutex.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/**
- * @file mutex.h
- * @author your name (you@domain.com)
- * @brief
- * @version 0.1
- * @date 2024-04-27
- *
- * @copyright Copyright (c) 2024
- *
- */
-#include <stdbool.h>
-
-typedef struct cel_mutex cel_mutex;
-
-cel_mutex mutex_create();
-
-void mutex_destroy(cel_mutex* mutex);
-
-/** @brief Blocks until the mutex can be acquired. if returns false then an error occurred and can
- * be checked (TODO) */
-bool mutex_lock(cel_mutex* mutex);
-
-/** @brief Tries to acquire the mutex like `mutex_lock` but returns immediately if the mutex has
- * already been locked */
-bool mutex_try_lock(cel_mutex* mutex);
-
-/** @brief Releases a mutex. If it is already unlocked then does nothing */
-void mutex_unlock(cel_mutex* mutex); \ No newline at end of file
diff --git a/src/platform/path.c b/src/platform/path.c
deleted file mode 100644
index f43d954..0000000
--- a/src/platform/path.c
+++ /dev/null
@@ -1,35 +0,0 @@
-#include "path.h"
-
-#include <stdlib.h>
-#include <string.h>
-#include "mem.h"
-#include "str.h"
-
-#if defined(CEL_PLATFORM_LINUX) || defined(CEL_PLATFORM_MAC)
-#include <libgen.h>
-path_opt path_parent(arena* a, const char* path) {
- // Duplicate the string because dirname doesnt like const literals
- char* path_copy = arena_alloc(a, strlen(path) + 1);
- strcpy(path_copy, path);
- char* path_dirname = dirname(path_copy);
- return (path_opt){ .path = Str8_cstr_view(path_dirname), .has_value = true };
-}
-#endif
-#ifdef CEL_PLATFORM_WINDOWS
-#include <shlwapi.h>
-#include <windows.h>
-#pragma comment(lib, "Shlwapi.lib")
-
-path_opt path_parent(arena* a, const char* path) {
- // Duplicate the string because PathRemoveFileSpec mutates in-place
- size_t len = strlen(path) + 1;
- char* path_copy = arena_alloc(a, len);
- strcpy_s(path_copy, len, path);
-
- if (PathRemoveFileSpecA(path_copy)) {
- return (path_opt){ .path = Str8_cstr_view(path_copy), .has_value = true };
- } else {
- return (path_opt){ .has_value = false };
- }
-}
-#endif
diff --git a/src/platform/path.h b/src/platform/path.h
deleted file mode 100644
index ce53339..0000000
--- a/src/platform/path.h
+++ /dev/null
@@ -1,16 +0,0 @@
-/**
- * @file path.h
- * @brief
- * @date 2024-03-11
- * @copyright Copyright (c) 2024
- */
-#pragma once
-
-#include "str.h"
-
-typedef struct path_opt {
- Str8 path;
- bool has_value;
-} path_opt;
-
-path_opt path_parent(arena* a, const char* path); // TODO: convert to using str8
diff --git a/src/platform/platform.h b/src/platform/platform.h
new file mode 100644
index 0000000..c2be630
--- /dev/null
+++ b/src/platform/platform.h
@@ -0,0 +1,37 @@
+#pragma once
+
+#include "defines.h"
+#include "str.h"
+
+// -- Paths
+typedef struct path_opt {
+ Str8 path;
+ bool has_value;
+} path_opt;
+
+// TODO: convert to using str8
+// TODO: use uppercase code style
+path_opt path_parent(arena* a, const char* path);
+
+// --- Threads
+typedef struct CelThread CelThread;
+
+CelThread Thread_Create();
+void Thread_Destroy(CelThread* thread);
+
+// --- Mutexes
+typedef struct CelMutex CelMutex;
+
+CelMutex Mutex_Create();
+void Mutex_Destroy(CelMutex* mutex);
+
+/** @brief Blocks until the mutex can be acquired. if returns false then an error occurred and can
+ * be checked (TODO) */
+bool Mutex_Lock(CelMutex* mutex);
+
+/** @brief Tries to acquire the mutex like `mutex_lock` but returns immediately if the mutex has
+ * already been locked */
+bool Mutex_TryLock(CelMutex* mutex);
+
+/** @brief Releases a mutex. If it is already unlocked then does nothing */
+void Mutex_Unlock(CelMutex* mutex);
diff --git a/src/platform/platform_unix.c b/src/platform/platform_unix.c
new file mode 100644
index 0000000..86ac170
--- /dev/null
+++ b/src/platform/platform_unix.c
@@ -0,0 +1,16 @@
+#include "platform.h"
+
+#if defined(CEL_PLATFORM_LINUX) || defined(CEL_PLATFORM_MAC)
+
+#include <libgen.h>
+#include <string.h>
+
+path_opt path_parent(arena* a, const char* path) {
+ // Duplicate the string because dirname doesnt like const literals
+ char* path_copy = arena_alloc(a, strlen(path) + 1);
+ strcpy(path_copy, path);
+ char* path_dirname = dirname(path_copy);
+ return (path_opt){ .path = Str8_cstr_view(path_dirname), .has_value = true };
+}
+
+#endif
diff --git a/src/platform/platform_windows.c b/src/platform/platform_windows.c
new file mode 100644
index 0000000..21ef359
--- /dev/null
+++ b/src/platform/platform_windows.c
@@ -0,0 +1,22 @@
+#include "platform.h"
+
+#if defined(CEL_PLATFORM_WINDOWS)
+
+#include <shlwapi.h>
+#include <windows.h>
+#pragma comment(lib, "Shlwapi.lib")
+
+path_opt path_parent(arena* a, const char* path) {
+ // Duplicate the string because PathRemoveFileSpec mutates in-place
+ size_t len = strlen(path) + 1;
+ char* path_copy = arena_alloc(a, len);
+ strcpy_s(path_copy, len, path);
+
+ if (PathRemoveFileSpecA(path_copy)) {
+ return (path_opt){ .path = Str8_cstr_view(path_copy), .has_value = true };
+ } else {
+ return (path_opt){ .has_value = false };
+ }
+}
+
+#endif
diff --git a/src/platform/thread.c b/src/platform/thread.c
deleted file mode 100644
index e69de29..0000000
--- a/src/platform/thread.c
+++ /dev/null
diff --git a/src/platform/thread.h b/src/platform/thread.h
deleted file mode 100644
index af07d3f..0000000
--- a/src/platform/thread.h
+++ /dev/null
@@ -1,17 +0,0 @@
-/**
- * @file thread.h
- * @author your name (you@domain.com)
- * @brief
- * @version 0.1
- * @date 2024-04-27
- *
- * @copyright Copyright (c) 2024
- *
- */
-
-typedef struct cel_thread cel_thread;
-
-cel_thread thread_create();
-void thread_destroy(cel_thread* thread);
-
-// join \ No newline at end of file
diff --git a/src/ral/backends/opengl/backend_opengl.c b/src/ral/backends/opengl/backend_opengl.c
index 7862a81..3822220 100644
--- a/src/ral/backends/opengl/backend_opengl.c
+++ b/src/ral/backends/opengl/backend_opengl.c
@@ -57,8 +57,7 @@ bool GPU_Backend_Init(const char* window_name, struct GLFWwindow* window,
}
// All of these are no-ops in OpenGL
-void GPU_Backend_Shutdown() { /* TODO */
-}
+void GPU_Backend_Shutdown() { /* TODO */ }
bool GPU_Device_Create(GPU_Device* out_device) { return true; }
void GPU_Device_Destroy(GPU_Device* device) {}
bool GPU_Swapchain_Create(GPU_Swapchain* out_swapchain) { return true; }
@@ -430,7 +429,7 @@ PUB void GPU_WriteTextureRegion(GPU_CmdEncoder* encoder, TextureHandle dst, u32
bool GPU_Backend_BeginFrame() {
glViewport(0, 0, context.swapchain.dimensions.x * 2, context.swapchain.dimensions.y * 2);
- glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
+ glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
return true;
}
diff --git a/src/ral/backends/opengl/opengl_helpers.h b/src/ral/backends/opengl/opengl_helpers.h
index 0450c74..8a78de5 100644
--- a/src/ral/backends/opengl/opengl_helpers.h
+++ b/src/ral/backends/opengl/opengl_helpers.h
@@ -57,7 +57,7 @@ static u32 opengl_bindcreate_vao(GPU_Buffer* buf, VertexDescription desc) {
// Attributes
u32 attr_count = desc.attributes_count;
- printf("N attributes %d\n", attr_count);
+ // printf("N attributes %d\n", attr_count);
u64 offset = 0;
size_t vertex_size = desc.use_full_vertex_size ? sizeof(Vertex) : VertexDesc_CalcStride(&desc);
for (u32 i = 0; i < desc.attributes_count; i++) {
@@ -67,7 +67,7 @@ static u32 opengl_bindcreate_vao(GPU_Buffer* buf, VertexDescription desc) {
desc.attr_names[i]);
glEnableVertexAttribArray(i); // nth index
size_t this_offset = VertexAttribSize(desc.attributes[i]);
- printf("offset total %lld this attr %zu\n", offset, this_offset);
+ // printf("offset total %lld this attr %zu\n", offset, this_offset);
offset += this_offset;
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
diff --git a/src/render/immdraw.c b/src/render/immdraw.c
index c711b0c..dfe189a 100644
--- a/src/render/immdraw.c
+++ b/src/render/immdraw.c
@@ -1,5 +1,8 @@
#include "immdraw.h"
+#include "core.h"
+#include "file.h"
#include "log.h"
+#include "maths.h"
#include "primitives.h"
#include "ral_common.h"
#include "ral_impl.h"
@@ -9,20 +12,91 @@
void Immdraw_Init(Immdraw_Storage* storage) {
INFO("Immediate drawing initialisation");
- // meshes
- // Geometry sphere_geo = Geo_CreateUVsphere(1.0, 8, 8);
- // storage->sphere = Mesh_Create(&sphere_geo, false);
- // pipeline / material
+ // Meshes
+ Geometry sphere_geo = Geo_CreateUVsphere(1.0, 8, 8);
+ storage->sphere = Mesh_Create(&sphere_geo, false);
+
+ Geometry cube_geo = Geo_CreateCuboid(f32x3(2.0, 2.0, 2.0));
+ storage->cube = Mesh_Create(&cube_geo, false);
+
+ Geometry plane_geo = Geo_CreatePlane(f32x2(2.0, 2.0));
+ storage->plane = Mesh_Create(&plane_geo, false);
+
+ // Pipeline / material
+ VertexDescription vertex_desc = {
+ .debug_label = "Immdraw Vertex",
+ .use_full_vertex_size = true,
+ };
+ VertexDesc_AddAttr(&vertex_desc, "position", ATTR_F32x3);
+ VertexDesc_AddAttr(&vertex_desc, "normal", ATTR_F32x3);
+
+ arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024);
+ Str8 vert_path = str8("assets/shaders/immdraw.vert");
+ Str8 frag_path = str8("assets/shaders/immdraw.frag");
+ str8_opt vertex_shader = str8_from_file(&scratch, vert_path);
+ str8_opt fragment_shader = str8_from_file(&scratch, frag_path);
+ if (!vertex_shader.has_value || !fragment_shader.has_value) {
+ ERROR_EXIT("Failed to load shaders from disk");
+ }
+
ShaderDataLayout camera_data = Binding_Camera_GetLayout(NULL);
+ ShaderDataLayout imm_uniform_data = ImmediateUniforms_GetLayout(NULL);
+
GraphicsPipelineDesc pipeline_desc = {
.debug_name = "Immediate Draw Pipeline",
- .data_layouts = { camera_data },
- .data_layouts_count = 1,
-
+ .vertex_desc = static_3d_vertex_description(),
+ .data_layouts = { camera_data, imm_uniform_data },
+ .data_layouts_count = 2,
+ .vs = { .debug_name = "Immdraw Vertex Shader",
+ .filepath = vert_path,
+ .code = vertex_shader.contents },
+ .fs = { .debug_name = "Immdraw Fragment Shader",
+ .filepath = frag_path,
+ .code = fragment_shader.contents },
+ .depth_test = true,
+ .wireframe = false,
};
- // storage->colour_pipeline = GPU_GraphicsPipeline_Create(pipeline_desc,
- // GPU_GetDefaultRenderpass());
+ GPU_Renderpass* rpass =
+ GPU_Renderpass_Create((GPU_RenderpassDesc){ .default_framebuffer = true });
+ storage->colour_pipeline = GPU_GraphicsPipeline_Create(pipeline_desc, rpass);
}
-void Immdraw_Sphere(Transform tf, f32 size, Vec4 colour, bool wireframe) {} \ No newline at end of file
+void Immdraw_Shutdown(Immdraw_Storage* storage) {
+ GraphicsPipeline_Destroy(storage->colour_pipeline);
+}
+
+void Immdraw_Sphere(Transform tf, f32 size, Vec4 colour, bool wireframe) {
+ INFO("Draw sphere");
+ Immdraw_Storage* imm = Render_GetImmdrawStorage();
+ GPU_CmdEncoder* enc = GPU_GetDefaultEncoder();
+
+ // begin renderpass
+ GPU_CmdEncoder_BeginRender(enc, imm->colour_pipeline->renderpass);
+ // bind pipeline
+ GPU_EncodeBindPipeline(enc, imm->colour_pipeline);
+
+ // update uniforms
+ ImmediateUniforms uniforms = {
+ .model = transform_to_mat(&tf),
+ .colour = colour,
+ };
+ Mat4 view, proj;
+ u32x2 dimensions = GPU_Swapchain_GetDimensions();
+ RenderScene* scene = Render_GetScene();
+ Camera_ViewProj(&scene->camera, (f32)dimensions.x, (f32)dimensions.y, &view, &proj);
+ Binding_Camera camera_data = { .view = view,
+ .projection = proj,
+ .viewPos = vec4(scene->camera.position.x, scene->camera.position.y,
+ scene->camera.position.z, 1.0) };
+ GPU_EncodeBindShaderData(enc, 0, Binding_Camera_GetLayout(&camera_data));
+ GPU_EncodeBindShaderData(enc, 1, ImmediateUniforms_GetLayout(&uniforms));
+
+ // draw call
+ GPU_EncodeSetVertexBuffer(enc, imm->plane.vertex_buffer);
+ GPU_EncodeSetIndexBuffer(enc, imm->plane.index_buffer);
+ GPU_EncodeDrawIndexed(enc, imm->plane.geometry.index_count);
+
+ // end renderpass
+ GPU_CmdEncoder_EndRender(enc);
+}
diff --git a/src/render/immdraw.h b/src/render/immdraw.h
index 0d58375..e635531 100644
--- a/src/render/immdraw.h
+++ b/src/render/immdraw.h
@@ -6,15 +6,21 @@
#include "defines.h"
#include "maths_types.h"
#include "ral_impl.h"
+#include "ral_types.h"
#include "render_types.h"
typedef struct Immdraw_Storage {
Mesh plane;
Mesh cube;
Mesh sphere;
- GPU_Pipeline* colour_pipeline;
+ GPU_Pipeline* colour_pipeline; /** @brief Pipeline for drawing geometry that has vertex colours */
} Immdraw_Storage;
+typedef struct ImmediateUniforms {
+ Mat4 model;
+ Vec4 colour;
+} ImmediateUniforms;
+
// --- Public API
PUB void Immdraw_Init(Immdraw_Storage* storage);
@@ -24,4 +30,25 @@ PUB void Immdraw_Shutdown(Immdraw_Storage* storage);
PUB void Immdraw_Plane(Transform tf, Vec4 colour, bool wireframe);
PUB void Immdraw_Cuboid(Transform tf, Vec4 colour, bool wireframe);
PUB void Immdraw_Sphere(Transform tf, f32 size, Vec4 colour, bool wireframe);
+
PUB void Immdraw_TransformGizmo(Transform tf, f32 size);
+
+// --- Internal
+
+static ShaderDataLayout ImmediateUniforms_GetLayout(void* data) {
+ ImmediateUniforms* d = (ImmediateUniforms*)data;
+ bool has_data = data != NULL;
+
+ ShaderBinding b1 = {
+ .label = "ImmUniforms",
+ .kind = BINDING_BYTES,
+ // .vis = VISIBILITY_VERTEX,
+ .data.bytes.size = sizeof(ImmediateUniforms)
+ };
+
+ if (has_data) {
+ b1.data.bytes.data = d;
+ }
+
+ return (ShaderDataLayout) {.bindings = { b1 }, .binding_count = 1};
+}
diff --git a/src/render/render.c b/src/render/render.c
index bad245a..bf8ac36 100644
--- a/src/render/render.c
+++ b/src/render/render.c
@@ -100,22 +100,12 @@ bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window
}
}
- // #if defined(CEL_REND_BACKEND_OPENGL)
- // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
- // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- // #elif defined(CEL_REND_BACKEND_VULKAN)
- // glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
- // #endif
-
ren->window = window;
*out_window = window;
glfwMakeContextCurrent(ren->window);
- // FIXME
- // DEBUG("Set up GLFW window callbacks");
+ DEBUG("Set up GLFW window callbacks");
glfwSetWindowSizeCallback(window, Render_WindowSizeChanged);
// set the RAL backend up
@@ -143,8 +133,8 @@ bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window
ren->terrain = calloc(1, sizeof(Terrain_Storage));
Terrain_Init(ren->terrain);
- ren->grid = calloc(1, sizeof(Grid_Storage));
- Grid_Init(ren->grid);
+ // ren->grid = calloc(1, sizeof(Grid_Storage));
+ // Grid_Init(ren->grid);
ren->immediate = calloc(1, sizeof(Immdraw_Storage));
Immdraw_Init(ren->immediate);
@@ -329,6 +319,11 @@ Grid_Storage* Render_GetGridStorage() {
return ren->grid;
}
+Immdraw_Storage* Render_GetImmdrawStorage() {
+ Renderer* ren = Core_GetRenderer(&g_core);
+ return ren->immediate;
+}
+
TextureHandle Render_GetWhiteTexture() {
Renderer* ren = Core_GetRenderer(&g_core);
return ren->white_1x1;
diff --git a/src/render/render.h b/src/render/render.h
index 5dc3853..785c140 100644
--- a/src/render/render.h
+++ b/src/render/render.h
@@ -6,6 +6,7 @@
#include "camera.h"
#include "defines.h"
#include "grid.h"
+#include "immdraw.h"
#include "maths_types.h"
#include "ral_types.h"
#include "render_types.h"
@@ -101,6 +102,7 @@ RenderScene* Render_GetScene();
Shadow_Storage* Render_GetShadowStorage();
Terrain_Storage* Render_GetTerrainStorage();
Grid_Storage* Render_GetGridStorage();
+Immdraw_Storage* Render_GetImmdrawStorage();
TextureHandle Render_GetWhiteTexture();
arena* Render_GetFrameArena();
Mesh_pool* Render_GetMeshPool();
diff --git a/src/render/render_types.h b/src/render/render_types.h
index 5fdca8a..924777a 100644
--- a/src/render/render_types.h
+++ b/src/render/render_types.h
@@ -41,8 +41,8 @@ typedef struct Mesh {
BufferHandle vertex_buffer;
BufferHandle index_buffer;
Geometry geometry; // NULL means it has been freed CPU-side
- // i32 material_index; // -1 => no material
MaterialHandle material;
+ bool is_skinned; // false = its static
bool is_uploaded; // has the data been uploaded to the GPU
} Mesh;
#ifndef TYPED_MESH_CONTAINERS
diff --git a/src/resources/gltf.c b/src/resources/gltf.c
index 01291ea..aa4fba5 100644
--- a/src/resources/gltf.c
+++ b/src/resources/gltf.c
@@ -11,8 +11,8 @@
#include "maths.h"
#include "maths_types.h"
#include "mem.h"
-#include "path.h"
#include "pbr.h"
+#include "platform.h"
#include "ral_types.h"
#include "render.h"
#include "render_types.h"
@@ -149,9 +149,9 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, Str8 rel
Material_darray *tmp_materials = Material_darray_new(1);
Mesh_darray *tmp_meshes = Mesh_darray_new(1);
u32_darray *tmp_material_indexes = u32_darray_new(1);
+ Joint_darray *tmp_joints = Joint_darray_new(256);
// FIXME
- // joint_darray *tmp_joints = joint_darray_new(256);
// vertex_bone_data_darray *tmp_vertex_bone_data = vertex_bone_data_darray_new(1000);
cgltf_options options = { 0 };
@@ -306,13 +306,15 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, Str8 rel
// Store vertices
printf("Positions %d Normals %d UVs %d\n", tmp_positions->len, tmp_normals->len,
tmp_uvs->len);
- assert(tmp_positions->len == tmp_normals->len);
- assert(tmp_normals->len == tmp_uvs->len);
+ // assert(tmp_positions->len == tmp_normals->len);
+ // assert(tmp_normals->len == tmp_uvs->len);
+ bool has_normals = tmp_normals->len > 0;
+ bool has_uvs = tmp_uvs->len > 0;
for (u32 v_i = 0; v_i < tmp_positions->len; v_i++) {
Vertex v = { .static_3d = {
.position = tmp_positions->data[v_i],
- .normal = tmp_normals->data[v_i],
- .tex_coords = tmp_uvs->data[v_i],
+ .normal = has_normals ? tmp_normals->data[v_i] : VEC3_ZERO,
+ .tex_coords = has_uvs ? tmp_uvs->data[v_i] : vec2_create(0., 0.),
} };
Vertex_darray_push(geo_vertices, v);
}
diff --git a/src/resources/obj.c b/src/resources/obj.c
index e5b2fc9..e02cb9a 100644
--- a/src/resources/obj.c
+++ b/src/resources/obj.c
@@ -16,9 +16,8 @@
#include "log.h"
#include "maths.h"
#include "mem.h"
-#include "path.h"
+#include "platform.h"
#include "render.h"
-// #include "render_backend.h"
#include "render_types.h"
#include "str.h"
diff --git a/src/scene.c b/src/scene.c
deleted file mode 100644
index f6f5fb7..0000000
--- a/src/scene.c
+++ /dev/null
@@ -1,57 +0,0 @@
-#include "scene.h"
-#include "camera.h"
-#include "core.h"
-#include "log.h"
-#include "maths.h"
-#include "render_types.h"
-
-extern Core g_core;
-
-// void scene_init(scene *s) {
-// memset(s, 0, sizeof(scene));
-// s->renderables = render_entity_darray_new(10);
-// // default camera position - moved slightly along Z axis looking at 0,0,0
-// Vec3 cam_pos = vec3_create(0, 0, -5);
-// s->camera = Camera_Create(cam_pos, vec3_negate(cam_pos), VEC3_Y, deg_to_rad(45.0));
-// }
-// void scene_free(scene *s) { render_entity_darray_free(s->renderables); }
-
-// void scene_set_dir_light(directional_light light) { g_core.default_scene.dir_light = light; }
-// void scene_add_point_light(point_light light) {
-// scene s = g_core.default_scene;
-// if (s.point_lights_count == 4) {
-// WARN("Already 4 point lights, we can't add more.");
-// } else {
-// s.point_lights[s.point_lights_count] = light;
-// s.point_lights_count++;
-// }
-// }
-// void scene_add_model(model_handle model, transform3d transform) {
-// render_entity renderable = { .model = model, .tf = transform };
-// render_entity_darray_push(g_core.default_scene.renderables, renderable);
-// }
-
-// bool scene_remove_model(model_handle model) {
-// scene s = g_core.default_scene;
-// for (u32 i = 0; i <= s.renderables->len; i++) {
-// if (s.renderables->data[i].model.raw == model.raw) {
-// // TODO: add remove function to darray
-// }
-// }
-// return true;
-// }
-
-// void scene_set_model_transform(model_handle model, transform3d new_transform) {
-// scene s = g_core.default_scene;
-// for (u32 i = 0; i <= s.renderables->len; i++) {
-// if (s.renderables->data[i].model.raw == model.raw) {
-// s.renderables->data[i].tf = new_transform;
-// }
-// }
-// }
-
-// void scene_set_camera(vec3 pos, vec3 front) {
-// scene s = g_core.default_scene;
-// s.camera.position = pos;
-// s.camera.front = front;
-// }
diff --git a/src/scene.h b/src/scene.h
deleted file mode 100644
index e414ea8..0000000
--- a/src/scene.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/**
- * @file scene.h
- * @author your name (you@domain.com)
- * @brief
- * @version 0.1
- * @date 2024-04-27
- *
- * @copyright Copyright (c) 2024
- *
- */
-#pragma once
-#include "camera.h"
-#include "defines.h"
-#include "maths_types.h"
-#include "render_types.h"
-
-// typedef struct scene {
-// // camera
-// Camera camera;
-// // lights
-// DirectionalLight dir_light;
-// PointLight point_lights[4];
-// size_t point_lights_count;
-// // geometry
-// render_entity_darray* renderables;
-// // TODO: tree - transform_hierarchy
-// } scene;
-
-// void scene_init(scene* s);
-// void scene_free(scene* s);
-
-// Simplified API - no scene pointer; gets and sets global scene
-
-// Add/Remove objects from the scene
-/* vec3 direction; */
-/* vec3 ambient; */
-/* vec3 diffuse; */
-/* vec3 specular; */
-// void scene_set_dir_light(directional_light light);
-// void _scene_set_dir_light(vec3 ambient, vec3 diffuse, vec3 specular, vec3 direction);
-
-// void scene_add_point_light(point_light light);
-// void scene_add_model(model_handle model, transform3d transform);
-// bool scene_remove_model(model_handle model);
-
-// // Getter & Setters
-// void scene_set_model_transform(model_handle model, transform3d new_transform);
-// void scene_set_camera(vec3 pos, vec3 front);
-
-/* // There can only be one heightmap terrain at a time right now. */
-/* bool scene_add_heightmap(scene* s /\* TODO *\/); */
-/* bool scene_delete_heightmap(scene* s); */
-
-// TODO: functions to load and save scenes from disk
diff --git a/src/std/containers/darray.h b/src/std/containers/darray.h
index ebb5795..d8c1e6d 100644
--- a/src/std/containers/darray.h
+++ b/src/std/containers/darray.h
@@ -38,12 +38,6 @@
#define PREFIX static
-/* if (arena != NULL) {\ */
-/* d = arena_alloc(arena, sizeof(T##_darray));\ */
-/* data = arena_alloc(arena, starting_capacity * sizeof(T));\ */
-/* } else {\ */
-/* }\ */
-
#define KITC_DECL_TYPED_ARRAY(T) DECL_TYPED_ARRAY(T, T)
#define DECL_TYPED_ARRAY(T, Type) \
diff --git a/src/std/str.h b/src/std/str.h
index 9e712e6..c25c615 100644
--- a/src/std/str.h
+++ b/src/std/str.h
@@ -33,6 +33,8 @@ typedef struct {
Str8 Str8_create(u8* buf, size_t len);
+// TODO: Str8_OntoArena(arena* a, Str8 s);
+
/** @brief Return a null-terminated C string cloned onto an arena */
char* Str8_to_cstr(arena* a, Str8 s);
diff --git a/src/ui/ui.h b/src/ui/ui.h
deleted file mode 100644
index c5ace97..0000000
--- a/src/ui/ui.h
+++ /dev/null
@@ -1,13 +0,0 @@
-/**
- * @brief
- */
-
-#pragma once
-#include "defines.h"
-
-typedef struct UI_Storage UI_Storage;
-
-PUB bool UI_Init(UI_Storage* storage);
-PUB void UI_Shutdown(UI_Storage* storage);
-
-// TODO: define immui api