From 282f78baaef13ba03efe2bcb37e3359c3412d05d Mon Sep 17 00:00:00 2001 From: matthew Date: Sun, 3 Aug 2025 16:54:46 +1000 Subject: [PATCH] fixed camera, some cleanup --- build.sh | 12 +++++ dub.json | 5 +- external/cglm/cglm.c | 3 ++ src/gears/game.d | 107 ++++++++++++------------------------------ src/gears/renderer.d | 2 +- src/gears/vulkan.d | 7 ++- src/shared/includes.c | 2 + src/shared/math.d | 76 +++++------------------------- 8 files changed, 68 insertions(+), 146 deletions(-) create mode 100644 external/cglm/cglm.c diff --git a/build.sh b/build.sh index f7f06ab..cf91ba1 100755 --- a/build.sh +++ b/build.sh @@ -83,3 +83,15 @@ if ! [ -f build/libm3d.a ]; then rm $obj fi +# CGLM +src="external/cglm/cglm.c" +flags="-std=c99 -Wno-everything -Iexternal/cglm -c -static" +obj="build/cglm.o" +lib="build/libcglm.a" + +if ! [ -f build/libcglm.a ]; then + $c_compiler $flags $src $out $obj + ar rcs $lib $obj + rm $obj +fi + diff --git a/dub.json b/dub.json index 2e2bc52..593d0a8 100644 --- a/dub.json +++ b/dub.json @@ -7,7 +7,7 @@ "targetType": "executable", "targetName": "Gears", "targetPath": "build", - "sourceFiles-linux": ["build/libvma.a", "build/libstb_image.a", "build/libm3d.a"], + "sourceFiles-linux": ["build/libvma.a", "build/libstb_image.a", "build/libm3d.a", "build/libcglm.a"], "sourceFiles-windows": [], "importPaths": ["src/gears", "src/shared", "src/generated", "external/xxhash", "external/inteli"], "sourcePaths": ["src/gears", "src/shared", "src/generated", "external/xxhash", "external/inteli"], @@ -15,6 +15,7 @@ "libs-windows": [], "preGenerateCommands-linux": ["./build.sh", "build/Packer"], "preGenerateCommands-windows": [], + "dflags": ["-Xcc=-mno-sse"], "dflags-dmd": ["-P=-DSTBI_NO_SIMD"] }, { @@ -24,7 +25,7 @@ "targetName": "Packer", "importPaths": ["src/packer", "src/shared", "src/generated", "external/xxhash", "external/inteli"], "sourcePaths": ["src/packer", "src/shared", "src/generated", "external/xxhash", "external/inteli"], - "sourceFiles-linux": ["build/libstb_image.a", "build/libm3d.a"], + "sourceFiles-linux": ["build/libstb_image.a", "build/libm3d.a", "build/libcglm"], "preGenerateCommands-linux": ["./build.sh"], "postGenerateCommands-linux": [], "preGenerateCommands-windows": [], diff --git a/external/cglm/cglm.c b/external/cglm/cglm.c new file mode 100644 index 0000000..6fe88a7 --- /dev/null +++ b/external/cglm/cglm.c @@ -0,0 +1,3 @@ +#define CGLM_FORCE_DEPTH_ZERO_TO_ONE + +#include "cglm.h" diff --git a/src/gears/game.d b/src/gears/game.d index 0a8f309..b99caf5 100644 --- a/src/gears/game.d +++ b/src/gears/game.d @@ -98,48 +98,6 @@ ProcessInputs(Game* g, Camera* cam) HandleInputs(cam, &g.window.inputs); } -/* -void -Update(Game* g, Camera* cam) -{ - Mat4 rotation = RotationMatrix(cam); - Vec3 v = cam.velocity; - cam.pos += (rotation * Vec4(v.x, v.y, v.z, 0.0)).xyz * g.delta; - Logf("%s", rotation.v); - - Extent ext = GetExtent(&g.rd); - - g.globals.view_matrix = ViewMatrix(cam); - Perspective(&g.globals.projection_matrix, Radians(90.0), cast(f32)(ext.x), cast(f32)(ext.y), 0.1, 10000.0); - g.globals.projection_matrix[1, 1] *= -1.0; - //g.globals.projection_matrix[2, 2] *= -1.0; - - g.globals.view_projection = g.globals.projection_matrix * g.globals.view_matrix; - - g.globals.res.x = cast(f32)ext.x; - g.globals.res.y = cast(f32)ext.y; -} - -pragma(inline): Mat4 -RotationMatrix(Camera* cam) -{ - Quat pitch_rotation, yaw_rotation; - QuatFromAxis(&pitch_rotation, cam.pitch, Vec3(1.0, 0.0, 0.0)); - QuatFromAxis(&yaw_rotation, cam.yaw, Vec3(0.0, -1.0, 0.0)); - Mat4 pitch_mat = cast(Mat4)(yaw_rotation) * cast(Mat4)(pitch_rotation); - return cast(Mat4)(yaw_rotation) * cast(Mat4)(pitch_rotation); -} - -pragma(inline): Mat4 -ViewMatrix(Camera* cam) -{ - Mat4 translation; - Translate(&translation, cam.pos); - Mat4 rotation = RotationMatrix(cam); - return Inverse(translation * rotation); -} -*/ - void Cycle(Game* g) { @@ -157,11 +115,10 @@ Cycle(Game* g) Extent ext = GetExtent(&g.rd); f32 aspect = (cast(f32)ext.x) / (cast(f32)ext.y); - Mat4 projection = Perspective(90.0, aspect, 0.1, 100.0); - projection[1, 1] *= -1.0; + Mat4 projection = Perspective(90.0, aspect, 10000.0, 0.1); g.globals.projection_matrix = projection; - g.globals.view_matrix = g.camera.view; + g.globals.view_matrix = ViewMatrix(&g.camera); g.globals.res.x = ext.x; g.globals.res.y = ext.y; SetUniform(&g.rd, &g.globals); @@ -217,21 +174,17 @@ Destroy(Game* g) struct Camera { - Mat4 view = Mat4Identity(); - Vec3 pos = Vec3(0.0, 0.0, 2.0); Vec3 velocity = Vec3(0.0); - Vec3 front = Vec3(0.0, 0.0, -1.0); - Vec3 up = Vec3(0.0, 1.0, 0.0); - Vec3 right = Vec3(0.0); - Vec3 world_up = Vec3(0.0, 1.0, 0.0); - f32 speed = 5000.0; - f32 yaw = -90.0; + Vec3 pos = Vec3(0.0, 0.0, 5.0); f32 pitch = 0.0; + f32 yaw = 0.0; } pragma(inline): void HandleInputs(Camera* cam, Inputs* inputs) { + Logf("%.04f %.04f", cam.pitch, cam.yaw); + foreach(i; 0 .. inputs.count) { InputEvent event = inputs.events[i]; @@ -246,37 +199,35 @@ HandleInputs(Camera* cam, Inputs* inputs) case Input.MouseMotion: { // (0, 0) top left - cam.yaw -= cast(f32)(event.rel_x) / 20.0; - cam.pitch += cast(f32)(event.rel_y) / 20.0; - - if (cam.pitch > 90.0) - { - cam.pitch = 90.0; - } - if (cam.pitch < -90.0) - { - cam.pitch = -90.0; - } + cam.yaw += cast(f32)(event.rel_x) / 200.0; + cam.pitch += cast(f32)(event.rel_y) / 200.0; } break; default: break; } } } -pragma(inline): void +Mat4 +RotationMatrix(Camera* cam) +{ + Quat pitch = QuatFromAxis(cam.pitch, Vec3(1.0, 0.0, 0.0)); + Quat yaw = QuatFromAxis(cam.yaw, Vec3(0.0, 1.0, 0.0)); + return cast(Mat4)(yaw) * cast(Mat4)(pitch); +} + +Mat4 +ViewMatrix(Camera* cam) +{ + Mat4 translation = Mat4Identity(); + Translate(&translation, cam.pos); + Mat4 rotation = RotationMatrix(cam); + return Inverse(translation * rotation); +} + +void Update(Camera* cam) { - Vec3 front = Vec3(0.0); - - cam.pos += cam.velocity * g_DELTA; - - front.x = cosf(Radians(cam.yaw)) * cosf(Radians(cam.pitch)); - front.y = sinf(Radians(cam.pitch)); - front.z = sinf(Radians(cam.yaw)) * cosf(Radians(cam.pitch)); - cam.front = Normalize(front); - - cam.right = Normalize(Cross(cam.front, cam.world_up)); - cam.up = Normalize(Cross(cam.right, cam.front)); - - cam.view = LookAt(cam.pos, cam.pos + cam.front, cam.up); + Mat4 rotation = RotationMatrix(cam); + Vec4 pos = rotation * Vec4(cam.velocity * 0.5 * g_DELTA, 0.0); + cam.pos += pos.xyz; } diff --git a/src/gears/renderer.d b/src/gears/renderer.d index 664dd6a..338d205 100644 --- a/src/gears/renderer.d +++ b/src/gears/renderer.d @@ -152,7 +152,7 @@ struct MeshPart struct Model { - Vec3 pos = Vec3(0.0, 0.0, 0.0); + Vec3 pos = Vec3(0.0); Buffer vertex_buffer; Buffer index_buffer; MeshPart[] parts; diff --git a/src/gears/vulkan.d b/src/gears/vulkan.d index 24e53f9..f3ced11 100644 --- a/src/gears/vulkan.d +++ b/src/gears/vulkan.d @@ -1187,9 +1187,12 @@ Bind(Vulkan* vk, PipelineHandle* pipeline) ); VkViewport viewport = { + x: 0.0, + y: 0.0, width: cast(f32)vk.swapchain_extent.width, height: cast(f32)vk.swapchain_extent.height, - maxDepth: 1.0F, + minDepth: 0.0, + maxDepth: 1.0, }; vkCmdSetViewport(vk.cmds[vk.frame_index], 0, 1, &viewport); @@ -1303,7 +1306,7 @@ CreateGraphicsPipeline(Vulkan* vk, GfxPipelineInfo* build_info) depthTestEnable: VK_TRUE, depthWriteEnable: VK_TRUE, depthCompareOp: VK_COMPARE_OP_GREATER_OR_EQUAL, - depthBoundsTestEnable: VK_TRUE, + depthBoundsTestEnable: VK_FALSE, stencilTestEnable: VK_FALSE, minDepthBounds: 0.0, maxDepthBounds: 1.0, diff --git a/src/shared/includes.c b/src/shared/includes.c index b32e952..a9a6826 100644 --- a/src/shared/includes.c +++ b/src/shared/includes.c @@ -8,6 +8,8 @@ # include #endif +#include + #if __linux__ # define VK_USE_PLATFORM_XCB_KHR #elif _WIN32 diff --git a/src/shared/math.d b/src/shared/math.d index 0d48a26..b745e7b 100644 --- a/src/shared/math.d +++ b/src/shared/math.d @@ -22,7 +22,7 @@ f32 Radians(f32 deg) struct Vector(T, int N) { - static assert(N > 0 && N <= 4); + static assert(N > 1 && N <= 4); enum _N = N; alias T _T; @@ -35,11 +35,8 @@ struct Vector(T, int N) T x; alias x r; - static if (N > 1) - { - T y; - alias y g; - } + T y; + alias y g; static if (N > 2) { @@ -52,7 +49,7 @@ struct Vector(T, int N) T w; alias w a; } - } + }; } this(Vec3, f32)(Vec3 v3, f32 f) if (N == 4 && is(T: f32)) @@ -475,13 +472,6 @@ align(16) struct Matrix(T, int D) return result; } - MatrixVec opBinary(string op)(T factor) if (op == "*") - { - Matrix result; - - return result; - } - static if (D == 4) { Vec4 opBinary(string op, U)(U x) if (is(U: Vec4) && is(T: f32) && (op == "*")) @@ -540,41 +530,9 @@ struct Quat U opCast(U)() if (is(U: Mat4)) { - f32 norm = Norm(&vec); - f32 s = norm > 0.0 ? 2.0 / norm : 0.0; - - f32 _x = x; - f32 _y = y; - f32 _z = z; - f32 _w = w; - - f32 xx = s * x * x; f32 xy = s * x * y; f32 wx = s * w * x; - f32 yy = s * y * y; f32 yz = s * y * z; f32 wy = s * w * y; - f32 zz = s * z * z; f32 xz = s * x * z; f32 wz = s * w * z; - - U mat; - - mat[0, 0] = 1.0 - yy - zz; - mat[1, 1] = 1.0 - xx - zz; - mat[2, 2] = 1.0 - xx - yy; - - mat[0, 1] = xy + wz; - mat[1, 2] = yz + wx; - mat[2, 0] = xz + wy; - - mat[1, 0] = xy - wz; - mat[2, 1] = yz - wx; - mat[0, 2] = xz - wy; - - mat[0, 3] = 0.0; - mat[1, 3] = 0.0; - mat[2, 3] = 0.0; - mat[3, 0] = 0.0; - mat[3, 1] = 0.0; - mat[3, 2] = 0.0; - mat[3, 3] = 1.0; - - return mat; + Mat4 result; + glm_quat_mat4(vec.ptr, result.glm_mat.ptr); + return result; } Quat opBinary(string op, U)(U r) if (op == "*" && is(U: Quat)) @@ -744,21 +702,12 @@ Mat2Identity() ); } -pragma(inline): void -QuatFromAxis(Quat* q, f32 angle, Vec3 axis) +pragma(inline): Quat +QuatFromAxis(f32 angle, Vec3 axis) { - Vec3 k; - - f32 a = angle * 0.5f; - f32 c = cosf(a); - f32 s = sinf(a); - - k = Normalize(axis); - - q.x = s * k.x; - q.y = s * k.y; - q.z = s * k.z; - q.w = c; + Quat q; + glm_quatv(q.vec.ptr, angle, axis.v.ptr); + return q; } pragma(inline): f32 @@ -836,6 +785,7 @@ Perspective(f32 fov, f32 aspect, f32 near, f32 far) Mat4 res; MatZero(&res); glm_perspective(fov, aspect, near, far, res.glm_mat.ptr); + res[1, 1] *= -1.0; return res; }