From b1e568e5c372be2281202f83637413738270a4d9 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sat, 28 Jan 2023 15:47:29 +0100 Subject: [PATCH] Added check to backface culling to see if the face is behind the camera. This will be required if we ditch depth-based culling in favor of leaf-based depth. Also makes for a nice speed boost in some scenarios already. --- common.h | 1 + input.c | 4 ++++ main.c | 1 + world.c | 6 ++++++ 4 files changed, 12 insertions(+) diff --git a/common.h b/common.h index 4daf011..224e89f 100644 --- a/common.h +++ b/common.h @@ -22,6 +22,7 @@ typedef struct _STVECTOR { extern VECTOR cam_pos; extern SVECTOR cam_rot; +extern SVECTOR cam_dir; extern u_short cam_leaf; extern u_short quake_clut; diff --git a/input.c b/input.c index d9cb894..7d96d38 100644 --- a/input.c +++ b/input.c @@ -34,6 +34,10 @@ void input_process() MATRIX cam_mtx; RotMatrixQ(&cam_rot, &cam_mtx); + cam_dir.vx = cam_mtx.m[0][1]; + cam_dir.vy = cam_mtx.m[1][1]; + cam_dir.vz = cam_mtx.m[2][1]; + int deltaTime = time_getDeltaTime(); int moveInterval = (moveSpeed * deltaTime) >> 12; int rotInterval = (moveSpeed * deltaTime) >> 12; diff --git a/main.c b/main.c index e773ebc..5b5e49c 100644 --- a/main.c +++ b/main.c @@ -11,6 +11,7 @@ extern u_long bsp_test[]; VECTOR cam_pos = { 2176, 1152, 272 }; // START //VECTOR cam_pos = { 1920, -1408, 352 }; // E1M1 SVECTOR cam_rot = { 0, 0, 0 }; +SVECTOR cam_dir = { 0, ONE, 0 }; u_short cam_leaf = 0; world_t world; diff --git a/world.c b/world.c index ded0618..d34182b 100644 --- a/world.c +++ b/world.c @@ -77,7 +77,13 @@ static INLINE char world_cull_backface(const world_t *world, const ps1bsp_face_t cam_vec.vx = face->center.vx - cam_pos.vx; cam_vec.vy = face->center.vy - cam_pos.vy; cam_vec.vz = face->center.vz - cam_pos.vz; + + // Check if the face is behind the camera + short camDot = m_dot12s(cam_vec, cam_dir); + if (camDot < 0) + return 1; + // Check if the face's plane points towards the camera const ps1bsp_plane_t *plane = &world->planes[face->planeId]; *dot = world_planeDot(&cam_vec, plane); return ((*dot >= 0) ^ face->side);