This commit is contained in:
InventorXtreme 2023-12-28 03:11:09 -05:00
parent 717c45260d
commit 9e26b69f7b
2 changed files with 56 additions and 15 deletions

View file

@ -1,6 +1,6 @@
CC = gcc
CFLAGS = -pg -g -O2
LDFLAGS = -pg -g
CFLAGS = -pg -g -O2 -Wall
LDFLAGS = -pg -g
LDLIBS = -lraylib -lm
objects = c3d.o

67
c3d.c
View file

@ -84,6 +84,8 @@ double Max(double a, double b){
return a;
}
//sort triangle verts so that point A is the "highest" point (lowest y val) and point C is the "lowest" pont (highest y val)
void Tri2DSortByY(Tri2D * t){
Vector2 temp;
if (t->a.y > t->b.y) {
@ -104,7 +106,12 @@ void Tri2DSortByY(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) {
void FillTopFlatZbuffer(Zee zee[][1080], Tri2D* t, Tri2D * tp) {
if (t->b.x < t->a.x) {
Vector2 e = t->b;
t->b = t->a;
t->a = e;
}
//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
@ -116,17 +123,24 @@ void FillTopFlatZbuffer(Zee zee[][1080], Tri2D* t) {
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;
zee[i][scanline].triangle = tp;
}
}
curx1 -= atocslopeinv; //subtract because we are working backwards (reason why we start with point c in slope equtn)
curx2 -= btocslopinv;
}
}
void PrintTri2D(Tri2D t) {
printf("{(TRI2D) A: (%f, %f), B: (%f, %f), C:(%f,%f) }\n ",t.a.x,t.a.y,t.b.x, t.b.y, t.c.x, t.c.y);
}
//Draws triangle with a flat bottomp. Note B and C must be the bottom points with A being the top "spike"
void FillBottomFlatZbuffer(Zee zee[][1080], Tri2D* t) {
void FillBottomFlatZbuffer(Zee zee[][1080], Tri2D* t, Tri2D * tp) {
if (t->c.x < t->b.x) {
Vector2 e = t->c;
t->c = t->b;
t->b = e;
}
//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
@ -138,7 +152,7 @@ void FillBottomFlatZbuffer(Zee zee[][1080], Tri2D* t) {
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;
zee[i][scanline].triangle = tp;
}
}
curx1 += atobslopeinv;
@ -147,16 +161,38 @@ void FillBottomFlatZbuffer(Zee zee[][1080], Tri2D* t) {
}
void DrawTriZuff(Zee zbuf[][1080], Tri2D t){
Tri2DSortByY(&t);
if (t.b.y == t.c.y) {
FillBottomFlatZbuffer(zbuf,&t);
void DrawTriZuff(Zee zbuf[][1080], Tri2D * t){
Tri2DSortByY(t);
if (t->b.y == t->c.y) { // if bottom of triangle is flat
FillBottomFlatZbuffer(zbuf,t,t);
}
else if (t.a.y == t.b.y) {
FillTopFlatZbuffer(zbuf,&t);
} else{ //funny split tri
else if (t->a.y == t->b.y) { //if top of triangle is flat
FillTopFlatZbuffer(zbuf,t,t);
} else{ //funny split tri
Vector2 v4; //v4 is the vertex on the line between a and c. It is used to split the triangle into a top and bottom
v4.y = t->b.y;
float slope = (float)((t->c.x) - (t->a.x)) /((float)(t->c.y - t->a.y)); //get slope in run over rise becasue we need to find x
float changeiny = (float) (t->b.y - t->a.y);
float officalxpos = t->a.x + (slope*changeiny);
v4.x = officalxpos;
Tri2D bottomflattrires;
bottomflattrires.a = t->a;
bottomflattrires.b = t->b;
bottomflattrires.c = v4;
bottomflattrires.color = t->color;
Tri2D topflattrires;
topflattrires.a = t->b;
topflattrires.b = v4;
topflattrires.c = t->c;
topflattrires.color = t->color;
FillBottomFlatZbuffer(zbuf, &bottomflattrires, t);
FillTopFlatZbuffer(zbuf, &topflattrires, t);
}
@ -430,7 +466,10 @@ int main() {
Tri2D fullscreentribottom = (Tri2D){ (Vector2){0,0}, (Vector2){0,1080}, (Vector2){1920,1080}, RED};
Tri2D blank = (Tri2D) { (Vector2){-10,-10},(Vector2){-10,-10}, (Vector2){-10,-10}};
Tri2D blank = (Tri2D) { (Vector2){-10,-10},(Vector2){-10,-10}, (Vector2){-10,-10}};
Tri2D norm = (Tri2D){ (Vector2){500,50},(Vector2){0,0},(Vector2){250,500},GREEN};
while (!WindowShouldClose()) {
float frametime = GetFrameTime();
CtrlLocalCam(&camera,frametime);
@ -485,6 +524,8 @@ int main() {
//FillTopFlatZbuffer(ZBuff, &fullscreentritop);
//FillBottomFlatZbuffer(ZBuff, &fullscreentribottom);
DrawTriZuff(ZBuff, &norm);
int index = 0;