From 7bbe879d2f4ac05fa0d32894b81b7df5d2e57de3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Tue, 1 Jun 2021 18:15:29 +0200 Subject: [PATCH] DeferredShading: Fix light spawning --- examples/DeferredShading/main.cpp | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/examples/DeferredShading/main.cpp b/examples/DeferredShading/main.cpp index 20525cd4f..4756f4be8 100644 --- a/examples/DeferredShading/main.cpp +++ b/examples/DeferredShading/main.cpp @@ -748,6 +748,25 @@ int main() float elapsedTime = 0.f; Nz::UInt64 time = Nz::GetElapsedMicroseconds(); + auto ComputeLightAnimationSpeed = [](const Nz::Vector3f& position) + { + return position.GetLength() / 10.f; + }; + + auto AnimateLightPosition = [](const Nz::Vector3f& position, float rotationSpeed, float elapsedTime) + { + rotationSpeed *= 45.f; + + return Nz::Matrix4f::Rotate(Nz::EulerAnglesf(0.f, elapsedTime * rotationSpeed, 0.f)) * position; + }; + + auto AnimateLightDirection = [](const Nz::Vector3f& direction, float rotationSpeed, float elapsedTime) + { + rotationSpeed *= 90.f; + + return Nz::Matrix4f::Rotate(Nz::EulerAnglesf(0.f, elapsedTime * rotationSpeed, 0.f)) * direction; + }; + while (window.IsOpen()) { Nz::UInt64 now = Nz::GetElapsedMicroseconds(); @@ -786,10 +805,12 @@ int main() { if (event.key.scancode == Nz::Keyboard::Scancode::Space) { + float rotationSpeed = ComputeLightAnimationSpeed(viewerPos); + auto& whiteLight = spotLights.emplace_back(); whiteLight.radius = 5.f; - whiteLight.position = viewerPos; - whiteLight.direction = camQuat * Nz::Vector3f::Forward(); + whiteLight.position = AnimateLightPosition(viewerPos, rotationSpeed, -elapsedTime); + whiteLight.direction = AnimateLightDirection(camQuat * Nz::Vector3f::Forward(), rotationSpeed, -elapsedTime); lightUpdate = true; } @@ -879,10 +900,10 @@ int main() for (const SpotLight& spotLight : spotLights) { - float rotationSpeed = spotLight.position.GetLength() / 10.f; + float rotationSpeed = ComputeLightAnimationSpeed(spotLight.position); - Nz::Vector3f position = Nz::Matrix4f::Rotate(Nz::EulerAnglesf(0.f, elapsedTime * 45.f * rotationSpeed, 0.f)) * spotLight.position; - Nz::Vector3f direction = Nz::Matrix4f::Rotate(Nz::EulerAnglesf(0.f, elapsedTime * 90.f * rotationSpeed, 0.f)) * spotLight.direction; + Nz::Vector3f position = AnimateLightPosition(spotLight.position, rotationSpeed, elapsedTime); + Nz::Vector3f direction = AnimateLightDirection(spotLight.direction, rotationSpeed, elapsedTime); Nz::AccessByOffset(lightDataPtr, colorOffset) = Nz::Vector3f(spotLight.color.r / 255.f, spotLight.color.g / 255.f, spotLight.color.b / 255.f); Nz::AccessByOffset(lightDataPtr, positionOffset) = position;