From 240119353ea903aa1c115271a92c92f986c92cc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Tue, 28 May 2019 18:51:12 +0200 Subject: [PATCH] Add near clamping --- small3dlib.h | 19 ++++++++++++++++--- testSDL.c | 37 ++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/small3dlib.h b/small3dlib.h index c8491b9..8acce9b 100644 --- a/small3dlib.h +++ b/small3dlib.h @@ -122,9 +122,17 @@ #endif #ifndef S3L_COMPUTE_DEPTH -#define S3L_COMPUTE_DEPTH 0 /**< Whether to compute depth for each pixel - (fragment). Some other options may turn this - on. */ +#define S3L_COMPUTE_DEPTH 0 /**< Whether to compute depth for each pixel + (fragment). Some other options may turn this + on. */ +#endif + +#ifndef S3L_NEAR_CLAMPING +#define S3L_NEAR_CLAMPING 0 /**< Whether to use depth clamping for the near + plane. Only works with S3L_COMPUTE_DEPTH + enabled! This may be a bit slower, but can + prevent errorneous rendering in specific cases + and is closer to traditional 3D engines. */ #endif #ifndef S3L_PERSPECTIVE_CORRECTION @@ -1599,6 +1607,11 @@ void _S3L_drawFilledTriangle( p->depth = S3L_getFastLerpValue(depthFLS); S3L_stepFastLerp(depthFLS); #endif + + #if S3L_NEAR_CLAMPING + if (p->depth < S3L_NEAR) + continue; + #endif #endif #if S3L_Z_BUFFER diff --git a/testSDL.c b/testSDL.c index 0f98501..334a669 100644 --- a/testSDL.c +++ b/testSDL.c @@ -19,26 +19,14 @@ #define S3L_RESOLUTION_Y 480 #define S3L_COMPUTE_DEPTH 1 -#define S3L_PERSPECTIVE_CORRECTION 0 +#define S3L_PERSPECTIVE_CORRECTION 1 +#define S3L_NEAR_CLAMPING 1 #include "small3dlib.h" #include "house.h" int32_t offScreenPixels = 0; - -const int16_t test_coords[] = - { - 100,100, 99,101, 101,101, // 0, small triangle - 190,50, 200,10, 400,80, // 1, arbitrary - 40,80, 60,50, 100,30, // 2, arbitrary - 350,270, 440,200, 490,220, // 3, arbitrary - 150,300, 290,400, 450,400, // 4, regular - 105,200, 120,200, 201,200, // 5, horizontal line - 300,200, 300,250, 300,220, // 6, vertical line - 496,15, 613,131, 552,203 - }; - const S3L_Unit ver[] = { S3L_CUBE_VERTICES }; const S3L_Index tri[] = { S3L_CUBE_TRIANGLES }; const S3L_Unit tex_coords[] = { S3L_CUBE_TEXCOORDS(16) }; @@ -114,6 +102,18 @@ void drawPixel(S3L_PixelInfo *p) const S3L_Unit *coords; coords = tex_coords + p->triangleID * 6; + + u = S3L_interpolateBarycentric( + 0, + 0, + 15, + p->barycentric0, p->barycentric1, p->barycentric2); + + v = S3L_interpolateBarycentric( + 0, + 15, + 0, + p->barycentric0, p->barycentric1, p->barycentric2); /* u = S3L_interpolateBarycentric( coords[0], @@ -127,13 +127,12 @@ void drawPixel(S3L_PixelInfo *p) coords[5], p->barycentric0, p->barycentric1, p->barycentric2); */ -// uint8_t col = texturePixel(u,v); + uint8_t col = texturePixel(u,v); + uint8_t dep = (p->depth / 5000.0) * 255; -// setPixel(p->x,p->y,col * 120,20,(2 - col) * 120); + setPixel(p->x,p->y,col * 120,dep,(2 - col) * 120); -uint8_t sss = (p->depth / 5000.0) * 255 ; - -setPixel(p->x,p->y,sss, (p->triangleID * 37) % 255 ,128); +//setPixel(p->x,p->y,sss, (p->triangleID * 37) % 255 ,128); //setPixel(p->x,p->y,p->modelID * 64,p->modelID * 128,255);