From 03cb36dd9ddb73113410a3690b6df1d8edf6bf7d Mon Sep 17 00:00:00 2001 From: Katajisto Date: Sun, 1 Jun 2025 17:29:51 +0300 Subject: [PATCH] finally actually solved the ray cast stuff --- src/editor/trile_editor.jai | 77 ++++++++++++++++++++++++++++++++++--- src/ray.jai | 19 ++++++++- 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/src/editor/trile_editor.jai b/src/editor/trile_editor.jai index 63fb274..74346c9 100644 --- a/src/editor/trile_editor.jai +++ b/src/editor/trile_editor.jai @@ -18,6 +18,8 @@ Trile_Editor_Tab :: enum { current_tab : Trile_Editor_Tab = .TOOLSET; +current_trile : Trile; + #scope_export current_color : Vector3 = .{1.0, 0.0, 0.0}; @@ -40,11 +42,6 @@ tick_trile_editor :: () { cam := get_trile_editor_camera(); ray := get_mouse_ray(*cam); - hit := does_ray_hit_cube(ray, .{ .{0, 0, 0}, .{1, 1, 1}}); - - - print("Ray direction %\n", ray.direction); - } get_trile_editor_camera :: () -> Camera { @@ -99,6 +96,76 @@ draw_material_tab :: (theme: *GR.Overall_Theme, area: GR.Rect) { } +draw_trile :: () { + cam := get_trile_editor_camera(); + + mvp := create_viewproj(*cam); + vs_params : Vs_Params; + vs_params.mvp = mvp.floats; + + trixels : [4096]Position_Color; + + hovered_trixel_x : int = -1; + hovered_trixel_y : int = -1; + hovered_trixel_z : int = -1; + min_distance : float = 999.0; + + ray := get_mouse_ray(*cam); + + for x: 0..15 { + for y: 0..15 { + for z: 0..15 { + hit := does_ray_hit_cube(ray, .{ .{x * TRIXEL_SIZE, y * TRIXEL_SIZE, z * TRIXEL_SIZE}, .{TRIXEL_SIZE, TRIXEL_SIZE, TRIXEL_SIZE}}); + if hit.hit && hit.distance < min_distance { + hovered_trixel_x = x; + hovered_trixel_y = y; + hovered_trixel_z = z; + min_distance = hit.distance; + } + } + } + } + + trixel_count : s32 = 0; + + for x: 0..15 { + for y: 0..15 { + for z: 0..15 { + if current_trile.trixels[x][y][z].empty then continue; + hit := does_ray_hit_cube(ray, .{ .{x * TRIXEL_SIZE, y * TRIXEL_SIZE, z * TRIXEL_SIZE}, .{TRIXEL_SIZE, TRIXEL_SIZE, TRIXEL_SIZE}}); + + + trixels[x * 16 * 16 + y * 16 + z].pos.x = x * (1.0 / 16.0) + TRIXEL_SIZE_HALF; + trixels[x * 16 * 16 + y * 16 + z].pos.y = y * (1.0 / 16.0) + TRIXEL_SIZE_HALF; + trixels[x * 16 * 16 + y * 16 + z].pos.z = z * (1.0 / 16.0) + TRIXEL_SIZE_HALF; + trixels[x * 16 * 16 + y * 16 + z].pos.w = 1.0; + trixel_color := current_trile.trixels[x][y][z].material.color; + + if hovered_trixel_x == x && + hovered_trixel_y == y && + hovered_trixel_z == z { + trixel_color = .{1.0, 0.0, 0.0}; + } + + trixels[x * 16 * 16 + y * 16 + z].col = .{trixel_color.x, trixel_color.y, trixel_color.z, 1.0}; + trixel_count += 1; + } + } + } + + sg_update_buffer(gPipelines.trixel.bind.vertex_buffers[1], *(sg_range.{ + ptr = trixels.data, + size = size_of(type_of(trixels)), + })); + + sg_begin_pass(*(sg_pass.{ action = state.pass_action_clear, swapchain = cast,force(sg_swapchain) sglue_swapchain() })); + sg_apply_pipeline(gPipelines.trixel.pipeline); + sg_apply_bindings(*gPipelines.trixel.bind); + sg_apply_uniforms(UB_vs_params, *(sg_range.{ ptr = *vs_params, size = size_of(type_of(vs_params)) })); + sg_draw(0, 36, trixel_count); + sg_end_pass(); +} + draw_trile_editor_ui :: (theme: *GR.Overall_Theme) { r := GR.get_rect(0, ui_h(5,0), ui_w(20, 20), ui_h(95, 0)); draw_bg_rectangle(r, theme); diff --git a/src/ray.jai b/src/ray.jai index 0a3b64f..5fce7ab 100644 --- a/src/ray.jai +++ b/src/ray.jai @@ -36,6 +36,8 @@ Collision_Cube :: struct { Ray_Collision :: struct { distance : float = 99999; hit : bool = false; + point : Vector3; + normal : Vector3; } // Ported over from Raylib. @@ -65,7 +67,22 @@ does_ray_hit_cube :: (og_ray: Ray, cube: Collision_Cube) -> Ray_Collision { t[7] = min(min(max(t[0], t[1]), max(t[2], t[3])), max(t[4], t[5])); collision.hit = !((t[7] < 0) || (t[6] > t[7])); - collision.distance = t[6]; + collision.distance = t[6]; + collision.point = ray.origin + ray.direction * collision.distance; + + collision.normal = lerp(bmin, bmax, 0.5); + collision.normal = collision.point - collision.normal; + + collision.normal.x = cast(float) cast(s64) collision.normal.x; + collision.normal.y = cast(float) cast(s64) collision.normal.y; + collision.normal.z = cast(float) cast(s64) collision.normal.z; + collision.normal = normalize(collision.normal); + + if insideBox { + ray.direction = -1.0 * ray.direction; + collision.distance = -1.0 * collision.distance; + collision.normal = -1.0 * collision.normal; + } return collision; }