Skip to content

Commit

Permalink
fix hlrad crashing on created BSP models
Browse files Browse the repository at this point in the history
don't know why it happens but this fixed it
  • Loading branch information
wootguy committed May 30, 2024
1 parent 9b444e8 commit 91e615e
Showing 1 changed file with 31 additions and 38 deletions.
69 changes: 31 additions & 38 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5007,25 +5007,18 @@ void Bsp::create_node_box(vec3 min, vec3 max, BSPMODEL* targetModel, int texture
// add new edges (minimum needed to refrence every vertex once)
int startEdge = edgeCount;
{
BSPEDGE* newEdges = new BSPEDGE[edgeCount + 4];
BSPEDGE* newEdges = new BSPEDGE[edgeCount + 8];
memcpy(newEdges, edges, edgeCount * sizeof(BSPEDGE));

newEdges[startEdge + 0] = BSPEDGE(startVert + 0, startVert + 1);
newEdges[startEdge + 1] = BSPEDGE(startVert + 2, startVert + 3);
newEdges[startEdge + 2] = BSPEDGE(startVert + 4, startVert + 5);
newEdges[startEdge + 3] = BSPEDGE(startVert + 6, startVert + 7);
// defining an edge for every vertex because otherwise hlrad crashes, even though
// only 4 edges are required to reference every vertex on the cube
for (int i = 0; i < 8; i++) {
newEdges[startEdge + i] = BSPEDGE(startVert + i, startVert + i);
}

replace_lump(LUMP_EDGES, newEdges, (edgeCount + 4) * sizeof(BSPEDGE));
replace_lump(LUMP_EDGES, newEdges, (edgeCount + 8) * sizeof(BSPEDGE));
}

// maps a vertex number to a surfedge value
int32_t vertToSurfEdge[8] = {
(startEdge + 0), -(startEdge + 0), // negative value = use 2nd vertex in edge struct
(startEdge + 1), -(startEdge + 1),
(startEdge + 2), -(startEdge + 2),
(startEdge + 3), -(startEdge + 3),
};

// add new surfedges (vertex lookups into edges which define the faces, 4 per face, clockwise order)
int startSurfedge = surfedgeCount;
{
Expand All @@ -5035,40 +5028,40 @@ void Bsp::create_node_box(vec3 min, vec3 max, BSPMODEL* targetModel, int texture
int32_t surfEdgeIdx = startSurfedge;

// left face
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[7];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[4];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[0];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[3];
newSurfedges[surfEdgeIdx++] = startEdge + 7;
newSurfedges[surfEdgeIdx++] = startEdge + 4;
newSurfedges[surfEdgeIdx++] = startEdge + 0;
newSurfedges[surfEdgeIdx++] = startEdge + 3;

// right face
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[5];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[6];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[2];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[1];
newSurfedges[surfEdgeIdx++] = startEdge + 5;
newSurfedges[surfEdgeIdx++] = startEdge + 6;
newSurfedges[surfEdgeIdx++] = startEdge + 2;
newSurfedges[surfEdgeIdx++] = startEdge + 1;

// front face
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[4];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[5];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[1];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[0];
newSurfedges[surfEdgeIdx++] = startEdge + 4;
newSurfedges[surfEdgeIdx++] = startEdge + 5;
newSurfedges[surfEdgeIdx++] = startEdge + 1;
newSurfedges[surfEdgeIdx++] = startEdge + 0;

// back face
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[6];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[7];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[3];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[2];
newSurfedges[surfEdgeIdx++] = startEdge + 6;
newSurfedges[surfEdgeIdx++] = startEdge + 7;
newSurfedges[surfEdgeIdx++] = startEdge + 3;
newSurfedges[surfEdgeIdx++] = startEdge + 2;

// bottom face
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[0];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[1];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[2];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[3];
newSurfedges[surfEdgeIdx++] = startEdge + 0;
newSurfedges[surfEdgeIdx++] = startEdge + 1;
newSurfedges[surfEdgeIdx++] = startEdge + 2;
newSurfedges[surfEdgeIdx++] = startEdge + 3;

// top face
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[7];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[6];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[5];
newSurfedges[surfEdgeIdx++] = vertToSurfEdge[4];
newSurfedges[surfEdgeIdx++] = startEdge + 7;
newSurfedges[surfEdgeIdx++] = startEdge + 6;
newSurfedges[surfEdgeIdx++] = startEdge + 5;
newSurfedges[surfEdgeIdx++] = startEdge + 4;

replace_lump(LUMP_SURFEDGES, newSurfedges, (surfedgeCount + 24) * sizeof(int32_t));
}
Expand Down

0 comments on commit 91e615e

Please sign in to comment.