diff options
author | Jesse Buhagiar <jooster669@gmail.com> | 2022-01-15 16:16:01 +1100 |
---|---|---|
committer | Idan Horowitz <idan.horowitz@gmail.com> | 2022-01-18 01:48:51 +0200 |
commit | 865e7bbe5e76c701cfdbdbc917b496a0c26c8216 (patch) | |
tree | 0439d91246c19637520881bafa324316d418f3c2 /Userland/Libraries/LibSoftGPU | |
parent | 5bb8c14c8ffd95825eef14030aa3b2bea921108e (diff) | |
download | serenity-865e7bbe5e76c701cfdbdbc917b496a0c26c8216.zip |
LibGL+LibSoftGPU+3DFileViewer: Implement Specular highlighting :^)
Diffstat (limited to 'Userland/Libraries/LibSoftGPU')
-rw-r--r-- | Userland/Libraries/LibSoftGPU/Device.cpp | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/Userland/Libraries/LibSoftGPU/Device.cpp b/Userland/Libraries/LibSoftGPU/Device.cpp index 19ecf5348c..3db2d7320e 100644 --- a/Userland/Libraries/LibSoftGPU/Device.cpp +++ b/Userland/Libraries/LibSoftGPU/Device.cpp @@ -856,9 +856,6 @@ void Device::draw_primitives(PrimitiveType primitive_type, FloatMatrix4x4 const& spotlight_factor = 0.0f; } - // FIXME: Specular. The math for it doesn't quite make sense... - (void)m_lighting_model.viewer_at_infinity; - // FIXME: The spec allows for splitting the colors calculated here into multiple different colors (primary/secondary color). Investigate what this means. (void)m_lighting_model.single_color; @@ -870,10 +867,27 @@ void Device::draw_primitives(PrimitiveType primitive_type, FloatMatrix4x4 const& // Diffuse auto const normal_dot_vertex_to_light = sgi_dot_operator(vertex.normal, vertex_to_light); - auto const diffuse_component = ((diffuse * light.diffuse_intensity) * normal_dot_vertex_to_light).clamped(0.0f, 1.0f); + auto const diffuse_component = ((diffuse * light.diffuse_intensity) * normal_dot_vertex_to_light); + + // Specular + FloatVector4 specular_component = { 0.0f, 0.0f, 0.0f, 0.0f }; + if (normal_dot_vertex_to_light > 0.0f) { + FloatVector3 half_vector_normalized; + if (!m_lighting_model.viewer_at_infinity) { + half_vector_normalized = (vertex_to_light + FloatVector3(0.0f, 0.0f, 1.0f)).normalized(); + } else { + auto const vertex_to_eye_point = sgi_arrow_operator(vertex.eye_coordinates.normalized(), FloatVector4(0.0f, 0.0f, 0.0f, 1.0f), vector_length); + half_vector_normalized = vertex_to_light + vertex_to_eye_point; + } + + auto const normal_dot_half_vector = sgi_dot_operator(vertex.normal.normalized(), half_vector_normalized); + auto const specular_coefficient = AK::pow(normal_dot_half_vector, material.shininess); + specular_component = (specular * light.specular_intensity) * specular_coefficient; + } FloatVector4 color = ambient_component; color += diffuse_component; + color += specular_component; color = color * light_attenuation_factor * spotlight_factor; result_color += color; } |