mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-24 11:59:14 +01:00
fix(render): the free camera blurs with its own movement in the OpenGL 4.6 and Vulkan renderers
This commit is contained in:
@@ -364,6 +364,7 @@ set(SOURCES
|
|||||||
"rendering/common/weatherstate.cpp"
|
"rendering/common/weatherstate.cpp"
|
||||||
"rendering/common/exposure.cpp"
|
"rendering/common/exposure.cpp"
|
||||||
"rendering/common/temporalaa.cpp"
|
"rendering/common/temporalaa.cpp"
|
||||||
|
"rendering/common/freecameramotion.cpp"
|
||||||
"rendering/common/screenreflections.cpp"
|
"rendering/common/screenreflections.cpp"
|
||||||
"gl/buffer.cpp"
|
"gl/buffer.cpp"
|
||||||
"gl/fence.cpp"
|
"gl/fence.cpp"
|
||||||
|
|||||||
@@ -686,8 +686,6 @@ void opengl_texture::upload_from_thread(size_t width, size_t height, const uint8
|
|||||||
set_filtering();
|
set_filtering();
|
||||||
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_mode_s);
|
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_mode_s);
|
||||||
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_mode_t);
|
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<GLsizei>(width), static_cast<GLsizei>(height), 0, GL_RGBA, GL_UNSIGNED_BYTE, raw);
|
glTexImage2D(target, 0, GL_SRGB8_ALPHA8, static_cast<GLsizei>(width), static_cast<GLsizei>(height), 0, GL_RGBA, GL_UNSIGNED_BYTE, raw);
|
||||||
if (glGenerateMipmap)
|
if (glGenerateMipmap)
|
||||||
glGenerateMipmap(target);
|
glGenerateMipmap(target);
|
||||||
|
|||||||
27
rendering/common/freecameramotion.cpp
Normal file
27
rendering/common/freecameramotion.cpp
Normal file
@@ -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<float>(jump))
|
||||||
|
{
|
||||||
|
movement = (length > longest) ? step * (longest / length) : step;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_previous = Position;
|
||||||
|
return movement;
|
||||||
|
}
|
||||||
45
rendering/common/freecameramotion.h
Normal file
45
rendering/common/freecameramotion.h
Normal file
@@ -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 <glm/glm.hpp>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
|
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<glm::dvec3> m_previous;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace gfx
|
||||||
@@ -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
|
if (vp.main && !vp.custom_backbuffer) // TODO: update window sizes also for extra viewports
|
||||||
target_size = Global.fb_size;
|
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
|
// 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.
|
// 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
|
// a vehicle setting off is blurred from its second frame, when its velocity is still far below a pixel
|
||||||
auto const motionblur{
|
auto const motionblur{
|
||||||
Global.gfx_postfx_motionblur_enabled
|
Global.gfx_postfx_motionblur_enabled
|
||||||
&& (m_vehiclesmoved
|
&& (m_vehiclesmoved
|
||||||
|
|| (freecameramovement != glm::vec3{0.f})
|
||||||
|| ((Global.pCamera.m_owner != nullptr) && (Global.pCamera.m_owner->get_future_movement() != glm::dvec3{0.0})))};
|
|| ((Global.pCamera.m_owner != nullptr) && (Global.pCamera.m_owner->get_future_movement() != glm::dvec3{0.0})))};
|
||||||
|
|
||||||
if (!Global.gfx_skippipeline)
|
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);
|
glm::mat4 mv = OpenGLMatrices.data(GL_MODELVIEW);
|
||||||
future = glm::translate(mv, -glm::vec3(vehicle->get_future_movement())) * glm::inverse(mv);
|
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 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
|
// the lists exist for a perspective view; without them the shading goes through the first few lights, as does the pass
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
#include "rendering/opengl46/opengl46shadowfit.h"
|
#include "rendering/opengl46/opengl46shadowfit.h"
|
||||||
#include "rendering/common/scenewalk.h"
|
#include "rendering/common/scenewalk.h"
|
||||||
#include "rendering/common/temporalaa.h"
|
#include "rendering/common/temporalaa.h"
|
||||||
|
#include "rendering/common/freecameramotion.h"
|
||||||
#include "rendering/common/screenreflections.h"
|
#include "rendering/common/screenreflections.h"
|
||||||
|
|
||||||
// bare-bones render controller, in lack of anything better yet
|
// 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<gl::postfx> m_pfx_motionblur;
|
std::unique_ptr<gl::postfx> m_pfx_motionblur;
|
||||||
std::unique_ptr<gl::postfx> m_pfx_taa;
|
std::unique_ptr<gl::postfx> m_pfx_taa;
|
||||||
gfx::temporal_aa m_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<gl::postfx> m_pfx_tonemapping;
|
std::unique_ptr<gl::postfx> m_pfx_tonemapping;
|
||||||
std::unique_ptr<gl::postfx> m_pfx_chromaticaberration;
|
std::unique_ptr<gl::postfx> m_pfx_chromaticaberration;
|
||||||
|
|
||||||
|
|||||||
@@ -1584,9 +1584,15 @@ void vulkan14_renderer::render_viewport(std::size_t const Index)
|
|||||||
render_ray_scene(vp);
|
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())
|
// 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 &&
|
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();
|
setup_matrices();
|
||||||
m_renderpass.draw_stats = {};
|
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);
|
glm::mat4 mv = OpenGLMatrices.data(GL_MODELVIEW);
|
||||||
future = glm::translate(mv, -glm::vec3(vehicle->get_future_movement())) * glm::inverse(mv);
|
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
|
// 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};
|
auto const clusters{Global.gfx_lights_clustered || Global.gfx_lights_clusterheatmap};
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
#include "rendering/common/shadowfit.h"
|
#include "rendering/common/shadowfit.h"
|
||||||
#include "rendering/common/gpuparticles.h"
|
#include "rendering/common/gpuparticles.h"
|
||||||
#include "rendering/common/temporalaa.h"
|
#include "rendering/common/temporalaa.h"
|
||||||
|
#include "rendering/common/freecameramotion.h"
|
||||||
#include "rendering/common/screenreflections.h"
|
#include "rendering/common/screenreflections.h"
|
||||||
#include "rendering/particles.h"
|
#include "rendering/particles.h"
|
||||||
#include "model/material.h"
|
#include "model/material.h"
|
||||||
@@ -566,6 +567,8 @@ class vulkan14_renderer : public gfx_renderer, public gfx::scene_walk
|
|||||||
bool m_vehiclesmoved{true};
|
bool m_vehiclesmoved{true};
|
||||||
// the temporal anti-aliasing of the main viewport (gfx.antialiasing taa), whose jitter and history dlss takes too
|
// the temporal anti-aliasing of the main viewport (gfx.antialiasing taa), whose jitter and history dlss takes too
|
||||||
gfx::temporal_aa m_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;
|
||||||
// dlss of the main viewport (gfx.antialiasing dlss): the size the scene is drawn at and the window it's upscaled to, planned at
|
// 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
|
// the start (0: dlss is off); the next frame starts its history anew
|
||||||
glm::ivec2 m_dlssrender{0};
|
glm::ivec2 m_dlssrender{0};
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ add_executable(${UNIT_TESTS_NAME}
|
|||||||
"${CMAKE_SOURCE_DIR}/editor/editorFouling.cpp"
|
"${CMAKE_SOURCE_DIR}/editor/editorFouling.cpp"
|
||||||
# which textures the shadow maps draw without the alpha test
|
# which textures the shadow maps draw without the alpha test
|
||||||
"${CMAKE_SOURCE_DIR}/rendering/common/texturealpha.cpp"
|
"${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
|
# and the http client the editor fetches its aids with
|
||||||
"${CMAKE_SOURCE_DIR}/editor/editorHttp.cpp"
|
"${CMAKE_SOURCE_DIR}/editor/editorHttp.cpp"
|
||||||
${UNIT_TEST_CASES})
|
${UNIT_TEST_CASES})
|
||||||
|
|||||||
56
tests/unit/test_free_camera_motion.cpp
Normal file
56
tests/unit/test_free_camera_motion.cpp
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
#include "stdafx.h"
|
||||||
|
#include "doctest.h"
|
||||||
|
#include "rendering/common/freecameramotion.h"
|
||||||
|
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
|
// 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<double>::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});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user