From bd75e3a5e3f4173f5cbaed22140c632f183b109e Mon Sep 17 00:00:00 2001 From: Lemiort Date: Sat, 24 Oct 2026 17:21:17 +0200 Subject: [PATCH] Use camera projection across renderers --- src/DeferredPipeline.cpp | 20 +++++--------------- src/ForwardPipeline.cpp | 14 ++------------ tests/MathMigrationTests.cpp | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/DeferredPipeline.cpp b/src/DeferredPipeline.cpp index 5725109..5465ffb 100644 --- a/src/DeferredPipeline.cpp +++ b/src/DeferredPipeline.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -15,15 +14,6 @@ #include "RenderQueue.hpp" namespace { -glm::mat4 RenderPassViewProjection(const Camera& camera) { - const glm::mat4 projection = - glm::perspectiveRH_NO(glm::radians(30.0F), - static_cast(camera.GetWidth()) / - static_cast(camera.GetHeight()), - 1.0F, 1000.0F); - return projection * camera.GetViewMatrix(); -} - void SetLightColor(GLint location, const glm::vec3& color) { const glm::mat4 matrix = glm::scale(glm::mat4(1.0F), color); glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(matrix)); @@ -117,7 +107,7 @@ void DeferredPipeline::GeometryPass(const SceneView& scene) { glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND); geometryShader_->Use(); - const glm::mat4 matrix = RenderPassViewProjection(scene.camera); + const glm::mat4 matrix = scene.camera.GetViewProjectionMatrix(); glUniformMatrix4fv(geometryShader_->GetUniformLocation("gVC"), 1, GL_FALSE, glm::value_ptr(matrix)); @@ -138,7 +128,7 @@ void DeferredPipeline::StencilPass(const PointLight& light, glStencilFunc(GL_ALWAYS, 0, 0); glStencilOpSeparate(GL_BACK, GL_KEEP, GL_INCR, GL_KEEP); glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_DECR, GL_KEEP); - const glm::mat4 matrix = RenderPassViewProjection(camera); + const glm::mat4 matrix = camera.GetViewProjectionMatrix(); glUniformMatrix4fv(stencilShader_->GetUniformLocation("gVC"), 1, GL_FALSE, glm::value_ptr(matrix)); pointLightVolume_.Draw(PointLightVolumeTransform(light), *stencilMaterial_, @@ -156,7 +146,7 @@ void DeferredPipeline::StencilPass(const SpotLight& light, glStencilFunc(GL_ALWAYS, 0, 0); glStencilOpSeparate(GL_BACK, GL_KEEP, GL_INCR, GL_KEEP); glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_DECR, GL_KEEP); - const glm::mat4 matrix = RenderPassViewProjection(camera); + const glm::mat4 matrix = camera.GetViewProjectionMatrix(); glUniformMatrix4fv(stencilShader_->GetUniformLocation("gVC"), 1, GL_FALSE, glm::value_ptr(matrix)); const std::optional transform = SpotLightVolumeTransform(light); @@ -182,7 +172,7 @@ void DeferredPipeline::PointLightPass(const PointLight& light, glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); pointLightShader_->Use(); - const glm::mat4 matrix = RenderPassViewProjection(camera); + const glm::mat4 matrix = camera.GetViewProjectionMatrix(); glUniformMatrix4fv(pointLightShader_->GetUniformLocation("gVC"), 1, GL_FALSE, glm::value_ptr(matrix)); glUniform3f(pointLightShader_->GetUniformLocation("s_vCamPos"), @@ -212,7 +202,7 @@ void DeferredPipeline::SpotLightPass(const SpotLight& light, glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); spotLightShader_->Use(); - const glm::mat4 matrix = RenderPassViewProjection(camera); + const glm::mat4 matrix = camera.GetViewProjectionMatrix(); glUniformMatrix4fv(spotLightShader_->GetUniformLocation("gVC"), 1, GL_FALSE, glm::value_ptr(matrix)); glUniform3f(spotLightShader_->GetUniformLocation("s_vCamPos"), diff --git a/src/ForwardPipeline.cpp b/src/ForwardPipeline.cpp index 714d464..2c33065 100644 --- a/src/ForwardPipeline.cpp +++ b/src/ForwardPipeline.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -19,15 +18,6 @@ #include "Mesh.hpp" namespace { -glm::mat4 RenderPassViewProjection(const Camera& camera) { - const glm::mat4 projection = - glm::perspectiveRH_NO(glm::radians(30.0F), - static_cast(camera.GetWidth()) / - static_cast(camera.GetHeight()), - 1.0F, 1000.0F); - return projection * camera.GetViewMatrix(); -} - void SetLightColor(GLint uniformLocation, const glm::vec3& color) { const glm::mat4 colorMatrix = glm::scale(glm::mat4(1.0F), color); glUniformMatrix4fv(uniformLocation, 1, GL_FALSE, @@ -75,7 +65,7 @@ void ForwardPipeline::RenderTransparent(const SceneView& scene) { void ForwardPipeline::PrepareLighting(const SceneView& scene) { meshShader_->Use(); - const glm::mat4 viewProjection = RenderPassViewProjection(scene.camera); + const glm::mat4 viewProjection = scene.camera.GetViewProjectionMatrix(); glUniformMatrix4fv(cameraViewProjectionUniform_, 1, GL_FALSE, glm::value_ptr(viewProjection)); @@ -121,7 +111,7 @@ void ForwardPipeline::PrepareLighting(const SceneView& scene) { scene.camera.GetZFar(), spotPosition, spotDirection, glm::vec3(0.0F, 1.0F, 0.0F)); const glm::mat4 lightViewProjection = - RenderPassViewProjection(lightCamera); + lightCamera.GetViewProjectionMatrix(); glUniformMatrix4fv(lightViewProjectionUniform_, 1, GL_FALSE, glm::value_ptr(lightViewProjection)); } else { diff --git a/tests/MathMigrationTests.cpp b/tests/MathMigrationTests.cpp index 5efdec4..6a5543f 100644 --- a/tests/MathMigrationTests.cpp +++ b/tests/MathMigrationTests.cpp @@ -244,6 +244,28 @@ TEST(CameraProjection, MapsNearAndFarPlanesToOpenGLDepth) { EXPECT_NEAR(farClip.z / farClip.w, 1.0F, kTolerance); } +TEST(CameraProjection, FieldOfViewChangesProjectedGeometrySize) { + Camera camera(800, 600, 60, 1, 100); + const glm::vec4 point(1, 0, -3, 1); + const glm::vec4 wideClip = camera.GetViewProjectionMatrix() * point; + + camera.SetFOV(30.0F); + const glm::vec4 narrowClip = camera.GetViewProjectionMatrix() * point; + + EXPECT_GT(narrowClip.x / narrowClip.w, wideClip.x / wideClip.w); +} + +TEST(CameraProjection, UpdatedNearPlaneControlsClipping) { + Camera camera(800, 600, 60, 1, 100); + camera.SetZNear(2.0F); + const glm::mat4 projectionView = camera.GetViewProjectionMatrix(); + const glm::vec4 beforeNear = projectionView * glm::vec4(0, 0, -1, 1); + const glm::vec4 onNear = projectionView * glm::vec4(0, 0, -2, 1); + + EXPECT_LT(beforeNear.z / beforeNear.w, -1.0F); + EXPECT_NEAR(onNear.z / onNear.w, -1.0F, kTolerance); +} + TEST(CameraProjection, ViewMatrixUsesPositionAndForwardDirection) { Camera const camera(800, 600, 60, 1, 100, glm::vec3(2, 3, 4), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0));