From 1c7dc246f570ffc6860aa07225ddd3c586eaff37 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Fri, 18 Nov 2022 21:44:05 +1000 Subject: [PATCH] Create functionally equivalent C for handwritten portalConvertCoordinates --- ld/gamefiles.jpn-final.inc | 2 +- ld/gamefiles.ntsc-beta.inc | 2 +- ld/gamefiles.ntsc-final.inc | 2 +- ld/gamefiles.pal-beta.inc | 2 +- ld/gamefiles.pal-final.inc | 2 +- src/game/bg.c | 4 +- src/game/portalconv.c | 99 +++++++++++++++++++++ src/game/{game_165360.s => portalconvasm.s} | 2 +- src/include/game/game_165360.h | 9 -- src/include/game/portalconv.h | 9 ++ src/include/types.h | 2 +- 11 files changed, 117 insertions(+), 18 deletions(-) create mode 100644 src/game/portalconv.c rename src/game/{game_165360.s => portalconvasm.s} (99%) delete mode 100644 src/include/game/game_165360.h create mode 100644 src/include/game/portalconv.h diff --git a/ld/gamefiles.jpn-final.inc b/ld/gamefiles.jpn-final.inc index a962b0d64..0ee9c13ad 100644 --- a/ld/gamefiles.jpn-final.inc +++ b/ld/gamefiles.jpn-final.inc @@ -189,7 +189,7 @@ build/ROMID/game/collisionutils.o (section); \ build/ROMID/game/bg.o (section); \ build/ROMID/game/bgbss.o (section); \ - build/ROMID/game/game_165360.o (section); \ + build/ROMID/game/portalconvasm.o (section); \ build/ROMID/game/stagetable.o (section); \ build/ROMID/game/gfxreplace.o (section); \ build/ROMID/game/env.o (section); \ diff --git a/ld/gamefiles.ntsc-beta.inc b/ld/gamefiles.ntsc-beta.inc index 5dd195310..08c291f42 100644 --- a/ld/gamefiles.ntsc-beta.inc +++ b/ld/gamefiles.ntsc-beta.inc @@ -190,7 +190,7 @@ build/ROMID/game/collisionutils.o (section); \ build/ROMID/game/bg.o (section); \ build/ROMID/game/bgbss.o (section); \ - build/ROMID/game/game_165360.o (section); \ + build/ROMID/game/portalconvasm.o (section); \ build/ROMID/game/stagetable.o (section); \ build/ROMID/game/gfxreplace.o (section); \ build/ROMID/game/env.o (section); \ diff --git a/ld/gamefiles.ntsc-final.inc b/ld/gamefiles.ntsc-final.inc index a962b0d64..0ee9c13ad 100644 --- a/ld/gamefiles.ntsc-final.inc +++ b/ld/gamefiles.ntsc-final.inc @@ -189,7 +189,7 @@ build/ROMID/game/collisionutils.o (section); \ build/ROMID/game/bg.o (section); \ build/ROMID/game/bgbss.o (section); \ - build/ROMID/game/game_165360.o (section); \ + build/ROMID/game/portalconvasm.o (section); \ build/ROMID/game/stagetable.o (section); \ build/ROMID/game/gfxreplace.o (section); \ build/ROMID/game/env.o (section); \ diff --git a/ld/gamefiles.pal-beta.inc b/ld/gamefiles.pal-beta.inc index 5af17c9e9..067965ff3 100644 --- a/ld/gamefiles.pal-beta.inc +++ b/ld/gamefiles.pal-beta.inc @@ -190,7 +190,7 @@ build/ROMID/game/collisionutils.o (section); \ build/ROMID/game/bg.o (section); \ build/ROMID/game/bgbss.o (section); \ - build/ROMID/game/game_165360.o (section); \ + build/ROMID/game/portalconvasm.o (section); \ build/ROMID/game/stagetable.o (section); \ build/ROMID/game/gfxreplace.o (section); \ build/ROMID/game/env.o (section); \ diff --git a/ld/gamefiles.pal-final.inc b/ld/gamefiles.pal-final.inc index a962b0d64..0ee9c13ad 100644 --- a/ld/gamefiles.pal-final.inc +++ b/ld/gamefiles.pal-final.inc @@ -189,7 +189,7 @@ build/ROMID/game/collisionutils.o (section); \ build/ROMID/game/bg.o (section); \ build/ROMID/game/bgbss.o (section); \ - build/ROMID/game/game_165360.o (section); \ + build/ROMID/game/portalconvasm.o (section); \ build/ROMID/game/stagetable.o (section); \ build/ROMID/game/gfxreplace.o (section); \ build/ROMID/game/env.o (section); \ diff --git a/src/game/bg.c b/src/game/bg.c index fcc914586..bf072b698 100644 --- a/src/game/bg.c +++ b/src/game/bg.c @@ -20,7 +20,7 @@ #include "game/gfxmemory.h" #include "game/gfxreplace.h" #include "game/bg.h" -#include "game/game_165360.h" +#include "game/portalconv.h" #include "game/stagetable.h" #include "game/env.h" #include "game/room.h" @@ -3689,7 +3689,7 @@ bool g_PortalGetScreenBbox(s32 portalnum, struct screenbox *box) return g_PortalThings[portalnum].unk06; } - len = func0f165360(portalnum, &start, things); + len = portalConvertCoordinates(portalnum, &start, things); sp2ec = 0; thing = &things[start]; diff --git a/src/game/portalconv.c b/src/game/portalconv.c new file mode 100644 index 000000000..4718f4489 --- /dev/null +++ b/src/game/portalconv.c @@ -0,0 +1,99 @@ +#include +#include "constants.h" +#include "bss.h" +#include "lib/memp.h" +#include "data.h" +#include "types.h" + +/** + * This function is not linked in the N64 build. The N64 build appears to use + * handwritten assembly for this function. The below is functionally equivalent + * C that can be dropped in place of the handwritten function by linking the + * different file. + */ +s32 portalConvertCoordinates(s32 portalnum, s32 *start, struct portalthing2 *things) +{ + Mtxf *mtx = g_Vars.currentplayer->worldtoscreenmtx; + struct portalvertices *pvertices = (struct portalvertices *) ((u32) g_BgPortals + g_BgPortals[portalnum].verticesoffset); + struct portalthing2 *left; + struct portalthing2 *right = &things[39]; + bool anybehind = false; + s32 i; + + for (i = 0; i < pvertices->count; i++) { + f32 x = pvertices->vertices[i].x; + f32 y = pvertices->vertices[i].y; + f32 z = pvertices->vertices[i].z; + + right->coord.x = mtx->m[0][0] * x + mtx->m[1][0] * y + mtx->m[2][0] * z; + right->coord.y = mtx->m[0][1] * x + mtx->m[1][1] * y + mtx->m[2][1] * z; + right->coord.z = mtx->m[0][2] * x + mtx->m[1][2] * y + mtx->m[2][2] * z; + + right->coord.x += mtx->m[3][0]; + right->coord.y += mtx->m[3][1]; + right->coord.z += mtx->m[3][2]; + + if (right->coord.z <= 0.0f) { + right->behind = false; + } else { + right->behind = true; + anybehind = true; + } + + right--; + } + + if (anybehind) { + s32 numfinalvertices = 0; + + right->coord.x = things[39].coord.x; + right->coord.y = things[39].coord.y; + right->coord.z = things[39].coord.z; + right->behind = things[39].behind; + + left = &things[0]; + + for (i = 0; i < pvertices->count; i++) { + s32 value = right[1].behind * 2 + right[0].behind; + f32 mult; + + if (value == 0) { + left->coord.x = right[1].coord.x; + left->coord.y = right[1].coord.y; + left->coord.z = right[1].coord.z; + left->behind = right[1].behind; + left++; + numfinalvertices++; + } else if (value == 1) { + left->coord.x = right[1].coord.x; + left->coord.y = right[1].coord.y; + left->coord.z = right[1].coord.z; + left->behind = right[1].behind; + left++; + numfinalvertices++; + + mult = -(right[0].coord.z / (right[1].coord.z - right[0].coord.z)); + left->coord.x = (right[1].coord.x - right[0].coord.x) * mult + right[0].coord.x; + left->coord.y = (right[1].coord.y - right[0].coord.y) * mult + right[0].coord.y; + left->coord.z = 0.0f; + left++; + numfinalvertices++; + } else { + mult = -(right[0].coord.z / (right[1].coord.z - right[0].coord.z)); + left->coord.x = (right[1].coord.x - right[0].coord.x) * mult + right[0].coord.x; + left->coord.y = (right[1].coord.y - right[0].coord.y) * mult + right[0].coord.y; + left->coord.z = 0.0f; + left++; + numfinalvertices++; + } + + right++; + } + + *start = 0; + return numfinalvertices; + } + + *start = 40 - pvertices->count; + return pvertices->count; +} diff --git a/src/game/game_165360.s b/src/game/portalconvasm.s similarity index 99% rename from src/game/game_165360.s rename to src/game/portalconvasm.s index 68d1b3e61..95e0c515e 100644 --- a/src/game/game_165360.s +++ b/src/game/portalconvasm.s @@ -10,7 +10,7 @@ glabel var7f1b76d0 .text -glabel func0f165360 +glabel portalConvertCoordinates mfc1 $t7, $f20 lui $a3, %hi(g_Vars) addiu $a3, $a3, %lo(g_Vars) diff --git a/src/include/game/game_165360.h b/src/include/game/game_165360.h deleted file mode 100644 index 554bd2f30..000000000 --- a/src/include/game/game_165360.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef _IN_GAME_GAME_165360_H -#define _IN_GAME_GAME_165360_H -#include -#include "data.h" -#include "types.h" - -s32 func0f165360(s32 portalnum, s32 *start, struct portalthing2 *things); - -#endif diff --git a/src/include/game/portalconv.h b/src/include/game/portalconv.h new file mode 100644 index 000000000..168d8265c --- /dev/null +++ b/src/include/game/portalconv.h @@ -0,0 +1,9 @@ +#ifndef _IN_GAME_PORTALCONV_H +#define _IN_GAME_PORTALCONV_H +#include +#include "data.h" +#include "types.h" + +s32 portalConvertCoordinates(s32 portalnum, s32 *start, struct portalthing2 *things); + +#endif diff --git a/src/include/types.h b/src/include/types.h index 53fd59ef4..18aee2e77 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -6357,7 +6357,7 @@ struct modelrwdatabinding { struct portalthing2 { struct coord coord; - u32 unk0c; + bool behind; }; struct var800a6538 {