diff --git a/environment/skydome.cpp b/environment/skydome.cpp index 654b3a345..2a12695d9 100644 --- a/environment/skydome.cpp +++ b/environment/skydome.cpp @@ -5,6 +5,27 @@ #include "simulation/simulationenvironment.h" #include "utilities/Globals.h" +namespace { + +// XYZ to Adobe RGB, as the sky was converted up to 26.05. the legacy renderer still gets its sky this way: +// it draws the colours of the dome as they are, with no tonemapping of its own +glm::vec3 +xyz_to_adobe_rgb( glm::vec3 const &XYZ ) { + + // M^-1 for Adobe RGB from http://www.brucelindbloom.com/Eqn_RGB_XYZ_Matrix.html + std::array const mi{ + glm::vec3{ 2.041369f, -0.969266f, 0.0134474f }, + glm::vec3{ -0.5649464f, 1.8760108f, -0.1183897f }, + glm::vec3{ -0.3446944f, 0.041556f, 1.0154096f } }; + + return glm::vec3{ + XYZ.x * mi[ 0 ].x + XYZ.y * mi[ 1 ].x + XYZ.z * mi[ 2 ].x, + XYZ.x * mi[ 0 ].y + XYZ.y * mi[ 1 ].y + XYZ.z * mi[ 2 ].y, + XYZ.x * mi[ 0 ].z + XYZ.y * mi[ 1 ].z + XYZ.z * mi[ 2 ].z }; +} + +} // namespace + // sky gradient based on "A practical analytic model for daylight" // by A. J. Preetham Peter Shirley Brian Smits (University of Utah) @@ -185,6 +206,11 @@ void CSkyDome::RebuildColors() { float twilightfactor = std::clamp( -simulation::Environment.sun().getAngle(), 0.0f, 18.0f ) / 18.0f; auto gammacorrection = glm::mix( glm::vec3( 1.0f ), glm::vec3( 0.45f ), twilightfactor ); + // the legacy renderer has no tonemapping and no srgb framebuffer, so its sky stays as 26.05 made it: + // display-encoded by day (gamma 0.45), with the brightness knee and the plain gradient, and no horizon band. + // the averages taken from these colours give it the ambient light and the fog of 26.05 too + bool const legacy { Global.LegacyRenderer }; + auto const legacygammacorrection = glm::mix( glm::vec3( 0.45f ), glm::vec3( 1.0f ), twilightfactor ); // get zenith luminance auto const chi = static_cast(( 4.0f / 9.0f - m_turbidity / 120.0f ) * ( M_PI - 2.0f * m_thetasun )); @@ -253,7 +279,7 @@ void CSkyDome::RebuildColors() { float const Z = (1.0f - x - y) / y * Y; colorconverter = glm::vec3( X, Y, Z ); - color = colors::XYZtoRGB( colorconverter ); + color = legacy ? xyz_to_adobe_rgb( colorconverter ) : colors::XYZtoRGB( colorconverter ); colorconverter = colors::RGBtoHSV(color); if ( m_linearexpcontrol ) { @@ -264,7 +290,12 @@ void CSkyDome::RebuildColors() { colorconverter.z = 1.0f - std::exp( -m_expfactor * colorconverter.z ); } - colorconverter.z = std::pow( std::max( colorconverter.z, 0.0f ), gammacorrection.x ); + if( false == legacy ) { + colorconverter.z = std::pow( std::max( colorconverter.z, 0.0f ), gammacorrection.x ); + } + else if( colorconverter.z > 0.85f ) { + colorconverter.z = 0.85f + ( colorconverter.z - 0.85f ) * 0.35f; + } colorconverter.y = std::clamp( colorconverter.y * Global.m_skysaturationcorrection, 0.0f, 1.0f ); // desaturate sky colour, based on overcast level @@ -281,7 +312,7 @@ void CSkyDome::RebuildColors() { // this height-based factor is reduced the farther the sun is up in the sky float const shiftfactor = std::clamp( std::lerp(heightbasedphase, sunbasedphase, sunbasedphase), 0.0f, 1.0f ); // h = 210 makes for 'typical' sky tone - glm::vec3 const skytonecolor = colors::HSVtoRGB( glm::vec3( 210.0f, 0.5f, colorconverter.z ) ); + glm::vec3 const skytonecolor = colors::HSVtoRGB( glm::vec3( 210.0f, legacy ? colorconverter.y : 0.5f, colorconverter.z ) ); color = colors::HSVtoRGB( colorconverter ); color = glm::mix( color, skytonecolor, shiftfactor * Global.m_skyhuecorrection ); @@ -296,14 +327,21 @@ void CSkyDome::RebuildColors() { color.x = 0.20f * color.z; color.y = 0.65f * color.z; } - // simple gradient, darkening towards the top - color *= std::clamp( 1.0f - vertex.y * 0.75f, 0.0f, 1.f ); + if( legacy ) { + // simple gradient, darkening towards the top, then the gamma correction + color *= std::clamp( 1.0f - vertex.y, 0.f, 1.f ); + color = glm::pow( glm::max( color, glm::vec3( 0.f ) ), legacygammacorrection ); + } + else { + // simple gradient, darkening towards the top + color *= std::clamp( 1.0f - vertex.y * 0.75f, 0.0f, 1.f ); - float const horizonboost = 1.5f + m_overcast; - float const horizonbandwidth = 0.2f; // boost tapers to 0 by ~11.5 degrees elevation - float const horizonband = std::clamp( 1.0f - vertex.y / horizonbandwidth, 0.0f, 1.0f ); + float const horizonboost = 1.5f + m_overcast; + float const horizonbandwidth = 0.2f; // boost tapers to 0 by ~11.5 degrees elevation + float const horizonband = std::clamp( 1.0f - vertex.y / horizonbandwidth, 0.0f, 1.0f ); - color *= std::lerp( 1.0, horizonboost, horizonband ); + color *= std::lerp( 1.0, horizonboost, horizonband ); + } //color *= ( 0.25f - vertex.y ); m_colours[ i ] = color; diff --git a/simulation/simulationenvironment.cpp b/simulation/simulationenvironment.cpp index 4e8c0e5f7..57c1074a7 100644 --- a/simulation/simulationenvironment.cpp +++ b/simulation/simulationenvironment.cpp @@ -190,7 +190,9 @@ world_environment::update() { // update the fog. setting it to match the average colour of the sky dome is cheap // but quite effective way to make the distant items blend with background better - Global.FogColor = m_skydome.GetAverageHorizonColor() * keylightcolor * + // the legacy renderer keeps the fog of 26.05, lit by the key light at its diffuse level + glm::vec3 const foglightcolor { Global.LegacyRenderer ? keylightcolor * diffuselevel : keylightcolor }; + Global.FogColor = m_skydome.GetAverageHorizonColor() * foglightcolor * std::clamp((float)Global.fLuminance, 0.f, 1.f); diff --git a/simulation/simulationtest.cpp b/simulation/simulationtest.cpp index d32fd0e72..5fb80adae 100644 --- a/simulation/simulationtest.cpp +++ b/simulation/simulationtest.cpp @@ -62,6 +62,10 @@ void apply_test_mode_settings() Global.local_start_vehicle = "ghostview"; Global.GfxRenderer = "null"; Global.NvRenderer = false; + // the flags of the gfxrenderer line read from the settings go with it: the legacy one picks the colours of the sky, + // and with them the light level the lamps and the AI go by, so a test run would depend on the renderer of the ini + Global.LegacyRenderer = false; + Global.BasicRenderer = false; Global.bSoundEnabled = false; Global.python_enabled = false; Global.bInactivePause = false;