diff options
| author | engineer124 <47598039+engineer124@users.noreply.github.com> | 2022-10-02 10:32:57 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-02 11:32:57 -0300 |
| commit | 841da5f13818741c1a50cd16605ad4934b9de36e (patch) | |
| tree | 186a7eb0fd39af3ee2071addcaf52be5f685def3 /src/code/z_view.c | |
| parent | 063d24f491e137778bdd6c65f0942d8ce74a2a91 (diff) | |
View & Shrink_Window Docs (#1049)
* view and shrink_window docs
* cleanup
* move func declaration to header
* move struct to local
* PR Suggestions
* g to s
Diffstat (limited to 'src/code/z_view.c')
| -rw-r--r-- | src/code/z_view.c | 243 |
1 files changed, 140 insertions, 103 deletions
diff --git a/src/code/z_view.c b/src/code/z_view.c index 915c196b7..105e6643a 100644 --- a/src/code/z_view.c +++ b/src/code/z_view.c @@ -1,4 +1,9 @@ #include "global.h" +#include "z64shrink_window.h" +#include "z64view.h" + +s32 View_ApplyPerspective(View* view); +s32 View_ApplyOrtho(View* view); void View_ViewportToVp(Vp* dest, Viewport* src) { s32 width = src->rightX - src->leftX; @@ -13,41 +18,45 @@ void View_ViewportToVp(Vp* dest, Viewport* src) { dest->vp.vtrans[2] = 0x01FF; dest->vp.vtrans[3] = 0; - if (src->leftX == 0 && src->rightX == 576 && src->topY == 0 && src->bottomY) {} + if ((src->leftX == 0) && (src->rightX == SCREEN_WIDTH_HIGH_RES) && (src->topY == 0) && + (src->bottomY == SCREEN_HEIGHT_HIGH_RES)) {} } void View_Init(View* view, GraphicsContext* gfxCtx) { view->gfxCtx = gfxCtx; + view->viewport.topY = 0; - view->viewport.bottomY = 240; + view->viewport.bottomY = SCREEN_HEIGHT; view->viewport.leftX = 0; - view->viewport.rightX = 320; - view->magic = 0x56494557; // "VIEW" + view->viewport.rightX = SCREEN_WIDTH; - view->unk164 = 0; - view->flags = 1 | 2 | 4; + view->magic = 0x56494557; // "VIEW" - if (1) { - ; - } + if (1) {} view->scale = 1.0f; - view->up.y = 1.0f; view->fovy = 60.0f; + view->zNear = 10.0f; + view->zFar = 12800.0f; + view->eye.x = 0.0f; view->eye.y = 0.0f; + view->eye.z = -1.0f; + view->at.x = 0.0f; + view->up.x = 0.0f; + view->up.y = 1.0f; view->up.z = 0.0f; - view->zNear = 10.0f; - view->zFar = 12800.0f; - view->eye.z = -1.0f; + + view->unk164 = 0; + view->flags = VIEW_VIEWING | VIEW_VIEWPORT | VIEW_PROJECTION_PERSPECTIVE; View_InitDistortion(view); } -void View_SetViewOrientation(View* view, Vec3f* eye, Vec3f* at, Vec3f* up) { - if (eye->x == at->x && eye->z == at->z) { +void View_LookAt(View* view, Vec3f* eye, Vec3f* at, Vec3f* up) { + if ((eye->x == at->x) && (eye->z == at->z)) { eye->z += 0.1f; up->z = 0.0f; up->x = 0.0f; @@ -57,17 +66,21 @@ void View_SetViewOrientation(View* view, Vec3f* eye, Vec3f* at, Vec3f* up) { view->eye = *eye; view->at = *at; view->up = *up; - view->flags |= 1; + view->flags |= VIEW_VIEWING; } -void func_8013F050(View* view, Vec3f* eye, Vec3f* at, Vec3f* up) { +/* + * Unused. View_LookAt is always used instead. This version is similar but + * is missing the input sanitization and the update to the flags. + */ +void View_LookAtUnsafe(View* view, Vec3f* eye, Vec3f* at, Vec3f* up) { view->eye = *eye; view->at = *at; view->up = *up; } void View_SetScale(View* view, f32 scale) { - view->flags |= 4; + view->flags |= VIEW_PROJECTION_PERSPECTIVE; view->scale = scale; } @@ -75,28 +88,32 @@ void View_GetScale(View* view, f32* scale) { *scale = view->scale; } -void func_8013F0D0(View* view, f32 fovy, f32 zNear, f32 zFar) { +void View_SetPerspective(View* view, f32 fovy, f32 zNear, f32 zFar) { view->fovy = fovy; view->zNear = zNear; view->zFar = zFar; - view->flags |= 4; + view->flags |= VIEW_PROJECTION_PERSPECTIVE; } -void func_8013F100(View* view, f32* fovy, f32* zNear, f32* zFar) { +void View_GetPerspective(View* view, f32* fovy, f32* zNear, f32* zFar) { *fovy = view->fovy; *zNear = view->zNear; *zFar = view->zFar; } -void func_8013F120(View* view, f32 fovy, f32 zNear, f32 zFar) { +void View_SetOrtho(View* view, f32 fovy, f32 zNear, f32 zFar) { view->fovy = fovy; view->zNear = zNear; view->zFar = zFar; - view->flags |= 8; + view->flags |= VIEW_PROJECTION_ORTHO; view->scale = 1.0f; } -void func_8013F15C(View* view, f32* fovy, f32* zNear, f32* zFar) { +/* + * Identical to View_GetPerspective, and never called. + * Named as it seems to fit the "set, get" pattern. + */ +void View_GetOrtho(View* view, f32* fovy, f32* zNear, f32* zFar) { *fovy = view->fovy; *zNear = view->zNear; *zFar = view->zFar; @@ -104,14 +121,14 @@ void func_8013F15C(View* view, f32* fovy, f32* zNear, f32* zFar) { void View_SetViewport(View* view, Viewport* viewport) { view->viewport = *viewport; - view->flags |= 2; + view->flags |= VIEW_VIEWPORT; } void View_GetViewport(View* view, Viewport* viewport) { *viewport = view->viewport; } -void View_WriteScissor(Gfx** gfx, s32 ulx, s32 uly, s32 lrx, s32 lry) { +void View_SetScissor(Gfx** gfx, s32 ulx, s32 uly, s32 lrx, s32 lry) { Gfx* gfxp = *gfx; gDPSetScissor(gfxp++, G_SC_NON_INTERLACE, ulx, uly, lrx, lry); @@ -119,7 +136,7 @@ void View_WriteScissor(Gfx** gfx, s32 ulx, s32 uly, s32 lrx, s32 lry) { *gfx = gfxp; } -void View_SyncAndWriteScissor(View* view, Gfx** gfx) { +void View_ClearScissor(View* view, Gfx** gfx) { Gfx* gfxp = *gfx; s32 ulx = view->viewport.leftX; s32 uly = view->viewport.topY; @@ -127,12 +144,12 @@ void View_SyncAndWriteScissor(View* view, Gfx** gfx) { s32 lry = view->viewport.bottomY; gDPPipeSync(gfxp++); - View_WriteScissor(&gfxp, ulx, uly, lrx, lry); + View_SetScissor(&gfxp, ulx, uly, lrx, lry); *gfx = gfxp; } -void View_SetScissorForLetterbox(View* view) { +void View_ApplyLetterbox(View* view) { s32 letterboxY; s32 letterboxX; s32 pad1; @@ -143,20 +160,21 @@ void View_SetScissorForLetterbox(View* view) { OPEN_DISPS(view->gfxCtx); - letterboxY = ShrinkWindow_GetLetterboxMagnitude(); + letterboxY = ShrinkWindow_Letterbox_GetSize(); + letterboxX = -1; // The following is optimized to varX = 0 but affects codegen if (letterboxX < 0) { letterboxX = 0; } - if (letterboxX > 160) { - letterboxX = 160; + if (letterboxX > (SCREEN_WIDTH / 2)) { + letterboxX = SCREEN_WIDTH / 2; } if (letterboxY < 0) { letterboxY = 0; - } else if (letterboxY > 120) { - letterboxY = 120; + } else if (letterboxY > (SCREEN_HEIGHT / 2)) { + letterboxY = SCREEN_HEIGHT / 2; } ulx = view->viewport.leftX + letterboxX; @@ -166,31 +184,31 @@ void View_SetScissorForLetterbox(View* view) { gDPPipeSync(POLY_OPA_DISP++); { - s32 pad3; + s32 pad2; Gfx* polyOpa; polyOpa = POLY_OPA_DISP; - View_WriteScissor(&polyOpa, ulx, uly, lrx, lry); + View_SetScissor(&polyOpa, ulx, uly, lrx, lry); POLY_OPA_DISP = polyOpa; } gDPPipeSync(POLY_XLU_DISP++); { Gfx* polyXlu; - s32 pad4; + s32 pad3; polyXlu = POLY_XLU_DISP; - View_WriteScissor(&polyXlu, ulx, uly, lrx, lry); + View_SetScissor(&polyXlu, ulx, uly, lrx, lry); POLY_XLU_DISP = polyXlu; } CLOSE_DISPS(view->gfxCtx); } -s32 View_SetDistortionDirRot(View* view, f32 dirRotX, f32 dirRotY, f32 dirRotZ) { - view->distortionDirRot.x = dirRotX; - view->distortionDirRot.y = dirRotY; - view->distortionDirRot.z = dirRotZ; +s32 View_SetDistortionOrientation(View* view, f32 rotX, f32 rotY, f32 rotZ) { + view->distortionOrientation.x = rotX; + view->distortionOrientation.y = rotY; + view->distortionOrientation.z = rotZ; return 1; } @@ -207,22 +225,22 @@ s32 View_SetDistortionSpeed(View* view, f32 speed) { } s32 View_InitDistortion(View* view) { - view->distortionDirRot.x = 0.0f; - view->distortionDirRot.y = 0.0f; - view->distortionDirRot.z = 0.0f; + view->distortionOrientation.x = 0.0f; + view->distortionOrientation.y = 0.0f; + view->distortionOrientation.z = 0.0f; view->distortionScale.x = 1.0f; view->distortionScale.y = 1.0f; view->distortionScale.z = 1.0f; - view->curDistortionDirRot = view->distortionDirRot; + view->curDistortionOrientation = view->distortionOrientation; view->curDistortionScale = view->distortionScale; view->distortionSpeed = 0.0f; return 1; } s32 View_ClearDistortion(View* view) { - view->distortionDirRot.x = 0.0f; - view->distortionDirRot.y = 0.0f; - view->distortionDirRot.z = 0.0f; + view->distortionOrientation.x = 0.0f; + view->distortionOrientation.y = 0.0f; + view->distortionOrientation.z = 0.0f; view->distortionScale.x = 1.0f; view->distortionScale.y = 1.0f; view->distortionScale.z = 1.0f; @@ -230,8 +248,8 @@ s32 View_ClearDistortion(View* view) { return 1; } -s32 View_SetDistortion(View* view, Vec3f dirRot, Vec3f scale, f32 speed) { - view->distortionDirRot = dirRot; +s32 View_SetDistortion(View* view, Vec3f orientation, Vec3f scale, f32 speed) { + view->distortionOrientation = orientation; view->distortionScale = scale; view->distortionSpeed = speed; return 1; @@ -242,17 +260,19 @@ s32 View_StepDistortion(View* view, Mtx* projectionMtx) { if (view->distortionSpeed == 0.0f) { return false; - } else if (view->distortionSpeed == 1.0f) { - view->curDistortionDirRot = view->distortionDirRot; + } + + if (view->distortionSpeed == 1.0f) { + view->curDistortionOrientation = view->distortionOrientation; view->curDistortionScale = view->distortionScale; view->distortionSpeed = 0.0f; } else { - view->curDistortionDirRot.x = - F32_LERPIMP(view->curDistortionDirRot.x, view->distortionDirRot.x, view->distortionSpeed); - view->curDistortionDirRot.y = - F32_LERPIMP(view->curDistortionDirRot.y, view->distortionDirRot.y, view->distortionSpeed); - view->curDistortionDirRot.z = - F32_LERPIMP(view->curDistortionDirRot.z, view->distortionDirRot.z, view->distortionSpeed); + view->curDistortionOrientation.x = + F32_LERPIMP(view->curDistortionOrientation.x, view->distortionOrientation.x, view->distortionSpeed); + view->curDistortionOrientation.y = + F32_LERPIMP(view->curDistortionOrientation.y, view->distortionOrientation.y, view->distortionSpeed); + view->curDistortionOrientation.z = + F32_LERPIMP(view->curDistortionOrientation.z, view->distortionOrientation.z, view->distortionSpeed); view->curDistortionScale.x = F32_LERPIMP(view->curDistortionScale.x, view->distortionScale.x, view->distortionSpeed); @@ -264,29 +284,32 @@ s32 View_StepDistortion(View* view, Mtx* projectionMtx) { Matrix_MtxToMtxF(projectionMtx, &projectionMtxF); Matrix_Put(&projectionMtxF); - Matrix_RotateXFApply(view->curDistortionDirRot.x); - Matrix_RotateYF(view->curDistortionDirRot.y, MTXMODE_APPLY); - Matrix_RotateZF(view->curDistortionDirRot.z, MTXMODE_APPLY); + Matrix_RotateXFApply(view->curDistortionOrientation.x); + Matrix_RotateYF(view->curDistortionOrientation.y, MTXMODE_APPLY); + Matrix_RotateZF(view->curDistortionOrientation.z, MTXMODE_APPLY); Matrix_Scale(view->curDistortionScale.x, view->curDistortionScale.y, view->curDistortionScale.z, MTXMODE_APPLY); - Matrix_RotateZF(-view->curDistortionDirRot.z, MTXMODE_APPLY); - Matrix_RotateYF(-view->curDistortionDirRot.y, MTXMODE_APPLY); - Matrix_RotateXFApply(-view->curDistortionDirRot.x); + Matrix_RotateZF(-view->curDistortionOrientation.z, MTXMODE_APPLY); + Matrix_RotateYF(-view->curDistortionOrientation.y, MTXMODE_APPLY); + Matrix_RotateXFApply(-view->curDistortionOrientation.x); Matrix_ToMtx(projectionMtx); return true; } -void View_RenderView(View* view, s32 uParm2) { - uParm2 = (view->flags & uParm2) | uParm2 >> 4; +/** + * Apply view to POLY_OPA_DISP, POLY_XLU_DISP (and OVERLAY_DISP if ortho) + */ +void View_Apply(View* view, s32 mask) { + mask = (view->flags & mask) | (mask >> 4); - if (uParm2 & 8) { - View_RenderToOrthographicMatrix(view); + if (mask & VIEW_PROJECTION_ORTHO) { + View_ApplyOrtho(view); } else { - View_RenderToPerspectiveMatrix(view); + View_ApplyPerspective(view); } } -s32 View_RenderToPerspectiveMatrix(View* view) { +s32 View_ApplyPerspective(View* view) { f32 aspect; s32 width; s32 height; @@ -297,36 +320,39 @@ s32 View_RenderToPerspectiveMatrix(View* view) { OPEN_DISPS(gfxCtx); - vp = GRAPH_ALLOC(gfxCtx, sizeof(*vp)); + // Viewport + vp = GRAPH_ALLOC(gfxCtx, sizeof(Vp)); View_ViewportToVp(vp, &view->viewport); view->vp = *vp; - View_SetScissorForLetterbox(view); + View_ApplyLetterbox(view); gSPViewport(POLY_OPA_DISP++, vp); gSPViewport(POLY_XLU_DISP++, vp); - projection = GRAPH_ALLOC(gfxCtx, sizeof(*projection)); + // Perspective projection + projection = GRAPH_ALLOC(gfxCtx, sizeof(Mtx)); view->projectionPtr = projection; width = view->viewport.rightX - view->viewport.leftX; height = view->viewport.bottomY - view->viewport.topY; aspect = (f32)width / (f32)height; - guPerspective(projection, &view->normal, view->fovy, aspect, view->zNear, view->zFar, view->scale); + guPerspective(projection, &view->perspNorm, view->fovy, aspect, view->zNear, view->zFar, view->scale); view->projection = *projection; View_StepDistortion(view, projection); - gSPPerspNormalize(POLY_OPA_DISP++, view->normal); + gSPPerspNormalize(POLY_OPA_DISP++, view->perspNorm); gSPMatrix(POLY_OPA_DISP++, projection, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_PROJECTION); - gSPPerspNormalize(POLY_XLU_DISP++, view->normal); + gSPPerspNormalize(POLY_XLU_DISP++, view->perspNorm); gSPMatrix(POLY_XLU_DISP++, projection, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_PROJECTION); - viewing = GRAPH_ALLOC(gfxCtx, sizeof(*viewing)); + // View matrix (look-at) + viewing = GRAPH_ALLOC(gfxCtx, sizeof(Mtx)); view->viewingPtr = viewing; - if (view->eye.x == view->at.x && view->eye.y == view->at.y && view->eye.z == view->at.z) { + if ((view->eye.x == view->at.x) && (view->eye.y == view->at.y) && (view->eye.z == view->at.z)) { view->eye.z += 2.0f; } @@ -343,24 +369,24 @@ s32 View_RenderToPerspectiveMatrix(View* view) { return 1; } -s32 View_RenderToOrthographicMatrix(View* view) { +s32 View_ApplyOrtho(View* view) { Vp* vp; Mtx* projection; GraphicsContext* gfxCtx = view->gfxCtx; OPEN_DISPS(gfxCtx); - vp = GRAPH_ALLOC(gfxCtx, sizeof(*vp)); + vp = GRAPH_ALLOC(gfxCtx, sizeof(Vp)); View_ViewportToVp(vp, &view->viewport); view->vp = *vp; - View_SetScissorForLetterbox(view); + View_ApplyLetterbox(view); gSPViewport(POLY_OPA_DISP++, vp); gSPViewport(POLY_XLU_DISP++, vp); gSPViewport(OVERLAY_DISP++, vp); - projection = GRAPH_ALLOC(gfxCtx, sizeof(*projection)); + projection = GRAPH_ALLOC(gfxCtx, sizeof(Mtx)); view->projectionPtr = projection; guOrtho(projection, gScreenWidth * -0.5f, gScreenWidth * 0.5f, gScreenHeight * -0.5f, gScreenHeight * 0.5f, @@ -376,7 +402,10 @@ s32 View_RenderToOrthographicMatrix(View* view) { return 1; } -s32 func_8013FBC8(View* view) { +/** + * Apply scissor, viewport and projection (ortho) to OVERLAY_DISP. + */ +s32 View_ApplyOrthoToOverlay(View* view) { Vp* vp; Mtx* projection; GraphicsContext* gfxCtx; @@ -385,7 +414,7 @@ s32 func_8013FBC8(View* view) { OPEN_DISPS(gfxCtx); - vp = GRAPH_ALLOC(gfxCtx, sizeof(*vp)); + vp = GRAPH_ALLOC(gfxCtx, sizeof(Vp)); View_ViewportToVp(vp, &view->viewport); view->vp = *vp; @@ -395,26 +424,29 @@ s32 func_8013FBC8(View* view) { s32 pad; overlay = OVERLAY_DISP; - View_WriteScissor(&overlay, view->viewport.leftX, view->viewport.topY, view->viewport.rightX, - view->viewport.bottomY); + View_SetScissor(&overlay, view->viewport.leftX, view->viewport.topY, view->viewport.rightX, + view->viewport.bottomY); OVERLAY_DISP = overlay; } gSPViewport(OVERLAY_DISP++, vp); - projection = GRAPH_ALLOC(gfxCtx, sizeof(*projection)); + projection = GRAPH_ALLOC(gfxCtx, sizeof(Mtx)); view->projectionPtr = projection; guOrtho(projection, gScreenWidth * -0.5f, gScreenWidth * 0.5f, gScreenHeight * -0.5f, gScreenHeight * 0.5f, view->zNear, view->zFar, view->scale); view->projection = *projection; - gSPMatrix(OVERLAY_DISP++, projection, G_MTX_LOAD | G_MTX_PROJECTION); + gSPMatrix(OVERLAY_DISP++, projection, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_PROJECTION); CLOSE_DISPS(gfxCtx); return 1; } -s32 func_8013FD74(View* view) { +/** + * Apply scissor, viewport, view and projection (perspective) to OVERLAY_DISP. + */ +s32 View_ApplyPerspectiveToOverlay(View* view) { f32 aspect; s32 width; s32 height; @@ -428,7 +460,7 @@ s32 func_8013FD74(View* view) { OPEN_DISPS(gfxCtx); - vp = GRAPH_ALLOC(gfxCtx, sizeof(*vp)); + vp = GRAPH_ALLOC(gfxCtx, sizeof(Vp)); View_ViewportToVp(vp, &view->viewport); view->vp = *vp; @@ -438,28 +470,30 @@ s32 func_8013FD74(View* view) { Gfx* overlay; overlay = OVERLAY_DISP; - View_WriteScissor(&overlay, view->viewport.leftX, view->viewport.topY, view->viewport.rightX, - view->viewport.bottomY); + View_SetScissor(&overlay, view->viewport.leftX, view->viewport.topY, view->viewport.rightX, + view->viewport.bottomY); OVERLAY_DISP = overlay; } gSPViewport(OVERLAY_DISP++, vp); - projection = GRAPH_ALLOC(gfxCtx, sizeof(*projection)); + projection = GRAPH_ALLOC(gfxCtx, sizeof(Mtx)); view->projectionPtr = projection; width = view->viewport.rightX - view->viewport.leftX; height = view->viewport.bottomY - view->viewport.topY; aspect = (f32)width / (f32)height; - guPerspective(projection, &view->normal, view->fovy, aspect, view->zNear, view->zFar, view->scale); + guPerspective(projection, &view->perspNorm, view->fovy, aspect, view->zNear, view->zFar, view->scale); + view->projection = *projection; - gSPPerspNormalize(OVERLAY_DISP++, view->normal); + gSPPerspNormalize(OVERLAY_DISP++, view->perspNorm); gSPMatrix(OVERLAY_DISP++, projection, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_PROJECTION); - viewing = GRAPH_ALLOC(gfxCtx, sizeof(*viewing)); + viewing = GRAPH_ALLOC(gfxCtx, sizeof(Mtx)); view->viewingPtr = viewing; + // This check avoids a divide-by-zero in guLookAt if eye == at if (view->eye.x == view->at.x && view->eye.y == view->at.y && view->eye.z == view->at.z) { view->eye.z += 2.0f; } @@ -476,7 +510,10 @@ s32 func_8013FD74(View* view) { return 1; } -s32 func_80140024(View* view) { +/** + * Just updates view's view matrix from its eye/at/up vectors. + */ +s32 View_UpdateViewingMatrix(View* view) { guLookAt(view->viewingPtr, view->eye.x, view->eye.y, view->eye.z, view->at.x, view->at.y, view->at.z, view->up.x, view->up.y, view->up.z); @@ -486,22 +523,22 @@ s32 func_80140024(View* view) { return 1; } -s32 func_801400CC(View* view, Gfx** gfxp) { +s32 View_ApplyTo(View* view, Gfx** gfxp) { Gfx* gfx = *gfxp; GraphicsContext* gfxCtx = view->gfxCtx; Viewport* viewport = &view->viewport; Mtx* projection; Vp* vp; - vp = GRAPH_ALLOC(gfxCtx, sizeof(*vp)); + vp = GRAPH_ALLOC(gfxCtx, sizeof(Vp)); View_ViewportToVp(vp, viewport); view->vp = *vp; - View_SyncAndWriteScissor(view, &gfx); + View_ClearScissor(view, &gfx); gSPViewport(gfx++, vp); - projection = GRAPH_ALLOC(gfxCtx, sizeof(*projection)); + projection = GRAPH_ALLOC(gfxCtx, sizeof(Mtx)); view->projectionPtr = projection; guOrtho(projection, gScreenWidth * -0.5f, gScreenWidth * 0.5f, gScreenHeight * -0.5f, gScreenHeight * 0.5f, @@ -509,7 +546,7 @@ s32 func_801400CC(View* view, Gfx** gfxp) { view->projection = *projection; - gSPMatrix(gfx++, projection, G_MTX_LOAD | G_MTX_PROJECTION); + gSPMatrix(gfx++, projection, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_PROJECTION); *gfxp = gfx; return 1; |
