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

Add triangle clipping

This commit is contained in:
Miloslav Číž 2019-05-11 17:19:41 +02:00
parent 78fe74caf3
commit 229c3f465a
2 changed files with 79 additions and 51 deletions

30
s3l.h
View file

@ -1022,6 +1022,13 @@ void _S3L_drawFilledTriangle(
initPC(t,r,r) initPC(t,r,r)
#endif #endif
// clip to the screen in y dimension:
endY = S3L_min(endY,S3L_RESOLUTION_Y);
/* Clipping above the screen (y < 0) can't be easily done here, will be
handled inside the loop. */
while (currentY < endY) /* draw the triangle from top to bottom -- the while (currentY < endY) /* draw the triangle from top to bottom -- the
bottom-most row is left out because, following bottom-most row is left out because, following
from the rasterization rules (see top of the from the rasterization rules (see top of the
@ -1062,6 +1069,9 @@ void _S3L_drawFilledTriangle(
stepSide(r) stepSide(r)
stepSide(l) stepSide(l)
if (currentY >= 0) /* clipping of pixels whose y < 0 (can't be easily done
outside the loop) */
{
p->y = currentY; p->y = currentY;
// draw the horizontal line // draw the horizontal line
@ -1097,7 +1107,24 @@ void _S3L_drawFilledTriangle(
S3L_Unit b1Step = lSideUnitPosScaled / rowLength; S3L_Unit b1Step = lSideUnitPosScaled / rowLength;
#endif #endif
for (S3L_ScreenCoord x = lX; x < rX; ++x) // clip to the screen in x dimension:
S3L_ScreenCoord rXClipped = S3L_min(rX,S3L_RESOLUTION_X),
lXClipped = lX;
if (lXClipped < 0)
{
lXClipped = 0;
#if S3L_PERSPECTIVE_CORRECTION != 1
b0 -= lX * b0Step;
b1 += lX * b1Step;
#endif
}
// draw the row:
for (S3L_ScreenCoord x = lXClipped; x < rXClipped; ++x)
{ {
#if S3L_PERSPECTIVE_CORRECTION == 1 #if S3L_PERSPECTIVE_CORRECTION == 1
@ -1120,6 +1147,7 @@ void _S3L_drawFilledTriangle(
p->x = x; p->x = x;
S3L_PIXEL_FUNCTION(p); S3L_PIXEL_FUNCTION(p);
} }
} // y clipping
lSideUnitPosScaled += lSideStep; lSideUnitPosScaled += lSideStep;
rSideUnitPosScaled += rSideStep; rSideUnitPosScaled += rSideStep;

View file

@ -132,8 +132,8 @@ void draw()
modelTransform.rotation.z = frame * 0.1; modelTransform.rotation.z = frame * 0.1;
modelTransform.rotation.x = frame * 0.3; modelTransform.rotation.x = frame * 0.3;
// modelTransform.translation.x = sin(frame >> 3) * 700; modelTransform.translation.x = sin(frame >> 7) * 700;
// modelTransform.translation.y = sin(frame >> 4) * 600; modelTransform.translation.y = sin(frame >> 8) * 600;
S3L_drawModelIndexed(ver,tri,12,modelTransform,&camera,&conf); S3L_drawModelIndexed(ver,tri,12,modelTransform,&camera,&conf);