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

Add BF culling

This commit is contained in:
Miloslav Číž 2018-11-18 15:59:58 +01:00
parent c3a70d6f7e
commit 2eff3913f4
2 changed files with 27 additions and 11 deletions

31
s3l.h
View file

@ -1,3 +1,10 @@
/*
WIP simple realtime 3D rasterization-based library.
author: Miloslav Ciz
license: CC0 1.0
*/
#ifndef S3L_H #ifndef S3L_H
#define 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; 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 dx = x1 - x0;
int16_t dy = y1 - y0; int16_t dy = y1 - y0;
@ -170,12 +178,23 @@ int S3L_bresenhamStep(S3L_BresenhamState *state)
return state->steps >= 0; 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 S3L_COORD
tPointX, tPointY, // top triangle point coords tPointX, tPointY, // top triangle point coords
lPointX, lPointY, // left triangle point coords lPointX, lPointY, // left triangle point coords
rPointX, rPointY; // right triangle point coords rPointX, rPointY; // right triangle point coords
S3L_PixelInfo p; 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 #undef handleLR

View file

@ -62,7 +62,6 @@ S3L_DrawConfig conf;
conf.backfaceCulling = S3L_BACKFACE_CULLING_NONE; conf.backfaceCulling = S3L_BACKFACE_CULLING_NONE;
for (int c = 0; c < 7; ++c) for (int c = 0; c < 7; ++c)
{ {
int int
@ -73,19 +72,17 @@ conf.backfaceCulling = S3L_BACKFACE_CULLING_NONE;
x2 = test_coords[6 * c + 4], x2 = test_coords[6 * c + 4],
y2 = test_coords[6 * c + 5]; y2 = test_coords[6 * c + 5];
//int cent = (x0 + x1 + x2) / 3.0; int cent = (x0 + x1 + x2) / 3.0;
//x1 = cent + (x1 - cent) * sin(frame * 0.01); x2 = cent + (x2 - cent) * sin(frame * 0.01) * 2;
S3L_drawTriangle(x0,y0,x1,y1,x2,y2,conf); S3L_drawTriangle(x0,y0,x1,y1,x2,y2,conf);
setPixel(x0,y0,255,0,0); setPixel(x0,y0,255,0,0);
setPixel(x1,y1,255,0,0); setPixel(x1,y1,255,0,0);
setPixel(x2,y2,255,0,0); setPixel(x2,y2,255,0,0);
} }
int16_t rotX0 = 200 + sin(frame * 0.01) * 50; int16_t rotX0 = 200 + sin(frame * 0.01) * 50;
int16_t rotY0 = 200 + cos(frame * 0.01) * 50; int16_t rotY0 = 200 + cos(frame * 0.01) * 50;