actually use optomizaiton on both types of tris

This commit is contained in:
InventorXtreme 2024-03-11 23:27:00 -04:00
parent f8a7fc9a3c
commit 2869f5ccdd

57
c3d.c
View file

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