From f37c4371f3c224d6fd79977cd1190f406ad4cd42 Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 8 Jun 2025 19:15:15 +0200 Subject: [PATCH] readd portalcullsector + sprite clipping improvements with portals --- src/hardware/hw_main.c | 11 ++++++ src/hardware/hw_portal.c | 6 +++- src/r_bsp.c | 10 ++++++ src/r_main.cpp | 30 +++++----------- src/r_portal.c | 6 ++-- src/r_portal.h | 1 + src/r_things.cpp | 78 ++++++++++++++++++++++++++-------------- src/r_things.h | 3 +- 8 files changed, 93 insertions(+), 52 deletions(-) diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index e26406b4c..d35c9d272 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -3233,6 +3233,17 @@ static void HWR_RenderBSPNode(INT32 bspnum) bspnum = bsp->children[side^1]; } + // PORTAL CULLING + if (portalcullsector) + { + // skip all subsectors encountered before the portal + // destination's front sector + if (portalcullsector != subsectors[bspnum & ~NF_SUBSECTOR].sector) + return; + else + portalcullsector = NULL; + } + // e6y: support for extended nodes HWR_Subsector(bspnum == -1 ? 0 : bspnum & ~NF_SUBSECTOR); } diff --git a/src/hardware/hw_portal.c b/src/hardware/hw_portal.c index 53d6f57e3..fe643df13 100644 --- a/src/hardware/hw_portal.c +++ b/src/hardware/hw_portal.c @@ -62,8 +62,11 @@ static gl_portal_t* HWR_Portal_Add (seg_t *seg) void HWR_FreePortalList(gl_portallist_t freelist) { + portalcullsector = NULL; + // free memory from portal list allocated by calls to Add2Lines gl_portal_t *gl_portal_temp = freelist.base; + while (gl_portal_temp) { gl_portal_t *nextportal = gl_portal_temp->next; @@ -123,11 +126,12 @@ void HWR_PortalFrame(gl_portal_t* portal) if (portal->clipline != -1) { portalclipline = &lines[portal->clipline]; - viewsector = portalclipline->frontsector; + portalcullsector = viewsector = portalclipline->frontsector; } else { portalclipline = NULL; + portalcullsector = NULL; viewsector = R_PointInSubsector(viewx, viewy)->sector; } } diff --git a/src/r_bsp.c b/src/r_bsp.c index 0dfba63e1..7fb61b882 100644 --- a/src/r_bsp.c +++ b/src/r_bsp.c @@ -364,8 +364,10 @@ static void R_AddLine(seg_t *line) { // Find the other side! INT32 line2 = P_FindSpecialLineFromTag(40, line->linedef->tag, -1); + if (line->linedef == &lines[line2]) line2 = P_FindSpecialLineFromTag(40, line->linedef->tag, line2); + if (line2 >= 0) // found it! { Portal_Add2Lines(line->linedef-lines, line2, x1, x2); // Remember the lines for later rendering @@ -1202,5 +1204,13 @@ void R_RenderBSPNode(INT32 bspnum) bspnum = bsp->children[side^1]; } + // PORTAL CULLING + if (portalcullsector) + { + if (subsectors[bspnum & ~NF_SUBSECTOR].sector != portalcullsector) + return; + portalcullsector = NULL; + } + R_Subsector(bspnum == -1 ? 0 : bspnum & ~NF_SUBSECTOR); } diff --git a/src/r_main.cpp b/src/r_main.cpp index 322dc43a9..3f77e0c08 100644 --- a/src/r_main.cpp +++ b/src/r_main.cpp @@ -1291,8 +1291,8 @@ static void R_PortalFrame(portal_t *portal) viewz = portal->viewz; viewangle = portal->viewangle; - // viewsin = FINESINE(viewangle>>ANGLETOFINESHIFT); - // viewcos = FINECOSINE(viewangle>>ANGLETOFINESHIFT); + //viewsin = FINESINE(viewangle>>ANGLETOFINESHIFT); + //viewcos = FINECOSINE(viewangle>>ANGLETOFINESHIFT); portalclipstart = portal->start; portalclipend = portal->end; @@ -1300,11 +1300,12 @@ static void R_PortalFrame(portal_t *portal) if (portal->clipline != -1) { portalclipline = &lines[portal->clipline]; - viewsector = portalclipline->frontsector; + viewsector = portalcullsector = portalclipline->frontsector; } else { portalclipline = NULL; + portalcullsector = NULL; viewsector = R_PointInSubsector(viewx, viewy)->sector; } } @@ -1415,16 +1416,10 @@ void R_RenderPlayerView(player_t *player) R_ClearDrawSegs(); R_ClearPlanes(); R_ClearSprites(); -#ifdef FLOORSPLATS - R_ClearVisibleFloorSplats(); -#endif R_RenderViewpoint(&masks[nummasks - 1], false); - R_ClipSprites(); + R_ClipSprites(drawsegs, NULL); R_DrawPlanes(); -#ifdef FLOORSPLATS - R_DrawVisibleFloorSplats(); -#endif // well sometimes synchronization is off and may result in some visual glitching, oh well R_DrawMasked(masks, nummasks); } @@ -1453,9 +1448,6 @@ void R_RenderPlayerView(player_t *player) } R_ClearDrawSegs(); R_ClearSprites(); -#ifdef FLOORSPLATS - R_ClearVisibleFloorSplats(); -#endif // check for new console commands. NetUpdate(); @@ -1463,10 +1455,9 @@ void R_RenderPlayerView(player_t *player) ps_numbspcalls.value.i = ps_numpolyobjects.value.i = ps_numdrawnodes.value.i = 0; PS_START_TIMING(ps_bsptime); R_RenderViewpoint(&masks[nummasks - 1], true); - PS_STOP_TIMING(ps_bsptime); PS_START_TIMING(ps_sw_spritecliptime); - R_ClipSprites(); + R_ClipSprites(drawsegs, NULL); PS_STOP_TIMING(ps_sw_spritecliptime); ps_numsprites.value.i = numvisiblesprites; @@ -1497,14 +1488,14 @@ void R_RenderPlayerView(player_t *player) validcount++; - // Render the BSP from the new viewpoint, and clip masks = static_cast(realloc(masks, (++nummasks)*sizeof(maskcount_t))); // Render the BSP from the new viewpoint, and clip // any sprites with the new clipsegs and window. - R_RenderViewpoint(&masks[nummasks - 1], true); - R_ClipSprites(); + R_RenderViewpoint(&masks[nummasks - 1], nummasks - 1); + + R_ClipSprites(ds_p - (masks[nummasks - 1].drawsegs[1] - masks[nummasks - 1].drawsegs[0]), portal); Portal_Remove(portal); } @@ -1514,9 +1505,6 @@ void R_RenderPlayerView(player_t *player) PS_START_TIMING(ps_sw_planetime); R_DrawPlanes(); PS_STOP_TIMING(ps_sw_planetime); -#ifdef FLOORSPLATS - R_DrawVisibleFloorSplats(); -#endif // draw mid texture and sprite // And now 3D floors/sides! PS_START_TIMING(ps_sw_maskedtime); diff --git a/src/r_portal.c b/src/r_portal.c index e5a431c6e..3f3b6c185 100644 --- a/src/r_portal.c +++ b/src/r_portal.c @@ -1,6 +1,6 @@ // SONIC ROBO BLAST 2 KART //----------------------------------------------------------------------------- -// Copyright (C) 2024 by Kart Krew. +// Copyright (C) 2025 by Kart Krew. // Copyright (C) 2020 by Sonic Team Junior. // Copyright (C) 2000 by DooM Legacy Team. // Copyright (C) 1996 by id Software, Inc. @@ -27,6 +27,7 @@ UINT8 portalrender; /**< When rendering a portal, it establishes the depth of portal_t *portal_base, *portal_cap; line_t *portalclipline; +sector_t *portalcullsector; INT32 portalclipstart, portalclipend; portal_t *g_portal; // is curline a portal seg? @@ -37,7 +38,6 @@ void Portal_InitList (void) portal_base = portal_cap = NULL; } - /** Store the clipping window for a portal in its given range. * * The window is copied from the current window at the time @@ -64,7 +64,6 @@ static void Portal_ClipRange (portal_t* portal) } } - /** Apply the clipping window from a portal. */ void Portal_ClipApply (const portal_t* portal) @@ -134,6 +133,7 @@ static portal_t* Portal_Add (const INT16 x1, const INT16 x2) void Portal_Remove (portal_t* portal) { + portalcullsector = NULL; portal_base = portal->next; Z_Free(portal->ceilingclip); Z_Free(portal->floorclip); diff --git a/src/r_portal.h b/src/r_portal.h index 62312da54..223dd83be 100644 --- a/src/r_portal.h +++ b/src/r_portal.h @@ -49,6 +49,7 @@ extern portal_t* portal_cap; extern UINT8 portalrender; extern line_t *portalclipline; +extern sector_t *portalcullsector; extern INT32 portalclipstart, portalclipend; void Portal_InitList (void); diff --git a/src/r_things.cpp b/src/r_things.cpp index efeee6632..0bcdc675d 100644 --- a/src/r_things.cpp +++ b/src/r_things.cpp @@ -2604,7 +2604,7 @@ static boolean R_CheckSpriteVisible(vissprite_t *spr, INT32 x1, INT32 x2) // R_ClipVisSprite // Clips vissprites without drawing, so that portals can work. -Red -static void R_ClipVisSprite(vissprite_t *spr, INT32 x1, INT32 x2) +static void R_ClipVisSprite(vissprite_t *spr, INT32 x1, INT32 x2, portal_t* portal) { drawseg_t *ds; INT32 x; @@ -2643,31 +2643,34 @@ static void R_ClipVisSprite(vissprite_t *spr, INT32 x1, INT32 x2) ds = curr->user; - if (ds->portalpass > 0 && ds->portalpass <= portalrender) - continue; // is a portal + if (ds->portalpass != 66) // unused? + { + if (ds->portalpass > 0 && ds->portalpass <= portalrender) + continue; // is a portal + + if (ds->scale1 > ds->scale2) + { + lowscale = ds->scale2; + scale = ds->scale1; + } + else + { + lowscale = ds->scale1; + scale = ds->scale2; + } + + if (scale < spr->sortscale || + (lowscale < spr->sortscale && + !R_PointOnSegSide (spr->gx, spr->gy, ds->curline))) + { + // seg is behind sprite + continue; + } + } r1 = ds->x1 < spr->x1 ? spr->x1 : ds->x1; r2 = ds->x2 > spr->x2 ? spr->x2 : ds->x2; - if (ds->scale1 > ds->scale2) - { - lowscale = ds->scale2; - scale = ds->scale1; - } - else - { - lowscale = ds->scale1; - scale = ds->scale2; - } - - if (scale < spr->sortscale || - (lowscale < spr->sortscale && - !R_PointOnSegSide (spr->gx, spr->gy, ds->curline))) - { - // seg is behind sprite - continue; - } - // clip this piece of the sprite silhouette = ds->silhouette; @@ -2792,11 +2795,34 @@ static void R_ClipVisSprite(vissprite_t *spr, INT32 x1, INT32 x2) if (!R_CheckSpriteVisible(spr, x1, x2)) spr->cut = static_cast(spr->cut | SC_NOTVISIBLE); } + + if (portal) + { + INT32 start_index = std::max(portal->start, x1); + INT32 end_index = std::min(portal->start + portal->end - portal->start, x2); + for (x = x1; x < start_index; x++) + { + spr->clipbot[x] = -1; + spr->cliptop[x] = -1; + } + for (x = start_index; x <= end_index; x++) + { + if (spr->clipbot[x] > portal->floorclip[x - portal->start]) + spr->clipbot[x] = portal->floorclip[x - portal->start]; + if (spr->cliptop[x] < portal->ceilingclip[x - portal->start]) + spr->cliptop[x] = portal->ceilingclip[x - portal->start]; + } + for (x = end_index + 1; x <= x2; x++) + { + spr->clipbot[x] = -1; + spr->cliptop[x] = -1; + } + } } -void R_ClipSprites(void) +void R_ClipSprites(drawseg_t* dsstart, portal_t* portal) { - const size_t maxdrawsegs = ds_p - drawsegs; + const size_t maxdrawsegs = ds_p - dsstart; const INT32 cx = viewwidth / 2; drawseg_t* ds; INT32 i; @@ -2829,7 +2855,7 @@ void R_ClipSprites(void) } } - for (ds = ds_p; ds-- > drawsegs;) + for (ds = ds_p; ds-- > dsstart;) { if (ds->silhouette || ds->maskedtexturecol) { @@ -2883,7 +2909,7 @@ void R_ClipSprites(void) drawsegs_xrange_count = drawsegs_xranges[0].count; } - R_ClipVisSprite(spr, spr->x1, spr->x2); + R_ClipVisSprite(spr, spr->x1, spr->x2, portal); if ((spr->cut & SC_NOTVISIBLE) == 0) numvisiblesprites++; diff --git a/src/r_things.h b/src/r_things.h index 251cb182d..47d12f707 100644 --- a/src/r_things.h +++ b/src/r_things.h @@ -19,6 +19,7 @@ extern "C" { #endif #include "r_plane.h" +#include "r_portal.h" // number of sprite lumps for spritewidth,offset,topoffset lookup tables // Fab: this is a hack : should allocate the lookup tables per sprite @@ -167,7 +168,7 @@ typedef struct vissprite_s extern UINT32 visspritecount, numvisiblesprites; -void R_ClipSprites(void); +void R_ClipSprites(drawseg_t* dsstart, portal_t* portal); void R_AllocVisSpriteMemory(void);