1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/small3dlib.git synced 2024-11-21 20:39:57 +01:00

Use stepping lerp

This commit is contained in:
Miloslav Číž 2019-05-06 19:02:58 +02:00
parent 5849cc6f17
commit d4872e6984
2 changed files with 23 additions and 20 deletions

38
s3l.h
View file

@ -121,12 +121,15 @@ typedef int32_t S3L_Unit; /**< Units of measurement in 3D space. There is
it will overflow. Also other things it will overflow. Also other things
may overflow, so rather don't do it. */ may overflow, so rather don't do it. */
#define S3L_LERP_QUALITY 3 /**< Quality (scaling) of SOME linear interpolations. #ifndef S3L_LERP_QUALITY
0 will most likely be faster, but artifacts can #define S3L_LERP_QUALITY 8 /**< Quality (scaling) of SOME linear
occur, while higher values can fix this -- in interpolations. 0 will most likely be faster,
theory all higher values will have the same but artifacts can occur for bigger tris,
speed (it is a shift value), but it mustn't be while higher values can fix this -- in theory
too high to prevent overflow (3 seems okay). */ all higher values will have the same speed
(it is a shift value), but it mustn't be too
high to prevent overflow. */
#endif
#define S3L_nonzero(value) ((value) != 0 ? (value) : 1) /**< prevents division #define S3L_nonzero(value) ((value) != 0 ? (value) : 1) /**< prevents division
by zero */ by zero */
@ -852,26 +855,25 @@ void S3L_drawTriangle(
// draw the horizontal line // draw the horizontal line
S3L_Unit tMax = S3L_nonzero(rX - lX - 1); // prevent division by zero S3L_Unit rowLength = S3L_nonzero(rX - lX - 1); // prevent zero div
S3L_Unit t1 = 0;
S3L_Unit t2 = tMax; S3L_Unit b0 = 0;
S3L_Unit b1 = lSideUnitPos;
S3L_Unit b0Step = rSideUnitPos / rowLength;
S3L_Unit b1Step = lSideUnitPos / rowLength;
for (S3L_ScreenCoord x = lX; x < rX; ++x) for (S3L_ScreenCoord x = lX; x < rX; ++x)
{ {
// TODO: try to do this with stepping and S3L_LERP_QUALITY *barycentric0 = b0 >> S3L_LERP_QUALITY;
*barycentric0 = *barycentric1 = b1 >> S3L_LERP_QUALITY;
S3L_interpolateFrom0(rSideUnitPos >> S3L_LERP_QUALITY,t1,tMax);
*barycentric1 =
S3L_interpolateFrom0(lSideUnitPos >> S3L_LERP_QUALITY,t2,tMax);
*barycentric2 = S3L_FRACTIONS_PER_UNIT - *barycentric0 - *barycentric1; *barycentric2 = S3L_FRACTIONS_PER_UNIT - *barycentric0 - *barycentric1;
p.x = x; p.x = x;
S3L_PIXEL_FUNCTION(&p); S3L_PIXEL_FUNCTION(&p);
++t1; b0 += b0Step;
--t2; b1 -= b1Step;
} }
lSideUnitPos += lSideUnitStep; lSideUnitPos += lSideUnitStep;

View file

@ -76,12 +76,13 @@ void drawPixel(S3L_PixelInfo *p)
uint8_t col = texturePixel(u,v); uint8_t col = texturePixel(u,v);
// setPixel(p->x,p->y,col * 120,20,(2 - col) * 120); setPixel(p->x,p->y,col * 120,20,(2 - col) * 120);
/*
setPixel(p->x,p->y, setPixel(p->x,p->y,
p->barycentric0 / ((float) S3L_FRACTIONS_PER_UNIT) * 255, p->barycentric0 / ((float) S3L_FRACTIONS_PER_UNIT) * 255,
p->barycentric1 / ((float) S3L_FRACTIONS_PER_UNIT) * 255, p->barycentric1 / ((float) S3L_FRACTIONS_PER_UNIT) * 255,
p->barycentric2 / ((float) S3L_FRACTIONS_PER_UNIT) * 255); p->barycentric2 / ((float) S3L_FRACTIONS_PER_UNIT) * 255);
*/
} }
const int16_t test_coords[] = const int16_t test_coords[] =