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

Create triangle suboutine

This commit is contained in:
Miloslav Číž 2019-05-06 19:19:24 +02:00
parent d4872e6984
commit 9881fadb94

69
s3l.h
View file

@ -627,29 +627,12 @@ int S3L_bresenhamStep(S3L_BresenhamState *state)
return state->steps >= 0; return state->steps >= 0;
} }
void S3L_drawTriangle( void _S3L_drawFilledTriangle(
S3L_ScreenCoord x0, S3L_ScreenCoord y0, S3L_ScreenCoord x0, S3L_ScreenCoord y0,
S3L_ScreenCoord x1, S3L_ScreenCoord y1, S3L_ScreenCoord x1, S3L_ScreenCoord y1,
S3L_ScreenCoord x2, S3L_ScreenCoord y2, S3L_ScreenCoord x2, S3L_ScreenCoord y2,
S3L_DrawConfig config, S3L_PixelInfo *p)
S3L_Index triangleID)
{ {
if (config.backfaceCulling != S3L_BACKFACE_CULLING_NONE)
{
int cw = // matrix determinant
x0 * y1 + y0 * x2 + x1 * y2 - y1 * x2 - y0 * x1 - x0 * y2 > 0;
if ((config.backfaceCulling == S3L_BACKFACE_CULLING_CW && !cw) ||
(config.backfaceCulling == S3L_BACKFACE_CULLING_CCW && cw))
return;
}
S3L_PixelInfo p;
S3L_initPixelInfo(&p);
p.triangleID = triangleID;
if (config.mode == S3L_MODE_TRIANGLES) // triangle mode
{
S3L_ScreenCoord S3L_ScreenCoord
tPointX, tPointY, // top triangle point coords tPointX, tPointY, // top triangle point coords
lPointX, lPointY, // left triangle point coords lPointX, lPointY, // left triangle point coords
@ -671,15 +654,15 @@ void S3L_drawTriangle(
{\ {\
lPointX = x##a; lPointY = y##a;\ lPointX = x##a; lPointY = y##a;\
rPointX = x##b; rPointY = y##b;\ rPointX = x##b; rPointY = y##b;\
barycentric0 = &p.barycentric##b;\ barycentric0 = &(p->barycentric##b);\
barycentric1 = &p.barycentric##a;\ barycentric1 = &(p->barycentric##a);\
}\ }\
else\ else\
{\ {\
lPointX = x##b; lPointY = y##b;\ lPointX = x##b; lPointY = y##b;\
rPointX = x##a; rPointY = y##a;\ rPointX = x##a; rPointY = y##a;\
barycentric0 = &p.barycentric##a;\ barycentric0 = &(p->barycentric##a);\
barycentric1 = &p.barycentric##b;\ barycentric1 = &(p->barycentric##b);\
} }
if (y0 <= y1) if (y0 <= y1)
@ -688,14 +671,14 @@ void S3L_drawTriangle(
{ {
tPointX = x0; tPointX = x0;
tPointY = y0; tPointY = y0;
barycentric2 = &p.barycentric0; barycentric2 = &(p->barycentric0);
handleLR(0,1,2) handleLR(0,1,2)
} }
else else
{ {
tPointX = x2; tPointX = x2;
tPointY = y2; tPointY = y2;
barycentric2 = &p.barycentric2; barycentric2 = &(p->barycentric2);
handleLR(2,0,1) handleLR(2,0,1)
} }
} }
@ -705,14 +688,14 @@ void S3L_drawTriangle(
{ {
tPointX = x1; tPointX = x1;
tPointY = y1; tPointY = y1;
barycentric2 = &p.barycentric1; barycentric2 = &(p->barycentric1);
handleLR(1,0,2) handleLR(1,0,2)
} }
else else
{ {
tPointX = x2; tPointX = x2;
tPointY = y2; tPointY = y2;
barycentric2 = &p.barycentric2; barycentric2 = &(p->barycentric2);
handleLR(2,0,1) handleLR(2,0,1)
} }
} }
@ -851,7 +834,7 @@ void S3L_drawTriangle(
stepSide(r) stepSide(r)
stepSide(l) stepSide(l)
p.y = currentY; p->y = currentY;
// draw the horizontal line // draw the horizontal line
@ -869,8 +852,8 @@ void S3L_drawTriangle(
*barycentric1 = b1 >> S3L_LERP_QUALITY; *barycentric1 = b1 >> S3L_LERP_QUALITY;
*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);
b0 += b0Step; b0 += b0Step;
b1 -= b1Step; b1 -= b1Step;
@ -884,6 +867,32 @@ void S3L_drawTriangle(
#undef initSide #undef initSide
#undef stepSide #undef stepSide
}
void S3L_drawTriangle(
S3L_ScreenCoord x0, S3L_ScreenCoord y0,
S3L_ScreenCoord x1, S3L_ScreenCoord y1,
S3L_ScreenCoord x2, S3L_ScreenCoord y2,
S3L_DrawConfig config,
S3L_Index triangleID)
{
if (config.backfaceCulling != S3L_BACKFACE_CULLING_NONE)
{
int cw = // matrix determinant
x0 * y1 + y0 * x2 + x1 * y2 - y1 * x2 - y0 * x1 - x0 * y2 > 0;
if ((config.backfaceCulling == S3L_BACKFACE_CULLING_CW && !cw) ||
(config.backfaceCulling == S3L_BACKFACE_CULLING_CCW && cw))
return;
}
S3L_PixelInfo p;
S3L_initPixelInfo(&p);
p.triangleID = triangleID;
if (config.mode == S3L_MODE_TRIANGLES) // triangle mode
{
_S3L_drawFilledTriangle(x0,y0,x1,y1,x2,y2,&p);
} }
else if (config.mode == S3L_MODE_LINES) // line mode else if (config.mode == S3L_MODE_LINES) // line mode
{ {