Skip to content

Commit

Permalink
Moved occlusion planes from per-scene to per-room
Browse files Browse the repository at this point in the history
  • Loading branch information
sauraen committed Jun 9, 2024
1 parent 118f391 commit 0cf008c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 49 deletions.
2 changes: 0 additions & 2 deletions include/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -1993,8 +1993,6 @@ void Play_EnableMotionBlur(u32 alpha);
void Play_DisableMotionBlur(void);

#if ENABLE_F3DEX3
void OcclusionPlane_NewScene(PlayState* play);
void OcclusionPlane_SceneCmd(PlayState* play, OcclusionPlaneCandidate* list, u8 count);
void OcclusionPlane_Draw_Phase(PlayState* play, OcclusionPlanePhase phase);
void OcclusionPlane_Draw_PostCamUpdate(PlayState* play);
#endif
Expand Down
7 changes: 0 additions & 7 deletions include/occlusionplanes.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,4 @@ typedef enum OcclusionPlanePhase {
OCCLUSION_PLANE_PHASE_COUNT
} OcclusionPlanePhase;

typedef struct {
OcclusionPlaneCandidate* list;
u8 count;
Gfx* planeCommands[OCCLUSION_PLANE_PHASE_COUNT];
Vec3f selCandidate[4];
} OcclusionPlaneContext;

#endif // OCCLUSIONPLANES_H
4 changes: 4 additions & 0 deletions include/z64.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ typedef struct {
/* 0x03 */ u8 behaviorType1;
/* 0x04 */ s8 echo;
/* 0x05 */ u8 lensMode;
#if ENABLE_F3DEX3
u8 occPlaneCount;
OcclusionPlaneCandidate* occPlaneList;
#endif
/* 0x08 */ RoomShape* roomShape; // original name: "ground_shape"
/* 0x0C */ void* segment;
/* 0x10 */ char unk_10[0x4];
Expand Down
66 changes: 33 additions & 33 deletions src/code/occlusionplanes.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "z64.h"
#include "functions.h"

static Gfx* planeCommands[OCCLUSION_PLANE_PHASE_COUNT];

#define DEBUG_OCCLUSION_PLANES true

static s32 OcclusionPlane_Choose(PlayState* play){
OcclusionPlaneContext* ctx = &play->occPlaneCtx;

static s32 OcclusionPlane_Choose(PlayState* play, Vec3f* selCandidate){
Vec3f* camPos = &play->view.eye;
Vec3f camDir = (Vec3f){play->view.at.x - camPos->x, play->view.at.y - camPos->y, play->view.at.z - camPos->z};
float len2 = camDir.x * camDir.x + camDir.y * camDir.y + camDir.z * camDir.z;
Expand All @@ -26,8 +26,20 @@ static s32 OcclusionPlane_Choose(PlayState* play){
GfxPrint_SetColor(&printer, 255, 255, 255, 255);
#endif

for(s32 c=0; c<ctx->count; ++c){
OcclusionPlaneCandidate* cand = &ctx->list[c];
s32 c = -1;
Room* room = &play->roomCtx.curRoom;
while(true){
++c;
if(c >= room->occPlaneCount){
if(room == &play->roomCtx.curRoom){
room = &play->roomCtx.prevRoom;
c = -1;
continue;
}
break;
}

OcclusionPlaneCandidate* cand = room->occPlaneList[c];
Vec3f v[4];
for(s32 p=0; p<4; ++p){
v[p].x = cand->v[p].x;
Expand Down Expand Up @@ -99,7 +111,7 @@ static s32 OcclusionPlane_Choose(PlayState* play){
#endif
if(score > bestScore){
bestScore = score;
bcopy(v, ctx->selCandidate, 4*sizeof(Vec3f));
bcopy(v, selCandidate, 4*sizeof(Vec3f));
}
}

Expand Down Expand Up @@ -542,50 +554,38 @@ static OcclusionPlane* ComputeOcclusionPlane(PlayState* play, Vec3f* worldBounds
}

static bool ShouldBotherComputingOccPlanes(PlayState* play){
OcclusionPlaneContext* ctx = &play->occPlaneCtx;
if(ctx->list == NULL || ctx->count == 0) return false;
if(gF3DEX3OccMode == F3DEX3_OCC_MODE_NEVER) return false;
return true;
}

void OcclusionPlane_NewScene(PlayState* play){
play->occPlaneCtx.list = NULL;
if(gF3DEX3OccMode == F3DEX3_OCC_MODE_AUTO){
gF3DEX3NOCVersion = 1; // Enable NOC
}
}

void OcclusionPlane_SceneCmd(PlayState* play, OcclusionPlaneCandidate* list, u8 count){
play->occPlaneCtx.list = list;
play->occPlaneCtx.count = count;
if(gF3DEX3OccMode == F3DEX3_OCC_MODE_AUTO){
gF3DEX3NOCVersion = (count == 0); // NOC if no planes, otherwise normal
}
return (play->roomCtx.curRoom.occPlaneCount > 0
|| play->roomCtx.prevRoom.occPlaneCount > 0);
}

void OcclusionPlane_Draw_Phase(PlayState* play, OcclusionPlanePhase phase){
if(!ShouldBotherComputingOccPlanes(play)) return;

GraphicsContext* gfxCtx = play->state.gfxCtx;
OPEN_DISPS(gfxCtx, "occlusionplanes.c", __LINE__);
play->occPlaneCtx.planeCommands[phase] = POLY_OPA_DISP;
planeCommands[phase] = POLY_OPA_DISP;
gSPOcclusionPlane(POLY_OPA_DISP++, &sNoOcclusionPlane);
CLOSE_DISPS(gfxCtx, "occlusionplanes.c", __LINE__);

if(phase == OCCLUSION_PLANE_PHASE_START){
// Post sky might not happen in debug
play->occPlaneCtx.planeCommands[OCCLUSION_PLANE_PHASE_POST_SKY] = NULL;
planeCommands[OCCLUSION_PLANE_PHASE_POST_SKY] = NULL;
}
}

void OcclusionPlane_Draw_PostCamUpdate(PlayState* play){
if(!ShouldBotherComputingOccPlanes(play)) return;
bool enableOcc = ShouldBotherComputingOccPlanes(play);
if(gF3DEX3OccMode == F3DEX3_OCC_MODE_AUTO){
gF3DEX3NOCVersion = !enableOcc;
}
if(!enableOcc) return;

OcclusionPlaneContext* ctx = &play->occPlaneCtx;
s32 result = OcclusionPlane_Choose(play);
Vec3f selCandidate[4];
s32 result = OcclusionPlane_Choose(play, selCandidate);
if(result != 0) return;

OcclusionPlane* mainPlane = ComputeOcclusionPlane(play, ctx->selCandidate);
OcclusionPlane* mainPlane = ComputeOcclusionPlane(play, selCandidate);
if(mainPlane != &sNoOcclusionPlane){
OcclusionPlane* skyPlane = mainPlane;
// The skybox is not actually really far away, it's a smallish box
Expand All @@ -599,9 +599,9 @@ void OcclusionPlane_Draw_PostCamUpdate(PlayState* play){
skyPlane->o.ky = 0;
skyPlane->o.kz = 0;
skyPlane->o.kc = 0x7FFF;
((u32*)(ctx->planeCommands[OCCLUSION_PLANE_PHASE_START]))[1] = (u32)skyPlane;
((u32*)(planeCommands[OCCLUSION_PLANE_PHASE_START]))[1] = (u32)skyPlane;
}
if(ctx->planeCommands[OCCLUSION_PLANE_PHASE_POST_SKY] != NULL){
((u32*)(ctx->planeCommands[OCCLUSION_PLANE_PHASE_POST_SKY]))[1] = (u32)mainPlane;
if(planeCommands[OCCLUSION_PLANE_PHASE_POST_SKY] != NULL){
((u32*)(planeCommands[OCCLUSION_PLANE_PHASE_POST_SKY]))[1] = (u32)mainPlane;
}
}
3 changes: 0 additions & 3 deletions src/code/z_play.c
Original file line number Diff line number Diff line change
Expand Up @@ -1693,9 +1693,6 @@ void Play_InitScene(PlayState* this, s32 spawn) {

this->numActorEntries = 0;

#if ENABLE_F3DEX3
OcclusionPlane_NewScene(this);
#endif
Object_InitContext(this, &this->objectCtx);
LightContext_Init(this, &this->lightCtx);
TransitionActor_InitContext(&this->state, &this->transiActorCtx);
Expand Down
6 changes: 4 additions & 2 deletions src/code/z_room.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,9 @@ void Room_DrawImage(PlayState* play, Room* room, u32 flags) {
void func_80096FD4(PlayState* play, Room* room) {
room->num = -1;
room->segment = NULL;
#if ENABLE_F3DEX3
room->occPlaneCount = 0;
#endif
}

u32 func_80096FE8(PlayState* play, RoomContext* roomCtx) {
Expand Down Expand Up @@ -652,8 +655,7 @@ void Room_Draw(PlayState* play, Room* room, u32 flags) {
}

void func_80097534(PlayState* play, RoomContext* roomCtx) {
roomCtx->prevRoom.num = -1;
roomCtx->prevRoom.segment = NULL;
func_80096FD4(play, &roomCtx->prevRoom);
func_80031B14(play, &play->actorCtx);
Actor_SpawnTransitionActors(play, &play->actorCtx);
Map_InitRoomData(play, roomCtx->curRoom.num);
Expand Down
4 changes: 2 additions & 2 deletions src/code/z_scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,8 @@ BAD_RETURN(s32) Scene_CommandMiscSettings(PlayState* play, SceneCmd* cmd) {

#if ENABLE_F3DEX3
BAD_RETURN(s32) Scene_CommandOccPlaneCandList(PlayState* play, SceneCmd* cmd){
OcclusionPlane_SceneCmd(play, SEGMENTED_TO_VIRTUAL(cmd->occPlaneCandList.list),
cmd->occPlaneCandList.count);
play->roomCtx.curRoom.occPlaneCount = cmd->occPlaneCandList.count;
play->roomCtx.curRoom.occPlaneList = SEGMENTED_TO_VIRTUAL(cmd->occPlaneCandList.list);
}
#endif

Expand Down

0 comments on commit 0cf008c

Please sign in to comment.