From 51c76da92753058674dda62e19d5c21bfabe3659 Mon Sep 17 00:00:00 2001 From: Hirek193 Date: Mon, 23 Nov 2026 20:55:50 +0100 Subject: [PATCH] fix(render): the free camera blurs with its own movement in the OpenGL 4.6 and Vulkan renderers --- CMakeLists.txt | 1 + model/Texture.cpp | 2 - rendering/common/freecameramotion.cpp | 27 ++++++++++++ rendering/common/freecameramotion.h | 45 ++++++++++++++++++++ rendering/opengl46/opengl46renderer.cpp | 15 ++++++- rendering/opengl46/opengl46renderer.h | 3 ++ rendering/vulkan14/vulkan14frame.cpp | 14 ++++++- rendering/vulkan14/vulkan14renderer.h | 3 ++ tests/unit/CMakeLists.txt | 2 + tests/unit/test_free_camera_motion.cpp | 56 +++++++++++++++++++++++++ 10 files changed, 164 insertions(+), 4 deletions(-) create mode 100644 rendering/common/freecameramotion.cpp create mode 100644 rendering/common/freecameramotion.h create mode 100644 tests/unit/test_free_camera_motion.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c9540572..d9ef586f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -364,6 +364,7 @@ set(SOURCES "rendering/common/weatherstate.cpp" "rendering/common/exposure.cpp" "rendering/common/temporalaa.cpp" +"rendering/common/freecameramotion.cpp" "rendering/common/screenreflections.cpp" "gl/buffer.cpp" "gl/fence.cpp" diff --git a/model/Texture.cpp b/model/Texture.cpp index 3f5fd9b2b..efd9e711b 100644 --- a/model/Texture.cpp +++ b/model/Texture.cpp @@ -686,8 +686,6 @@ void opengl_texture::upload_from_thread(size_t width, size_t height, const uint8 set_filtering(); glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_mode_s); glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_mode_t); - if (!glGenerateMipmap) - glTexParameteri(target, GL_GENERATE_MIPMAP, GL_TRUE); glTexImage2D(target, 0, GL_SRGB8_ALPHA8, static_cast(width), static_cast(height), 0, GL_RGBA, GL_UNSIGNED_BYTE, raw); if (glGenerateMipmap) glGenerateMipmap(target); diff --git a/rendering/common/freecameramotion.cpp b/rendering/common/freecameramotion.cpp new file mode 100644 index 000000000..b1fd226b2 --- /dev/null +++ b/rendering/common/freecameramotion.cpp @@ -0,0 +1,27 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +#include "stdafx.h" +#include "rendering/common/freecameramotion.h" + +glm::vec3 gfx::free_camera_motion::next_frame(glm::dvec3 const &Position) +{ + glm::vec3 movement{0.f}; + if (m_previous) + { + auto const step{glm::vec3{Position - *m_previous}}; + // a position that isn't a number (a camera gone wrong) fails the comparison and gives no movement either + if (auto const length{glm::length(step)}; length < static_cast(jump)) + { + movement = (length > longest) ? step * (longest / length) : step; + } + } + m_previous = Position; + return movement; +} diff --git a/rendering/common/freecameramotion.h b/rendering/common/freecameramotion.h new file mode 100644 index 000000000..314582c6c --- /dev/null +++ b/rendering/common/freecameramotion.h @@ -0,0 +1,45 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +// the motion blur of the camera with no vehicle of its own (freefly, the editor): the world is blurred by the movement of the +// camera, as it is by the movement of the vehicle the camera sits in. without it the ground racing under a low camera strobes in +// rings, at the distances its texture repeats. the movement from the frame before to this one stands for the one to come + +#pragma once + +#include +#include + +namespace gfx +{ + +class free_camera_motion +{ + public: + // from this far in a frame on the camera jumped (a camera preset, another place of the scenery), which is no movement + static constexpr double jump{50.0}; + // the longest movement of a frame the blur is given. it draws the scene where the camera will be: a faster camera (shift, ctrl) + // passes the ground at the bottom of the screen within a frame, ends up behind it there, and its motion turns over into a + // bright band. half a metre a frame still blurs metres with the shutter of the motion blur + static constexpr float longest{0.5f}; + + // the camera of the frame. gives its movement since the frame before, no longer than the longest one; none in the first frame + // and after a jump + glm::vec3 next_frame(glm::dvec3 const &Position); + // the camera sits in a vehicle: the free camera starts anew the next time + void reset() + { + m_previous.reset(); + } + + private: + std::optional m_previous; +}; + +} // namespace gfx diff --git a/rendering/opengl46/opengl46renderer.cpp b/rendering/opengl46/opengl46renderer.cpp index 656425fa0..4180d3b12 100644 --- a/rendering/opengl46/opengl46renderer.cpp +++ b/rendering/opengl46/opengl46renderer.cpp @@ -1738,13 +1738,20 @@ void opengl46_renderer::Render_pass(viewport_config &vp, rendermode const Mode) if (vp.main && !vp.custom_backbuffer) // TODO: update window sizes also for extra viewports target_size = Global.fb_size; - // the motion blur only when something in the view moves: the vehicle of the camera, or a vehicle drawn in the last frame. + // the camera with no vehicle of its own (freefly, the editor) blurs the world with its own movement, as a vehicle does with + // its (rendering/common/freecameramotion.h) + if (vp.main && (Global.pCamera.m_owner != nullptr)) + m_freecamera.reset(); + auto const freecameramovement{(vp.main && (Global.pCamera.m_owner == nullptr)) ? m_freecamera.next_frame(Global.pCamera.Pos) : glm::vec3{0.f}}; + // the motion blur only when something in the view moves: the vehicle of the camera, the camera with no vehicle of its own, + // or a vehicle drawn in the last frame. // with nothing moving the velocity is zero everywhere, the blur takes a single sample of each pixel and gives back the // image it was given, so the velocity target, its resolve and the blur are left out and the picture stays the same. // a vehicle setting off is blurred from its second frame, when its velocity is still far below a pixel auto const motionblur{ Global.gfx_postfx_motionblur_enabled && (m_vehiclesmoved + || (freecameramovement != glm::vec3{0.f}) || ((Global.pCamera.m_owner != nullptr) && (Global.pCamera.m_owner->get_future_movement() != glm::dvec3{0.0})))}; if (!Global.gfx_skippipeline) @@ -1805,6 +1812,12 @@ void opengl46_renderer::Render_pass(viewport_config &vp, rendermode const Mode) glm::mat4 mv = OpenGLMatrices.data(GL_MODELVIEW); future = glm::translate(mv, -glm::vec3(vehicle->get_future_movement())) * glm::inverse(mv); } + else if (freecameramovement != glm::vec3{0.f}) + { + // with the temporal anti-aliasing the velocities to the last frame hold the movement of the camera already + glm::mat4 mv = OpenGLMatrices.data(GL_MODELVIEW); + future = glm::translate(mv, -freecameramovement) * glm::inverse(mv); + } // the cluster grid and its lists of lights are kept up only while something reads them: the shading or the heatmap. // the lists exist for a perspective view; without them the shading goes through the first few lights, as does the pass diff --git a/rendering/opengl46/opengl46renderer.h b/rendering/opengl46/opengl46renderer.h index 1c3b14613..08d92e4b7 100644 --- a/rendering/opengl46/opengl46renderer.h +++ b/rendering/opengl46/opengl46renderer.h @@ -51,6 +51,7 @@ http://mozilla.org/MPL/2.0/. #include "rendering/opengl46/opengl46shadowfit.h" #include "rendering/common/scenewalk.h" #include "rendering/common/temporalaa.h" +#include "rendering/common/freecameramotion.h" #include "rendering/common/screenreflections.h" // bare-bones render controller, in lack of anything better yet @@ -867,6 +868,8 @@ class opengl46_renderer : public gfx_renderer, public gfx::scene_walk { std::unique_ptr m_pfx_motionblur; std::unique_ptr m_pfx_taa; gfx::temporal_aa m_taa; + // the movement of the camera with no vehicle of its own in the main view, for the motion blur + gfx::free_camera_motion m_freecamera; std::unique_ptr m_pfx_tonemapping; std::unique_ptr m_pfx_chromaticaberration; diff --git a/rendering/vulkan14/vulkan14frame.cpp b/rendering/vulkan14/vulkan14frame.cpp index 2317e5668..82aa2645d 100644 --- a/rendering/vulkan14/vulkan14frame.cpp +++ b/rendering/vulkan14/vulkan14frame.cpp @@ -1584,9 +1584,15 @@ void vulkan14_renderer::render_viewport(std::size_t const Index) render_ray_scene(vp); } + // the camera with no vehicle of its own (freefly, the editor) blurs the world with its own movement, as a vehicle does with its + // (rendering/common/freecameramotion.h) + if (vp.main && (Global.pCamera.m_owner != nullptr)) + m_freecamera.reset(); + auto const freecameramovement{(vp.main && (Global.pCamera.m_owner == nullptr)) ? m_freecamera.next_frame(Global.pCamera.Pos) : glm::vec3{0.f}}; // the motion blur only when something in the view moves (see opengl46_renderer::Render_pass()) auto const motionblur{Global.gfx_postfx_motionblur_enabled && targets.motion && - (m_vehiclesmoved || ((Global.pCamera.m_owner != nullptr) && (Global.pCamera.m_owner->get_future_movement() != glm::dvec3{0.0})))}; + (m_vehiclesmoved || (freecameramovement != glm::vec3{0.f}) || + ((Global.pCamera.m_owner != nullptr) && (Global.pCamera.m_owner->get_future_movement() != glm::dvec3{0.0})))}; setup_matrices(); m_renderpass.draw_stats = {}; @@ -1602,6 +1608,12 @@ void vulkan14_renderer::render_viewport(std::size_t const Index) glm::mat4 mv = OpenGLMatrices.data(GL_MODELVIEW); future = glm::translate(mv, -glm::vec3(vehicle->get_future_movement())) * glm::inverse(mv); } + else if (freecameramovement != glm::vec3{0.f}) + { + // with the temporal anti-aliasing the velocities to the last frame hold the movement of the camera already + glm::mat4 mv = OpenGLMatrices.data(GL_MODELVIEW); + future = glm::translate(mv, -freecameramovement) * glm::inverse(mv); + } // the light clusters of the viewport (the buffers of the grid are taken by each viewport in turn), and the lights of the pass auto const clusters{Global.gfx_lights_clustered || Global.gfx_lights_clusterheatmap}; diff --git a/rendering/vulkan14/vulkan14renderer.h b/rendering/vulkan14/vulkan14renderer.h index db9a5a7f9..66cf70fd3 100644 --- a/rendering/vulkan14/vulkan14renderer.h +++ b/rendering/vulkan14/vulkan14renderer.h @@ -29,6 +29,7 @@ http://mozilla.org/MPL/2.0/. #include "rendering/common/shadowfit.h" #include "rendering/common/gpuparticles.h" #include "rendering/common/temporalaa.h" +#include "rendering/common/freecameramotion.h" #include "rendering/common/screenreflections.h" #include "rendering/particles.h" #include "model/material.h" @@ -566,6 +567,8 @@ class vulkan14_renderer : public gfx_renderer, public gfx::scene_walk bool m_vehiclesmoved{true}; // the temporal anti-aliasing of the main viewport (gfx.antialiasing taa), whose jitter and history dlss takes too gfx::temporal_aa m_taa; + // the movement of the camera with no vehicle of its own in the main view, for the motion blur + gfx::free_camera_motion m_freecamera; // dlss of the main viewport (gfx.antialiasing dlss): the size the scene is drawn at and the window it's upscaled to, planned at // the start (0: dlss is off); the next frame starts its history anew glm::ivec2 m_dlssrender{0}; diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index fcf8fb111..b8e324e3b 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -34,6 +34,8 @@ add_executable(${UNIT_TESTS_NAME} "${CMAKE_SOURCE_DIR}/editor/editorFouling.cpp" # which textures the shadow maps draw without the alpha test "${CMAKE_SOURCE_DIR}/rendering/common/texturealpha.cpp" + # and the movement of the free camera the motion blur takes + "${CMAKE_SOURCE_DIR}/rendering/common/freecameramotion.cpp" # and the http client the editor fetches its aids with "${CMAKE_SOURCE_DIR}/editor/editorHttp.cpp" ${UNIT_TEST_CASES}) diff --git a/tests/unit/test_free_camera_motion.cpp b/tests/unit/test_free_camera_motion.cpp new file mode 100644 index 000000000..1bc03210a --- /dev/null +++ b/tests/unit/test_free_camera_motion.cpp @@ -0,0 +1,56 @@ +#include "stdafx.h" +#include "doctest.h" +#include "rendering/common/freecameramotion.h" + +#include + +// the movement of the camera with no vehicle of its own, which the motion blur takes: the step since the frame before, held to +// half a metre, and none for the first frame, after a jump or after the camera sat in a vehicle +TEST_CASE("free camera motion: the first frame gives no movement, the next ones the step since the frame before") +{ + gfx::free_camera_motion motion; + CHECK(motion.next_frame(glm::dvec3{100.0, 10.0, -50.0}) == glm::vec3{0.f}); + auto const step{motion.next_frame(glm::dvec3{100.25, 10.0, -50.0})}; + CHECK(step.x == doctest::Approx(0.25f)); + CHECK(step.y == 0.f); + CHECK(step.z == 0.f); + // a camera standing still + CHECK(motion.next_frame(glm::dvec3{100.25, 10.0, -50.0}) == glm::vec3{0.f}); +} + +TEST_CASE("free camera motion: a fast camera is held to the longest movement, in its direction") +{ + gfx::free_camera_motion motion; + motion.next_frame(glm::dvec3{0.0}); + auto const step{motion.next_frame(glm::dvec3{3.0, 0.0, -4.0})}; + CHECK(glm::length(step) == doctest::Approx(gfx::free_camera_motion::longest)); + CHECK(step.x == doctest::Approx(0.3f)); + CHECK(step.y == 0.f); + CHECK(step.z == doctest::Approx(-0.4f)); +} + +TEST_CASE("free camera motion: a jump is no movement, and the frame after it moves from where the camera landed") +{ + gfx::free_camera_motion motion; + motion.next_frame(glm::dvec3{0.0}); + CHECK(motion.next_frame(glm::dvec3{0.0, 0.0, gfx::free_camera_motion::jump}) == glm::vec3{0.f}); + CHECK(motion.next_frame(glm::dvec3{0.0, 0.0, gfx::free_camera_motion::jump + 0.1}).z == doctest::Approx(0.1f)); +} + +TEST_CASE("free camera motion: after a reset the camera starts anew") +{ + gfx::free_camera_motion motion; + motion.next_frame(glm::dvec3{0.0}); + motion.reset(); + CHECK(motion.next_frame(glm::dvec3{0.2, 0.0, 0.0}) == glm::vec3{0.f}); + CHECK(motion.next_frame(glm::dvec3{0.4, 0.0, 0.0}).x == doctest::Approx(0.2f)); +} + +TEST_CASE("free camera motion: a position that isn't a number gives no movement") +{ + gfx::free_camera_motion motion; + motion.next_frame(glm::dvec3{0.0}); + CHECK(motion.next_frame(glm::dvec3{std::numeric_limits::quiet_NaN(), 0.0, 0.0}) == glm::vec3{0.f}); + // nor does the frame after it, which would move from that position + CHECK(motion.next_frame(glm::dvec3{0.1, 0.0, 0.0}) == glm::vec3{0.f}); +}