diff --git a/frustum.c b/frustum.c index 26b2e25..f50865f 100644 --- a/frustum.c +++ b/frustum.c @@ -20,9 +20,9 @@ void frustum_update(int width, int height) int near; gte_ReadGeomScreen(&near); - //FntPrint(-1, "N = %d, W = %d, H = %d\n", near, width, height); + // FntPrint(-1, "N = %d, W = %d, H = %d\n", near, width, height); - // 2 * near, shifted left for division + // 2 * ONE * near near <<= 13; l.vx = near / width; @@ -53,7 +53,7 @@ void frustum_update(int width, int height) // left.vx, left.pad, right.vx, right.pad, top.vz, top.pad, bottom.vz, bottom.pad); } -u_char frustum_pointInside(SVECTOR *point) +u_char frustum_pointInside(const SVECTOR *point) { if (m_dot12(left, *point) + left.pad < 0) return 0; if (m_dot12(right, *point) + right.pad < 0) return 0; @@ -62,7 +62,7 @@ u_char frustum_pointInside(SVECTOR *point) return 1; } -u_char frustum_sphereInside(SVECTOR *sphere) +u_char frustum_sphereInside(const SVECTOR *sphere) { short radius = -sphere->pad; if (m_dot12(left, *sphere) + left.pad < radius) return 0; diff --git a/frustum.h b/frustum.h index adcd50c..244502d 100644 --- a/frustum.h +++ b/frustum.h @@ -2,7 +2,7 @@ #define __FRUSTUM_H__ void frustum_update(int width, int height); -u_char frustum_pointInside(SVECTOR *point); -u_char frustum_sphereInside(SVECTOR *sphere); +u_char frustum_pointInside(const SVECTOR *point); +u_char frustum_sphereInside(const SVECTOR *sphere); #endif // __FRUSTUM_H__ diff --git a/ps1bsp.h b/ps1bsp.h index 9570fda..7b79a99 100755 --- a/ps1bsp.h +++ b/ps1bsp.h @@ -119,8 +119,8 @@ typedef struct // Used for backface culling SVECTOR center; - // Which frame was this face last drawn on? Used to check if this face should be drawn. - u_long drawFrame; + // Run-time data + u_long drawFrame; // Which frame was this face last drawn on? Used to check if this face should be drawn. } ps1bsp_face_t; typedef struct @@ -136,9 +136,7 @@ typedef struct short children[2]; // TODO: add bounding box for frustum culling (or bounding sphere, might be cheaper) - - u_short firstFace; - u_short numFaces; + SVECTOR boundingSphere; } ps1bsp_node_t; typedef struct @@ -151,6 +149,10 @@ typedef struct u_short firstLeafFace; u_short numLeafFaces; + + // Run-time data + u_short nextLeaf; // For chaining leafs in drawing order + u_short leafDepth; // At what depth the leaf's faces should be placed in the ordering table } ps1bsp_leaf_t; // Pre-parsed and encoded entity data (this runs the risk of becoming too bloated) diff --git a/test.ps1bsp b/test.ps1bsp index 0ff67a1..a78b035 100755 Binary files a/test.ps1bsp and b/test.ps1bsp differ diff --git a/world.c b/world.c index c2d4518..ab179ca 100644 --- a/world.c +++ b/world.c @@ -3,6 +3,7 @@ #include "display.h" #include "time.h" #include "draw.h" +#include "frustum.h" static CVECTOR colors[] = { @@ -212,7 +213,11 @@ static void world_drawnode(const world_t *world, short nodeIdx, u_char *pvs) return; } + // Perform frustum culling const ps1bsp_node_t *node = &world->nodes[nodeIdx]; + if (!frustum_sphereInside(&node->boundingSphere)) + return; + const ps1bsp_plane_t *plane = &world->planes[node->planeId]; short dist = world_pointPlaneDist(&cam_pos, plane);