From 5cf967e4f29a9f48554b19674c899835564ccbc0 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Mon, 21 Feb 2022 01:31:24 +0100 Subject: [PATCH] LibGL: Improve `glFrustum` precision and error handling Do not convert to float too early. Additionally, handle some error cases for the input parameters. --- Userland/Libraries/LibGL/SoftwareGLContext.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibGL/SoftwareGLContext.cpp b/Userland/Libraries/LibGL/SoftwareGLContext.cpp index fe2b858b81..8ce1055fab 100644 --- a/Userland/Libraries/LibGL/SoftwareGLContext.cpp +++ b/Userland/Libraries/LibGL/SoftwareGLContext.cpp @@ -382,17 +382,18 @@ void SoftwareGLContext::gl_frustum(GLdouble left, GLdouble right, GLdouble botto APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_frustum, left, right, bottom, top, near_val, far_val); RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION); + RETURN_WITH_ERROR_IF(near_val < 0 || far_val < 0, GL_INVALID_VALUE); + RETURN_WITH_ERROR_IF(left == right || bottom == top || near_val == far_val, GL_INVALID_VALUE); // Let's do some math! - // FIXME: Are we losing too much precision by doing this? - float a = static_cast((right + left) / (right - left)); - float b = static_cast((top + bottom) / (top - bottom)); - float c = static_cast(-((far_val + near_val) / (far_val - near_val))); - float d = static_cast(-((2 * (far_val * near_val)) / (far_val - near_val))); + auto a = static_cast((right + left) / (right - left)); + auto b = static_cast((top + bottom) / (top - bottom)); + auto c = static_cast(-((far_val + near_val) / (far_val - near_val))); + auto d = static_cast(-((2 * far_val * near_val) / (far_val - near_val))); FloatMatrix4x4 frustum { - ((2 * (float)near_val) / ((float)right - (float)left)), 0, a, 0, - 0, ((2 * (float)near_val) / ((float)top - (float)bottom)), b, 0, + static_cast(2 * near_val / (right - left)), 0, a, 0, + 0, static_cast(2 * near_val / (top - bottom)), b, 0, 0, 0, c, d, 0, 0, -1, 0 };