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
CFLAGS = -pg -g -Wall -O2 -pg #set compiler flags
CFLAGS = -pg -g -Wall -O3 -pg #set compiler flags
LDFLAGS = -pg -g #set linker flags
LDLIBS = -lraylib -lm #set libraries to use
objects = c3d.o reader.o arrayfuncs.o vecfunc.o #list all object files

91
c3d.c
View file

@ -118,36 +118,32 @@ 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});
float f1depth = DepthAtBary(trianglepointer, f1baryatpoint);
Vector3 f1baryatpoint = Tri2DBaryAtPoint(trianglepointer, (Vector2){Max(start, 0), scanline});
float f1depth = DepthAtBary(trianglepointer, f1baryatpoint);
Vector3 f1baryatpoint2 = Tri2DBaryAtPoint(trianglepointer, (Vector2){Max(start, 0) + 1, scanline});
float f1depth2 = DepthAtBary(trianglepointer, f1baryatpoint2);
float dslope = f1depth2 - f1depth;
Vector3 f1baryatpoint2 =
Tri2DBaryAtPoint(trianglepointer, (Vector2){Max(start, 0) + 1, scanline});
float f1depth2 = DepthAtBary(trianglepointer, f1baryatpoint2);
float dslope = f1depth2 - f1depth;
for (int i = Max(start, 0); i < Min(end, RENDERWIDTH); i++) {
/* zee[IndexOfZBuff(i, scanline)].triangle = tp; */
for (int i = Max(start, 0); i < Min(end, RENDERWIDTH); i++) {
/* zee[IndexOfZBuff(i, scanline)].triangle = tp; */
/* Vector3 baryatpoint = Tri2DBaryAtPoint(tp, (Vector2){i, scanline}); */
/* float depth = DepthAtBary(tp, baryatpoint); */
/* Vector3 baryatpoint = Tri2DBaryAtPoint(tp, (Vector2){i, scanline}); */
/* 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) {
zee[IndexOfZBuff(i, scanline)].c = trianglepointer->color;
zee[IndexOfZBuff(i, scanline)].depth = aproxdepth;
}
}
if (aproxdepth > zee[IndexOfZBuff(i, scanline)].depth) {
zee[IndexOfZBuff(i, scanline)].c = trianglepointer->color;
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
// being the bottom "spike"
void FillTopFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
@ -168,14 +164,13 @@ void FillTopFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
double curx1 = t->c.x;
double curx2 = t->c.x;
int scanbottom = t->c.y;
int scantop = Max(t->a.y, 0);
int scanbottom = t->c.y;
int scantop = Max(t->a.y, 0);
for (int scanline = scanbottom; scanline >= scantop; scanline--) {
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
// 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) {
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);
@ -209,15 +203,17 @@ void FillBottomFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
double curx1 = 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 scanbottom = Min(t->c.y, RENDERHEIGHT); // we can stop rendering as soon as it goes off screen
int scantop = t->a.y; // start must always be the true beging of the triangle, otherwise
// 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;
scanline++) { // TODO: Possibly more optimization possible here, use linear correspondance
// for y, not just x to get depth
if (0 <= scanline && scanline < RENDERHEIGHT) {
DrawScanline(zee, tp, curx1, curx2, scanline);
}
DrawScanline(zee, tp, curx1, curx2, scanline);
}
curx1 += atobslopeinv;
curx2 += atocslopeinv;
}
@ -258,7 +254,6 @@ void DrawTriZuff(Zee *zbuf, Tri2D *t) {
}
}
void CtrlLocalCam(LocalCam *cam, float time) {
cam->velocity.x = 0;
cam->velocity.y = 0;
@ -469,11 +464,11 @@ int main() {
Tri2D norm = (Tri2D){(Vector2){500, 50}, (Vector2){0, 0}, (Vector2){250, 500}, 0, 0, 0, GREEN};
bool run3d = true;
Object3D t = ReadObjectFromFile("mario.obj");
for (int i = 0; i < t.triangles->length; i++){
//printf("t: %f\n", t.triangles->arr[i].a.x);
TriArrayAppend(&tarr, t.triangles->arr[i]);
}
Object3D t = ReadObjectFromFile("teapot.obj");
for (int i = 0; i < t.triangles->length; i++) {
// printf("t: %f\n", t.triangles->arr[i].a.x);
TriArrayAppend(&tarr, t.triangles->arr[i]);
}
while (!WindowShouldClose() && run3d) {
float frametime = GetFrameTime();
CtrlLocalCam(&camera, frametime);
@ -505,6 +500,12 @@ int main() {
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);
// FillBottomFlatZbuffer(ZBuff,&funners2);
@ -517,21 +518,11 @@ int main() {
int index = 0;
for (int y = 0; y < RENDERHEIGHT; y++) {
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;
// Zee test = ZBuff[IndexOfZBuff(x,y)];
// display[index] = test.triangle->color;
//}
if (ZBuff[IndexOfZBuff(x, y)].tri != 0) {
display[index] = ZBuff[IndexOfZBuff(x, y)].tri->color;
}
index = index + 1;
}
}

View file

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