diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-10-06 12:48:40 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-10-06 14:03:33 +0100 |
commit | 10998a8302b3d7651b4afc046311961eb2dea2c8 (patch) | |
tree | 1b58847ae898f9d3c7fa807bfcaa4b4103d6f35f | |
parent | Add LightDirection class (diff) | |
download | ilt-10998a8302b3d7651b4afc046311961eb2dea2c8.tar.bz2 ilt-10998a8302b3d7651b4afc046311961eb2dea2c8.tar.xz ilt-10998a8302b3d7651b4afc046311961eb2dea2c8.zip |
Use LightDirection for calculating/passing all light dir components
-rw-r--r-- | game/environment.cpp | 11 | ||||
-rw-r--r-- | gfx/gl/sceneProvider.cpp | 2 | ||||
-rw-r--r-- | gfx/gl/sceneRenderer.cpp | 8 | ||||
-rw-r--r-- | gfx/gl/sceneRenderer.h | 3 | ||||
-rw-r--r-- | gfx/gl/shadowMapper.cpp | 7 | ||||
-rw-r--r-- | gfx/gl/shadowMapper.h | 3 | ||||
-rw-r--r-- | gfx/gl/shadowStenciller.cpp | 15 | ||||
-rw-r--r-- | gfx/gl/shadowStenciller.h | 5 | ||||
-rw-r--r-- | test/test-assetFactory.cpp | 2 | ||||
-rw-r--r-- | test/test-geoData.cpp | 2 | ||||
-rw-r--r-- | test/test-render.cpp | 6 |
11 files changed, 34 insertions, 30 deletions
diff --git a/game/environment.cpp b/game/environment.cpp index 19aad84..5aa1f09 100644 --- a/game/environment.cpp +++ b/game/environment.cpp @@ -1,4 +1,5 @@ #include "environment.h" +#include "gfx/lightDirection.h" #include <chronology.h> #include <gfx/gl/sceneRenderer.h> @@ -16,14 +17,12 @@ Environment::render(const SceneRenderer & renderer, const SceneProvider & scene) constexpr RGB baseAmbient {0.1F}, baseDirectional {0.0F}; constexpr RGB relativeAmbient {0.3F, 0.3F, 0.4F}, relativeDirectional {0.6F, 0.6F, 0.5F}; - const auto sunPos = getSunPos({}, worldTime); - const auto sunDir = (glm::mat3 {rotate_yp({sunPos.y + pi, sunPos.x})} * north); - const auto vertical = -std::min(0.F, sunDir.z - 0.1F); - const auto ambient = baseAmbient + relativeAmbient * vertical; - const auto directional = baseDirectional + relativeDirectional * vertical; + const LightDirection sunPos = getSunPos({}, worldTime); + const auto ambient = baseAmbient + relativeAmbient * sunPos.vertical(); + const auto directional = baseDirectional + relativeDirectional * sunPos.vertical(); renderer.setAmbientLight(ambient); - renderer.setDirectionalLight(directional, sunDir, scene); + renderer.setDirectionalLight(directional, sunPos, scene); } // Based on the C++ code published at https://www.psa.es/sdg/sunpos.htm diff --git a/gfx/gl/sceneProvider.cpp b/gfx/gl/sceneProvider.cpp index 2e8604c..4e271db 100644 --- a/gfx/gl/sceneProvider.cpp +++ b/gfx/gl/sceneProvider.cpp @@ -5,7 +5,7 @@ void SceneProvider::environment(const SceneShader &, const SceneRenderer & renderer) const { renderer.setAmbientLight({0.5F, 0.5F, 0.5F}); - renderer.setDirectionalLight({0.6F, 0.6F, 0.6F}, {-1, 1, -1}, *this); + renderer.setDirectionalLight({0.6F, 0.6F, 0.6F}, {{-quarter_pi, -quarter_pi}}, *this); } void diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp index 7aace49..b2a7d78 100644 --- a/gfx/gl/sceneRenderer.cpp +++ b/gfx/gl/sceneRenderer.cpp @@ -114,7 +114,8 @@ SceneRenderer::setAmbientLight(const RGB & colour) const } void -SceneRenderer::setDirectionalLight(const RGB & colour, const Direction3D & direction, const SceneProvider & scene) const +SceneRenderer::setDirectionalLight( + const RGB & colour, const LightDirection & direction, const SceneProvider & scene) const { if (colour.r > 0 || colour.g > 0 || colour.b > 0) { const auto lvp = shadowMapper.update(scene, direction, camera); @@ -128,7 +129,7 @@ SceneRenderer::setDirectionalLight(const RGB & colour, const Direction3D & direc glBindTexture(GL_TEXTURE_2D_ARRAY, shadowMapper); glViewport(0, 0, size.x, size.y); dirLight.use(); - dirLight.setDirectionalLight(colour, direction, camera.getPosition(), lvp); + dirLight.setDirectionalLight(colour, direction.vector(), camera.getPosition(), lvp); renderQuad(); } } @@ -154,8 +155,7 @@ SceneRenderer::DirectionalLightProgram::setDirectionalLight( return toTextureSpaceMat * m; }; glUniform(colourLoc, c); - const auto nd = glm::normalize(d); - glUniform(directionLoc, nd); + glUniform(directionLoc, d); glUniform(lightPointLoc, p); glUniform(lightViewProjectionCountLoc, static_cast<GLuint>(lvp.size())); glUniform(lightViewProjectionLoc, std::span<const glm::mat4> {lvp * toTextureSpace}); diff --git a/gfx/gl/sceneRenderer.h b/gfx/gl/sceneRenderer.h index 4195bcf..93470f5 100644 --- a/gfx/gl/sceneRenderer.h +++ b/gfx/gl/sceneRenderer.h @@ -1,6 +1,7 @@ #pragma once #include "camera.h" +#include "gfx/lightDirection.h" #include "glArrays.h" #include "program.h" #include "sceneProvider.h" @@ -14,7 +15,7 @@ public: void render(const SceneProvider &) const; void setAmbientLight(const RGB & colour) const; - void setDirectionalLight(const RGB & colour, const Direction3D & direction, const SceneProvider &) const; + void setDirectionalLight(const RGB & colour, const LightDirection & direction, const SceneProvider &) const; Camera camera; diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index b08538d..4f7eac1 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -13,6 +13,7 @@ #include "gfx/gl/shaders/vs-shadowDynamicPointStencil.h" #include "gfx/gl/shaders/vs-shadowLandmass.h" #include "gfx/gl/shadowStenciller.h" +#include "gfx/lightDirection.h" #include "gfx/renderable.h" #include "gl_traits.h" #include "location.h" @@ -78,12 +79,12 @@ ShadowMapper::getBandViewExtents(const Camera & camera, const glm::mat4 & lightV } ShadowMapper::Definitions -ShadowMapper::update(const SceneProvider & scene, const Direction3D & dir, const Camera & camera) const +ShadowMapper::update(const SceneProvider & scene, const LightDirection & dir, const Camera & camera) const { glCullFace(GL_FRONT); glEnable(GL_DEPTH_TEST); - shadowStenciller.setLightDirection(dir, up); + shadowStenciller.setLightDirection(dir); for (const auto & [id, asset] : gameState->assets) { if (const auto r = std::dynamic_pointer_cast<const Renderable>(asset)) { r->updateStencil(shadowStenciller); @@ -94,7 +95,7 @@ ShadowMapper::update(const SceneProvider & scene, const Direction3D & dir, const glClear(GL_DEPTH_BUFFER_BIT); glViewport(0, 0, size.x, size.y); - const auto lightViewDir = glm::lookAt({}, dir, up); + const auto lightViewDir = glm::lookAt({}, dir.vector(), up); const auto lightViewPoint = camera.getPosition(); const auto bandViewExtents = getBandViewExtents(camera, lightViewDir); Definitions out; diff --git a/gfx/gl/shadowMapper.h b/gfx/gl/shadowMapper.h index 3aa224e..951e29c 100644 --- a/gfx/gl/shadowMapper.h +++ b/gfx/gl/shadowMapper.h @@ -11,6 +11,7 @@ class SceneProvider; class Camera; +class LightDirection; class ShadowMapper { public: @@ -21,7 +22,7 @@ public: using Definitions = std::vector<glm::mat4x4>; using Sizes = std::vector<RelativePosition3D>; - [[nodiscard]] Definitions update(const SceneProvider &, const Direction3D & direction, const Camera &) const; + [[nodiscard]] Definitions update(const SceneProvider &, const LightDirection & direction, const Camera &) const; class ShadowProgram : public Program { public: diff --git a/gfx/gl/shadowStenciller.cpp b/gfx/gl/shadowStenciller.cpp index da2b3a0..ae4012a 100644 --- a/gfx/gl/shadowStenciller.cpp +++ b/gfx/gl/shadowStenciller.cpp @@ -3,6 +3,7 @@ #include "gfx/gl/shaders/fs-shadowStencil.h" #include "gfx/gl/shaders/gs-shadowStencil.h" #include "gfx/gl/shaders/vs-shadowStencil.h" +#include "gfx/lightDirection.h" #include "gfx/models/mesh.h" #include "glArrays.h" #include "gl_traits.h" @@ -26,10 +27,9 @@ ShadowStenciller::ShadowStenciller() : } void -ShadowStenciller::setLightDirection(const Direction3D & lightDir, const Direction3D & lightDirUp) +ShadowStenciller::setLightDirection(const LightDirection & lightDir) { - lightDirMat = glm::lookAt(-lightDir, {}, lightDirUp); - viewProjections = angles * [this](const auto & a) { + viewProjections = angles * [lightDirMat = rotate_pitch(lightDir.position().y)](const auto & a) { return lightDirMat * a; }; } @@ -68,10 +68,11 @@ ShadowStenciller::renderStencil(const glTexture & stencil, const MeshBase & mesh glViewport(0, 0, 256, 256); const auto & centre = mesh.getDimensions().centre; const auto & size = mesh.getDimensions().size; - const auto extentsMat - = glm::translate(glm::ortho(-size, size, -size, size, -size, size), {-centre.x, -centre.z, -centre.y}); - glUniform(viewProjectionLoc, std::span<const glm::mat4> {viewProjections * [&](const auto & vp) { - return extentsMat * vp; + glUniform(viewProjectionLoc, + std::span<const glm::mat4> {viewProjections * + [extentsMat = glm::translate(glm::ortho(-size, size, -size, size, -size, size), -centre)]( + const auto & vp) { + return vp * extentsMat; }}); mesh.Draw(); } diff --git a/gfx/gl/shadowStenciller.h b/gfx/gl/shadowStenciller.h index 2edb955..03efced 100644 --- a/gfx/gl/shadowStenciller.h +++ b/gfx/gl/shadowStenciller.h @@ -5,13 +5,15 @@ #include "gfx/models/texture.h" #include "glArrays.h" +class LightDirection; + class ShadowStenciller { public: ShadowStenciller(); [[nodiscard]] static glTexture createStencilTexture(GLsizei width, GLsizei height); - void setLightDirection(const Direction3D & lightDir, const Direction3D & lightDirUp); + void setLightDirection(const LightDirection & lightDir); void renderStencil(const glTexture &, const MeshBase &, const Texture::AnyPtr texture) const; private: @@ -19,6 +21,5 @@ private: Program shadowCaster; Program::RequiredUniformLocation viewProjectionLoc {shadowCaster, "viewProjection"}; - glm::mat4 lightDirMat {}; std::array<glm::mat4, 8> viewProjections; }; diff --git a/test/test-assetFactory.cpp b/test/test-assetFactory.cpp index 0b5b278..6036721 100644 --- a/test/test-assetFactory.cpp +++ b/test/test-assetFactory.cpp @@ -54,7 +54,7 @@ public: environment(const SceneShader &, const SceneRenderer & sceneRenderer) const override { sceneRenderer.setAmbientLight({.4, .4, .4}); - sceneRenderer.setDirectionalLight({.6, .6, .6}, east + south + south + down, *this); + sceneRenderer.setDirectionalLight({.6, .6, .6}, {{0.9, 0.5}}, *this); } void diff --git a/test/test-geoData.cpp b/test/test-geoData.cpp index 11d634d..4a7b98d 100644 --- a/test/test-geoData.cpp +++ b/test/test-geoData.cpp @@ -253,7 +253,7 @@ BOOST_DATA_TEST_CASE(deform, loadFixtureJson<DeformTerrainData>("geoData/deform/ environment(const SceneShader &, const SceneRenderer & sr) const override { sr.setAmbientLight({0.1, 0.1, 0.1}); - sr.setDirectionalLight({1, 1, 1}, south + down, *this); + sr.setDirectionalLight({1, 1, 1}, {{quarter_pi, -3 * half_pi}}, *this); } void diff --git a/test/test-render.cpp b/test/test-render.cpp index 0a92689..775eb5c 100644 --- a/test/test-render.cpp +++ b/test/test-render.cpp @@ -1,4 +1,3 @@ -#include "game/environment.h" #define BOOST_TEST_MODULE test_render #include "testHelpers.h" @@ -8,6 +7,7 @@ #include <boost/test/unit_test.hpp> #include <assetFactory/assetFactory.h> +#include <game/environment.h> #include <game/gamestate.h> #include <game/geoData.h> #include <game/network/rail.h> @@ -166,7 +166,7 @@ BOOST_AUTO_TEST_CASE(terrain) environment(const SceneShader &, const SceneRenderer & sr) const override { sr.setAmbientLight({0.1, 0.1, 0.1}); - sr.setDirectionalLight({1, 1, 1}, south + down, *this); + sr.setDirectionalLight({1, 1, 1}, {{0, quarter_pi}}, *this); } void @@ -213,7 +213,7 @@ BOOST_AUTO_TEST_CASE(railnet) environment(const SceneShader &, const SceneRenderer & sr) const override { sr.setAmbientLight({0.1, 0.1, 0.1}); - sr.setDirectionalLight({1, 1, 1}, south + down, *this); + sr.setDirectionalLight({1, 1, 1}, {{0, quarter_pi}}, *this); } void |