This commit is contained in:
InventorXtreme 2024-01-03 15:13:45 -05:00
parent 9e26b69f7b
commit 3a274a9d61
2 changed files with 29 additions and 12 deletions

View file

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

39
c3d.c
View file

@ -1,8 +1,10 @@
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "raylib.h" #include "raylib.h"
const int RENDERWIDTH = 1920; const int RENDERWIDTH = 1920;
const int RENDERHEIGHT = 1080; const int RENDERHEIGHT = 1080;
@ -85,6 +87,11 @@ double Max(double a, double b){
} }
int IndexOfZBuff(int row, int col) {
return row*RENDERWIDTH + col;
}
//sort triangle verts so that point A is the "highest" point (lowest y val) and point C is the "lowest" pont (highest y val) //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){ void Tri2DSortByY(Tri2D * t){
Vector2 temp; Vector2 temp;
@ -106,7 +113,7 @@ 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" //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, Tri2D * tp) { void FillTopFlatZbuffer(Zee * zee , Tri2D* t, Tri2D * tp) {
if (t->b.x < t->a.x) { if (t->b.x < t->a.x) {
Vector2 e = t->b; Vector2 e = t->b;
t->b = t->a; t->b = t->a;
@ -123,7 +130,7 @@ void FillTopFlatZbuffer(Zee zee[][1080], Tri2D* t, Tri2D * tp) {
for(int scanline=t->c.y; scanline >= t->a.y; scanline--){ for(int scanline=t->c.y; scanline >= t->a.y; scanline--){
if (0 <= scanline && scanline < RENDERHEIGHT){ if (0 <= scanline && scanline < RENDERHEIGHT){
for (int i = Max(curx1,0); i < Min(curx2,RENDERWIDTH); i++) { for (int i = Max(curx1,0); i < Min(curx2,RENDERWIDTH); i++) {
zee[i][scanline].triangle = tp; zee[IndexOfZBuff(i,scanline) ].triangle = tp;
} }
} }
curx1 -= atocslopeinv; //subtract because we are working backwards (reason why we start with point c in slope equtn) curx1 -= atocslopeinv; //subtract because we are working backwards (reason why we start with point c in slope equtn)
@ -135,7 +142,7 @@ void PrintTri2D(Tri2D t) {
} }
//Draws triangle with a flat bottomp. Note B and C must be the bottom points with A being the top "spike" //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, Tri2D * tp) { void FillBottomFlatZbuffer(Zee * zee, Tri2D* t, Tri2D * tp) {
if (t->c.x < t->b.x) { if (t->c.x < t->b.x) {
Vector2 e = t->c; Vector2 e = t->c;
t->c = t->b; t->c = t->b;
@ -152,7 +159,7 @@ void FillBottomFlatZbuffer(Zee zee[][1080], Tri2D* t, Tri2D * tp) {
for(int scanline=t->a.y; scanline < t->c.y; scanline++){ for(int scanline=t->a.y; scanline < t->c.y; scanline++){
if (0 <= scanline && scanline < RENDERHEIGHT){ if (0 <= scanline && scanline < RENDERHEIGHT){
for (int i = Max(curx1,0); i < Min(curx2,RENDERWIDTH); i++) { for (int i = Max(curx1,0); i < Min(curx2,RENDERWIDTH); i++) {
zee[i][scanline].triangle = tp; zee[ IndexOfZBuff(i,scanline) ].triangle = tp;
} }
} }
curx1 += atobslopeinv; curx1 += atobslopeinv;
@ -163,7 +170,7 @@ void FillBottomFlatZbuffer(Zee zee[][1080], Tri2D* t, Tri2D * tp) {
void DrawTriZuff(Zee zbuf[][1080], Tri2D * t){ void DrawTriZuff(Zee * zbuf, Tri2D * t){
Tri2DSortByY(t); Tri2DSortByY(t);
if (t->b.y == t->c.y) { // if bottom of triangle is flat if (t->b.y == t->c.y) { // if bottom of triangle is flat
@ -401,6 +408,9 @@ Tri2D ConvertTriToTri2D(Tri* t){
rettri2d.color = t->color; rettri2d.color = t->color;
return rettri2d; return rettri2d;
} }
int main() { int main() {
proj = HALFWIDTH / tan(half_fov); proj = HALFWIDTH / tan(half_fov);
@ -454,8 +464,8 @@ int main() {
Tri2Darr.length=0; Tri2Darr.length=0;
Tri2Darr.arr = internaltri2darray; Tri2Darr.arr = internaltri2darray;
static Zee ZBuff[1920][1080] = {{(Zee){10000,NULL}}}; //FIXME: Stupid static makes the file 32 Megs because pog //static Zee ZBuff[1920][1080] = {{(Zee){10000,NULL}}}; //FIXME: Stupid static makes the file 32 Megs because pog
Zee * ZBuff = malloc(RENDERHEIGHT*RENDERWIDTH*sizeof(Zee));
static Color display[1920*1080*4]; static Color display[1920*1080*4];
memset(display,0,sizeof(display)); memset(display,0,sizeof(display));
@ -495,7 +505,10 @@ int main() {
Tri2DArrayAppend(&Tri2Darr,ConvertTriToTri2D(&TransformedTris.arr[i])); Tri2DArrayAppend(&Tri2Darr,ConvertTriToTri2D(&TransformedTris.arr[i]));
} }
memset(ZBuff,0,sizeof(ZBuff)); //memset(ZBuff,NULL ,sizeof(Zee) * RENDERHEIGHT*RENDERWIDTH );
for (int i = 0; i < RENDERHEIGHT*RENDERWIDTH; i ++) {
ZBuff[i] = (Zee){10000,NULL};
}
memset(display,0,sizeof(display)); memset(display,0,sizeof(display));
@ -538,12 +551,16 @@ int main() {
display[index+2] = c->b; display[index+2] = c->b;
display[index+3] = c->a; display[index+3] = c->a;
*/ */
printf("%d\n",IndexOfZBuff(x,y));
ZBuff[IndexOfZBuff(x,y)].triangle = NULL;
if (ZBuff[IndexOfZBuff(x,y)].triangle != NULL) {
if (ZBuff[x][y].triangle != NULL) {
//DrawPixel(x,y,ZBuff[x][y].triangle->color); //DrawPixel(x,y,ZBuff[x][y].triangle->color);
display[index] = ZBuff[x][y].triangle->color; //display[index] = ZBuff[IndexOfZBuff(x,y)].triangle->color;
Zee test = ZBuff[IndexOfZBuff(x,y)];
display[index] = test.triangle->color;
} }
index = index+1; index = index+1;