]> git.lizzy.rs Git - minetest.git/commitdiff
Use Sky class to obtain directional light source position for shadows (#12662)
authorx2048 <codeforsmile@gmail.com>
Wed, 17 Aug 2022 14:30:05 +0000 (16:30 +0200)
committerGitHub <noreply@github.com>
Wed, 17 Aug 2022 14:30:05 +0000 (16:30 +0200)
* Also remove unused Sky::getSkyBodyOrbitTilt method

Fixes misalignment of sun position and shadow direction at high tilt values.

src/client/game.cpp
src/client/sky.cpp
src/client/sky.h

index 0e86de4963e08aca2fe4e219ffc53364155fdf72..34bfb5aa61e71a6daf86416782ced2583814f9cd 100644 (file)
@@ -4065,10 +4065,7 @@ void Game::updateShadows()
        timeoftheday = fmod(timeoftheday + 0.75f, 0.5f) + 0.25f;
        const float offset_constant = 10000.0f;
 
-       v3f light(0.0f, 0.0f, -1.0f);
-       light.rotateXZBy(90);
-       light.rotateXYBy(timeoftheday * 360 - 90);
-       light.rotateYZBy(sky->getSkyBodyOrbitTilt());
+       v3f light = is_day ? sky->getSunDirection() : sky->getMoonDirection();
 
        v3f sun_pos = light * offset_constant;
 
index ca56889b410ab9b3fe3d82aaffabd09a9a2cce85..5541b16aa3e25383e29128f8773b37a558fa431e 100644 (file)
@@ -554,6 +554,25 @@ void Sky::update(float time_of_day, float time_brightness,
        }
 }
 
+static v3f getSkyBodyPosition(float horizon_position, float day_position, float orbit_tilt)
+{
+       v3f result = v3f(0, 0, -1);
+       result.rotateXZBy(horizon_position);
+       result.rotateXYBy(day_position);
+       result.rotateYZBy(orbit_tilt);
+       return result;
+}
+
+v3f Sky::getSunDirection()
+{
+       return getSkyBodyPosition(90, getWickedTimeOfDay(m_time_of_day) * 360 - 90, m_sky_body_orbit_tilt);
+}
+
+v3f Sky::getMoonDirection()
+{
+       return getSkyBodyPosition(270, getWickedTimeOfDay(m_time_of_day) * 360 - 90, m_sky_body_orbit_tilt);
+}
+
 void Sky::draw_sun(video::IVideoDriver *driver, float sunsize, const video::SColor &suncolor,
        const video::SColor &suncolor2, float wicked_time_of_day)
        /* Draw sun in the sky.
@@ -703,15 +722,13 @@ void Sky::place_sky_body(
        * day_position: turn the body around the Z axis, to place it depending of the time of the day
        */
 {
-       v3f centrum(0, 0, -1);
-       centrum.rotateXZBy(horizon_position);
-       centrum.rotateXYBy(day_position);
-       centrum.rotateYZBy(m_sky_body_orbit_tilt);
+       v3f centrum = getSkyBodyPosition(horizon_position, day_position, m_sky_body_orbit_tilt);
+       v3f untilted_centrum = getSkyBodyPosition(horizon_position, day_position, 0.f);
        for (video::S3DVertex &vertex : vertices) {
                // Body is directed to -Z (south) by default
                vertex.Pos.rotateXZBy(horizon_position);
                vertex.Pos.rotateXYBy(day_position);
-               vertex.Pos.Z += centrum.Z;
+               vertex.Pos += centrum - untilted_centrum;
        }
 }
 
index cbb1186aac05528d4bcfac78dcaac755882e9e14..381abd0b9a08fa199c85ed5d5b5f76df8347d9b0 100644 (file)
@@ -71,12 +71,14 @@ class Sky : public scene::ISceneNode
        void setSunScale(f32 sun_scale) { m_sun_params.scale = sun_scale; }
        void setSunriseVisible(bool glow_visible) { m_sun_params.sunrise_visible = glow_visible; }
        void setSunriseTexture(const std::string &sunglow_texture, ITextureSource* tsrc);
+       v3f getSunDirection();
 
        void setMoonVisible(bool moon_visible) { m_moon_params.visible = moon_visible; }
        bool getMoonVisible() const { return m_moon_params.visible; }
        void setMoonTexture(const std::string &moon_texture,
                const std::string &moon_tonemap, ITextureSource *tsrc);
        void setMoonScale(f32 moon_scale) { m_moon_params.scale = moon_scale; }
+       v3f getMoonDirection();
 
        void setStarsVisible(bool stars_visible) { m_star_params.visible = stars_visible; }
        void setStarCount(u16 star_count);
@@ -108,8 +110,6 @@ class Sky : public scene::ISceneNode
                ITextureSource *tsrc);
        const video::SColorf &getCurrentStarColor() const { return m_star_color; }
 
-       float getSkyBodyOrbitTilt() const { return m_sky_body_orbit_tilt; }
-
 private:
        aabb3f m_box;
        video::SMaterial m_materials[SKY_MATERIAL_COUNT];