Skip to content

Commit

Permalink
Add MDL TO BSP TEST (debug key f1 map->menu)
Browse files Browse the repository at this point in the history
  • Loading branch information
UnrealKaraulov committed Feb 2, 2024
1 parent 9de4fa1 commit ec001c1
Show file tree
Hide file tree
Showing 14 changed files with 713 additions and 384 deletions.
64 changes: 50 additions & 14 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,8 @@ bool Bsp::vertex_manipulation_sync(int modelIdx, std::vector<TransformVert>& hul
}

BSPPLANE testPlane;
bool expectedFlip = testPlane.update(planes[iPlane].vNormal, planes[iPlane].fDist);
bool flipped = newPlane.update(newPlane.vNormal, newPlane.fDist);
bool expectedFlip = testPlane.update_plane(planes[iPlane].vNormal, planes[iPlane].fDist);
bool flipped = newPlane.update_plane(newPlane.vNormal, newPlane.fDist);

testPlane = newPlane;

Expand Down Expand Up @@ -6276,27 +6276,27 @@ void Bsp::simplify_model_collision(int modelIdx, int hullIdx)

int Bsp::create_clipnode()
{
BSPCLIPNODE32* newNodes = new BSPCLIPNODE32[clipnodeCount + 1]{};
BSPCLIPNODE32* newNodes = new BSPCLIPNODE32[clipnodeCount + 1];
memcpy(newNodes, clipnodes, clipnodeCount * sizeof(BSPCLIPNODE32));

newNodes[clipnodeCount] = BSPCLIPNODE32();
replace_lump(LUMP_CLIPNODES, newNodes, (clipnodeCount + 1) * sizeof(BSPCLIPNODE32));

return clipnodeCount - 1;
}

int Bsp::create_plane()
{
BSPPLANE* newPlanes = new BSPPLANE[planeCount + 1]{};
BSPPLANE* newPlanes = new BSPPLANE[planeCount + 1];
memcpy(newPlanes, planes, planeCount * sizeof(BSPPLANE));

newPlanes[planeCount] = BSPPLANE();
replace_lump(LUMP_PLANES, newPlanes, (planeCount + 1) * sizeof(BSPPLANE));

return planeCount - 1;
}

int Bsp::create_model()
{
BSPMODEL* newModels = new BSPMODEL[modelCount + 1]{};
BSPMODEL* newModels = new BSPMODEL[modelCount + 1];
memcpy(newModels, models, modelCount * sizeof(BSPMODEL));

newModels[modelCount] = BSPMODEL();
Expand All @@ -6309,7 +6309,7 @@ int Bsp::create_model()

int Bsp::create_texinfo()
{
BSPTEXTUREINFO* newTexinfos = new BSPTEXTUREINFO[texinfoCount + 1]{};
BSPTEXTUREINFO* newTexinfos = new BSPTEXTUREINFO[texinfoCount + 1];
memcpy(newTexinfos, texinfos, texinfoCount * sizeof(BSPTEXTUREINFO));

newTexinfos[texinfoCount] = BSPTEXTUREINFO();
Expand Down Expand Up @@ -6733,7 +6733,7 @@ bool Bsp::leaf_del_face(int faceIdx, int leafIdx)
}

leaves[i].iFirstMarkSurface = surface_idx;
surface_idx = all_mark_surfaces.size();
surface_idx = (int)all_mark_surfaces.size();
leaves[i].nMarkSurfaces = surface_idx - leaves[i].iFirstMarkSurface;
}

Expand Down Expand Up @@ -7333,12 +7333,18 @@ bool Bsp::is_worldspawn_ent(int entIdx)

int Bsp::regenerate_clipnodes_from_nodes(int iNode, int hullIdx)
{
if (iNode == 2269)
{
print_log("Regen {} of {}\n", iNode, nodeCount);
if (iNode < 0 || iNode >= nodeCount) {
// Handle out-of-bounds index for nodes array
return -1;
}

BSPNODE32& node = nodes[iNode];

if (node.iPlane < 0 || node.iPlane >= planeCount) {
// Handle out-of-bounds index for planes array
return -1;
}

switch (planes[node.iPlane].nType)
{
case PLANE_X: case PLANE_Y: case PLANE_Z:
Expand All @@ -7350,7 +7356,12 @@ int Bsp::regenerate_clipnodes_from_nodes(int iNode, int hullIdx)
{
if (node.iChildren[i] < 0)
{
BSPLEAF32& leaf = leaves[~node.iChildren[i]];
int leafIndex = ~node.iChildren[i];
if (leafIndex < 0 || leafIndex >= leafCount) {
// Handle out-of-bounds index for leaves array
return -1;
}
BSPLEAF32& leaf = leaves[leafIndex];
childContents[i] = leaf.nContents;
}
}
Expand All @@ -7374,6 +7385,10 @@ int Bsp::regenerate_clipnodes_from_nodes(int iNode, int hullIdx)
}

int newClipnodeIdx = create_clipnode();
if (newClipnodeIdx < 0 || newClipnodeIdx >= clipnodeCount) {
// Handle out-of-bounds index for clipnodes array
return -1;
}
clipnodes[newClipnodeIdx].iPlane = create_plane();

int solidChild = -1;
Expand All @@ -7382,12 +7397,21 @@ int Bsp::regenerate_clipnodes_from_nodes(int iNode, int hullIdx)
if (node.iChildren[i] >= 0)
{
int childIdx = regenerate_clipnodes_from_nodes(node.iChildren[i], hullIdx);
if (childIdx < 0 || childIdx >= clipnodeCount) {
// Handle out-of-bounds index for clipnodes array
return -1;
}
clipnodes[newClipnodeIdx].iChildren[i] = childIdx;
solidChild = solidChild == -1 ? i : -1;
}
else
{
BSPLEAF32& leaf = leaves[~node.iChildren[i]];
int leafIndex = ~node.iChildren[i];
if (leafIndex < 0 || leafIndex >= leafCount) {
// Handle out-of-bounds index for leaves array
return -1;
}
BSPLEAF32& leaf = leaves[leafIndex];
clipnodes[newClipnodeIdx].iChildren[i] = leaf.nContents;
if (leaf.nContents == CONTENTS_SOLID)
{
Expand All @@ -7396,7 +7420,15 @@ int Bsp::regenerate_clipnodes_from_nodes(int iNode, int hullIdx)
}
}

if (node.iPlane < 0 || node.iPlane >= planeCount) {
// Handle out-of-bounds index for planes array
return -1;
}
BSPPLANE& nodePlane = planes[node.iPlane];
if (clipnodes[newClipnodeIdx].iPlane < 0 || clipnodes[newClipnodeIdx].iPlane >= planeCount) {
// Handle out-of-bounds index for planes array
return -1;
}
BSPPLANE& clipnodePlane = planes[clipnodes[newClipnodeIdx].iPlane];
clipnodePlane = nodePlane;

Expand All @@ -7414,6 +7446,10 @@ int Bsp::regenerate_clipnodes_from_nodes(int iNode, int hullIdx)
// enough to "link" clipnode planes to node planes during scaling because BSP trees might not match.
if (solidChild != -1)
{
if (clipnodes[newClipnodeIdx].iPlane < 0 || clipnodes[newClipnodeIdx].iPlane >= planeCount) {
// Handle out-of-bounds index for planes array
return -1;
}
BSPPLANE& p = planes[clipnodes[newClipnodeIdx].iPlane];
vec3 planePoint = p.vNormal * p.fDist;
vec3 newPlanePoint = planePoint + p.vNormal * (solidChild == 0 ? -extent : extent);
Expand Down
2 changes: 1 addition & 1 deletion src/bsp/bsptypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ BSPEDGE32::BSPEDGE32(unsigned int v1, unsigned int v2)
iVertex[1] = v2;
}

bool BSPPLANE::update(vec3 newNormal, float fdist)
bool BSPPLANE::update_plane(vec3 newNormal, float fdist)
{
double fx = abs(newNormal.x);
double fy = abs(newNormal.y);
Expand Down
66 changes: 63 additions & 3 deletions src/bsp/bsptypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "vectors.h"
#include "bsplimits.h"
#include <vector>
#include <array>

#pragma pack(push, 1)

Expand Down Expand Up @@ -166,7 +167,7 @@ struct BSPPLANE
int nType;

// returns true if the plane was flipped
bool update(vec3 newNormal, float fdist);
bool update_plane(vec3 newNormal, float fdist);

BSPPLANE() :vNormal(vec3())
{
Expand All @@ -189,6 +190,14 @@ struct BSPTEXTUREINFO
float shiftT;
int iMiptex;
int nFlags;

BSPTEXTUREINFO()
{
vS = vec3(1.0f, 0.0f, 0.0f);
vT = vec3(0.0f, 0.0f, -1.0f);
shiftS = shiftT = 0.0f;
iMiptex = nFlags = 0;
}
};


Expand All @@ -209,6 +218,13 @@ struct BSPFACE32
int iTextureInfo; // Index of the texture info structure
unsigned char nStyles[MAX_LIGHTMAPS]; // Specify lighting styles
int nLightmapOffset; // Offsets into the raw lightmap data
BSPFACE32()
{
iPlane = nPlaneSide = iFirstEdge = nEdges = iTextureInfo = 0;
nLightmapOffset = -1;
nStyles[0] = nStyles[1] =
nStyles[2] = nStyles[3] = 255;
}
};


Expand All @@ -221,7 +237,16 @@ struct BSPLEAF32
int iFirstMarkSurface;
int nMarkSurfaces;
unsigned char nAmbientLevels[MAX_AMBIENTS];

BSPLEAF32()
{
nContents = CONTENTS_EMPTY;
nVisOffset = -1;
nMins = nMaxs = vec3();
iFirstMarkSurface = 0;
nMarkSurfaces = 0;
nAmbientLevels[0] = nAmbientLevels[1] =
nAmbientLevels[2] = nAmbientLevels[3] = 0;
}
bool isEmpty();
};

Expand All @@ -241,8 +266,15 @@ struct BSPMODEL
int iHeadnodes[MAX_MAP_HULLS]; // Index into nodes array
int nVisLeafs; // ???
int iFirstFace, nFaces; // Index and count into faces
};

BSPMODEL()
{
nMins = nMaxs = vOrigin = vec3();
iHeadnodes[0] = iHeadnodes[1] =
iHeadnodes[2] = iHeadnodes[3] = -1;
nVisLeafs = iFirstFace = nFaces = 0;
}
};
struct BSPNODE32
{
int iPlane;
Expand All @@ -251,12 +283,40 @@ struct BSPNODE32
vec3 nMaxs;
int iFirstFace;
int nFaces; // counting both sides
BSPNODE32()
{
iPlane = 0;
iChildren[0] = iChildren[1] = 0;
nMins = nMaxs = vec3();
iFirstFace = nFaces = 0;
}
BSPNODE32(int plane, std::array<int,2> childs, vec3 mins, vec3 maxs, int firstface, int faces)
{
iPlane = plane;
iChildren[0] = childs[0];
iChildren[1] = childs[1];
nMins = mins;
nMaxs = maxs;
iFirstFace = firstface;
nFaces = faces;
}
};

struct BSPCLIPNODE32
{
int iPlane; // Index into planes
int iChildren[2]; // negative numbers are contents
BSPCLIPNODE32()
{
iPlane = 0;
iChildren[0] = iChildren[1] = -1;
}
BSPCLIPNODE32(int plane, std::array<int,2> childs)
{
iPlane = plane;
iChildren[0] = childs[0];
iChildren[1] = childs[1];
}
};

/* other */
Expand Down
Loading

0 comments on commit ec001c1

Please sign in to comment.