From 71fa8e21e34fede5c919859a62037ae0c7960bc5 Mon Sep 17 00:00:00 2001 From: InventorXtreme Date: Mon, 4 Dec 2023 22:29:23 -0500 Subject: [PATCH] Adding new triangle alg --- c3d.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 3 deletions(-) diff --git a/c3d.c b/c3d.c index 44f7c67..2cd37bc 100644 --- a/c3d.c +++ b/c3d.c @@ -74,6 +74,69 @@ struct Tri2DArray { }; typedef struct Tri2DArray Tri2DArray; +double Min(double a, double b){ + if (a>b) { + return b; + } + return a; +} + +double Max(double a, double b){ + if (ac.x - t->a.x) / (t->c.y - t->a.y); //dif in x from start to end with a and c + float btocslopinv = (t->c.x - t->b.x) / (t->c.y - t->b.y); //dif in x from start to end with b and c + + //start at bottom of triangle (point c) so that we do not need to determine which vertex is on which side and increment it with its proper slope + double curx1 = t->c.x; + double curx2 = t->c.x; + + for(int scanline=t->c.y; scanline > t->a.y; scanline--){ + if (0 <= scanline && scanline < RENDERHEIGHT){ + for (int i = Max(curx1,0); i < Min(curx2,RENDERWIDTH); i++) { + zee[i][scanline].triangle = t; + } + } + curx1 -= atocslopeinv; //subtract because we are working backwards (reason why we start with point c in slope equtn) + curx2 -= btocslopinv; + } +} + + +//Draws triangle with a flat top. Note B and C must be the bottom points with A being the top "spike" +void FillBottomFlatZbuffer(Zee zee[][1080], Tri2D* t) { + //Becasue we are trying to get the x values in terms of the y values, we need inverse slope + float atobslopeinv = (t->b.x - t->a.x) / (t->b.y - t->a.y); //dif in x from start to end with a and c + float atocslopeinv = (t->c.x - t->a.x) / (t->c.y - t->a.y); //dif in x from start to end with b and c + + //start at bottom of triangle (point c) so that we do not need to determine which vertex is on which side and increment it with its proper slope + double curx1 = t->c.x; + double curx2 = t->c.x; + + for(int scanline=t->a.y; scanline < t->c.y; scanline++){ + if (0 <= scanline && scanline < RENDERHEIGHT){ + for (int i = Max(curx1,0); i < Min(curx2,RENDERWIDTH); i++) { + zee[i][scanline].triangle = t; + } + } + curx1 += atobslopeinv; + curx2 += atocslopeinv; + } + +} + + 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); } @@ -198,14 +261,15 @@ void CtrlLocalCam(LocalCam *cam, float time) { } if (IsKeyDown(KEY_Q)){ - cam->angleVelocity.y = cam->angleVelocity.y + time*1000; + cam->angleVelocity.y = cam->angleVelocity.y + time*10; } if (IsKeyDown(KEY_E)){ - cam->angleVelocity.y = cam->angleVelocity.y - time*1000; + cam->angleVelocity.y = cam->angleVelocity.y - time*10; } if (IsKeyDown(KEY_G)) { - printf("PosX: %f PosY: %f, PosZ: %f\n",cam->position.x,cam->position.y,cam->position.z); + printf("PosX: %f PosY: %f PosZ: %f\n",cam->position.x,cam->position.y,cam->position.z); + printf("RotX; %f RotY: %f RotZ: %f\n",cam->angles.x,cam->angles.y,cam->angles.z); } } @@ -365,6 +429,14 @@ int main() { } } + Tri2D funners = (Tri2D){ (Vector2){50,50},(Vector2){500,50},(Vector2){500,500},GREEN}; + + FillTopFlatZbuffer(ZBuff, &funners); + + Tri2D funners2 = (Tri2D){ (Vector2){600,0},(Vector2){600,500},(Vector2){1000,500},RED}; + + FillBottomFlatZbuffer(ZBuff,&funners2); + for(int y = 0; y < RENDERHEIGHT; y++) { for(int x = 0; x < RENDERWIDTH; x++) { if (ZBuff[x][y].triangle != NULL){