From e081b9be74484f76633f962863d2db84a156d83f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sun, 5 May 2019 22:32:52 +0200 Subject: [PATCH] Rasterization looks correct --- s3l.h | 13 +++++++++---- test.c | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/s3l.h b/s3l.h index 74b7831..76da3a3 100644 --- a/s3l.h +++ b/s3l.h @@ -734,8 +734,9 @@ void S3L_drawTriangle( specifics of this algorithm are among others: - drawing possibly a NON-CONTINUOUS line - - NOT tracing the line exactly, but rather rasterizing on one (right) - side of it, according to the pixel CENTERS + - NOT tracing the line exactly, but rather rasterizing one the right + side of it, according to the pixel CENTERS, INCLUDING the pixel + centers The principle is this: @@ -756,6 +757,7 @@ void S3L_drawTriangle( lDy, rDy, // dy (end point - start point) lInc, rInc, // direction in which to increment (1 or -1) lErr, rErr, // current error (Bresenham) + lErrCmp, rErrCmp, // helper for deciding comparison (> vs >=) lErrAdd, rErrAdd, // error value to add in each Bresenham cycle lErrSub, rErrSub; // error value to substract when moving in x direction @@ -782,13 +784,16 @@ void S3L_drawTriangle( s##SideUnitStep *= -1;\ }\ s##Inc = s##Dx >= 0 ? 1 : -1;\ - s##Err = (s##Dx < 0) ? 0 : s##Dy;\ + if (s##Dx < 0)\ + {s##Err = 0; s##ErrCmp = 0;}\ + else\ + {s##Err = s##Dy; s##ErrCmp = 1;}\ s##ErrAdd = S3L_abs(s##Dx);\ s##ErrSub = s##Dy != 0 ? s##Dy : 1; /* don't allow 0, could lead to an infinite substracting loop */ #define stepSide(s)\ - while (s##Err > s##Dy)\ + while (s##Err - s##Dy >= s##ErrCmp)\ {\ s##X += s##Inc;\ s##Err -= s##ErrSub;\ diff --git a/test.c b/test.c index 85465e8..2dfa9e1 100644 --- a/test.c +++ b/test.c @@ -289,7 +289,7 @@ uint16_t testRasterization() dt(12,12,12); dt(9,10,10); dt(1,10,10); -// dt(9,6,1); + dt(9,6,1); dt(0,6,10); #undef dt