Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions src/DeferredPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <array>
#include <functional>
#include <glm/ext/matrix_clip_space.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/trigonometric.hpp>
Expand All @@ -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<float>(camera.GetWidth()) /
static_cast<float>(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));
Expand Down Expand Up @@ -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));

Expand All @@ -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_,
Expand All @@ -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> transform = SpotLightVolumeTransform(light);
Expand All @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down
14 changes: 2 additions & 12 deletions src/ForwardPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <array>
#include <concepts>
#include <functional>
#include <glm/ext/matrix_clip_space.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <glm/geometric.hpp>
#include <glm/gtc/type_ptr.hpp>
Expand All @@ -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<float>(camera.GetWidth()) /
static_cast<float>(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,
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions tests/MathMigrationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading