Adding new triangle alg

This commit is contained in:
InventorXtreme 2023-12-04 22:29:23 -05:00
parent 042c004341
commit 71fa8e21e3

78
c3d.c
View file

@ -74,6 +74,69 @@ struct Tri2DArray {
}; };
typedef struct Tri2DArray 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 (a<b){
return b;
}
return a;
}
void Tri2DSort(Tri2D * t){
}
//Draws triangle with a flat top. Note A and B must be the top points with C being the bottom "spike"
void FillTopFlatZbuffer(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 atocslopeinv = (t->c.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) { 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); 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)){ if (IsKeyDown(KEY_Q)){
cam->angleVelocity.y = cam->angleVelocity.y + time*1000; cam->angleVelocity.y = cam->angleVelocity.y + time*10;
} }
if (IsKeyDown(KEY_E)){ if (IsKeyDown(KEY_E)){
cam->angleVelocity.y = cam->angleVelocity.y - time*1000; cam->angleVelocity.y = cam->angleVelocity.y - time*10;
} }
if (IsKeyDown(KEY_G)) { 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 y = 0; y < RENDERHEIGHT; y++) {
for(int x = 0; x < RENDERWIDTH; x++) { for(int x = 0; x < RENDERWIDTH; x++) {
if (ZBuff[x][y].triangle != NULL){ if (ZBuff[x][y].triangle != NULL){