]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/sky.h
Fall back to default when rendering mode (3d_mode) is set invalid (#10922)
[dragonfireclient.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 "irrlichttypes_extrabloated.h"
21 #include <ISceneNode.h>
22 #include <array>
23 #include "camera.h"
24 #include "irr_ptr.h"
25 #include "shader.h"
26 #include "skyparams.h"
27
28 #pragma once
29
30 #define SKY_MATERIAL_COUNT 12
31
32 class ITextureSource;
33
34 // Skybox, rendered with zbuffer turned off, before all other nodes.
35 class Sky : public scene::ISceneNode
36 {
37 public:
38         //! constructor
39         Sky(s32 id, ITextureSource *tsrc, IShaderSource *ssrc);
40
41         virtual void OnRegisterSceneNode();
42
43         //! renders the node.
44         virtual void render();
45
46         virtual const aabb3f &getBoundingBox() const { return m_box; }
47
48         // Used by Irrlicht for optimizing rendering
49         virtual video::SMaterial &getMaterial(u32 i) { return m_materials[i]; }
50         virtual u32 getMaterialCount() const { return SKY_MATERIAL_COUNT; }
51
52         void update(float m_time_of_day, float time_brightness, float direct_brightness,
53                         bool sunlight_seen, CameraMode cam_mode, float yaw, float pitch);
54
55         float getBrightness() { return m_brightness; }
56
57         const video::SColor &getBgColor() const
58         {
59                 return m_visible ? m_bgcolor : m_fallback_bg_color;
60         }
61
62         const video::SColor &getSkyColor() const
63         {
64                 return m_visible ? m_skycolor : m_fallback_bg_color;
65         }
66
67         void setSunVisible(bool sun_visible) { m_sun_params.visible = sun_visible; }
68         void setSunTexture(std::string sun_texture,
69                 std::string sun_tonemap, ITextureSource *tsrc);
70         void setSunScale(f32 sun_scale) { m_sun_params.scale = sun_scale; }
71         void setSunriseVisible(bool glow_visible) { m_sun_params.sunrise_visible = glow_visible; }
72         void setSunriseTexture(std::string sunglow_texture, ITextureSource* tsrc);
73
74         void setMoonVisible(bool moon_visible) { m_moon_params.visible = moon_visible; }
75         void setMoonTexture(std::string moon_texture,
76                 std::string moon_tonemap, ITextureSource *tsrc);
77         void setMoonScale(f32 moon_scale) { m_moon_params.scale = moon_scale; }
78
79         void setStarsVisible(bool stars_visible) { m_star_params.visible = stars_visible; }
80         void setStarCount(u16 star_count, bool force_update);
81         void setStarColor(video::SColor star_color) { m_star_params.starcolor = star_color; }
82         void setStarScale(f32 star_scale) { m_star_params.scale = star_scale; updateStars(); }
83
84         bool getCloudsVisible() const { return m_clouds_visible && m_clouds_enabled; }
85         const video::SColorf &getCloudColor() const { return m_cloudcolor_f; }
86
87         void setVisible(bool visible) { m_visible = visible; }
88         // Set only from set_sky API
89         void setCloudsEnabled(bool clouds_enabled) { m_clouds_enabled = clouds_enabled; }
90         void setFallbackBgColor(const video::SColor &fallback_bg_color)
91         {
92                 m_fallback_bg_color = fallback_bg_color;
93         }
94         void overrideColors(const video::SColor &bgcolor, const video::SColor &skycolor)
95         {
96                 m_bgcolor = bgcolor;
97                 m_skycolor = skycolor;
98         }
99         void setSkyColors(const SkyColor &sky_color);
100         void setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
101                 std::string use_sun_tint);
102         void setInClouds(bool clouds) { m_in_clouds = clouds; }
103         void clearSkyboxTextures() { m_sky_params.textures.clear(); }
104         void addTextureToSkybox(std::string texture, int material_id,
105                 ITextureSource *tsrc);
106         const video::SColorf &getCurrentStarColor() const { return m_star_color; }
107
108 private:
109         aabb3f m_box;
110         video::SMaterial m_materials[SKY_MATERIAL_COUNT];
111         // How much sun & moon transition should affect horizon color
112         float m_horizon_blend()
113         {
114                 if (!m_sunlight_seen)
115                         return 0;
116                 float x = m_time_of_day >= 0.5 ? (1 - m_time_of_day) * 2
117                                                : m_time_of_day * 2;
118
119                 if (x <= 0.3)
120                         return 0;
121                 if (x <= 0.4) // when the sun and moon are aligned
122                         return (x - 0.3) * 10;
123                 if (x <= 0.5)
124                         return (0.5 - x) * 10;
125                 return 0;
126         }
127
128         // Mix two colors by a given amount
129         video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
130         {
131                 video::SColor result = video::SColor(
132                                 col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor,
133                                 col1.getRed() * (1 - factor) + col2.getRed() * factor,
134                                 col1.getGreen() * (1 - factor) + col2.getGreen() * factor,
135                                 col1.getBlue() * (1 - factor) + col2.getBlue() * factor);
136                 return result;
137         }
138         video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
139         {
140                 video::SColorf result =
141                                 video::SColorf(col1.r * (1 - factor) + col2.r * factor,
142                                                 col1.g * (1 - factor) + col2.g * factor,
143                                                 col1.b * (1 - factor) + col2.b * factor,
144                                                 col1.a * (1 - factor) + col2.a * factor);
145                 return result;
146         }
147
148         bool m_visible = true;
149         // Used when m_visible=false
150         video::SColor m_fallback_bg_color = video::SColor(255, 255, 255, 255);
151         bool m_first_update = true;
152         float m_time_of_day;
153         float m_time_brightness;
154         bool m_sunlight_seen;
155         float m_brightness = 0.5f;
156         float m_cloud_brightness = 0.5f;
157         bool m_clouds_visible; // Whether clouds are disabled due to player underground
158         bool m_clouds_enabled = true; // Initialised to true, reset only by set_sky API
159         bool m_directional_colored_fog;
160         bool m_in_clouds = true; // Prevent duplicating bools to remember old values
161         bool m_enable_shaders = false;
162
163         video::SColorf m_bgcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
164         video::SColorf m_skycolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
165         video::SColorf m_cloudcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
166         video::SColor m_bgcolor;
167         video::SColor m_skycolor;
168         video::SColorf m_cloudcolor_f;
169
170         // pure white: becomes "diffuse light component" for clouds
171         video::SColorf m_cloudcolor_day_f = video::SColorf(1, 1, 1, 1);
172         // dawn-factoring version of pure white (note: R is above 1.0)
173         video::SColorf m_cloudcolor_dawn_f = video::SColorf(
174                 255.0f/240.0f,
175                 223.0f/240.0f,
176                 191.0f/255.0f
177         );
178
179         SkyboxParams m_sky_params;
180         SunParams m_sun_params;
181         MoonParams m_moon_params;
182         StarParams m_star_params;
183
184         bool m_default_tint = true;
185
186         u64 m_seed = 0;
187         irr_ptr<scene::SMeshBuffer> m_stars;
188         video::SColorf m_star_color;
189
190         video::ITexture *m_sun_texture;
191         video::ITexture *m_moon_texture;
192         video::ITexture *m_sun_tonemap;
193         video::ITexture *m_moon_tonemap;
194
195         void updateStars();
196
197         void draw_sun(video::IVideoDriver *driver, float sunsize, const video::SColor &suncolor,
198                 const video::SColor &suncolor2, float wicked_time_of_day);
199         void draw_moon(video::IVideoDriver *driver, float moonsize, const video::SColor &mooncolor,
200                 const video::SColor &mooncolor2, float wicked_time_of_day);
201         void draw_sky_body(std::array<video::S3DVertex, 4> &vertices,
202                 float pos_1, float pos_2, const video::SColor &c);
203         void draw_stars(video::IVideoDriver *driver, float wicked_time_of_day);
204         void place_sky_body(std::array<video::S3DVertex, 4> &vertices,
205                 float horizon_position, float day_position);
206         void setSkyDefaults();
207 };