diff --git a/c3d.c b/c3d.c index ab98fba..f13f10f 100644 --- a/c3d.c +++ b/c3d.c @@ -168,7 +168,10 @@ void FillTopFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) { double curx1 = t->c.x; double curx2 = t->c.x; - for (int scanline = t->c.y; scanline >= t->a.y; scanline--) { + int scanbottom = t->c.y; + int scantop = Max(t->a.y, 0); + + for (int scanline = scanbottom; scanline >= scantop; scanline--) { if (0 <= scanline && scanline < RENDERHEIGHT) { DrawScanline(zee, tp, curx1, curx2, scanline); @@ -206,7 +209,10 @@ void FillBottomFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) { double curx1 = t->a.x; double curx2 = t->a.x; - for (int scanline = t->a.y; scanline < t->c.y; + int scantop = t->a.y; // start must always be the true beging of the triangle, otherwise everything will be offset + int scanbottom = Min(t->c.y, RENDERHEIGHT); // we can stop rendering as soon as it goes off screen + + for (int scanline = scantop; scanline < scanbottom; scanline++) { // TODO: Possibly more optimization possible here, use linear correspondance // for y, not just x to get depth if (0 <= scanline && scanline < RENDERHEIGHT) {