]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/sky.cpp
Fix building under MSVC
[dragonfireclient.git] / src / sky.cpp
index 76dcac8b7fcd68ba4eb998b3ca0a44e95f6dea6f..e8fd3a1d8564658ffd4df39f8f8815742be02504 100644 (file)
@@ -3,21 +3,24 @@
 #include "ISceneManager.h"
 #include "ICameraSceneNode.h"
 #include "S3DVertex.h"
-#include "utility.h" // MYMIN
 #include "tile.h" // getTexturePath
 #include "noise.h" // easeCurve
 #include "main.h" // g_profiler
 #include "profiler.h"
+#include "util/numeric.h" // MYMIN
+#include <cmath>
+#include "settings.h"
 
 //! constructor
-Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id):
+Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id, LocalPlayer* player):
                scene::ISceneNode(parent, mgr, id),
                m_first_update(true),
                m_brightness(0.5),
                m_cloud_brightness(0.5),
                m_bgcolor_bright_f(1,1,1,1),
                m_skycolor_bright_f(1,1,1,1),
-               m_cloudcolor_bright_f(1,1,1,1)
+               m_cloudcolor_bright_f(1,1,1,1),
+               m_player(player)
 {
        setAutomaticCulling(scene::EAC_OFF);
        Box.MaxEdge.set(0,0,0);
@@ -54,6 +57,8 @@ Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id):
                );
                m_stars[i].normalize();
        }
+
+       m_directional_colored_fog = g_settings->getBool("directional_colored_fog");
 }
 
 void Sky::OnRegisterSceneNode()
@@ -431,11 +436,17 @@ void Sky::update(float time_of_day, float time_brightness,
                        video::SColor(255, 240,240,255);
        //video::SColorf cloudcolor_bright_dawn_f(1.0, 0.591, 0.4);
        //video::SColorf cloudcolor_bright_dawn_f(1.0, 0.65, 0.44);
-       video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
+       //video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
+       video::SColorf cloudcolor_bright_dawn_f(1.0, 0.875, 0.75);
 
+       float cloud_color_change_fraction = 0.95;
        if(sunlight_seen){
-               //m_brightness = m_brightness * 0.95 + direct_brightness * 0.05;
-               m_brightness = m_brightness * 0.95 + time_brightness * 0.05;
+               if(fabs(time_brightness - m_brightness) < 0.2){
+                       m_brightness = m_brightness * 0.95 + time_brightness * 0.05;
+               } else {
+                       m_brightness = m_brightness * 0.80 + time_brightness * 0.20;
+                       cloud_color_change_fraction = 0.0;
+               }
        }
        else{
                if(direct_brightness < m_brightness)
@@ -465,41 +476,88 @@ void Sky::update(float time_of_day, float time_brightness,
        } else {
                m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
                                bgcolor_bright_indoor_f, color_change_fraction);
-               m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
-                               cloudcolor_bright_normal_f, color_change_fraction);
                m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
                                bgcolor_bright_indoor_f, color_change_fraction);
+               m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
+                               cloudcolor_bright_normal_f, color_change_fraction);
                m_clouds_visible = false;
        }
+
+       // Horizon coloring based on sun and moon direction during sunset and sunrise
+       video::SColor pointcolor = video::SColor(255, 255, 255, m_bgcolor.getAlpha());
+       if (m_directional_colored_fog) {
+               if (m_horizon_blend() != 0)
+               {
+                       // calculate hemisphere value from yaw
+                       f32 pointcolor_blend = wrapDegrees_0_360(m_player->getYaw() + 90);
+                       if (pointcolor_blend > 180)
+                               pointcolor_blend = 360 - pointcolor_blend;
+                       pointcolor_blend /= 180;
+                       // bound view angle to determine where transition starts and ends
+                       pointcolor_blend = rangelim(1 - pointcolor_blend * 1.375, 0, 1 / 1.375) * 1.375;
+                       // combine the colors when looking up or down, otherwise turning looks weird
+                       pointcolor_blend += (0.5 - pointcolor_blend) * (1 - MYMIN((90 - std::abs(m_player->getPitch())) / 90 * 1.5, 1));
+                       // invert direction to match where the sun and moon are rising
+                       if (m_time_of_day > 0.5)
+                               pointcolor_blend = 1 - pointcolor_blend;
+
+                       // horizon colors of sun and moon
+                       f32 pointcolor_light = rangelim(m_time_brightness * 3, 0.2, 1);
+                       video::SColorf pointcolor_sun_f(1, 1, 1, 1);
+                       pointcolor_sun_f.r = pointcolor_light * 1;
+                       pointcolor_sun_f.b = pointcolor_light * (0.25 + (rangelim(m_time_brightness, 0.25, 0.75) - 0.25) * 2 * 0.75);
+                       pointcolor_sun_f.g = pointcolor_light * (pointcolor_sun_f.b * 0.375 + (rangelim(m_time_brightness, 0.05, 0.15) - 0.05) * 10 * 0.625);
+                       video::SColorf pointcolor_moon_f(0.5 * pointcolor_light, 0.6 * pointcolor_light, 0.8 * pointcolor_light, 1);
+                       video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
+                       video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
+                       // calculate the blend color
+                       pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
+               }
+       }
+
        video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
        m_bgcolor = video::SColor(
-                       255,
-                       bgcolor_bright.getRed() * m_brightness,
-                       bgcolor_bright.getGreen() * m_brightness,
-                       bgcolor_bright.getBlue() * m_brightness);
-       
+               255,
+               bgcolor_bright.getRed() * m_brightness,
+               bgcolor_bright.getGreen() * m_brightness,
+               bgcolor_bright.getBlue() * m_brightness);
+       if (m_directional_colored_fog) {
+               m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
+       }
+
        video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
        m_skycolor = video::SColor(
-                       255,
-                       skycolor_bright.getRed() * m_brightness,
-                       skycolor_bright.getGreen() * m_brightness,
-                       skycolor_bright.getBlue() * m_brightness);
-       
+               255,
+               skycolor_bright.getRed() * m_brightness,
+               skycolor_bright.getGreen() * m_brightness,
+               skycolor_bright.getBlue() * m_brightness);
+       if (m_directional_colored_fog) {
+               m_skycolor = m_mix_scolor(m_skycolor, pointcolor, m_horizon_blend() * 0.25);
+       }
+
        float cloud_direct_brightness = 0;
-       if(sunlight_seen){
-               cloud_direct_brightness = time_brightness;
-               if(time_brightness >= 0.2 && time_brightness < 0.7)
+       if(sunlight_seen) {
+               if (!m_directional_colored_fog) {
+                       cloud_direct_brightness = time_brightness;
+                       if(time_brightness >= 0.2 && time_brightness < 0.7)
                                cloud_direct_brightness *= 1.3;
+               }
+               else {
+                       cloud_direct_brightness = MYMIN(m_horizon_blend() * 0.15 + m_time_brightness, 1);
+               }
        } else {
                cloud_direct_brightness = direct_brightness;
        }
-       m_cloud_brightness = m_cloud_brightness * 0.95 +
-                       cloud_direct_brightness * (1.0 - 0.95);
+       m_cloud_brightness = m_cloud_brightness * cloud_color_change_fraction +
+                       cloud_direct_brightness * (1.0 - cloud_color_change_fraction);
        m_cloudcolor_f = video::SColorf(
-                       m_cloudcolor_bright_f.getRed() * m_cloud_brightness,
-                       m_cloudcolor_bright_f.getGreen() * m_cloud_brightness,
-                       m_cloudcolor_bright_f.getBlue() * m_cloud_brightness,
+                       m_cloudcolor_bright_f.r * m_cloud_brightness,
+                       m_cloudcolor_bright_f.g * m_cloud_brightness,
+                       m_cloudcolor_bright_f.b * m_cloud_brightness,
                        1.0);
+       if (m_directional_colored_fog) {
+               m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f, video::SColorf(pointcolor), m_horizon_blend() * 0.25);
+       }
 
 }