From 2869f5ccdd697c3603a76ca8c6585dd1330b8adb Mon Sep 17 00:00:00 2001 From: InventorXtreme Date: Mon, 11 Mar 2024 23:27:00 -0400 Subject: [PATCH] actually use optomizaiton on both types of tris --- c3d.c | 57 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/c3d.c b/c3d.c index f5c58d1..d95ea90 100644 --- a/c3d.c +++ b/c3d.c @@ -30,6 +30,23 @@ float proj; // HALFWIDTH / tan(half_fov) Inited later bool printdebug; +float Sign(Vector2 *v1, Vector2 *v2, Vector2 *v3) { + return (v1->x - v3->x) * (v2->y - v3->y) - (v2->x - v3->x) * (v1->y - v3->y); +} + +static inline bool IsInTri(Tri2D tri, Vector2 p) { + float d1, d2, d3; + bool has_neg, has_pos; + + d1 = Sign(&p, &tri.a, &tri.b); + d2 = Sign(&p, &tri.b, &tri.c); + d3 = Sign(&p, &tri.c, &tri.a); + + has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0); + has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0); + + return !(has_neg && has_pos); +} static inline Vector3 Tri2DBaryAtPoint(Tri2D *t, Vector2 p) { Vector3 retvec; Vector2 v0 = Vector2Subtract(t->b, t->a); // cachable @@ -123,14 +140,27 @@ void FillTopFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) { for (int scanline = t->c.y; scanline >= t->a.y; scanline--) { if (0 <= scanline && scanline < RENDERHEIGHT) { + + Vector3 f1baryatpoint = Tri2DBaryAtPoint(tp, (Vector2){Max(curx1, 0), scanline}); + float f1depth = DepthAtBary(tp, f1baryatpoint); + + Vector3 f1baryatpoint2 = Tri2DBaryAtPoint(tp, (Vector2){Max(curx1, 0) + 1, scanline}); + float f1depth2 = DepthAtBary(tp, f1baryatpoint2); + float dslope = f1depth2 - f1depth; + for (int i = Max(curx1, 0); i < Min(curx2, RENDERWIDTH); i++) { /* zee[IndexOfZBuff(i, scanline)].triangle = tp; */ - Vector3 baryatpoint = Tri2DBaryAtPoint(tp, (Vector2){i, scanline}); - float depth = DepthAtBary(tp, baryatpoint); - if (depth > zee[IndexOfZBuff(i, scanline)].depth) { + /* Vector3 baryatpoint = Tri2DBaryAtPoint(tp, (Vector2){i, scanline}); */ + /* float depth = DepthAtBary(tp, baryatpoint); */ + + + + float aproxdepth = f1depth + (dslope * ((float)i - (float)(Max(curx1, 0)))); + + if (aproxdepth > zee[IndexOfZBuff(i, scanline)].depth) { zee[IndexOfZBuff(i, scanline)].triangle = tp; - zee[IndexOfZBuff(i, scanline)].depth = depth; + zee[IndexOfZBuff(i, scanline)].depth = aproxdepth; // printf("here\n"); } } @@ -182,6 +212,8 @@ void FillBottomFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) { /* Vector3 baryatpoint = Tri2DBaryAtPoint(tp, (Vector2){i, scanline}); */ /* float depth = DepthAtBary(tp, baryatpoint); */ + + float aproxdepth = f1depth + (dslope * ((float)i - (float)(Max(curx1, 0)))); if (aproxdepth > zee[IndexOfZBuff(i, scanline)].depth) { @@ -231,23 +263,6 @@ void DrawTriZuff(Zee *zbuf, Tri2D *t) { } } -float Sign(Vector2 *v1, Vector2 *v2, Vector2 *v3) { - return (v1->x - v3->x) * (v2->y - v3->y) - (v2->x - v3->x) * (v1->y - v3->y); -} - -static inline bool IsInTri(Tri2D tri, Vector2 p) { - float d1, d2, d3; - bool has_neg, has_pos; - - d1 = Sign(&p, &tri.a, &tri.b); - d2 = Sign(&p, &tri.b, &tri.c); - d3 = Sign(&p, &tri.c, &tri.a); - - has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0); - has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0); - - return !(has_neg && has_pos); -} void CtrlLocalCam(LocalCam *cam, float time) { cam->velocity.x = 0;