diff options
author | chai <chaifix@163.com> | 2020-07-28 00:30:33 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2020-07-28 00:30:33 +0800 |
commit | 5dfdb6b58b2dc7bbd3348004c1fcd17e23fea48b (patch) | |
tree | a03f1a01382595ed50cc4780a728b22013f6d5a0 /src/core/device.c | |
parent | 690f20cdbe8dad3fce7a547934a5a8322303342d (diff) |
Diffstat (limited to 'src/core/device.c')
-rw-r--r-- | src/core/device.c | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/src/core/device.c b/src/core/device.c index 5ff39ef..b810c04 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -500,7 +500,23 @@ static struct { extern ClippedBuffer clip_buffer; /*clipping result*/ static bool is_back_face(Vec4* c0, Vec4* c1, Vec4*c2) { +#define OPENGL_BACKFACE_CULLING 0 /*cull in ndc*/ +#if OPENGL_BACKFACE_CULLING + /*opengl backface culling algrithm*/ + float w0 = max(1 / c0->w, 0), w1 = 1 / c1->w, w2 = 1 / c2->w; + Vec2 a = { c0->x * w0, c0->y * w0 }; + Vec2 b = { c1->x * w1, c1->y * w1 }; + Vec2 c = { c2->x * w2, c2->y * w2 }; + float signed_area = 0; + signed_area += a.x * b.y - a.y * b.x; + signed_area += b.x * c.y - b.y * c.x; + signed_area += c.x * a.y - c.y * a.x; + if (signed_area <= 0) { + return TRUE; + } + return FALSE; +#else float w0 = 1 / c0->w, w1 = 1 / c1->w, w2 = 1 / c2->w; bool all_front = w0 > 0 && w1 > 0 && w2 > 0; if (all_front) @@ -511,25 +527,11 @@ static bool is_back_face(Vec4* c0, Vec4* c1, Vec4*c2) { ac.x = c2->x * w2 - c0->x * w0; ac.y = c2->y * w2 - c0->y * w0; if (ab.x * ac.y - ab.y * ac.x <= 0) { - return TRUE; + return TRUE; } } return FALSE; - /*OpenGL algrithm*/ - /* - float w0 = max(1 / c0->w, 0), w1 = 1 / c1->w, w2 = 1 / c2->w; - Vec2 a = { c0->x * w0, c0->y * w0 }; - Vec2 b = { c1->x * w1, c1->y * w1 }; - Vec2 c = { c2->x * w2, c2->y * w2 }; - float signed_area = 0; - signed_area += a.x * b.y - a.y * b.x; - signed_area += b.x * c.y - b.y * c.x; - signed_area += c.x * a.y - c.y * a.x; - if (signed_area <= 0) { - return TRUE; - } - return FALSE; - */ +#endif } static void render_prims_triangle(uint varying_flag) { |