- 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.
This commit is contained in:
Christoph Oelckers 2021-05-14 10:50:22 +02:00
parent 5ae1c334fa
commit 3d6ae40bc0
2 changed files with 26 additions and 22 deletions

View file

@ -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();

View file

@ -31,7 +31,7 @@ class BunchDrawer
FixedBitArray<MAXSECTORS*5/4> gotsection2;
FixedBitArray<MAXWALLS> gotwall;
FixedBitArray<MAXWALLS> blockwall;
binangle ang1, ang2;
binangle ang1, ang2, angrange;
int sectionstartang[MAXSECTORS*5/4], sectionendang[MAXSECTORS*5/4];
@ -44,6 +44,7 @@ private:
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);