From ba68bc128e27d75b524d926033597f698ce28bb6 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sun, 15 Jan 2023 17:15:34 +0100 Subject: [PATCH] Write face vertex indices to output file instead of triangle data, which takes up less space and allows the PS1 code to decide how to draw the primitives. --- main.cpp | 16 +++++++--------- ps1bsp.h | 9 +++------ 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/main.cpp b/main.cpp index fc5a7cd..8667249 100644 --- a/main.cpp +++ b/main.cpp @@ -211,6 +211,9 @@ int process_faces(const world_t* world) { face_t* face = &world->faces[faceIdx]; + ps1bsp_face_t outFace = { 0 }; + outFace.firstVertexIndex = faceVertIndices.size(); + // Traverse the list of face edges to collect all of the face's vertices for (int edgeListIdx = 0; edgeListIdx < face->ledge_num; ++edgeListIdx) { @@ -223,9 +226,6 @@ int process_faces(const world_t* world) faceVertIndices.push_back(vertIndex); } - ps1bsp_face_t outFace = { 0 }; - outFace.firstTriangleId = outTriangles.size(); - //printf("Face %d: %d vertices\n", faceIdx, faceVerts.size()); // Triangulate face into polygons (triangle fan, the naive method) @@ -240,19 +240,17 @@ int process_faces(const world_t* world) outTriangles.push_back(outTriangle); } - outFace.numTriangles = outTriangles.size() - outFace.firstTriangleId; + outFace.numVertices = faceVertIndices.size() - outFace.firstVertexIndex; outFaces.push_back(outFace); - - faceVertIndices.clear(); } // Write triangle and face data to file - fwrite(outTriangles.data(), sizeof(ps1bsp_triangle_t), outTriangles.size(), fbsp); + fwrite(faceVertIndices.data(), sizeof(unsigned short), faceVertIndices.size(), fbsp); fwrite(outFaces.data(), sizeof(ps1bsp_face_t), outFaces.size(), fbsp); // Update header information outHeader.numVertices = world->numVertices; - outHeader.numTriangles = outTriangles.size(); + outHeader.numFaceVertIndices = faceVertIndices.size(); outHeader.numFaces = outFaces.size(); // Write final header @@ -260,7 +258,7 @@ int process_faces(const world_t* world) fwrite(&outHeader, sizeof(ps1bsp_header_t), 1, fbsp); fclose(fbsp); - printf("PS1BSP: wrote %d vertices, %d triangles, %d faces\n", outHeader.numVertices, outHeader.numTriangles, outHeader.numFaces); + printf("PS1BSP: wrote %d vertices, %d indices, %d faces\n", outHeader.numVertices, outHeader.numFaceVertIndices, outHeader.numFaces); return 1; } diff --git a/ps1bsp.h b/ps1bsp.h index 34490e9..eafbc55 100644 --- a/ps1bsp.h +++ b/ps1bsp.h @@ -22,7 +22,7 @@ Probable rendering process: typedef struct { unsigned short numVertices; - unsigned short numTriangles; + unsigned short numFaceVertIndices; unsigned short numFaces; } ps1bsp_header_t; @@ -62,11 +62,8 @@ typedef struct typedef struct { - unsigned short firstTriangleId; // TODO: could also just do first-index, num-indices here. No real need for a triangle_t struct. - unsigned short numTriangles; - - unsigned short firstQuadId; // For if/when we decide to add quads to the mix - unsigned short numQuads; + unsigned short firstVertexIndex; + unsigned short numVertices; } ps1bsp_face_t; // Pre-parsed and encoded entity data (this runs the risk of becoming too bloated)