]> git.lizzy.rs Git - minetest.git/blob - src/client/sky.h
17654501575ce7e961389e1ebf97fa18f245e036
[minetest.git] / src / client / sky.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <ISceneNode.h>
21 #include <array>
22 #include "camera.h"
23 #include "irrlichttypes_extrabloated.h"
24 #include "irr_ptr.h"
25 #include "skyparams.h"
26
27 #pragma once
28
29 #define SKY_MATERIAL_COUNT 12
30
31 class ITextureSource;
32
33 // Skybox, rendered with zbuffer turned off, before all other nodes.
34 class Sky : public scene::ISceneNode
35 {
36 public:
37         //! constructor
38         Sky(s32 id, ITextureSource *tsrc);
39
40         virtual void OnRegisterSceneNode();
41
42         //! renders the node.
43         virtual void render();
44
45         virtual const aabb3f &getBoundingBox() const { return m_box; }
46
47         // Used by Irrlicht for optimizing rendering
48         virtual video::SMaterial &getMaterial(u32 i) { return m_materials[i]; }
49         virtual u32 getMaterialCount() const { return SKY_MATERIAL_COUNT; }
50
51         void update(float m_time_of_day, float time_brightness, float direct_brightness,
52                         bool sunlight_seen, CameraMode cam_mode, float yaw, float pitch);
53
54         float getBrightness() { return m_brightness; }
55
56         const video::SColor &getBgColor() const
57         {
58                 return m_visible ? m_bgcolor : m_fallback_bg_color;
59         }
60
61         const video::SColor &getSkyColor() const
62         {
63                 return m_visible ? m_skycolor : m_fallback_bg_color;
64         }
65
66         void setSunVisible(bool sun_visible) { m_sun_params.visible = sun_visible; }
67         void setSunTexture(std::string sun_texture,
68                 std::string sun_tonemap, ITextureSource *tsrc);
69         void setSunScale(f32 sun_scale) { m_sun_params.scale = sun_scale; }
70         void setSunriseVisible(bool glow_visible) { m_sun_params.sunrise_visible = glow_visible; }
71         void setSunriseTexture(std::string sunglow_texture, ITextureSource* tsrc);
72
73         void setMoonVisible(bool moon_visible) { m_moon_params.visible = moon_visible; }
74         void setMoonTexture(std::string moon_texture,
75                 std::string moon_tonemap, ITextureSource *tsrc);
76         void setMoonScale(f32 moon_scale) { m_moon_params.scale = moon_scale; }
77
78         void setStarsVisible(bool stars_visible) { m_star_params.visible = stars_visible; }
79         void setStarCount(u16 star_count, bool force_update);
80         void setStarColor(video::SColor star_color) { m_star_params.starcolor = star_color; }
81         void setStarScale(f32 star_scale) { m_star_params.scale = star_scale; updateStars(); }
82
83         bool getCloudsVisible() const { return m_clouds_visible && m_clouds_enabled; }
84         const video::SColorf &getCloudColor() const { return m_cloudcolor_f; }
85
86         void setVisible(bool visible) { m_visible = visible; }
87         // Set only from set_sky API
88         void setCloudsEnabled(bool clouds_enabled) { m_clouds_enabled = clouds_enabled; }
89         void setFallbackBgColor(const video::SColor &fallback_bg_color)
90         {
91                 m_fallback_bg_color = fallback_bg_color;
92         }
93         void overrideColors(const video::SColor &bgcolor, const video::SColor &skycolor)
94         {
95                 m_bgcolor = bgcolor;
96                 m_skycolor = skycolor;
97         }
98         void setSkyColors(const SkyColor &sky_color);
99         void setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
100                 std::string use_sun_tint);
101         void setInClouds(bool clouds) { m_in_clouds = clouds; }
102         void clearSkyboxTextures() { m_sky_params.textures.clear(); }
103         void addTextureToSkybox(std::string texture, int material_id,
104                 ITextureSource *tsrc);
105 private:
106         aabb3f m_box;
107         video::SMaterial m_materials[SKY_MATERIAL_COUNT];
108         // How much sun & moon transition should affect horizon color
109         float m_horizon_blend()
110         {
111                 if (!m_sunlight_seen)
112                         return 0;
113                 float x = m_time_of_day >= 0.5 ? (1 - m_time_of_day) * 2
114                                                : m_time_of_day * 2;
115
116                 if (x <= 0.3)
117                         return 0;
118                 if (x <= 0.4) // when the sun and moon are aligned
119                         return (x - 0.3) * 10;
120                 if (x <= 0.5)
121                         return (0.5 - x) * 10;
122                 return 0;
123         }
124
125         // Mix two colors by a given amount
126         video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
127         {
128                 video::SColor result = video::SColor(
129                                 col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor,
130                                 col1.getRed() * (1 - factor) + col2.getRed() * factor,
131                                 col1.getGreen() * (1 - factor) + col2.getGreen() * factor,
132                                 col1.getBlue() * (1 - factor) + col2.getBlue() * factor);
133                 return result;
134         }
135         video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
136         {
137                 video::SColorf result =
138                                 video::SColorf(col1.r * (1 - factor) + col2.r * factor,
139                                                 col1.g * (1 - factor) + col2.g * factor,
140                                                 col1.b * (1 - factor) + col2.b * factor,
141                                                 col1.a * (1 - factor) + col2.a * factor);
142                 return result;
143         }
144
145         bool m_visible = true;
146         // Used when m_visible=false
147         video::SColor m_fallback_bg_color = video::SColor(255, 255, 255, 255);
148         bool m_first_update = true;
149         float m_time_of_day;
150         float m_time_brightness;
151         bool m_sunlight_seen;
152         float m_brightness = 0.5f;
153         float m_cloud_brightness = 0.5f;
154         bool m_clouds_visible; // Whether clouds are disabled due to player underground
155         bool m_clouds_enabled = true; // Initialised to true, reset only by set_sky API
156         bool m_directional_colored_fog;
157         bool m_in_clouds = true; // Prevent duplicating bools to remember old values
158
159         video::SColorf m_bgcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
160         video::SColorf m_skycolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
161         video::SColorf m_cloudcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
162         video::SColor m_bgcolor;
163         video::SColor m_skycolor;
164         video::SColorf m_cloudcolor_f;
165
166         // pure white: becomes "diffuse light component" for clouds
167         video::SColorf m_cloudcolor_day_f = video::SColorf(1, 1, 1, 1);
168         // dawn-factoring version of pure white (note: R is above 1.0)
169         video::SColorf m_cloudcolor_dawn_f = video::SColorf(
170                 255.0f/240.0f,
171                 223.0f/240.0f,
172                 191.0f/255.0f
173         );
174
175         SkyboxParams m_sky_params;
176         SunParams m_sun_params;
177         MoonParams m_moon_params;
178         StarParams m_star_params;
179
180         bool m_default_tint = true;
181
182         irr_ptr<scene::SMeshBuffer> m_stars;
183
184         video::ITexture *m_sun_texture;
185         video::ITexture *m_moon_texture;
186         video::ITexture *m_sun_tonemap;
187         video::ITexture *m_moon_tonemap;
188
189         void updateStars();
190         void updateStarsColor(video::SColor color);
191
192         void draw_sun(video::IVideoDriver *driver, float sunsize, const video::SColor &suncolor,
193                 const video::SColor &suncolor2, float wicked_time_of_day);
194         void draw_moon(video::IVideoDriver *driver, float moonsize, const video::SColor &mooncolor,
195                 const video::SColor &mooncolor2, float wicked_time_of_day);
196         void draw_sky_body(std::array<video::S3DVertex, 4> &vertices,
197                 float pos_1, float pos_2, const video::SColor &c);
198         void draw_stars(video::IVideoDriver *driver, float wicked_time_of_day);
199         void place_sky_body(std::array<video::S3DVertex, 4> &vertices,
200                 float horizon_position, float day_position);
201         void setSkyDefaults();
202 };