From e6d4fbd1824f9a58ba4d19af23193bca98c5b5be Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Sun, 8 Mar 2026 14:02:35 -0500 Subject: [PATCH] flying first-person camera --- include/test.h | 3 ++- main.lua | 12 ++++++++++-- src/test.cpp | 43 ++++++++++++++++++++++++++++++------------- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/include/test.h b/include/test.h index 96e34a7..aa4a777 100644 --- a/include/test.h +++ b/include/test.h @@ -6,7 +6,8 @@ extern "C" { void load(); void draw(); - void update(float lx, float ly, float ry); + void update(float lx, float ly, float rx, float ry, float tl, float tr, + int up, int down, int left, int right); #ifdef __cplusplus } diff --git a/main.lua b/main.lua index 8161182..9bf1941 100644 --- a/main.lua +++ b/main.lua @@ -7,7 +7,8 @@ function init() ffi.cdef[[ void load(); void draw(); -void update(float lx, float ly, float ry); +void update(float lx, float ly, float rx, float ry, float tl, float tr, + int up, int down, int left, int right); ]] test = ffi.load("./test.so") test.load() @@ -18,8 +19,15 @@ local update = function(dt) for _, joystick in ipairs(joysticks) do local lx = joystick:getGamepadAxis("leftx") local ly = joystick:getGamepadAxis("lefty") + local rx = joystick:getGamepadAxis("rightx") local ry = joystick:getGamepadAxis("righty") - test.update(lx, ly, ry) + local tl = joystick:getGamepadAxis("triggerleft") + local tr = joystick:getGamepadAxis("triggerright") + local up = joystick:isGamepadDown("dpup") + local down = joystick:isGamepadDown("dpdown") + local left = joystick:isGamepadDown("dpleft") + local right = joystick:isGamepadDown("dpright") + test.update(lx, ly, rx, ry, tl, tr, up, down, left, right) end end diff --git a/src/test.cpp b/src/test.cpp index c3d900c..752468e 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -237,6 +237,15 @@ extern "C" { void * SDL_GL_GetProcAddress(const char *proc); } +struct view_state { + XMVECTOR up; + XMVECTOR eye; + XMVECTOR direction; + float fov; +}; + +view_state view_state; + void load() { fprintf(stderr, "getproc %p\n", SDL_GL_GetProcAddress); @@ -245,6 +254,13 @@ void load() load_program(); load_buffers(); load_textures(); + + view_state.up = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); + view_state.eye = XMVectorSet(0, 0, 0, 1); + view_state.direction = XMVectorSet(1, 0, 0, 0); + + view_state.fov = 1.0; + //load_texture_shader_storage(); //unsigned int textures_layout = glGetUniformBlockIndex(test_program, "TexturesLayout"); @@ -252,15 +268,18 @@ void load() //printf("textures_layout %d\n", textures_layout); } -static float vx = 0.0; -static float vy = 0.0; -static float vz = 0.0; - -void update(float lx, float ly, float ry) +void update(float lx, float ly, float rx, float ry, float tl, float tr, + int up, int down, int left, int right) { - vx += 2.5 * lx; - vy += -2.5 * ry; - vz += -2.5 * ly; + //view_state.yaw += rx; + XMMATRIX mrz = XMMatrixRotationZ(rx * -0.035); + XMMATRIX mry = XMMatrixRotationY(ry * -0.035); + view_state.direction = XMVector3Transform(view_state.direction, mrz * mry); + + XMVECTOR normal = XMVector3Cross(view_state.direction, view_state.up); + view_state.eye += view_state.direction * -ly + normal * lx + view_state.up * (tl - tr); + + view_state.fov += 0.01 * up + -0.01 * down; } static inline int popcount(int x) @@ -270,12 +289,10 @@ static inline int popcount(int x) void draw() { - XMVECTOR eye = XMVectorSet(vx + -50.0f, vz + -50.0f, vy + 150.0f, 0.0f); - XMVECTOR at = XMVectorSet(vx + 50.0f, vz + 50.0f, vy + 50.0f, 0.0f); - XMVECTOR up = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); - XMMATRIX view = XMMatrixLookAtRH(eye, at, up); + XMVECTOR at = XMVectorAdd(view_state.eye, view_state.direction); + XMMATRIX view = XMMatrixLookAtRH(view_state.eye, at, view_state.up); - float fov_angle_y = XMConvertToRadians(45 * 0.75); + float fov_angle_y = XMConvertToRadians(45 * view_state.fov); float aspect_ratio = 1.0; float near_z = 1.0; float far_z = 0.1;