Skip to content

Commit

Permalink
Merge branch 'master' into cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pskelton authored Nov 6, 2024
2 parents 3d29f04 + 00e7d17 commit 6ded4a3
Show file tree
Hide file tree
Showing 16 changed files with 412 additions and 407 deletions.
5 changes: 3 additions & 2 deletions src/Engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "Engine/Graphics/LightsStack.h"
#include "Engine/Graphics/Outdoor.h"
#include "Engine/Graphics/Indoor.h"
#include "Engine/Graphics/BspRenderer.h"
#include "Engine/Graphics/Image.h"
#include "Engine/Graphics/Overlays.h"
#include "Engine/Graphics/PaletteManager.h"
Expand Down Expand Up @@ -279,7 +280,7 @@ void Engine::DrawGUI() {
if (uCurrentlyLoadedLevelType == LEVEL_INDOOR) {
int sector_id = pBLVRenderParams->uPartySectorID;
pPrimaryWindow->DrawText(assets->pFontArrus.get(), { 16, debug_info_offset }, colorTable.White,
fmt::format("Party Sector ID: {}/{}\n", sector_id, pIndoor->pSectors.size()));
fmt::format("Party Sector ID: {}/{} ({})\n", sector_id, pIndoor->pSectors.size(), pBLVRenderParams->uPartyEyeSectorID));
debug_info_offset += 16;
}

Expand All @@ -291,7 +292,7 @@ void Engine::DrawGUI() {
int uFaceID;
int sector_id = pBLVRenderParams->uPartySectorID;
float floor_level = BLV_GetFloorLevel(pParty->pos/* + Vec3f(0,0,40) */, sector_id, &uFaceID);
floor_level_str = fmt::format("BLV_GetFloorLevel: {} face_id {}\n", floor_level, uFaceID);
floor_level_str = fmt::format("BLV_GetFloorLevel: {} face_id {}\nNodes: {}, Faces: {} ({}), Sectors: {}\n", floor_level, uFaceID, pBspRenderer->num_nodes, pBspRenderer->num_faces, pBLVRenderParams->uNumFacesRenderedThisFrame, pBspRenderer->uNumVisibleNotEmptySectors);
} else if (uCurrentlyLoadedLevelType == LEVEL_OUTDOOR) {
bool on_water = false;
int bmodel_pid;
Expand Down
343 changes: 199 additions & 144 deletions src/Engine/Graphics/BspRenderer.cpp

Large diffs are not rendered by default.

31 changes: 20 additions & 11 deletions src/Engine/Graphics/BspRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@

#include "Library/Geometry/Plane.h"

struct BspRenderer;

struct BspRenderer_ViewportNode {
int uSectorID = 0; // sector that this node shows
int uFaceID = 0; // face id of the portal through which we're seeing this node
int parentNodeId = 0;
std::array<Planef, 4> ViewportNodeFrustum = {{}}; // frustum planes of portal
std::array<RenderVertexSoft, 4> pPortalBounding = {{}}; // extents of portal

private:
void SetFrustumToCamera();

friend BspRenderer;
};

struct BspFace {
Expand All @@ -19,22 +26,24 @@ struct BspFace {
};

struct BspRenderer {
void AddFaceToRenderList_d3d(int node_id, int uFaceID);
void MakeVisibleSectorList();
public:
void Init();

// TODO(yoctozepto): hide these
unsigned int num_faces = 0;
std::array<BspFace, 1500> faces = {{}};
std::array<BspFace, 1500> faces = { {} };

unsigned int num_nodes = 0;
std::array<BspRenderer_ViewportNode, 150> nodes = {{}};
std::array<BspRenderer_ViewportNode, 150> nodes = { {} };

unsigned int uNumVisibleNotEmptySectors = 0;
std::array<int, 150> pVisibleSectorIDs_toDrawDecorsActorsEtcFrom = {{}};
std::array<int, 150> pVisibleSectorIDs_toDrawDecorsActorsEtcFrom = { {} };

private:
void AddFace(const int node_id, const int uFaceID);
void AddNode();
void AddBSPFaces(const int node_id, const int bspNodeId);
void AddSector(int sectorId);
};

extern BspRenderer *pBspRenderer;

void PrepareBspRenderList_BLV();
void AddBspNodeToRenderList(int node_id);
void AddNodeBSPFaces(int node_id, int uFirstNode); // idb

37 changes: 12 additions & 25 deletions src/Engine/Graphics/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,11 @@ void Camera3D::GetFacetOrientation(const Vec3f &normal, Vec3f *outU, Vec3f *outV

//----- (00438258) --------------------------------------------------------
bool Camera3D::is_face_faced_to_cameraBLV(BLVFace *pFace) {
// if (pFace->Portal()) return false;

if (pFace->uNumVertices == 0)
return false; // TODO(captainurist): would be great to just filter these our on load & assert instead.

float x = pIndoor->pVertices[pFace->pVertexIDs[0]].x;
float y = pIndoor->pVertices[pFace->pVertexIDs[0]].y;
float z = pIndoor->pVertices[pFace->pVertexIDs[0]].z;

if ((z - pCamera3D->vCameraPos.z) * pFace->facePlane.normal.z +
(y - pCamera3D->vCameraPos.y) * pFace->facePlane.normal.y +
(x - pCamera3D->vCameraPos.x) * pFace->facePlane.normal.x <
0.0f)
return true;

return false;
return pFace->facePlane.dist +
pCamera3D->vCameraPos.z * pFace->facePlane.normal.z +
pCamera3D->vCameraPos.y * pFace->facePlane.normal.y +
pCamera3D->vCameraPos.x * pFace->facePlane.normal.x >
0.0f;
}

bool Camera3D::is_face_faced_to_cameraODM(ODMFace *pFace, RenderVertexSoft *a2) {
Expand Down Expand Up @@ -389,30 +378,28 @@ bool Camera3D::CullFaceToFrustum(RenderVertexSoft *a1, unsigned int *pOutNumVert
bool Camera3D::ClipFaceToFrustum(RenderVertexSoft *pInVertices,
unsigned int *pOutNumVertices,
RenderVertexSoft *pVertices,
Planef *CameraFrustrum,
signed int NumFrustumPlanes, char DebugLines,
int _unused) {
const Planef *CameraFrustrum) {
// NumFrustumPlanes usually 4 - top, bottom, left, right - near and far done elsewhere
// DebugLines 0 or 1 - 1 when debug lines

RenderVertexSoft *v14; // eax@8
RenderVertexSoft *v15; // edx@8
// float v17; // [sp+44h] [bp-10h]@1
// int v18; // [sp+48h] [bp-Ch]@5
int VertsAdjusted = 0; // [sp+53h] [bp-1h]@5
bool VertsAdjusted = false; // [sp+53h] [bp-1h]@5
// bool a6a; // [sp+70h] [bp+1Ch]@5

// TODO(yoctozepto): just have this as a global constant instead of random vars/4 around
const int NumFrustumPlanes = 4;

// v17 = 0.0;
// thisa = engine->pStru9Instance;

static RenderVertexSoft sr_vertices_50D9D8[64];

// result = 0;
// VertsAdjusted = 0;
int MinVertsAllowed = 2 * (DebugLines == 0) + 1; // 3 normally 1 for debuglines
// a6a = 0;
// v18 = MinVertsAllowed;
if (NumFrustumPlanes <= 0) return false;
const int MinVertsAllowed = 3;

// v12 = *pOutNumVertices;
// v13 = (char *)&a4->y;
Expand All @@ -431,7 +418,7 @@ bool Camera3D::ClipFaceToFrustum(RenderVertexSoft *pInVertices,

ClippingFunctions::ClipVertsToFrustumPlane(
v15, *pOutNumVertices, v14, pOutNumVertices, &CameraFrustrum[i].normal, -CameraFrustrum[i].dist,
(char*)&VertsAdjusted, _unused);
&VertsAdjusted);

// v12 = *pOutNumVertices;
if (*pOutNumVertices < MinVertsAllowed) {
Expand Down
4 changes: 1 addition & 3 deletions src/Engine/Graphics/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ struct Camera3D {
bool ClipFaceToFrustum(RenderVertexSoft *pInVertices,
unsigned int *pOutNumVertices,
RenderVertexSoft *pVertices,
Planef *CameraFrustrum,
int NumFrustumPlanes, char DebugLines,
int _unused);
const Planef *CameraFrustrum);

void BuildViewFrustum();
void CreateViewMatrixAndProjectionScale();
Expand Down
3 changes: 1 addition & 2 deletions src/Engine/Graphics/ClippingFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ bool ClippingFunctions::ClipVertsToPortal(RenderVertexSoft *pPortalBounding, //
bool ClippingFunctions::ClipVertsToFrustumPlane(RenderVertexSoft *pInVertices, signed int pInNumVertices,
RenderVertexSoft *pOutVertices,
unsigned int *pOutNumVertices,
Vec3f *CamFrustumNormal, float CamDotDistance, char *VertsAdjusted,
int unused) {
const Vec3f *CamFrustumNormal, float CamDotDistance, bool *VertsAdjusted) {
// this cycles through adjust vertice posisiton to supplied frustum plane
// points are inside frstum plane when point dot product is greater than camera dot distance

Expand Down
2 changes: 1 addition & 1 deletion src/Engine/Graphics/ClippingFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct ClippingFunctions {
RenderVertexSoft *pVertices2, unsigned int *pOutNumVertices);

static bool ClipVertsToFrustumPlane(RenderVertexSoft *pInVertices, signed int pInNumVertices, RenderVertexSoft *pOutVertices,
unsigned int *pOutNumVertices, Vec3f *CamFrustumNormal, float CamDotDistance, char *VertsAdjusted, int unused);
unsigned int *pOutNumVertices, const Vec3f *CamFrustumNormal, float CamDotDistance, bool *VertsAdjusted);

static void AddVertex(VertexBuffer *pVertexBuffer, RenderVertexSoft *pVertex);

Expand Down
15 changes: 6 additions & 9 deletions src/Engine/Graphics/Indoor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,6 @@ static constexpr IndexedArray<SoundId, MAP_FIRST, MAP_LAST> pDoorSoundIDsByLocat

//----- (0043F39E) --------------------------------------------------------
void PrepareDrawLists_BLV() {
// unsigned int v7; // ebx@8
BLVSector *v8; // esi@8

pBLVRenderParams->Reset();
uNumDecorationsDrawnThisFrame = 0;
uNumSpritesDrawnThisFrame = 0;
Expand All @@ -149,17 +146,17 @@ void PrepareDrawLists_BLV() {
//pStationaryLightsStack->uNumLightsActive = 0;
engine->StackPartyTorchLight();

PrepareBspRenderList_BLV();
pBspRenderer->Init();

render->DrawSpriteObjects();
pOutdoor->PrepareActorsDrawList();

for (unsigned i = 0; i < pBspRenderer->uNumVisibleNotEmptySectors; ++i) {
int v7 = pBspRenderer->pVisibleSectorIDs_toDrawDecorsActorsEtcFrom[i];
v8 = &pIndoor->pSectors[pBspRenderer->pVisibleSectorIDs_toDrawDecorsActorsEtcFrom[i]];
int sectorId = pBspRenderer->pVisibleSectorIDs_toDrawDecorsActorsEtcFrom[i];
BLVSector *sector = &pIndoor->pSectors[pBspRenderer->pVisibleSectorIDs_toDrawDecorsActorsEtcFrom[i]];

for (unsigned j = 0; j < v8->uNumDecorations; ++j)
pIndoor->PrepareDecorationsRenderList_BLV(v8->pDecorationIDs[j], v7);
for (unsigned j = 0; j < sector->uNumDecorations; ++j)
pIndoor->PrepareDecorationsRenderList_BLV(sector->pDecorationIDs[j], sectorId);
}

FindBillboardsLightLevels_BLV();
Expand Down Expand Up @@ -226,7 +223,7 @@ void BLVFace::FromODM(ODMFace *face) {
}

//----- (004AE5BA) --------------------------------------------------------
GraphicsImage *BLVFace::GetTexture() {
GraphicsImage *BLVFace::GetTexture() const {
if (this->IsTextureFrameTable())
// TODO(captainurist): using pEventTimer here is weird. This means that e.g. cleric in the haunted mansion is
// not animated in turn-based mode. Use misc timer? Also see ODMFace::GetTexture.
Expand Down
4 changes: 1 addition & 3 deletions src/Engine/Graphics/Indoor.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct BLVFace { // 60h
void FromODM(ODMFace *face);

void SetTexture(std::string_view filename);
GraphicsImage *GetTexture();
GraphicsImage *GetTexture() const;

inline bool Invisible() const {
return uAttributes & FACE_IsInvisible;
Expand Down Expand Up @@ -381,5 +381,3 @@ bool Check_LOS_Obscurred_Indoors(const Vec3f &target, const Vec3f &from);
* @return True if line of sight obscurred by outdoor models
*/
bool Check_LOS_Obscurred_Outdoors_Bmodels(const Vec3f &target, const Vec3f &from);

extern BspRenderer *pBspRenderer;
53 changes: 20 additions & 33 deletions src/Engine/Graphics/PortalFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int _49CE9E_sub0_z(RenderVertexSoft *pVertices, unsigned int uNumVertices,
}

//----- (0049CE9E) --------------------------------------------------------
void _49CE9E(BLVFace *pFace, RenderVertexSoft *pVertices,
void _49CE9E(const BLVFace *pFace, RenderVertexSoft *pVertices,
unsigned int uNumVertices, RenderVertexSoft *pOutLimits) {
assert(sizeof(RenderVertexSoft) == 0x30);

Expand All @@ -94,7 +94,7 @@ void _49CE9E(BLVFace *pFace, RenderVertexSoft *pVertices,
}

//----- (0049D379) --------------------------------------------------------
void CalcPolygonLimits(BLVFace *pFace, RenderVertexSoft *pOutVertices) {
void CalcPolygonLimits(const BLVFace *pFace, RenderVertexSoft *pOutVertices) {
FlatFace points;
pFace->Flatten(&points, MODEL_INDOOR);

Expand Down Expand Up @@ -156,7 +156,7 @@ void CalcPolygonLimits(BLVFace *pFace, RenderVertexSoft *pOutVertices) {
}

//----- (0049C9E3) --------------------------------------------------------
bool CalcFaceBounding(BLVFace *pFace, RenderVertexSoft *pFaceLimits,
bool CalcFaceBounding(const BLVFace *pFace, RenderVertexSoft *pFaceLimits,
unsigned int uNumVertices,
RenderVertexSoft *pOutBounding) {
Vec3f a1;
Expand Down Expand Up @@ -341,44 +341,31 @@ bool CalcFaceBounding(BLVFace *pFace, RenderVertexSoft *pFaceLimits,
}

//----- (0049C5DA) --------------------------------------------------------
bool CalcPortalShapePoly(BLVFace *pFace, RenderVertexSoft *pVertices,
unsigned int *pNumVertices, Planef *pOutFrustum,
RenderVertexSoft *pOutBounding) {
bool CalcPortalShapePoly(const BLVFace *pFace, RenderVertexSoft *pVertices,
unsigned int *pNumVertices, Planef *pOutFrustum, RenderVertexSoft *pOutBounding) {
// calc poly limits
RenderVertexSoft pLimits[4];
_49CE9E(pFace, pVertices, *pNumVertices, pLimits);

if (CalcFaceBounding(pFace, pLimits, 4, pOutBounding)) {
pCamera3D->ViewTransform(pOutBounding, 4);
pCamera3D->Project(pOutBounding, 4);
RenderVertexSoft temp[4];
memcpy(temp, pOutBounding, sizeof(RenderVertexSoft) * 4);
if (!CalcFaceBounding(pFace, pLimits, 4, pOutBounding)) {
return false;
}

// make sure frustum planes will be on correct side
if (pOutBounding[0].vWorldViewProjX > pOutBounding[3].vWorldViewProjX) {
pOutBounding[0] = temp[3];
pOutBounding[2] = temp[1];
pOutBounding[3] = temp[0];
pOutBounding[1] = temp[2];
}
pCamera3D->ViewTransform(pOutBounding, 4);
pCamera3D->Project(pOutBounding, 4);

// calculate the new frustum for this portal
bool test = CalcPortalFrustum(pOutBounding, pOutFrustum);

// if normal in z - portals dont work out so use camera
if (pFace->facePlane.normal.z == 1.0 || pFace->facePlane.normal.z == -1.0) {
for (int i = 0; i < 4; i++) {
pOutFrustum[i].normal.x = pCamera3D->FrustumPlanes[i].x;
pOutFrustum[i].normal.y = pCamera3D->FrustumPlanes[i].y;
pOutFrustum[i].normal.z = pCamera3D->FrustumPlanes[i].z;
pOutFrustum[i].dist = -pCamera3D->FrustumPlanes[i].w;
}
}

return test;
// make sure frustum planes will be on correct side
if (pOutBounding[0].vWorldViewProjX > pOutBounding[3].vWorldViewProjX) {
RenderVertexSoft temp[4];
memcpy(temp, pOutBounding, sizeof(RenderVertexSoft) * 4);
pOutBounding[0] = temp[3];
pOutBounding[2] = temp[1];
pOutBounding[3] = temp[0];
pOutBounding[1] = temp[2];
}

return false;
// calculate the new frustum for this portal
return CalcPortalFrustum(pOutBounding, pOutFrustum);
}

//----- (0049C720) --------------------------------------------------------
Expand Down
11 changes: 5 additions & 6 deletions src/Engine/Graphics/PortalFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@
struct BLVFace;
struct RenderVertexSoft;

bool CalcPortalShapePoly(BLVFace *pFace, RenderVertexSoft *pVertices,
unsigned int *pNumVertices, Planef *a5,
RenderVertexSoft *pOutBounding);
bool CalcPortalShapePoly(const BLVFace *pFace, RenderVertexSoft *pVertices,
unsigned int *pNumVertices, Planef *pOutFrustum, RenderVertexSoft* pOutBounding);
bool CalcPortalFrustum(RenderVertexSoft *pFaceBounding, Planef *pPortalDataFrustum);
bool CalcPortalFrustumPlane(RenderVertexSoft *pFaceBounding1,
RenderVertexSoft *pFaceBounding2,
Vec3f *pRayStart,
Planef *pPortalDataFrustum);
bool CalcFaceBounding(BLVFace *pFace,
bool CalcFaceBounding(const BLVFace *pFace,
RenderVertexSoft *pFaceLimits,
unsigned int uNumVertices,
RenderVertexSoft *pOutBounding);
void CalcPolygonLimits(BLVFace *pFace,
void CalcPolygonLimits(const BLVFace *pFace,
RenderVertexSoft pOutVertices[4]);
void _49CE9E(BLVFace *pFace, RenderVertexSoft *pVertices,
void _49CE9E(const BLVFace *pFace, RenderVertexSoft *pVertices,
unsigned int uNumVertices, RenderVertexSoft *pOutLimits);
2 changes: 2 additions & 0 deletions src/Engine/Graphics/RenderEntities.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ struct RenderVertexSoft {
float u = 0;
float v = 0;
float flt_2C = 0;

friend bool operator==(const RenderVertexSoft &l, const RenderVertexSoft &r) = default;
};

struct RenderVertexD3D3 {
Expand Down
Loading

0 comments on commit 6ded4a3

Please sign in to comment.