From cee28c6166063f105344d0d504e61fa435e6ce29 Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 25 Oct 2024 22:52:19 +0200 Subject: [PATCH] refactor a bunch of crap and fix skyportals --- src/r_bsp.c | 1 + src/r_bsp.h | 1 + src/r_main.c | 66 ++++++++++++--- src/r_plane.c | 56 +++++-------- src/r_plane.h | 5 +- src/r_portal.c | 147 ++++++++++++++++++++++----------- src/r_portal.h | 15 +++- src/r_segs.c | 4 + src/r_state.h | 4 - src/r_things.c | 214 +++++++++++++++++++++++++++++++++++-------------- src/r_things.h | 18 ++++- 11 files changed, 364 insertions(+), 167 deletions(-) diff --git a/src/r_bsp.c b/src/r_bsp.c index e6a3044c0..b22fc9a61 100644 --- a/src/r_bsp.c +++ b/src/r_bsp.c @@ -33,6 +33,7 @@ sector_t *backsector; // very ugly realloc() of drawsegs at run-time, I upped it to 512 // instead of 256.. and someone managed to send me a level with // 896 drawsegs! So too bad here's a limit removal a-la-Boom +drawseg_t *curdrawsegs = NULL; /**< This is used to handle multiple lists for masked drawsegs. */ drawseg_t *drawsegs = NULL; drawseg_t *ds_p = NULL; diff --git a/src/r_bsp.h b/src/r_bsp.h index f93541d7b..49862b8be 100644 --- a/src/r_bsp.h +++ b/src/r_bsp.h @@ -32,6 +32,7 @@ extern portal_t *g_portal; // is curline a portal seg? extern INT32 checkcoord[12][4]; +extern drawseg_t *curdrawsegs; extern drawseg_t *drawsegs; extern drawseg_t *ds_p; extern INT32 doorclosed; diff --git a/src/r_main.c b/src/r_main.c index 37c3a842b..7367459c5 100644 --- a/src/r_main.c +++ b/src/r_main.c @@ -1479,8 +1479,8 @@ static void R_PortalFrame(portal_t *portal) viewz = portal->viewz; viewangle = portal->viewangle; - // newview->sin = FINESINE(newview->angle>>ANGLETOFINESHIFT); - // newview->cos = FINECOSINE(newview->angle>>ANGLETOFINESHIFT); + viewsin = FINESINE(viewangle>>ANGLETOFINESHIFT); + viewcos = FINECOSINE(viewangle>>ANGLETOFINESHIFT); portalclipstart = portal->start; portalclipend = portal->end; @@ -1488,15 +1488,33 @@ static void R_PortalFrame(portal_t *portal) if (portal->clipline != -1) { portalclipline = &lines[portal->clipline]; - viewsector = portalcullsector = portalclipline->frontsector; + portalcullsector = portalclipline->frontsector; + viewsector = portalclipline->frontsector; } else { portalclipline = NULL; - viewsector = portalcullsector = R_PointInSubsector(viewx, viewy)->sector; + portalcullsector = NULL; + viewsector = R_PointInSubsector(viewx, viewy)->sector; } } +static void Mask_Pre (maskcount_t* m) +{ + m->drawsegs[0] = ds_p - drawsegs; + m->vissprites[0] = visspritecount; + m->viewx = viewx; + m->viewy = viewy; + m->viewz = viewz; + m->viewsector = viewsector; +} + +static void Mask_Post (maskcount_t* m) +{ + m->drawsegs[1] = ds_p - drawsegs; + m->vissprites[1] = visspritecount; +} + // ================ // R_RenderView // ================ @@ -1509,6 +1527,9 @@ static void R_PortalFrame(portal_t *portal) void R_RenderPlayerView(player_t *player) { + INT32 nummasks = 1; + maskcount_t* masks = malloc(sizeof(maskcount_t)); + // if this is display player 1 if (cv_homremoval.value && player == &players[displayplayers[0]]) { @@ -1561,18 +1582,26 @@ void R_RenderPlayerView(player_t *player) Portal_InitList(); // The head node is the last node output. - + Mask_Pre(&masks[nummasks - 1]); + curdrawsegs = ds_p; ps_numbspcalls.value.i = ps_numpolyobjects.value.i = ps_numdrawnodes.value.i = 0; PS_START_TIMING(ps_bsptime); R_RenderBSPNode((INT32)numnodes - 1); PS_STOP_TIMING(ps_bsptime); + Mask_Post(&masks[nummasks - 1]); + R_AddPrecipitationSprites(); + PS_START_TIMING(ps_sw_spritecliptime); - R_ClipSprites(); + R_ClipSprites(drawsegs, NULL); PS_STOP_TIMING(ps_sw_spritecliptime); ps_numsprites.value.i = numvisiblesprites; + // Add skybox portals caused by sky visplanes. + if (skyboxmo[0] && cv_skybox.value) + Portal_AddSkyboxPortals(); + PS_START_TIMING(ps_sw_portaltime); // Portal rendering. Hijacks the BSP traversal. if (portal_base) @@ -1583,6 +1612,8 @@ void R_RenderPlayerView(player_t *player) { portalrender = portal->pass; // Recursiveness depth. + R_ClearFFloorClips(); + // Apply the viewpoint stored for the portal. R_PortalFrame(portal); @@ -1595,17 +1626,26 @@ void R_RenderPlayerView(player_t *player) // that were previously stored. Portal_ClipApply(portal); + validcount++; + + masks = realloc(masks, (++nummasks)*sizeof(maskcount_t)); + + Mask_Pre(&masks[nummasks - 1]); + curdrawsegs = ds_p; + + portalskipprecipmobjs = portal->isskybox; + // Render the BSP from the new viewpoint, and clip // any sprites with the new clipsegs and window. R_RenderBSPNode((INT32)numnodes - 1); - R_ClipSprites(); + Mask_Post(&masks[nummasks - 1]); + + portalskipprecipmobjs = false; + + R_ClipSprites(ds_p - (masks[nummasks - 1].drawsegs[1] - masks[nummasks - 1].drawsegs[0]), portal); Portal_Remove(portal); - - validcount++; } - - portalcullsector = NULL; // Just in case... } PS_STOP_TIMING(ps_sw_portaltime); @@ -1618,8 +1658,10 @@ void R_RenderPlayerView(player_t *player) // draw mid texture and sprite // And now 3D floors/sides! PS_START_TIMING(ps_sw_maskedtime); - R_DrawMasked(); + R_DrawMasked(masks, nummasks); PS_STOP_TIMING(ps_sw_maskedtime); + + free(masks); } // ========================================================================= diff --git a/src/r_plane.c b/src/r_plane.c index ab21ef208..24cafdd82 100644 --- a/src/r_plane.c +++ b/src/r_plane.c @@ -41,13 +41,7 @@ // good night sweet prince //#define SHITPLANESPARENCY -//SoM: 3/23/2000: Use Boom visplane hashing. -#define VISPLANEHASHBITS 9 -#define VISPLANEHASHMASK ((1<high = hi; plane->low = low; } - -/** Creates portals for the currently existing sky visplanes. - * The visplanes are also removed and cleared from the list. - */ -void Portal_AddSkyboxPortals (void) -{ - visplane_t *pl; - INT32 i; - UINT16 count = 0; - - for (i = 0; i < MAXVISPLANES; i++, pl++) - { - for (pl = visplanes[i]; pl; pl = pl->next) - { - if (pl->picnum == skyflatnum) - { - Portal_AddSkybox(pl); - - pl->minx = 0; - pl->maxx = -1; - - count++; - } - } - } - - CONS_Debug(DBG_RENDER, "Skybox portals: %d\n", count); -} diff --git a/src/r_plane.h b/src/r_plane.h index fdab8e8ac..e6d31c610 100644 --- a/src/r_plane.h +++ b/src/r_plane.h @@ -18,6 +18,7 @@ #include "r_data.h" #include "p_polyobj.h" +//SoM: 3/23/2000: Use Boom visplane hashing. #define VISPLANEHASHBITS 9 #define VISPLANEHASHMASK ((1<start; - INT32 end = portal->end; - INT16 *ceil = portal->ceilingclip; - INT16 *floor = portal->floorclip; - fixed_t *scale = portal->frontscale; - - INT32 i; - for (i = 0; i < end-start; i++) - { - *ceil = ceilingclip[start+i]; - ceil++; - *floor = floorclip[start+i]; - floor++; - *scale = frontscale[start+i]; - scale++; - } -} - /** Apply the clipping window from a portal. */ void Portal_ClipApply (const portal_t* portal) @@ -95,12 +73,12 @@ void Portal_ClipApply (const portal_t* portal) } } -portal_t* Portal_Add (const INT16 x1, const INT16 x2) +static portal_t* Portal_Add (const INT16 x1, const INT16 x2) { portal_t *portal = Z_Malloc(sizeof(portal_t), PU_LEVEL, NULL); - INT16 *ceilingclipsave = Z_Malloc(sizeof(INT16)*(x2-x1), PU_LEVEL, NULL); - INT16 *floorclipsave = Z_Malloc(sizeof(INT16)*(x2-x1), PU_LEVEL, NULL); - fixed_t *frontscalesave = Z_Malloc(sizeof(fixed_t)*(x2-x1), PU_LEVEL, NULL); + INT16 *ceilingclipsave = Z_Malloc(sizeof(INT16)*(x2-x1 + 1), PU_LEVEL, NULL); + INT16 *floorclipsave = Z_Malloc(sizeof(INT16)*(x2-x1 + 1), PU_LEVEL, NULL); + fixed_t *frontscalesave = Z_Malloc(sizeof(fixed_t)*(x2-x1 + 1), PU_LEVEL, NULL); // Linked list. if (!portal_base) @@ -125,11 +103,14 @@ portal_t* Portal_Add (const INT16 x1, const INT16 x2) // Increase recursion level. portal->pass = portalrender+1; + portal->isskybox = false; + return portal; } void Portal_Remove (portal_t* portal) { + portalcullsector = NULL; portal_base = portal->next; Z_Free(portal->ceilingclip); Z_Free(portal->floorclip); @@ -181,8 +162,6 @@ void Portal_Add2Lines (const INT32 line1, const INT32 line2, const INT32 x1, con portal->clipline = line2; - Portal_ClipRange(portal); - g_portal = portal; // this tells R_StoreWallRange that curline is a portal seg } @@ -199,26 +178,68 @@ static void Portal_ClipVisplane (const visplane_t* plane, portal_t* portal) for (i = 0; i < end - start; i++) { - portal->ceilingclip[i] = plane->top[i + start]; + // Invalid column. + if (plane->top[i + start] == 65535) + { + portal->ceilingclip[i] = -1; + portal->floorclip[i] = -1; + continue; + } + portal->ceilingclip[i] = plane->top[i + start] - 1; portal->floorclip[i] = plane->bottom[i + start] + 1; + portal->frontscale[i] = INT32_MAX; } } extern INT32 viewwidth; +static boolean TrimVisplaneBounds (const visplane_t* plane, INT16* start, INT16* end) +{ + *start = plane->minx; + *end = plane->maxx + 1; + + // Visplanes have 1-px pads on their sides (extra columns). + // Trim them, else it may render out of bounds. + if (*end > viewwidth) + *end = viewwidth; + + if (!(*start < *end)) + return true; + + + /** Trims a visplane's horizontal gap to match its render area. + * + * Visplanes' minx/maxx may sometimes exceed the area they're + * covering. This merely adjusts the boundaries to the next + * valid area. + */ + + while (plane->bottom[*start] == 0 && plane->top[*start] == 65535 && *start < *end) + { + (*start)++; + } + + + while (plane->bottom[*end - 1] == 0 && plane->top[*start] == 65535 && *end > *start) + { + (*end)--; + } + + return false; +} + /** Creates a skybox portal out of a visplane. * * Applies the necessary offsets and rotation to give * a depth illusion to the skybox. */ -void Portal_AddSkybox (const visplane_t* plane) +static void Portal_AddSkybox (const visplane_t* plane) { - INT16 start = plane->minx; - INT16 end = plane->maxx + 1; + INT16 start, end; mapheader_t *mh; portal_t* portal; - if (!(start < end)) + if (TrimVisplaneBounds(plane, &start, &end)) return; portal = Portal_Add(start, end); @@ -227,7 +248,9 @@ void Portal_AddSkybox (const visplane_t* plane) portal->viewx = skyboxmo[0]->x; portal->viewy = skyboxmo[0]->y; - portal->viewz = skyboxmo[0]->z; + portal->viewz = 0; + if (skyboxmo[0]->spawnpoint) + portal->viewz = ((fixed_t)skyboxmo[0]->spawnpoint->angle)<viewangle = viewangle + skyboxmo[0]->angle; mh = mapheaderinfo[gamemap-1]; @@ -236,6 +259,7 @@ void Portal_AddSkybox (const visplane_t* plane) if (skyboxmo[1]) { fixed_t x = 0, y = 0; + angle_t ang = skyboxmo[0]->angle>>ANGLETOFINESHIFT; if (mh->skybox_scalex > 0) x = (viewx - skyboxmo[1]->x) / mh->skybox_scalex; @@ -247,8 +271,9 @@ void Portal_AddSkybox (const visplane_t* plane) else if (mh->skybox_scaley < 0) y = (viewy - skyboxmo[1]->y) * -mh->skybox_scaley; - portal->viewx += x; - portal->viewy += y; + // Apply transform to account for the skybox viewport angle. + portal->viewx += FixedMul(x,FINECOSINE(ang)) - FixedMul(y, FINESINE(ang)); + portal->viewy += FixedMul(x, FINESINE(ang)) + FixedMul(y,FINECOSINE(ang)); } if (mh->skybox_scalez > 0) @@ -257,4 +282,36 @@ void Portal_AddSkybox (const visplane_t* plane) portal->viewz += viewz * -mh->skybox_scalez; portal->clipline = -1; + portal->isskybox = true; +} + +/** Creates portals for the currently existing sky visplanes. + * The visplanes are also removed and cleared from the list. + */ +void Portal_AddSkyboxPortals (void) +{ + visplane_t *pl; + INT32 i; + UINT16 count = 0; + + for (i = 0; i < MAXVISPLANES; i++, pl++) + { + for (pl = visplanes[i]; pl; pl = pl->next) + { + //if (pl->minx >= pl->maxx) + //continue; + + if (pl->picnum == skyflatnum) + { + Portal_AddSkybox(pl); + + pl->minx = 0; + pl->maxx = -1; + + count++; + } + } + } + + //CONS_Debug(DBG_RENDER, "Skybox portals: %d\n", count); } diff --git a/src/r_portal.h b/src/r_portal.h index e870681ec..e6271aa9f 100644 --- a/src/r_portal.h +++ b/src/r_portal.h @@ -11,10 +11,12 @@ /// \file r_portal.h /// \brief Software renderer portal struct, functions, linked list extern. +#ifndef __R_PORTAL__ +#define __R_PORTAL__ + #include "r_data.h" #include "r_plane.h" // visplanes - /* * Portal structure for the software renderer. */ @@ -29,6 +31,7 @@ typedef struct portal_s angle_t viewangle; UINT8 pass; /**< Keeps track of the portal's recursion depth. */ + boolean isskybox; INT32 clipline; /**< Optional clipline for line-based portals. */ // Clipping information. @@ -42,11 +45,19 @@ typedef struct portal_s extern portal_t* portal_base; extern portal_t* portal_cap; extern UINT8 portalrender; +extern boolean portalskipprecipmobjs; + +extern line_t *portalclipline; +extern sector_t *portalcullsector; +extern INT32 portalclipstart, portalclipend; void Portal_InitList (void); void Portal_Remove (portal_t* portal); void Portal_Add2Lines (const INT32 line1, const INT32 line2, const INT32 x1, const INT32 x2); -void Portal_AddSkybox (const visplane_t* plane); void Portal_ClipRange (portal_t* portal); void Portal_ClipApply (const portal_t* portal); + +void Portal_AddSkyboxPortals (void); + +#endif diff --git a/src/r_segs.c b/src/r_segs.c index 452351aa3..50010fec8 100644 --- a/src/r_segs.c +++ b/src/r_segs.c @@ -15,6 +15,7 @@ #include "r_local.h" #include "r_sky.h" +#include "r_portal.h" #include "r_splats.h" #include "w_wad.h" @@ -1642,6 +1643,7 @@ void R_StoreWallRange(INT32 start, INT32 stop) if (ds_p == drawsegs+maxdrawsegs) { + size_t curpos = curdrawsegs - drawsegs; size_t pos = ds_p - drawsegs; size_t newmax = maxdrawsegs ? maxdrawsegs*2 : 128; if (firstseg) @@ -1649,6 +1651,7 @@ void R_StoreWallRange(INT32 start, INT32 stop) drawsegs = Z_Realloc(drawsegs, newmax*sizeof (*drawsegs), PU_STATIC, NULL); ds_p = drawsegs + pos; maxdrawsegs = newmax; + curdrawsegs = drawsegs + curpos; if (firstseg) firstseg = drawsegs + (size_t)firstseg; } @@ -1701,6 +1704,7 @@ void R_StoreWallRange(INT32 start, INT32 stop) // borrowed fix from *cough* zdoom *cough* // [RH] We also need to adjust the openings pointers that // were already stored in drawsegs. + //for (ds = curdrawsegs; ds < ds_p; ds++) for (ds = drawsegs; ds < ds_p; ds++) { #define ADJUST(p) if (ds->p + ds->x1 >= oldopenings && ds->p + ds->x1 <= oldlast) ds->p = ds->p - oldopenings + openings; diff --git a/src/r_state.h b/src/r_state.h index 2df2ffbc6..63cfb5ff9 100644 --- a/src/r_state.h +++ b/src/r_state.h @@ -92,10 +92,6 @@ extern angle_t viewangle, aimingangle, viewroll; extern UINT8 viewssnum; // splitscreen view number extern sector_t *viewsector; extern player_t *viewplayer; -extern UINT8 portalrender; -extern sector_t *portalcullsector; -extern line_t *portalclipline; -extern INT32 portalclipstart, portalclipend; extern consvar_t cv_allowmlook; extern consvar_t cv_maxportals; diff --git a/src/r_things.c b/src/r_things.c index 17d198801..8112db783 100644 --- a/src/r_things.c +++ b/src/r_things.c @@ -25,6 +25,7 @@ #include "r_things.h" #include "r_patch.h" #include "r_plane.h" +#include "r_portal.h" #include "p_tick.h" #include "p_local.h" #include "p_slopes.h" @@ -1970,6 +1971,12 @@ void R_AddPrecipitationSprites(void) return; } + // do not render in skybox + if (portalskipprecipmobjs) + { + return; + } + R_GetRenderBlockMapDimensions(drawdist, &xl, &xh, &yl, &yh); for (bx = xl; bx <= xh; bx++) @@ -1993,46 +2000,47 @@ void R_AddPrecipitationSprites(void) // // R_SortVisSprites // -static vissprite_t vsprsortedhead; -void R_SortVisSprites(void) +static void R_SortVisSprites(vissprite_t* vsprsortedhead, UINT32 start, UINT32 end) { - UINT32 i; + UINT32 i, count = 0; vissprite_t *ds, *dsprev, *dsnext, *dsfirst; vissprite_t *best = NULL; vissprite_t unsorted; fixed_t bestscale; INT32 bestdispoffset; - if (!visspritecount) - return; + dsfirst = &unsorted; + dsprev = dsfirst; + dsnext = dsfirst; - unsorted.next = unsorted.prev = &unsorted; + I_Assert(start <= end); - dsfirst = R_GetVisSprite(0); - - // The first's prev and last's next will be set to - // nonsense, but are fixed in a moment - for (i = 0, dsnext = dsfirst, ds = NULL; i < visspritecount; i++) + for (i = start; i < end; ++i) { - dsprev = ds; - ds = dsnext; - if (i < visspritecount - 1) dsnext = R_GetVisSprite(i + 1); + ds = R_GetVisSprite(i); - ds->next = dsnext; - ds->prev = dsprev; + // Do not include this sprite, since it is completely obscured + if (ds->cut & SC_NOTVISIBLE) + { + continue; + } + + dsnext = ds; + + dsprev->next = dsnext; + dsnext->prev = dsprev; + dsprev = dsnext; + + count++; } - // Fix first and last. ds still points to the last one after the loop - dsfirst->prev = &unsorted; - unsorted.next = dsfirst; - if (ds) - ds->next = &unsorted; - unsorted.prev = ds; + dsnext->next = dsfirst; + dsfirst->prev = dsnext; // pull the vissprites out by scale - vsprsortedhead.next = vsprsortedhead.prev = &vsprsortedhead; - for (i = 0; i < visspritecount; i++) + vsprsortedhead->next = vsprsortedhead->prev = vsprsortedhead; + for (i = 0; i < count; i++) { bestscale = bestdispoffset = INT32_MAX; for (ds = unsorted.next; ds != &unsorted; ds = ds->next) @@ -2058,15 +2066,12 @@ void R_SortVisSprites(void) best = ds; } } - if (best) - { - best->next->prev = best->prev; - best->prev->next = best->next; - best->next = &vsprsortedhead; - best->prev = vsprsortedhead.prev; - vsprsortedhead.prev->next = best; - vsprsortedhead.prev = best; - } + best->next->prev = best->prev; + best->prev->next = best->next; + best->next = vsprsortedhead; + best->prev = vsprsortedhead->prev; + vsprsortedhead->prev->next = best; + vsprsortedhead->prev = best; } } @@ -2076,28 +2081,28 @@ void R_SortVisSprites(void) static drawnode_t *R_CreateDrawNode(drawnode_t *link); static drawnode_t nodebankhead; -static drawnode_t nodehead; -static void R_CreateDrawNodes(void) +static void R_CreateDrawNodes(maskcount_t* mask, drawnode_t* head, boolean tempskip) { drawnode_t *entry; drawseg_t *ds; INT32 i, p, best, x1, x2; fixed_t bestdelta, delta; vissprite_t *rover; + static vissprite_t vsprsortedhead; drawnode_t *r2; visplane_t *plane; INT32 sintersect; fixed_t scale = 0; // Add the 3D floors, thicksides, and masked textures... - for (ds = ds_p; ds-- > drawsegs ;) + for (ds = drawsegs + mask->drawsegs[1]; ds-- > drawsegs + mask->drawsegs[0];) { if (ds->numthicksides) { for (i = 0; i < ds->numthicksides; i++) { - entry = R_CreateDrawNode(&nodehead); + entry = R_CreateDrawNode(head); entry->thickseg = ds; entry->ffloor = ds->thicksides[i]; } @@ -2113,7 +2118,7 @@ static void R_CreateDrawNodes(void) else { // Put it in! - entry = R_CreateDrawNode(&nodehead); + entry = R_CreateDrawNode(head); entry->plane = plane; entry->seg = ds; } @@ -2121,7 +2126,7 @@ static void R_CreateDrawNodes(void) } if (ds->maskedtexturecol) { - entry = R_CreateDrawNode(&nodehead); + entry = R_CreateDrawNode(head); entry->seg = ds; } if (ds->numffloorplanes) @@ -2153,7 +2158,7 @@ static void R_CreateDrawNodes(void) } if (best != -1) { - entry = R_CreateDrawNode(&nodehead); + entry = R_CreateDrawNode(head); entry->plane = ds->ffloorplanes[best]; entry->seg = ds; ds->ffloorplanes[best] = NULL; @@ -2164,6 +2169,9 @@ static void R_CreateDrawNodes(void) } } + if (tempskip) + return; + // find all the remaining polyobject planes and add them on the end of the list // probably this is a terrible idea if we wanted them to be sorted properly // but it works getting them in for now @@ -2179,16 +2187,18 @@ static void R_CreateDrawNodes(void) PolyObjects[i].visplane = NULL; continue; } - entry = R_CreateDrawNode(&nodehead); + entry = R_CreateDrawNode(head); entry->plane = plane; // note: no seg is set, for what should be obvious reasons PolyObjects[i].visplane = NULL; } - if (visspritecount == 0) + // No vissprites in this mask? + if (mask->vissprites[1] - mask->vissprites[0] == 0) return; - R_SortVisSprites(); + R_SortVisSprites(&vsprsortedhead, mask->vissprites[0], mask->vissprites[1]); + for (rover = vsprsortedhead.prev; rover != &vsprsortedhead; rover = rover->prev) { if (rover->szt > vid.height || rover->sz < 0) @@ -2196,7 +2206,7 @@ static void R_CreateDrawNodes(void) sintersect = (rover->x1 + rover->x2) / 2; - for (r2 = nodehead.next; r2 != &nodehead; r2 = r2->next) + for (r2 = head->next; r2 != head; r2 = r2->next) { if (r2->plane) { @@ -2335,9 +2345,9 @@ static void R_CreateDrawNodes(void) } } } - if (r2 == &nodehead) + if (r2 == head) { - entry = R_CreateDrawNode(&nodehead); + entry = R_CreateDrawNode(head); entry->sprite = rover; } } @@ -2381,25 +2391,24 @@ static void R_DoneWithNode(drawnode_t *node) (node->prev = &nodebankhead)->next = node; } -static void R_ClearDrawNodes(void) +static void R_ClearDrawNodes(drawnode_t* head) { drawnode_t *rover; drawnode_t *next; - for (rover = nodehead.next; rover != &nodehead ;) + for (rover = head->next; rover != head;) { next = rover->next; R_DoneWithNode(rover); rover = next; } - nodehead.next = nodehead.prev = &nodehead; + head->next = head->prev = head; } void R_InitDrawNodes(void) { nodebankhead.next = nodebankhead.prev = &nodebankhead; - nodehead.next = nodehead.prev = &nodehead; } // @@ -2463,7 +2472,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; @@ -2502,8 +2511,36 @@ 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))) + { + // masked mid texture? + /* + if (ds->maskedtexturecol) + R_RenderMaskedSegRange (ds, r1, r2); + */ + + // seg is behind sprite + continue; + } + } r1 = ds->x1 < spr->x1 ? spr->x1 : ds->x1; r2 = ds->x2 > spr->x2 ? spr->x2 : ds->x2; @@ -2654,11 +2691,34 @@ static void R_ClipVisSprite(vissprite_t *spr, INT32 x1, INT32 x2) if (!R_CheckSpriteVisible(spr, x1, x2)) spr->cut |= SC_NOTVISIBLE; } + + if (portal) + { + INT32 start_index = max(portal->start, x1); + INT32 end_index = 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; @@ -2691,7 +2751,7 @@ void R_ClipSprites(void) } } - for (ds = ds_p; ds-- > drawsegs;) + for (ds = ds_p; ds-- > dsstart;) { if (ds->silhouette || ds->maskedtexturecol) { @@ -2745,7 +2805,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++; @@ -2790,14 +2850,12 @@ boolean R_ThingWithinDist (mobj_t *thing, fixed_t limit_dist) // // R_DrawMasked // -void R_DrawMasked(void) +static void R_DrawMaskedList (drawnode_t* head) { drawnode_t *r2; drawnode_t *next; - R_CreateDrawNodes(); - - for (r2 = nodehead.next; r2 != &nodehead; r2 = r2->next) + for (r2 = head->next; r2 != head; r2 = r2->next) { if (r2->plane) { @@ -2835,7 +2893,39 @@ void R_DrawMasked(void) r2 = next; } } - R_ClearDrawNodes(); +} + +void R_DrawMasked(maskcount_t* masks, INT32 nummasks) +{ + drawnode_t *heads; /**< Drawnode lists; as many as number of views/portals. */ + INT32 i; + + heads = calloc(nummasks, sizeof(drawnode_t)); + + for (i = 0; i < nummasks; i++) + { + heads[i].next = heads[i].prev = &heads[i]; + + viewx = masks[i].viewx; + viewy = masks[i].viewy; + viewz = masks[i].viewz; + viewsector = masks[i].viewsector; + + R_CreateDrawNodes(&masks[i], &heads[i], false); + } + + for (; nummasks > 0; nummasks--) + { + viewx = masks[nummasks - 1].viewx; + viewy = masks[nummasks - 1].viewy; + viewz = masks[nummasks - 1].viewz; + viewsector = masks[nummasks - 1].viewsector; + + R_DrawMaskedList(&heads[nummasks - 1]); + R_ClearDrawNodes(&heads[nummasks - 1]); + } + + free(heads); } // ========================================================================== diff --git a/src/r_things.h b/src/r_things.h index 898cc7ecd..5cc1352b3 100644 --- a/src/r_things.h +++ b/src/r_things.h @@ -16,6 +16,7 @@ #include "sounds.h" #include "r_plane.h" +#include "r_portal.h" #include "r_patch.h" // "Left" and "Right" character symbols for additional rotation functionality @@ -60,7 +61,6 @@ extern INT32 lengthcol; fixed_t R_GetShadowZ(mobj_t *thing, pslope_t **shadowslope); void R_DrawMaskedColumn(column_t *column); -void R_SortVisSprites(void); //faB: find sprites in wadfile, replace existing, add new ones // (only sprites from namelist are added or replaced) @@ -71,7 +71,19 @@ void R_AddSprites(sector_t *sec, INT32 lightlevel); void R_AddPrecipitationSprites(void); void R_InitSprites(void); void R_ClearSprites(void); -void R_DrawMasked(void); + +/** Used to count the amount of masked elements + * per portal to later group them in separate + * drawnode lists. + */ +typedef struct +{ + size_t drawsegs[2]; + size_t vissprites[2]; + fixed_t viewx, viewy, viewz; /**< View z stored at the time of the BSP traversal for the view/portal. Masked sorting/drawing needs it. */ + sector_t* viewsector; +} maskcount_t; +void R_DrawMasked(maskcount_t* masks, INT32 nummasks); boolean R_ThingVisible (mobj_t *thing); boolean R_ThingWithinDist (mobj_t *thing, fixed_t limit_dist); @@ -193,7 +205,7 @@ typedef struct vissprite_s extern UINT32 visspritecount, numvisiblesprites; -void R_ClipSprites(void); +void R_ClipSprites(drawseg_t* dsstart, portal_t* portal); UINT8 *R_GetSpriteTranslation(vissprite_t *vis);