From addbcd42d7e5bb34d48bcbe9ee0830bb1fd76442 Mon Sep 17 00:00:00 2001 From: Stephan Unverwerth Date: Mon, 16 Aug 2021 19:25:16 +0200 Subject: [PATCH] LibGL: Fix triangle winding calculation Since we operate in screen space where y points down we need to reverse what is considered clock wise and what is considered counter clockwise. The rasterizer always expects triangles with a consistent winding order thus swap 2 vertices if necessary to reverse the winding before passing the triangle on to the rasterization stage. --- Userland/Applications/3DFileViewer/main.cpp | 2 +- Userland/Libraries/LibGL/SoftwareGLContext.cpp | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Userland/Applications/3DFileViewer/main.cpp b/Userland/Applications/3DFileViewer/main.cpp index 85744461bc..870381c97d 100644 --- a/Userland/Applications/3DFileViewer/main.cpp +++ b/Userland/Applications/3DFileViewer/main.cpp @@ -60,7 +60,7 @@ private: start_timer(20); GL::make_context_current(m_context); - glFrontFace(GL_CW); + glFrontFace(GL_CCW); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); diff --git a/Userland/Libraries/LibGL/SoftwareGLContext.cpp b/Userland/Libraries/LibGL/SoftwareGLContext.cpp index 70355668d2..45511b382a 100644 --- a/Userland/Libraries/LibGL/SoftwareGLContext.cpp +++ b/Userland/Libraries/LibGL/SoftwareGLContext.cpp @@ -246,7 +246,7 @@ void SoftwareGLContext::gl_end() continue; if (m_cull_faces) { - bool is_front = (m_front_face == GL_CCW ? area > 0 : area < 0); + bool is_front = (m_front_face == GL_CCW ? area < 0 : area > 0); if (is_front && (m_culled_sides == GL_FRONT || m_culled_sides == GL_FRONT_AND_BACK)) continue; @@ -255,6 +255,10 @@ void SoftwareGLContext::gl_end() continue; } + if (area > 0) { + swap(triangle.vertices[0], triangle.vertices[1]); + } + m_rasterizer.submit_triangle(triangle, m_texture_units); }