From 6632978887f62d240a3121b37313160d53602b2a Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 14 May 2021 10:50:22 +0200 Subject: [PATCH] - normalize all angles to the actual viewing range in the drawer. This simplifies the debugging of this code a lot because we no longer have to deal with range overflows and can do direct comparisons everywhere. --- .../core/rendering/scene/hw_bunchdrawer.cpp | 45 ++++++++++--------- source/core/rendering/scene/hw_bunchdrawer.h | 3 +- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/source/core/rendering/scene/hw_bunchdrawer.cpp b/source/core/rendering/scene/hw_bunchdrawer.cpp index b81b9c0ef44..f4369f2abba 100644 --- a/source/core/rendering/scene/hw_bunchdrawer.cpp +++ b/source/core/rendering/scene/hw_bunchdrawer.cpp @@ -57,6 +57,7 @@ void BunchDrawer::Init(HWDrawInfo *_di, Clipper* c, vec2_t& view, binangle a1, b { ang1 = a1; ang2 = a2; + angrange = ang2 - ang1; di = _di; clipper = c; viewx = view.x * (1/ 16.f); @@ -108,10 +109,10 @@ bool BunchDrawer::StartBunch(int sectnum, int linenum, binangle startan, binangl bunch->sectnum = sectnum; bunch->startline = bunch->endline = linenum; - bunch->startangle = (startan.asbam() - ang1.asbam()) > ANGLE_180? ang1 :startan; - bunch->endangle = (endan.asbam() - ang2.asbam()) < ANGLE_180 ? ang2 : endan; + bunch->startangle = (startan.asbam()) > ANGLE_180? bamang(0) :startan; + bunch->endangle = (endan.asbam() - angrange.asbam()) < ANGLE_180 ? angrange : endan; bunch->portal = portal; - return bunch->endangle != ang2; + return bunch->endangle != angrange; } //========================================================================== @@ -123,8 +124,8 @@ bool BunchDrawer::StartBunch(int sectnum, int linenum, binangle startan, binangl bool BunchDrawer::AddLineToBunch(int line, binangle newan) { Bunches[LastBunch].endline++; - Bunches[LastBunch].endangle = (newan.asbam() - ang2.asbam()) < ANGLE_180 ? ang2 : newan; - return Bunches[LastBunch].endangle != ang2; + Bunches[LastBunch].endangle = (newan.asbam() - angrange.asbam()) < ANGLE_180 ? angrange : newan; + return Bunches[LastBunch].endangle != angrange; } //========================================================================== @@ -202,8 +203,8 @@ int BunchDrawer::ClipLine(int aline, bool portal) int section = cline->section; int line = cline->wall; - auto startAngleBam = wall[cline->startpoint].clipangle; - auto endAngleBam = wall[cline->endpoint].clipangle; + auto startAngleBam = ClipAngle(cline->startpoint); + auto endAngleBam = ClipAngle(cline->endpoint); // Back side, i.e. backface culling - read: endAngle <= startAngle! if (startAngleBam.asbam() - endAngleBam.asbam() < ANGLE_180) @@ -213,10 +214,10 @@ int BunchDrawer::ClipLine(int aline, bool portal) if (line >= 0 && blockwall[line]) return CL_Draw; // convert to clipper coordinates and clamp to valid range. - int startAngle = startAngleBam.asbam() - ang1.asbam(); - int endAngle = endAngleBam.asbam() - ang1.asbam(); + int startAngle = startAngleBam.asbam(); + int endAngle = endAngleBam.asbam(); if (startAngle < 0) startAngle = 0; - if (endAngle < 0) endAngle = INT_MAX; + if (endAngle < 0 || endAngle > (int)angrange.asbam()) endAngle = angrange.asbam(); // since these values are derived from previous calls of this function they cannot be out of range. int sectStartAngle = sectionstartang[section]; @@ -403,7 +404,7 @@ int BunchDrawer::BunchInFront(FBunch* b1, FBunch* b2) // Find the wall in b1 that overlaps b2->startangle for (int i = b1->startline; i <= b1->endline; i++) { - endang = wall[wall[i].point2].clipangle - b1->startangle; + endang = ClipAngle(wall[i].point2) - b1->startangle; if (endang.asbam() > anglecheck.asbam()) { // found a line @@ -420,7 +421,7 @@ int BunchDrawer::BunchInFront(FBunch* b1, FBunch* b2) // Find the wall in b2 that overlaps b1->startangle for (int i = b2->startline; i <= b2->endline; i++) { - endang = wall[wall[i].point2].clipangle - b2->startangle; + endang = ClipAngle(wall[i].point2) - b2->startangle; if (endang.asbam() > anglecheck.asbam()) { // found a line @@ -484,7 +485,7 @@ int BunchDrawer::FindClosestBunch() } } - //Printf("picked bunch starting at %d\n", Bunches[closest].startline); + Printf("picked bunch starting at %d\n", Bunches[closest].startline); return closest; } @@ -553,12 +554,12 @@ void BunchDrawer::ProcessSection(int sectionnum, bool portal) //DVector2 start = { WallStartX(thiswall), WallStartY(thiswall) }; //DVector2 end = { WallStartX(thiswall->point2), WallStartY(thiswall->point2) }; #endif - binangle walang1 = wall[thisline->startpoint].clipangle; - binangle walang2 = wall[thisline->endpoint].clipangle; + binangle walang1 = ClipAngle(thisline->startpoint); + binangle walang2 = ClipAngle(thisline->endpoint); // outside the visible area or seen from the backside. - if ((walang1.asbam() - ang1.asbam() > ANGLE_180 && walang2.asbam() - ang1.asbam() > ANGLE_180) || - (walang1.asbam() - ang2.asbam() < ANGLE_180 && walang2.asbam() - ang2.asbam() < ANGLE_180) || + if ((walang1.asbam() > ANGLE_180 && walang2.asbam() > ANGLE_180) || + (walang1.asbam() - angrange.asbam() < ANGLE_180 && walang2.asbam() - angrange.asbam() < ANGLE_180) || (walang1.asbam() - walang2.asbam() < ANGLE_180)) { inbunch = false; @@ -566,12 +567,12 @@ void BunchDrawer::ProcessSection(int sectionnum, bool portal) else if (!inbunch) { startangle = walang1; - //Printf("Starting bunch:\n\tWall %d\n", sect->wallptr + i); + Printf("Starting bunch:\n\tWall %d\n", section->lines[i]); inbunch = StartBunch(sectnum, section->lines[i], walang1, walang2, portal); } else { - //Printf("\tWall %d\n", sect->wallptr + i); + Printf("\tWall %d\n", section->lines[i]); inbunch = AddLineToBunch(section->lines[i], walang2); } if (thisline->endpoint != section->lines[i] + 1) inbunch = false; @@ -586,7 +587,7 @@ void BunchDrawer::ProcessSection(int sectionnum, bool portal) void BunchDrawer::RenderScene(const int* viewsectors, unsigned sectcount, bool portal) { - //Printf("----------------------------------------- \nstart at sector %d\n", viewsectors[0]); + Printf("----------------------------------------- \nstart at sector %d\n", viewsectors[0]); auto process = [&]() { clipper->Clear(ang1); @@ -596,7 +597,7 @@ void BunchDrawer::RenderScene(const int* viewsectors, unsigned sectcount, bool p for (auto j : sectionspersector[viewsectors[i]]) { sectionstartang[j] = 0; - sectionendang[j] = int(ang2.asbam() - ang1.asbam()); + sectionendang[j] = int(angrange.asbam()); } } for (unsigned i = 0; i < sectcount; i++) @@ -626,10 +627,12 @@ void BunchDrawer::RenderScene(const int* viewsectors, unsigned sectcount, bool p auto rotang = di->Viewpoint.RotAngle; ang1 = bamang(rotang - ANGLE_90); ang2 = bamang(rotang + ANGLE_90 - 1); + angrange = ang2 - ang1; process(); gotsection2.Zero(); ang1 = bamang(rotang + ANGLE_90); ang2 = bamang(rotang - ANGLE_90 - 1); + angrange = ang2 - ang1; process(); } Bsp.Unclock(); diff --git a/source/core/rendering/scene/hw_bunchdrawer.h b/source/core/rendering/scene/hw_bunchdrawer.h index 685ca25bc51..a8b12fa6c8a 100644 --- a/source/core/rendering/scene/hw_bunchdrawer.h +++ b/source/core/rendering/scene/hw_bunchdrawer.h @@ -31,7 +31,7 @@ class BunchDrawer FixedBitArray gotsection2; FixedBitArray gotwall; FixedBitArray blockwall; - binangle ang1, ang2; + binangle ang1, ang2, angrange; int sectionstartang[MAXSECTORS*5/4], sectionendang[MAXSECTORS*5/4]; @@ -44,6 +44,7 @@ class BunchDrawer CL_Pass = 2, }; + binangle ClipAngle(int wal) { return wall[wal].clipangle - ang1; } void StartScene(); bool StartBunch(int sectnum, int linenum, binangle startan, binangle endan, bool portal); bool AddLineToBunch(int line, binangle newan);