From 60d5067105f22761750377d6d50e7cf74340ec13 Mon Sep 17 00:00:00 2001 From: wootguy Date: Tue, 23 Apr 2024 06:43:14 -0700 Subject: [PATCH] add validation check for invalid plane normals (fixes #89) and fix the normal if possible. Also log when other validation checks fix the problem. --- src/bsp/Bsp.cpp | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/bsp/Bsp.cpp b/src/bsp/Bsp.cpp index 8b8640dc..f6dbbed9 100644 --- a/src/bsp/Bsp.cpp +++ b/src/bsp/Bsp.cpp @@ -3483,12 +3483,14 @@ bool Bsp::validate() { for (int i = 0; i < leafCount; i++) { if ((leaves[i].iFirstMarkSurface < 0 || leaves[i].iFirstMarkSurface + leaves[i].nMarkSurfaces > marksurfCount)) { - logf("Bad marksurf reference in leaf %d: (%d + %d) / %d\n", + logf("Bad marksurf reference in leaf %d: (%d + %d) / %d", i, leaves[i].iFirstMarkSurface, leaves[i].nMarkSurfaces, marksurfCount); if (leaves[i].nMarkSurfaces == 0) { + logf(" (fixed!)"); leaves[i].iFirstMarkSurface = 0; } + logf("\n"); isValid = false; } @@ -3504,10 +3506,12 @@ bool Bsp::validate() { } for (int i = 0; i < nodeCount; i++) { if ((nodes[i].firstFace < 0 || nodes[i].firstFace + nodes[i].nFaces > faceCount)) { - logf("Bad face reference in node %d: %d / %d\n", i, nodes[i].firstFace, faceCount); + logf("Bad face reference in node %d: %d / %d", i, nodes[i].firstFace, faceCount); if (nodes[i].nFaces == 0) { nodes[i].firstFace = 0; + logf(" (fixed!)"); } + logf("\n"); isValid = false; } if (nodes[i].iPlane < 0 || nodes[i].iPlane >= planeCount) { @@ -3525,6 +3529,22 @@ bool Bsp::validate() { } } } + for (int i = 0; i < planeCount; i++) { + BSPPLANE& plane = planes[i]; + float normLen = plane.vNormal.length(); + + + if (normLen < 0.5f) { + logf("Bad normal for plane %d", i); + if (normLen > 0) { + plane.vNormal = plane.vNormal.normalize(1.0f); + logf(" (fixed!)"); + } + logf("\n"); + + isValid = false; + } + } for (int i = 0; i < clipnodeCount; i++) { if (clipnodes[i].iPlane < 0 || clipnodes[i].iPlane >= planeCount) { @@ -3551,10 +3571,12 @@ bool Bsp::validate() { totalVisLeaves += models[i].nVisLeafs; totalFaces += models[i].nFaces; if ((models[i].iFirstFace < 0 || models[i].iFirstFace + models[i].nFaces > faceCount)) { - logf("Bad face reference in model %d: %d / %d\n", i, models[i].iFirstFace, faceCount); + logf("Bad face reference in model %d: %d / %d", i, models[i].iFirstFace, faceCount); if (models[i].nFaces == 0) { models[i].iFirstFace = 0; + logf(" (fixed!)"); } + logf("\n"); isValid = false; } if (models[i].iHeadnodes[0] >= nodeCount) {