From f9fb4a0a1742fddc32a4e1a4e0608a7b3f46d0a9 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Wed, 29 Nov 2017 00:12:15 +0100 Subject: [PATCH] - Cull back facing triangles for the span drawers --- src/polyrenderer/drawers/poly_triangle.cpp | 13 +++++++++++-- src/polyrenderer/drawers/poly_triangle.h | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/polyrenderer/drawers/poly_triangle.cpp b/src/polyrenderer/drawers/poly_triangle.cpp index 386c4c6304..990ca581db 100644 --- a/src/polyrenderer/drawers/poly_triangle.cpp +++ b/src/polyrenderer/drawers/poly_triangle.cpp @@ -243,6 +243,15 @@ bool PolyTriangleDrawer::is_degenerate(const ShadedTriVertex *vert) return crosslengthsqr <= 1.e-6f; } +bool PolyTriangleDrawer::is_frontfacing(TriDrawTriangleArgs *args) +{ + float a = + args->v1->x * args->v2->y - args->v2->x * args->v1->y + + args->v2->x * args->v3->y - args->v3->x * args->v2->y + + args->v3->x * args->v1->y - args->v1->x * args->v3->y; + return a <= 0.0f; +} + void PolyTriangleDrawer::draw_shaded_triangle(const ShadedTriVertex *vert, bool ccw, TriDrawTriangleArgs *args, WorkerThreadData *thread) { // Reject triangle if degenerate @@ -324,7 +333,7 @@ void PolyTriangleDrawer::draw_shaded_triangle(const ShadedTriVertex *vert, bool args->v1 = &clippedvert[numclipvert - 1]; args->v2 = &clippedvert[i - 1]; args->v3 = &clippedvert[i - 2]; - if (args->CalculateGradients()) + if (is_frontfacing(args) && args->CalculateGradients()) ScreenTriangle::Draw(args, thread); } } @@ -335,7 +344,7 @@ void PolyTriangleDrawer::draw_shaded_triangle(const ShadedTriVertex *vert, bool args->v1 = &clippedvert[0]; args->v2 = &clippedvert[i - 1]; args->v3 = &clippedvert[i]; - if (args->CalculateGradients()) + if (!is_frontfacing(args) && args->CalculateGradients()) ScreenTriangle::Draw(args, thread); } } diff --git a/src/polyrenderer/drawers/poly_triangle.h b/src/polyrenderer/drawers/poly_triangle.h index eea3b8531f..7ddfbed19a 100644 --- a/src/polyrenderer/drawers/poly_triangle.h +++ b/src/polyrenderer/drawers/poly_triangle.h @@ -44,6 +44,7 @@ private: static void draw_arrays(const PolyDrawArgs &args, WorkerThreadData *thread); static void draw_shaded_triangle(const ShadedTriVertex *vertices, bool ccw, TriDrawTriangleArgs *args, WorkerThreadData *thread); static bool is_degenerate(const ShadedTriVertex *vertices); + static bool is_frontfacing(TriDrawTriangleArgs *args); static int clipedge(const ShadedTriVertex *verts, ShadedTriVertex *clippedvert);