flying first-person camera

This commit is contained in:
Zack Buhman 2026-03-08 14:02:35 -05:00
parent f890e4e550
commit e6d4fbd182
3 changed files with 42 additions and 16 deletions

View File

@ -6,7 +6,8 @@ extern "C" {
void load(); void load();
void draw(); 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 #ifdef __cplusplus
} }

View File

@ -7,7 +7,8 @@ function init()
ffi.cdef[[ ffi.cdef[[
void load(); void load();
void draw(); 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 = ffi.load("./test.so")
test.load() test.load()
@ -18,8 +19,15 @@ local update = function(dt)
for _, joystick in ipairs(joysticks) do for _, joystick in ipairs(joysticks) do
local lx = joystick:getGamepadAxis("leftx") local lx = joystick:getGamepadAxis("leftx")
local ly = joystick:getGamepadAxis("lefty") local ly = joystick:getGamepadAxis("lefty")
local rx = joystick:getGamepadAxis("rightx")
local ry = joystick:getGamepadAxis("righty") 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
end end

View File

@ -237,6 +237,15 @@ extern "C" {
void * SDL_GL_GetProcAddress(const char *proc); void * SDL_GL_GetProcAddress(const char *proc);
} }
struct view_state {
XMVECTOR up;
XMVECTOR eye;
XMVECTOR direction;
float fov;
};
view_state view_state;
void load() void load()
{ {
fprintf(stderr, "getproc %p\n", SDL_GL_GetProcAddress); fprintf(stderr, "getproc %p\n", SDL_GL_GetProcAddress);
@ -245,6 +254,13 @@ void load()
load_program(); load_program();
load_buffers(); load_buffers();
load_textures(); 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(); //load_texture_shader_storage();
//unsigned int textures_layout = glGetUniformBlockIndex(test_program, "TexturesLayout"); //unsigned int textures_layout = glGetUniformBlockIndex(test_program, "TexturesLayout");
@ -252,15 +268,18 @@ void load()
//printf("textures_layout %d\n", textures_layout); //printf("textures_layout %d\n", textures_layout);
} }
static float vx = 0.0; void update(float lx, float ly, float rx, float ry, float tl, float tr,
static float vy = 0.0; int up, int down, int left, int right)
static float vz = 0.0;
void update(float lx, float ly, float ry)
{ {
vx += 2.5 * lx; //view_state.yaw += rx;
vy += -2.5 * ry; XMMATRIX mrz = XMMatrixRotationZ(rx * -0.035);
vz += -2.5 * ly; 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) static inline int popcount(int x)
@ -270,12 +289,10 @@ static inline int popcount(int x)
void draw() void draw()
{ {
XMVECTOR eye = XMVectorSet(vx + -50.0f, vz + -50.0f, vy + 150.0f, 0.0f); XMVECTOR at = XMVectorAdd(view_state.eye, view_state.direction);
XMVECTOR at = XMVectorSet(vx + 50.0f, vz + 50.0f, vy + 50.0f, 0.0f); XMMATRIX view = XMMatrixLookAtRH(view_state.eye, at, view_state.up);
XMVECTOR up = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
XMMATRIX view = XMMatrixLookAtRH(eye, at, up);
float fov_angle_y = XMConvertToRadians(45 * 0.75); float fov_angle_y = XMConvertToRadians(45 * view_state.fov);
float aspect_ratio = 1.0; float aspect_ratio = 1.0;
float near_z = 1.0; float near_z = 1.0;
float far_z = 0.1; float far_z = 0.1;