diff options
author | Jelle Raaijmakers <jelle@gmta.nl> | 2022-12-12 00:32:11 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-12-20 10:42:31 +0100 |
commit | 8c094699dbb9e409e6ee0d82412f1f81372e35fe (patch) | |
tree | 873e01cea8aecde9a656cd59fdc398f05980382c /Userland/Libraries | |
parent | a074b7e871c3776e66abfb676051f3fefbf53fd7 (diff) | |
download | serenity-8c094699dbb9e409e6ee0d82412f1f81372e35fe.zip |
LibGL: Implement `glLightModel` integer normalization
For the ambient light model, integers need to be remapped to a range of
`-1.` through `1.`. Add the `+` and `-` operators to `VectorN` to make
it a bit easier to normalize 4 values at once.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibGL/Lighting.cpp | 35 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/VectorN.h | 20 |
2 files changed, 36 insertions, 19 deletions
diff --git a/Userland/Libraries/LibGL/Lighting.cpp b/Userland/Libraries/LibGL/Lighting.cpp index 222e28baa5..2e5409d9d0 100644 --- a/Userland/Libraries/LibGL/Lighting.cpp +++ b/Userland/Libraries/LibGL/Lighting.cpp @@ -193,26 +193,23 @@ void GLContext::gl_light_model(GLenum pname, GLfloat x, GLfloat y, GLfloat z, GL void GLContext::gl_light_modelv(GLenum pname, void const* params, GLenum type) { - auto invoke_implementation = [&](auto const* params) { - switch (pname) { - case GL_LIGHT_MODEL_AMBIENT: - gl_light_model(pname, params[0], params[1], params[2], params[3]); - return; - default: - gl_light_model(pname, params[0], 0.f, 0.f, 0.f); - return; - } + VERIFY(type == GL_FLOAT || type == GL_INT); + + auto parameters_to_vector = [&]<typename T>(T const* params) -> FloatVector4 { + return (pname == GL_LIGHT_MODEL_AMBIENT) + ? Vector4<T> { params[0], params[1], params[2], params[3] }.template to_type<float>() + : Vector4<T> { params[0], 0, 0, 0 }.template to_type<float>(); }; - switch (type) { - case GL_FLOAT: - invoke_implementation(reinterpret_cast<GLfloat const*>(params)); - break; - case GL_INT: - invoke_implementation(reinterpret_cast<GLint const*>(params)); - break; - default: - VERIFY_NOT_REACHED(); - } + + auto light_model_parameters = (type == GL_FLOAT) + ? parameters_to_vector(reinterpret_cast<GLfloat const*>(params)) + : parameters_to_vector(reinterpret_cast<GLint const*>(params)); + + // Normalize integers to -1..1 + if (pname == GL_LIGHT_MODEL_AMBIENT && type == GL_INT) + light_model_parameters = (light_model_parameters + 2147483648.f) / 2147483647.5f - 1.f; + + gl_light_model(pname, light_model_parameters[0], light_model_parameters[1], light_model_parameters[2], light_model_parameters[3]); } void GLContext::gl_lightf(GLenum light, GLenum pname, GLfloat param) diff --git a/Userland/Libraries/LibGfx/VectorN.h b/Userland/Libraries/LibGfx/VectorN.h index 7e67a72695..687c0ea7df 100644 --- a/Userland/Libraries/LibGfx/VectorN.h +++ b/Userland/Libraries/LibGfx/VectorN.h @@ -159,6 +159,26 @@ public: } template<typename U> + [[nodiscard]] constexpr VectorN operator+(U f) const + { + VectorN result; + UNROLL_LOOP + for (auto i = 0u; i < N; ++i) + result.m_data[i] = m_data[i] + f; + return result; + } + + template<typename U> + [[nodiscard]] constexpr VectorN operator-(U f) const + { + VectorN result; + UNROLL_LOOP + for (auto i = 0u; i < N; ++i) + result.m_data[i] = m_data[i] - f; + return result; + } + + template<typename U> [[nodiscard]] constexpr VectorN operator*(U f) const { VectorN result; |