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. Also fixed new textures causing "palette size is not 256" error.
  • Loading branch information
wootguy committed May 31, 2024
1 parent 9b444e8 commit 3675051
Showing 1 changed file with 33 additions and 38 deletions.
71 changes: 33 additions & 38 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4944,6 +4944,8 @@ int Bsp::add_texture(const char* name, byte* data, int width, int height) {
memcpy(newTexData + newTexOffset + newMipTex->nOffsets[2], mip[2], (width >> 2) * (height >> 2));
memcpy(newTexData + newTexOffset + newMipTex->nOffsets[3], mip[3], (width >> 3) * (height >> 3));
memcpy(newTexData + newTexOffset + palleteOffset, palette, sizeof(COLOR3)*256);
uint16_t* colorCountPtr = (uint16_t*)(newTexData + newTexOffset + palleteOffset - 2);
*colorCountPtr = 256; // required for hlrad

for (int i = 0; i < MIPLEVELS; i++) {
delete[] mip[i];
Expand Down Expand Up @@ -5007,25 +5009,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 +5030,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 3675051

Please sign in to comment.