From eb548d13d886d72f89f18241347497a889f93ddb Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Thu, 19 Jan 2023 11:52:30 +0100 Subject: [PATCH] Added plane and center point to face struct, for early backface culling. --- main.cpp | 23 +++++++++++++++++++++-- ps1bsp.h | 5 +++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 096dfbe..c34657d 100644 --- a/main.cpp +++ b/main.cpp @@ -280,6 +280,16 @@ static SVECTOR convertNormal(vec3_t normal) return outNormal; } +static SVECTOR convertPoint(vec3_t point) +{ + SVECTOR outPoint; + outPoint.vx = (short)(point.x * 4); + outPoint.vy = (short)(point.y * 4); + outPoint.vz = (short)(point.z * 4); + outPoint.pad = 1; + return outPoint; +} + int process_faces(const world_t* world) { // Write some data to a file @@ -324,8 +334,12 @@ int process_faces(const world_t* world) face_t* face = &world->faces[faceIdx]; ps1bsp_face_t outFace = { 0 }; + outFace.planeId = face->plane_id; + outFace.side = face->side; outFace.firstFaceVertex = (unsigned short)outFaceVertices.size(); + Vec3 vertexSum; + // Traverse the list of face edges to collect all of the face's vertices BoundBox bounds; for (int edgeListIdx = 0; edgeListIdx < face->ledge_num; ++edgeListIdx) @@ -343,10 +357,14 @@ int process_faces(const world_t* world) // Calculate bounding box of this face const vertex_t* vertex = &world->vertices[vertIndex]; + Vec3 vertexPoint = vertex->toVec(); if (edgeListIdx == 0) - bounds.init(vertex->toVec()); + bounds.init(vertexPoint); else - bounds.includePoint(vertex->toVec()); + bounds.includePoint(vertexPoint); + + // Sum all vertices to calculate an average center point + vertexSum = vertexSum + vertexPoint; } // Sample lightmap contribution of this face on each vertex @@ -369,6 +387,7 @@ int process_faces(const world_t* world) // export_lightmap(world, face, bounds, faceIdx); outFace.numFaceVertices = (unsigned short)(outFaceVertices.size() - outFace.firstFaceVertex); + outFace.centerPoint = convertPoint(vertexSum / outFace.numFaceVertices); outFaces.push_back(outFace); } diff --git a/ps1bsp.h b/ps1bsp.h index 1fbf7e3..f840c67 100644 --- a/ps1bsp.h +++ b/ps1bsp.h @@ -71,9 +71,14 @@ typedef struct typedef struct { + unsigned short planeId; + unsigned short side; + unsigned short firstFaceVertex; unsigned short numFaceVertices; + SVECTOR centerPoint; + u_long drawFrame; // Which frame was this face last drawn on? Used to check if this face should be drawn. } ps1bsp_face_t;