From 33783250932a78695c24ad32bdd35dde39eae7d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Mon, 3 Jun 2019 18:06:06 +0200 Subject: [PATCH] Fix disappearing triangles --- small3dlib.h | 12 ++++++++++++ testSDL.c | 6 +++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/small3dlib.h b/small3dlib.h index 7336b41..83beaff 100644 --- a/small3dlib.h +++ b/small3dlib.h @@ -1531,6 +1531,18 @@ void S3L_drawTriangle( loop */ #endif + if (rXClipped < lXClipped) + { + /* This can sometimes happen because of numerical errors in sorting + left vs right triangle point, which are compared based on SLOPE, not + x coordinates. Here we swap the values to prevents not drawing the + triagle. */ + + S3L_ScreenCoord tmp = rXClipped; + rXClipped = lXClipped; + lXClipped = tmp; + } + // draw the row -- inner loop: for (S3L_ScreenCoord x = lXClipped; x < rXClipped; ++x) diff --git a/testSDL.c b/testSDL.c index 1f75f8b..0ef8922 100644 --- a/testSDL.c +++ b/testSDL.c @@ -18,8 +18,8 @@ #define S3L_PIXEL_FUNCTION drawPixel -#define S3L_RESOLUTION_X 640 -#define S3L_RESOLUTION_Y 480 +#define S3L_RESOLUTION_X 110 +#define S3L_RESOLUTION_Y 88 #include "small3dlib.h" @@ -280,7 +280,7 @@ scene.camera.transform.rotation.x = -35; scene.camera.transform.rotation.y = 128; scene.camera.transform.rotation.z = 0; -S3L_setTransform3D(-542,-449,3259,39,216,0,512,512,512,&(scene.camera.transform)); +S3L_setTransform3D(-542,-449,4000,39,216,0,512,512,512,&(scene.camera.transform)); scene.modelCount = 2; scene.models = models;