Modernize client code (#6250)
[oweals/minetest.git] / src / sky.cpp
index 5414f74bd5056d0f9578cce1f663052882036f6e..d92697f3041430fbbd0e30a47fd3d221f6e9ceb7 100644 (file)
@@ -8,21 +8,14 @@
 #include "profiler.h"
 #include "util/numeric.h"
 #include <cmath>
+#include "client/renderingengine.h"
 #include "settings.h"
 #include "camera.h"  // CameraModes
 
 
-Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id,
-               ITextureSource *tsrc):
-               scene::ISceneNode(parent, mgr, id),
-               m_visible(true),
-               m_fallback_bg_color(255, 255, 255, 255),
-               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)
+Sky::Sky(s32 id, ITextureSource *tsrc):
+               scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
+                       RenderingEngine::get_scene_manager(), id)
 {
        setAutomaticCulling(scene::EAC_OFF);
        m_box.MaxEdge.set(0, 0, 0);
@@ -85,8 +78,6 @@ Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id,
        }
 
        m_directional_colored_fog = g_settings->getBool("directional_colored_fog");
-
-       m_clouds_enabled = true;
 }
 
 
@@ -109,7 +100,7 @@ void Sky::render()
 
        if (!camera || !driver)
                return;
-       
+
        ScopeProfiler sp(g_profiler, "Sky::render()", SPT_AVG);
 
        // Draw perspective skybox
@@ -138,7 +129,7 @@ void Sky::render()
                float moonsize = 0.04;
                video::SColorf mooncolor_f(0.50, 0.57, 0.65, 1);
                video::SColorf mooncolor2_f(0.85, 0.875, 0.9, 1);
-               
+
                float nightlength = 0.415;
                float wn = nightlength / 2;
                float wicked_time_of_day = 0;
@@ -181,11 +172,11 @@ void Sky::render()
                const f32 o = 0.0f;
                static const u16 indices[4] = {0, 1, 2, 3};
                video::S3DVertex vertices[4];
-               
+
                driver->setMaterial(m_materials[1]);
-               
+
                video::SColor cloudyfogcolor = m_bgcolor;
-               
+
                // Draw far cloudy fog thing blended with skycolor
                for (u32 j = 0; j < 4; j++) {
                        video::SColor c = cloudyfogcolor.getInterpolated(m_skycolor, 0.45);
@@ -242,6 +233,10 @@ void Sky::render()
                vertices[3] = video::S3DVertex(-1, -1.0, 1, 0, 1, 0, c, t, o);
                driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
 
+               // If sun, moon and stars are (temporarily) disabled, abort here
+               if (!m_bodies_visible)
+                       return;
+
                driver->setMaterial(m_materials[2]);
 
                // Draw sunrise/sunset horizon glow texture (textures/base/pack/sunrisebg.png)
@@ -361,7 +356,7 @@ void Sky::render()
                                        vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
                                }
                                driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
-                       
+
                                d = moonsize * 1.3;
                                c = mooncolor;
                                c.setAlpha(0.15 * 255);
@@ -421,8 +416,8 @@ void Sky::render()
                }
 
                // Draw stars
-               driver->setMaterial(m_materials[1]);
                do {
+                       driver->setMaterial(m_materials[1]);
                        float starbrightness = MYMAX(0, MYMIN(1,
                                (0.285 - fabs(wicked_time_of_day < 0.5 ?
                                wicked_time_of_day : (1.0 - wicked_time_of_day))) * 10));
@@ -466,7 +461,7 @@ void Sky::render()
                                indices, SKY_STAR_COUNT, video::EVT_STANDARD,
                                scene::EPT_QUADS, video::EIT_16BIT);
                } while(0);
-               
+
                // Draw far cloudy fog thing below east and west horizons
                for (u32 j = 0; j < 2; j++) {
                        video::SColor c = cloudyfogcolor;
@@ -510,7 +505,8 @@ void Sky::update(float time_of_day, float time_brightness,
        m_time_of_day = time_of_day;
        m_time_brightness = time_brightness;
        m_sunlight_seen = sunlight_seen;
-       
+       m_bodies_visible = true;
+
        bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
 
        /*
@@ -535,7 +531,7 @@ void Sky::update(float time_of_day, float time_brightness,
        video::SColorf skycolor_bright_normal_f = video::SColor(255, 140, 186, 250);
        video::SColorf skycolor_bright_dawn_f = video::SColor(255, 180, 186, 250);
        video::SColorf skycolor_bright_night_f = video::SColor(255, 0, 107, 255);
-       
+
        // pure white: becomes "diffuse light component" for clouds
        video::SColorf cloudcolor_bright_normal_f = video::SColor(255, 255, 255, 255);
        // dawn-factoring version of pure white (note: R is above 1.0)
@@ -555,7 +551,7 @@ void Sky::update(float time_of_day, float time_brightness,
                else
                        m_brightness = m_brightness * 0.98 + direct_brightness * 0.02;
        }
-       
+
        m_clouds_visible = true;
        float color_change_fraction = 0.98;
        if (sunlight_seen) {
@@ -609,7 +605,7 @@ void Sky::update(float time_of_day, float time_brightness,
        );
 
        // Horizon coloring based on sun and moon direction during sunset and sunrise
-       video::SColor pointcolor = video::SColor(255, 255, 255, m_bgcolor.getAlpha());
+       video::SColor pointcolor = video::SColor(m_bgcolor.getAlpha(), 255, 255, 255);
        if (m_directional_colored_fog) {
                if (m_horizon_blend() != 0) {
                        // Calculate hemisphere value from yaw, (inverted in third person front view)