1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/small3dlib.git synced 2024-11-23 20:59:58 +01:00

Add linear depth interpolation

This commit is contained in:
Miloslav Číž 2019-05-21 14:55:24 +02:00
parent 6ff62a8995
commit 0084e48b2c
2 changed files with 45 additions and 3 deletions

41
s3l.h
View file

@ -443,6 +443,9 @@ static inline void S3L_rotate2DPoint(S3L_Unit *x, S3L_Unit *y, S3L_Unit angle);
//============================================================================= //=============================================================================
// privates // privates
#define S3L_COMPUTE_LERP_DEPTH\
(S3L_COMPUTE_DEPTH && (S3L_PERSPECTIVE_CORRECTION != 1))
#define S3L_SIN_TABLE_LENGTH 128 #define S3L_SIN_TABLE_LENGTH 128
static const S3L_Unit S3L_sinTable[S3L_SIN_TABLE_LENGTH] = static const S3L_Unit S3L_sinTable[S3L_SIN_TABLE_LENGTH] =
{ {
@ -865,6 +868,7 @@ void S3L_initPixelInfo(S3L_PixelInfo *p) // TODO: maybe non-pointer for p
p->barycentric1 = 0; p->barycentric1 = 0;
p->barycentric2 = 0; p->barycentric2 = 0;
p->triangleID = 0; p->triangleID = 0;
p->depth = 0;
} }
void S3L_initDrawConfig(S3L_DrawConfig *config) void S3L_initDrawConfig(S3L_DrawConfig *config)
@ -1217,8 +1221,18 @@ void _S3L_drawFilledTriangle(
lErrAdd, rErrAdd, // error value to add in each Bresenham cycle lErrAdd, rErrAdd, // error value to add in each Bresenham cycle
lErrSub, rErrSub; // error value to substract when moving in x direction lErrSub, rErrSub; // error value to substract when moving in x direction
S3L_FastLerpState S3L_FastLerpState lSideFLS, rSideFLS;
lSideFLS, rSideFLS;
#if S3L_COMPUTE_LERP_DEPTH
S3L_FastLerpState lDepthFLS, rDepthFLS;
#define initDepthFLS(s,p1,p2)\
s##DepthFLS.valueScaled = p1##PointPP->z << S3L_FAST_LERP_QUALITY;\
s##DepthFLS.stepScaled = ((p2##PointPP->z << S3L_FAST_LERP_QUALITY) -\
s##DepthFLS.valueScaled) / (s##Dy != 0 ? s##Dy : 1);
#else
#define initDepthFLS(s,p1,p2) ;
#endif
/* init side for the algorithm, params: /* init side for the algorithm, params:
s - which side (l or r) s - which side (l or r)
@ -1229,6 +1243,7 @@ void _S3L_drawFilledTriangle(
s##X = p1##PointSx;\ s##X = p1##PointSx;\
s##Dx = p2##PointSx - p1##PointSx;\ s##Dx = p2##PointSx - p1##PointSx;\
s##Dy = p2##PointSy - p1##PointSy;\ s##Dy = p2##PointSy - p1##PointSy;\
initDepthFLS(s,p1,p2)\
s##SideFLS.stepScaled = (S3L_FRACTIONS_PER_UNIT << S3L_FAST_LERP_QUALITY)\ s##SideFLS.stepScaled = (S3L_FRACTIONS_PER_UNIT << S3L_FAST_LERP_QUALITY)\
/ (s##Dy != 0 ? s##Dy : 1);\ / (s##Dy != 0 ? s##Dy : 1);\
s##SideFLS.valueScaled = 0;\ s##SideFLS.valueScaled = 0;\
@ -1357,6 +1372,14 @@ void _S3L_drawFilledTriangle(
#else #else
S3L_FastLerpState b0FLS, b1FLS; S3L_FastLerpState b0FLS, b1FLS;
#if S3L_COMPUTE_LERP_DEPTH
S3L_FastLerpState depthFLS;
depthFLS.valueScaled = lDepthFLS.valueScaled;
depthFLS.stepScaled =
(rDepthFLS.valueScaled - lDepthFLS.valueScaled) / rowLength;
#endif
b0FLS.valueScaled = 0; b0FLS.valueScaled = 0;
b1FLS.valueScaled = lSideFLS.valueScaled; b1FLS.valueScaled = lSideFLS.valueScaled;
@ -1376,6 +1399,10 @@ void _S3L_drawFilledTriangle(
#if S3L_PERSPECTIVE_CORRECTION != 1 #if S3L_PERSPECTIVE_CORRECTION != 1
b0FLS.valueScaled -= lX * b0FLS.stepScaled; b0FLS.valueScaled -= lX * b0FLS.stepScaled;
b1FLS.valueScaled -= lX * b1FLS.stepScaled; b1FLS.valueScaled -= lX * b1FLS.stepScaled;
#if S3L_COMPUTE_LERP_DEPTH
depthFLS.valueScaled -= lX * depthFLS.stepScaled;
#endif
#endif #endif
} }
@ -1400,6 +1427,11 @@ void _S3L_drawFilledTriangle(
S3L_stepFastLerp(b0FLS); S3L_stepFastLerp(b0FLS);
S3L_stepFastLerp(b1FLS); S3L_stepFastLerp(b1FLS);
#if S3L_COMPUTE_LERP_DEPTH
p->depth = S3L_getFastLerpValue(depthFLS);
S3L_stepFastLerp(depthFLS);
#endif
#endif #endif
*barycentric2 = S3L_FRACTIONS_PER_UNIT - *barycentric0 - *barycentric1; *barycentric2 = S3L_FRACTIONS_PER_UNIT - *barycentric0 - *barycentric1;
@ -1412,6 +1444,11 @@ void _S3L_drawFilledTriangle(
S3L_stepFastLerp(lSideFLS); S3L_stepFastLerp(lSideFLS);
S3L_stepFastLerp(rSideFLS); S3L_stepFastLerp(rSideFLS);
#if S3L_COMPUTE_LERP_DEPTH
S3L_stepFastLerp(lDepthFLS);
S3L_stepFastLerp(rDepthFLS);
#endif
++currentY; ++currentY;
} }

View file

@ -12,6 +12,7 @@
#define S3L_RESOLUTION_X 640 #define S3L_RESOLUTION_X 640
#define S3L_RESOLUTION_Y 480 #define S3L_RESOLUTION_Y 480
#define S3L_COMPUTE_DEPTH 1
#define S3L_PERSPECTIVE_CORRECTION 0 #define S3L_PERSPECTIVE_CORRECTION 0
#include "s3l.h" #include "s3l.h"
@ -116,7 +117,11 @@ 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);
uint8_t sss = (p->depth / 5000.0) * 255 ;
setPixel(p->x,p->y,sss,sss,sss);
// setPixel(p->x,p->y,p->barycentric0 / ((float) S3L_FRACTIONS_PER_UNIT) * 255,p->barycentric1 / ((float) S3L_FRACTIONS_PER_UNIT) * 255,p->barycentric2 / ((float) S3L_FRACTIONS_PER_UNIT) * 255); // setPixel(p->x,p->y,p->barycentric0 / ((float) S3L_FRACTIONS_PER_UNIT) * 255,p->barycentric1 / ((float) S3L_FRACTIONS_PER_UNIT) * 255,p->barycentric2 / ((float) S3L_FRACTIONS_PER_UNIT) * 255);
} }