fixxed missing pixels

This commit is contained in:
InventorXtreme 2024-03-12 16:25:24 -04:00
parent 3147e6e9a5
commit 8165a5c760

41
c3d.c
View file

@ -128,7 +128,7 @@ void DrawScanline(Zee *zee, Tri2D *trianglepointer, double start, double end, in
float f1depth2 = DepthAtBary(trianglepointer, f1baryatpoint2); float f1depth2 = DepthAtBary(trianglepointer, f1baryatpoint2);
float dslope = f1depth2 - f1depth; float dslope = f1depth2 - f1depth;
for (int i = Max(start, 0); i < Min(end, RENDERWIDTH); i++) { for (int i = floor(Max(start, 0)); i <= ceil(Min(end, RENDERWIDTH)); i++) {
/* zee[IndexOfZBuff(i, scanline)].triangle = tp; */ /* zee[IndexOfZBuff(i, scanline)].triangle = tp; */
/* Vector3 baryatpoint = Tri2DBaryAtPoint(tp, (Vector2){i, scanline}); */ /* Vector3 baryatpoint = Tri2DBaryAtPoint(tp, (Vector2){i, scanline}); */
@ -167,9 +167,10 @@ void FillTopFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
int scanbottom = t->c.y; int scanbottom = t->c.y;
int scantop = Max(t->a.y, 0); int scantop = Max(t->a.y, 0);
for (int scanline = scanbottom; scanline >= scantop; scanline--) {
if (0 <= scanline && scanline < RENDERHEIGHT) {
for (float scanline = scanbottom; scanline >= scantop; scanline--) {
if (0 <= scanline && scanline < RENDERHEIGHT) {
//tp->color = RED;
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
@ -206,13 +207,16 @@ void FillBottomFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) {
int scantop = t->a.y; // start must always be the true beging of the triangle, otherwise int scantop = t->a.y; // start must always be the true beging of the triangle, otherwise
// everything will be offset // everything will be offset
int scanbottom = int scanbottom =
Min(t->c.y, RENDERHEIGHT); // we can stop rendering as soon as it goes off screen Min(t->c.y, RENDERHEIGHT-1); // 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
// for y, not just x to get depth // for y, not just x to get depth
if (0 <= scanline && scanline < RENDERHEIGHT) { if (0 <= scanline && scanline < RENDERHEIGHT) {
DrawScanline(zee, tp, curx1, curx2, scanline); Tri2D nt = *tp;
nt.color = RED;
DrawScanline(zee, &nt, curx1, curx2, scanline);
} }
curx1 += atobslopeinv; curx1 += atobslopeinv;
curx2 += atocslopeinv; curx2 += atocslopeinv;
@ -427,11 +431,11 @@ int main() {
TriArray tarr; TriArray tarr;
tarr.arr = internaltriarray; tarr.arr = internaltriarray;
tarr.length = 0; tarr.length = 0;
TriArrayAppend(&tarr, (Tri){(Vector3){0, 0, -1000}, (Vector3){0, 800, -1000}, /* TriArrayAppend(&tarr, (Tri){(Vector3){0, 0, -1000}, (Vector3){0, 800, -1000}, */
(Vector3){800, 800, -1000}, WHITE}); /* (Vector3){800, 800, -1000}, WHITE}); */
TriArrayAppend(&tarr, (Tri){(Vector3){0, 0, -2000}, (Vector3){0, 800, -2000}, /* TriArrayAppend(&tarr, (Tri){(Vector3){0, 0, -2000}, (Vector3){0, 800, -2000}, */
(Vector3){800, 800, -2000}, BLUE}); /* (Vector3){800, 800, -2000}, BLUE}); */
static Tri internaltransformedtriarray[50000]; static Tri internaltransformedtriarray[50000];
TriArray TransformedTris; TriArray TransformedTris;
@ -450,7 +454,7 @@ int main() {
memset(display, 0, sizeof(display)); memset(display, 0, sizeof(display));
Tri2D funners = Tri2D funners =
(Tri2D){(Vector2){50, 50}, (Vector2){500, 50}, (Vector2){500, 500}, 0, 0, 0, GREEN}; (Tri2D){(Vector2){0, 0}, (Vector2){500, 0}, (Vector2){500, 500}, -1000, -1000, -1000, GREEN};
Tri2D funners2 = Tri2D funners2 =
(Tri2D){(Vector2){600, 0}, (Vector2){600, 500}, (Vector2){1000, 500}, 0, 0, 0, RED}; (Tri2D){(Vector2){600, 0}, (Vector2){600, 500}, (Vector2){1000, 500}, 0, 0, 0, RED};
@ -464,11 +468,17 @@ 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("teapot.obj");
if (true) {
Object3D t = ReadObjectFromFile("cube.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]);
} }
}
while (!WindowShouldClose() && run3d) { while (!WindowShouldClose() && run3d) {
float frametime = GetFrameTime(); float frametime = GetFrameTime();
CtrlLocalCam(&camera, frametime); CtrlLocalCam(&camera, frametime);
@ -502,11 +512,12 @@ int main() {
if (IsKeyDown(KEY_H)) { if (IsKeyDown(KEY_H)) {
if (ZBuff[IndexOfZBuff(RENDERWIDTH/2, RENDERHEIGHT/2 )].tri != 0) { if (ZBuff[IndexOfZBuff(RENDERWIDTH/2, RENDERHEIGHT/2 )].tri != 0) {
ZBuff[IndexOfZBuff(RENDERWIDTH/2, RENDERHEIGHT/2)].tri->color = RED; //ZBuff[IndexOfZBuff(RENDERWIDTH/2, RENDERHEIGHT/2)].tri->color = RED;
printf("%f\n", ZBuff[IndexOfZBuff(RENDERWIDTH/2, RENDERHEIGHT/2)].tri->a.x);
} }
} }
// FillTopFlatZbuffer(ZBuff, &funners); //FillTopFlatZbuffer(ZBuff, &funners, &funners);
// FillBottomFlatZbuffer(ZBuff,&funners2); // FillBottomFlatZbuffer(ZBuff,&funners2);
// FillTopFlatZbuffer(ZBuff, &fullscreentritop); // FillTopFlatZbuffer(ZBuff, &fullscreentritop);
@ -519,7 +530,7 @@ int main() {
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++) {
//REMOVE THE IF AND REPALCE THE VALUE THAT DISPLAY IS SET TO WITH DIRECT COLOR WHEN DONE DEBUGGING
if (ZBuff[IndexOfZBuff(x, y)].tri != 0) { if (ZBuff[IndexOfZBuff(x, y)].tri != 0) {
display[index] = ZBuff[IndexOfZBuff(x, y)].tri->color; display[index] = ZBuff[IndexOfZBuff(x, y)].tri->color;
} }