From a9a2a4c8b5aad22d055c670c72eb41fc22b0d812 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 5 Oct 2024 12:17:55 +0100 Subject: Add LightDirection class --- gfx/lightDirection.cpp | 8 ++++++++ gfx/lightDirection.h | 32 ++++++++++++++++++++++++++++++++ test/test-environment.cpp | 27 +++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 gfx/lightDirection.cpp create mode 100644 gfx/lightDirection.h diff --git a/gfx/lightDirection.cpp b/gfx/lightDirection.cpp new file mode 100644 index 0000000..6d7d1ea --- /dev/null +++ b/gfx/lightDirection.cpp @@ -0,0 +1,8 @@ +#include "lightDirection.h" +#include "maths.h" + +LightDirection::LightDirection(const Direction2D sunPos) : + pos {sunPos}, vec {glm::mat3 {rotate_yp(pi + sunPos.x, -sunPos.y)} * north}, + vert {-glm::clamp(-1.F, 0.F, vec.z - 0.1F)} +{ +} diff --git a/gfx/lightDirection.h b/gfx/lightDirection.h new file mode 100644 index 0000000..f1e1cc4 --- /dev/null +++ b/gfx/lightDirection.h @@ -0,0 +1,32 @@ +#pragma once + +#include "config/types.h" + +class LightDirection { +public: + // NOLINTNEXTLINE(hicpp-explicit-conversions) deliberately a helper + LightDirection(Direction2D sunPos); + + [[nodiscard]] Direction2D + position() const noexcept + { + return pos; + } + + [[nodiscard]] Direction3D + vector() const noexcept + { + return vec; + } + + [[nodiscard]] float + vertical() const noexcept + { + return vert; + } + +private: + Direction2D pos; + Direction3D vec; + float vert; +}; diff --git a/test/test-environment.cpp b/test/test-environment.cpp index b6e0e4f..76144a4 100644 --- a/test/test-environment.cpp +++ b/test/test-environment.cpp @@ -1,4 +1,5 @@ #define BOOST_TEST_MODULE environment +#include "testHelpers.h" #include #include #include @@ -7,9 +8,11 @@ #include #include #include +#include #include using sunPosTestData = std::tuple; +using sunDirTestData = std::tuple; constexpr Direction2D Doncaster = {-1.1, 53.5}; constexpr Direction2D NewYork = {74.0, 40.7}; constexpr Direction2D Syndey = {-151.2, -33.9}; @@ -19,6 +22,7 @@ BOOST_DATA_TEST_CASE(sun_position, boost::unit_test::data::make({ {EqGM, "2024-01-02T00:00:00"_time_t, {181.52F, -66.86F}}, {EqGM, "2024-01-02T06:00:00"_time_t, {113.12F, -0.85F}}, + {EqGM, "2024-01-02T06:30:00"_time_t, {113.12F, 6.05F}}, {EqGM, "2024-01-02T12:00:00"_time_t, {177.82F, 66.97F}}, {EqGM, "2024-01-02T18:00:00"_time_t, {246.99F, 0.90F}}, {EqGM, "2024-01-03T00:00:00"_time_t, {181.52F, -67.04F}}, @@ -33,3 +37,26 @@ BOOST_DATA_TEST_CASE(sun_position, BOOST_CHECK_CLOSE(sunPos.x, expSunPos.x, 1.F); BOOST_CHECK_CLOSE(sunPos.y, expSunPos.y, 1.F); } + +BOOST_DATA_TEST_CASE(sun_direction, + boost::unit_test::data::make({ + {{0.F, 0.F}, south, 0.1F}, + {{90.F, 0.F}, west, 0.1F}, + {{-90.F, 0.F}, east, 0.1F}, + // From above + // EqGM midnight, sun below horizon, shining upwards + {{181.52F, -66.86F}, {-0.01F, 0.39F, 0.919F}, 0}, + // EqGM just before sunrise, mostly west, north a bit, up a bit + {{113.12F, -0.85F}, {-0.92F, 0.39F, 0.015F}, 0.085F}, + // EqGM just after sunrise, mostly west, north a bit, down a bit + {{113.12F, 6.05F}, {-0.92F, 0.39F, -0.015F}, 0.205F}, + // Doncaster noon, roughly from south to north, high in the sky, downward + {{176.34F, 59.64F}, {-0.03F, 0.5F, -0.86F}, 0.96F}, + }), + position, direction, vert) +{ + const LightDirection ld {position * degreesToRads}; + BOOST_CHECK_CLOSE_VEC(ld.vector(), direction); + BOOST_CHECK_CLOSE(glm::length(ld.vector()), 1.F, 1); + BOOST_CHECK_CLOSE(ld.vertical(), vert, 5); +} -- cgit v1.2.3