This commit is contained in:
InventorXtreme 2024-03-12 15:27:45 -04:00
parent adf5e6a761
commit 3147e6e9a5
3 changed files with 43 additions and 51 deletions

View file

@ -1,5 +1,5 @@
CC = clang #Set compiler CC = clang #Set compiler
CFLAGS = -pg -g -Wall -O2 -pg #set compiler flags CFLAGS = -pg -g -Wall -O3 -pg #set compiler flags
LDFLAGS = -pg -g #set linker flags LDFLAGS = -pg -g #set linker flags
LDLIBS = -lraylib -lm #set libraries to use LDLIBS = -lraylib -lm #set libraries to use
objects = c3d.o reader.o arrayfuncs.o vecfunc.o #list all object files objects = c3d.o reader.o arrayfuncs.o vecfunc.o #list all object files

43
c3d.c
View file

@ -118,13 +118,13 @@ void Tri2DSortByY(Tri2D *t) {
} }
} }
void DrawScanline(Zee *zee, Tri2D *trianglepointer, double start, double end, int scanline) { void DrawScanline(Zee *zee, Tri2D *trianglepointer, double start, double end, int scanline) {
Vector3 f1baryatpoint = Tri2DBaryAtPoint(trianglepointer, (Vector2){Max(start, 0), scanline}); Vector3 f1baryatpoint = Tri2DBaryAtPoint(trianglepointer, (Vector2){Max(start, 0), scanline});
float f1depth = DepthAtBary(trianglepointer, f1baryatpoint); float f1depth = DepthAtBary(trianglepointer, f1baryatpoint);
Vector3 f1baryatpoint2 = Tri2DBaryAtPoint(trianglepointer, (Vector2){Max(start, 0) + 1, scanline}); Vector3 f1baryatpoint2 =
Tri2DBaryAtPoint(trianglepointer, (Vector2){Max(start, 0) + 1, scanline});
float f1depth2 = DepthAtBary(trianglepointer, f1baryatpoint2); float f1depth2 = DepthAtBary(trianglepointer, f1baryatpoint2);
float dslope = f1depth2 - f1depth; float dslope = f1depth2 - f1depth;
@ -134,20 +134,16 @@ void DrawScanline(Zee * zee, Tri2D * trianglepointer, double start, double end,
/* Vector3 baryatpoint = Tri2DBaryAtPoint(tp, (Vector2){i, scanline}); */ /* Vector3 baryatpoint = Tri2DBaryAtPoint(tp, (Vector2){i, scanline}); */
/* float depth = DepthAtBary(tp, baryatpoint); */ /* float depth = DepthAtBary(tp, baryatpoint); */
float aproxdepth = f1depth + (dslope * ((float)i - (float)(Max(start, 0)))); float aproxdepth = f1depth + (dslope * ((float)i - (float)(Max(start, 0))));
if (aproxdepth > zee[IndexOfZBuff(i, scanline)].depth) { if (aproxdepth > zee[IndexOfZBuff(i, scanline)].depth) {
zee[IndexOfZBuff(i, scanline)].c = trianglepointer->color; zee[IndexOfZBuff(i, scanline)].c = trianglepointer->color;
zee[IndexOfZBuff(i, scanline)].depth = aproxdepth; zee[IndexOfZBuff(i, scanline)].depth = aproxdepth;
zee[IndexOfZBuff(i, scanline)].tri = trianglepointer;
} }
} }
} }
// Draws triangle with a flat top. Note A and B must be the top points with C // Draws triangle with a flat top. Note A and B must be the top points with C
// being the bottom "spike" // being the bottom "spike"
void FillTopFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) { void FillTopFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
@ -175,7 +171,6 @@ void FillTopFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
if (0 <= scanline && scanline < RENDERHEIGHT) { if (0 <= scanline && scanline < RENDERHEIGHT) {
DrawScanline(zee, tp, curx1, curx2, scanline); DrawScanline(zee, tp, curx1, curx2, scanline);
} }
curx1 -= atocslopeinv; // subtract because we are working backwards (reason curx1 -= atocslopeinv; // subtract because we are working backwards (reason
// why we start with point c in slope equtn) // why we start with point c in slope equtn)
@ -183,7 +178,6 @@ void FillTopFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
} }
} }
void PrintTri2D(Tri2D t) { 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, 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); t.c.y);
@ -209,8 +203,10 @@ void FillBottomFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
double curx1 = t->a.x; double curx1 = t->a.x;
double curx2 = t->a.x; double curx2 = t->a.x;
int scantop = t->a.y; // start must always be the true beging of the triangle, otherwise everything will be offset int scantop = t->a.y; // start must always be the true beging of the triangle, otherwise
int scanbottom = Min(t->c.y, RENDERHEIGHT); // we can stop rendering as soon as it goes off screen // everything will be offset
int scanbottom =
Min(t->c.y, RENDERHEIGHT); // we can stop rendering as soon as it goes off screen
for (int scanline = scantop; scanline < scanbottom; for (int scanline = scantop; scanline < scanbottom;
scanline++) { // TODO: Possibly more optimization possible here, use linear correspondance scanline++) { // TODO: Possibly more optimization possible here, use linear correspondance
@ -258,7 +254,6 @@ void DrawTriZuff(Zee *zbuf, Tri2D *t) {
} }
} }
void CtrlLocalCam(LocalCam *cam, float time) { void CtrlLocalCam(LocalCam *cam, float time) {
cam->velocity.x = 0; cam->velocity.x = 0;
cam->velocity.y = 0; cam->velocity.y = 0;
@ -469,7 +464,7 @@ int main() {
Tri2D norm = (Tri2D){(Vector2){500, 50}, (Vector2){0, 0}, (Vector2){250, 500}, 0, 0, 0, GREEN}; Tri2D norm = (Tri2D){(Vector2){500, 50}, (Vector2){0, 0}, (Vector2){250, 500}, 0, 0, 0, GREEN};
bool run3d = true; bool run3d = true;
Object3D t = ReadObjectFromFile("mario.obj"); Object3D t = ReadObjectFromFile("teapot.obj");
for (int i = 0; i < t.triangles->length; i++) { for (int i = 0; i < t.triangles->length; i++) {
// printf("t: %f\n", t.triangles->arr[i].a.x); // printf("t: %f\n", t.triangles->arr[i].a.x);
TriArrayAppend(&tarr, t.triangles->arr[i]); TriArrayAppend(&tarr, t.triangles->arr[i]);
@ -505,6 +500,12 @@ int main() {
DrawTriZuff(ZBuff, &Tri2Darr.arr[i]); DrawTriZuff(ZBuff, &Tri2Darr.arr[i]);
} }
if (IsKeyDown(KEY_H)) {
if (ZBuff[IndexOfZBuff(RENDERWIDTH/2, RENDERHEIGHT/2 )].tri != 0) {
ZBuff[IndexOfZBuff(RENDERWIDTH/2, RENDERHEIGHT/2)].tri->color = RED;
}
}
// FillTopFlatZbuffer(ZBuff, &funners); // FillTopFlatZbuffer(ZBuff, &funners);
// FillBottomFlatZbuffer(ZBuff,&funners2); // FillBottomFlatZbuffer(ZBuff,&funners2);
@ -517,21 +518,11 @@ int main() {
int index = 0; int index = 0;
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++) {
// int index = (x+y*RENDERWIDTH);
/* Color * c = &ZBuff[x][y].triangle->color;
display[index] = c->r;
display[index+1] = c->g;
display[index+2] = c->b;
display[index+3] = c->a;
*/
//if (ZBuff[IndexOfZBuff(x, y)].triangle != 0 ) { // memset sets this to 0
// DrawPixel(x,y,ZBuff[x][y].triangle->color);
display[index] = ZBuff[IndexOfZBuff(x, y)].c; if (ZBuff[IndexOfZBuff(x, y)].tri != 0) {
// Zee test = ZBuff[IndexOfZBuff(x,y)]; display[index] = ZBuff[IndexOfZBuff(x, y)].tri->color;
// display[index] = test.triangle->color; }
//}
index = index + 1; index = index + 1;
} }
} }

View file

@ -40,6 +40,7 @@ typedef struct Tri2D Tri2D;
struct Zee { struct Zee {
int depth; int depth;
Tri2D * tri;
Color c; Color c;
}; };
typedef struct Zee Zee; typedef struct Zee Zee;