diff --git a/s3l.h b/s3l.h index f88b927..209acd5 100644 --- a/s3l.h +++ b/s3l.h @@ -1,3 +1,10 @@ +/* + WIP simple realtime 3D rasterization-based library. + + author: Miloslav Ciz + license: CC0 1.0 +*/ + #ifndef S3L_H #define S3L_H @@ -112,7 +119,8 @@ static inline int16_t S3L_interpolateFrom0(int16_t v2, int16_t t, int16_t tMax) return (v2 * t) / tMax; } -void S3L_bresenhamInit(S3L_BresenhamState *state, int16_t x0, int16_t y0, int16_t x1, int16_t y1) +void S3L_bresenhamInit(S3L_BresenhamState *state, int16_t x0, int16_t y0, + int16_t x1, int16_t y1) { int16_t dx = x1 - x0; int16_t dy = y1 - y0; @@ -170,12 +178,23 @@ int S3L_bresenhamStep(S3L_BresenhamState *state) return state->steps >= 0; } -void S3L_drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, S3L_DrawConfig config) +void S3L_drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, + int16_t x2, int16_t y2, S3L_DrawConfig config) { + 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_COORD - tPointX, tPointY, // top triangle point coords - lPointX, lPointY, // left triangle point coords - rPointX, rPointY; // right triangle point coords + tPointX, tPointY, // top triangle point coords + lPointX, lPointY, // left triangle point coords + rPointX, rPointY; // right triangle point coords S3L_PixelInfo p; @@ -245,7 +264,7 @@ void S3L_drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2 } } - // Now drive the triangle line by line. + // Now draw the triangle line by line. #undef handleLR diff --git a/testSDL.c b/testSDL.c index 95c5a50..e813e53 100644 --- a/testSDL.c +++ b/testSDL.c @@ -62,7 +62,6 @@ S3L_DrawConfig conf; conf.backfaceCulling = S3L_BACKFACE_CULLING_NONE; - for (int c = 0; c < 7; ++c) { int @@ -73,19 +72,17 @@ conf.backfaceCulling = S3L_BACKFACE_CULLING_NONE; x2 = test_coords[6 * c + 4], y2 = test_coords[6 * c + 5]; -//int cent = (x0 + x1 + x2) / 3.0; -//x1 = cent + (x1 - cent) * sin(frame * 0.01); +int cent = (x0 + x1 + x2) / 3.0; +x2 = cent + (x2 - cent) * sin(frame * 0.01) * 2; S3L_drawTriangle(x0,y0,x1,y1,x2,y2,conf); - setPixel(x0,y0,255,0,0); setPixel(x1,y1,255,0,0); setPixel(x2,y2,255,0,0); } - int16_t rotX0 = 200 + sin(frame * 0.01) * 50; int16_t rotY0 = 200 + cos(frame * 0.01) * 50;