Minetest 5.9.0-dev
 
Loading...
Searching...
No Matches
sky.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21
23#include <ISceneNode.h>
24#include <array>
25#include "camera.h"
26#include "irr_ptr.h"
27#include "shader.h"
28#include "skyparams.h"
29
30#define SKY_MATERIAL_COUNT 12
31
32class ITextureSource;
33
34// Skybox, rendered with zbuffer turned off, before all other nodes.
35class Sky : public scene::ISceneNode
36{
37public:
39 Sky(s32 id, RenderingEngine *rendering_engine, ITextureSource *tsrc, IShaderSource *ssrc);
40
41 virtual void OnRegisterSceneNode();
42
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 video::SColor getBgColor() const
58 {
60 }
61
62 video::SColor getSkyColor() const
63 {
65 }
66
67 void setSunVisible(bool sun_visible) { m_sun_params.visible = sun_visible; }
68 bool getSunVisible() const { return m_sun_params.visible; }
69 void setSunTexture(const std::string &sun_texture,
70 const std::string &sun_tonemap, ITextureSource *tsrc);
71 void setSunScale(f32 sun_scale) { m_sun_params.scale = sun_scale; }
72 void setSunriseVisible(bool glow_visible) { m_sun_params.sunrise_visible = glow_visible; }
73 void setSunriseTexture(const std::string &sunglow_texture, ITextureSource* tsrc);
75
76 void setMoonVisible(bool moon_visible) { m_moon_params.visible = moon_visible; }
77 bool getMoonVisible() const { return m_moon_params.visible; }
78 void setMoonTexture(const std::string &moon_texture,
79 const std::string &moon_tonemap, ITextureSource *tsrc);
80 void setMoonScale(f32 moon_scale) { m_moon_params.scale = moon_scale; }
82
83 void setStarsVisible(bool stars_visible) { m_star_params.visible = stars_visible; }
84 void setStarCount(u16 star_count);
85 void setStarColor(video::SColor star_color) { m_star_params.starcolor = star_color; }
86 void setStarScale(f32 star_scale) { m_star_params.scale = star_scale; updateStars(); }
87 void setStarDayOpacity(f32 day_opacity) { m_star_params.day_opacity = day_opacity; }
88
90 const video::SColorf &getCloudColor() const { return m_cloudcolor_f; }
91
92 void setVisible(bool visible) { m_visible = visible; }
93
94 // Set only from set_sky API
95 void setCloudsEnabled(bool clouds_enabled) { m_clouds_enabled = clouds_enabled; }
96 void setFallbackBgColor(video::SColor fallback_bg_color)
97 {
98 m_fallback_bg_color = fallback_bg_color;
99 }
100 void setBodyOrbitTilt(float body_orbit_tilt)
101 {
102 if (body_orbit_tilt != SkyboxParams::INVALID_SKYBOX_TILT)
103 m_sky_params.body_orbit_tilt = rangelim(body_orbit_tilt, -90.f, 90.f);
104 }
105 void overrideColors(video::SColor bgcolor, video::SColor skycolor)
106 {
107 m_bgcolor = bgcolor;
108 m_skycolor = skycolor;
109 }
110 void setSkyColors(const SkyColor &sky_color);
111 void setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
112 const std::string &use_sun_tint);
113 void setInClouds(bool clouds) { m_in_clouds = clouds; }
115 void addTextureToSkybox(const std::string &texture, int material_id,
116 ITextureSource *tsrc);
117
118 // Note: the Sky class doesn't use these values. It just stores them.
119 void setFogDistance(s16 fog_distance) { m_sky_params.fog_distance = fog_distance; }
121
122 void setFogStart(float fog_start) { m_sky_params.fog_start = fog_start; }
123 float getFogStart() const { return m_sky_params.fog_start; }
124
125 void setFogColor(video::SColor v) { m_sky_params.fog_color = v; }
126 video::SColor getFogColor() const {
127 if (m_sky_params.fog_color.getAlpha() > 0)
128 return m_sky_params.fog_color;
129 return getBgColor();
130 }
131
132private:
135 // How much sun & moon transition should affect horizon color
137 {
138 if (!m_sunlight_seen)
139 return 0;
140 float x = m_time_of_day >= 0.5 ? (1 - m_time_of_day) * 2
141 : m_time_of_day * 2;
142
143 if (x <= 0.3)
144 return 0;
145 if (x <= 0.4) // when the sun and moon are aligned
146 return (x - 0.3) * 10;
147 if (x <= 0.5)
148 return (0.5 - x) * 10;
149 return 0;
150 }
151
152 // Mix two colors by a given amount
153 static video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
154 {
155 video::SColor result = video::SColor(
156 col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor,
157 col1.getRed() * (1 - factor) + col2.getRed() * factor,
158 col1.getGreen() * (1 - factor) + col2.getGreen() * factor,
159 col1.getBlue() * (1 - factor) + col2.getBlue() * factor);
160 return result;
161 }
162 static video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
163 {
164 video::SColorf result =
165 video::SColorf(col1.r * (1 - factor) + col2.r * factor,
166 col1.g * (1 - factor) + col2.g * factor,
167 col1.b * (1 - factor) + col2.b * factor,
168 col1.a * (1 - factor) + col2.a * factor);
169 return result;
170 }
171
172 bool m_visible = true;
173 // Used when m_visible=false
174 video::SColor m_fallback_bg_color = video::SColor(255, 255, 255, 255);
175 bool m_first_update = true; // Set before the sky is updated for the first time
179 float m_brightness = 0.5f;
180 float m_cloud_brightness = 0.5f;
181 bool m_clouds_visible; // Whether clouds are disabled due to player underground
182 bool m_clouds_enabled = true; // Initialised to true, reset only by set_sky API
184 bool m_in_clouds = true; // Prevent duplicating bools to remember old values
185 bool m_enable_shaders = false;
186
187 video::SColorf m_bgcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
188 video::SColorf m_skycolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
189 video::SColorf m_cloudcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
190 video::SColor m_bgcolor;
191 video::SColor m_skycolor;
192 video::SColorf m_cloudcolor_f;
193
194 // pure white: becomes "diffuse light component" for clouds
195 video::SColorf m_cloudcolor_day_f = video::SColorf(1, 1, 1, 1);
196 // dawn-factoring version of pure white (note: R is above 1.0)
197 video::SColorf m_cloudcolor_dawn_f = video::SColorf(
198 255.0f/240.0f,
199 223.0f/240.0f,
200 191.0f/255.0f
201 );
202
207
208 bool m_default_tint = true;
209
210 u64 m_seed = 0;
212
213 video::ITexture *m_sun_texture;
214 video::ITexture *m_moon_texture;
215 video::ITexture *m_sun_tonemap;
216 video::ITexture *m_moon_tonemap;
217
218 void updateStars();
219
220 void draw_sun(video::IVideoDriver *driver, const video::SColor &suncolor,
221 const video::SColor &suncolor2, float wicked_time_of_day);
222 void draw_moon(video::IVideoDriver *driver, const video::SColor &mooncolor,
223 const video::SColor &mooncolor2, float wicked_time_of_day);
224 void draw_sky_body(std::array<video::S3DVertex, 4> &vertices,
225 float pos_1, float pos_2, const video::SColor &c);
226 void draw_stars(video::IVideoDriver *driver, float wicked_time_of_day);
227 void place_sky_body(std::array<video::S3DVertex, 4> &vertices,
228 float horizon_position, float day_position);
229};
230
231// calculates value for sky body positions for the given observed time of day
232// this is used to draw both Sun/Moon and shadows
233float getWickedTimeOfDay(float time_of_day);
CameraMode
Definition: camera.h:75
Definition: shader.h:216
Definition: texturesource.h:45
Definition: renderingengine.h:80
Definition: sky.h:36
bool m_in_clouds
Definition: sky.h:184
v3f getSunDirection()
Definition: sky.cpp:559
void setCloudsEnabled(bool clouds_enabled)
Definition: sky.h:95
void place_sky_body(std::array< video::S3DVertex, 4 > &vertices, float horizon_position, float day_position)
Definition: sky.cpp:718
void setStarCount(u16 star_count)
Definition: sky.cpp:813
void update(float m_time_of_day, float time_brightness, float direct_brightness, bool sunlight_seen, CameraMode cam_mode, float yaw, float pitch)
Definition: sky.cpp:335
static video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
Definition: sky.h:153
s16 getFogDistance() const
Definition: sky.h:120
void setHorizonTint(video::SColor sun_tint, video::SColor moon_tint, const std::string &use_sun_tint)
Definition: sky.cpp:876
void draw_sun(video::IVideoDriver *driver, const video::SColor &suncolor, const video::SColor &suncolor2, float wicked_time_of_day)
Definition: sky.cpp:569
v3f getMoonDirection()
Definition: sky.cpp:564
bool m_directional_colored_fog
Definition: sky.h:183
MoonParams m_moon_params
Definition: sky.h:205
void setSunriseVisible(bool glow_visible)
Definition: sky.h:72
void setMoonVisible(bool moon_visible)
Definition: sky.h:76
static video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
Definition: sky.h:162
void setSunScale(f32 sun_scale)
Definition: sky.h:71
bool m_visible
Definition: sky.h:172
virtual video::SMaterial & getMaterial(u32 i)
Definition: sky.h:49
video::SColor m_skycolor
Definition: sky.h:191
video::SColorf m_cloudcolor_bright_f
Definition: sky.h:189
void draw_moon(video::IVideoDriver *driver, const video::SColor &mooncolor, const video::SColor &mooncolor2, float wicked_time_of_day)
Definition: sky.cpp:618
void setStarColor(video::SColor star_color)
Definition: sky.h:85
video::SMaterial m_materials[SKY_MATERIAL_COUNT]
Definition: sky.h:134
SunParams m_sun_params
Definition: sky.h:204
void setStarDayOpacity(f32 day_opacity)
Definition: sky.h:87
float m_time_of_day
Definition: sky.h:176
StarParams m_star_params
Definition: sky.h:206
void draw_sky_body(std::array< video::S3DVertex, 4 > &vertices, float pos_1, float pos_2, const video::SColor &c)
Definition: sky.cpp:701
void updateStars()
Definition: sky.cpp:822
video::ITexture * m_sun_texture
Definition: sky.h:213
video::ITexture * m_moon_texture
Definition: sky.h:214
const video::SColorf & getCloudColor() const
Definition: sky.h:90
void setStarScale(f32 star_scale)
Definition: sky.h:86
video::SColor getBgColor() const
Definition: sky.h:57
bool m_clouds_enabled
Definition: sky.h:182
float m_horizon_blend()
Definition: sky.h:136
video::SColor m_fallback_bg_color
Definition: sky.h:174
float getFogStart() const
Definition: sky.h:123
video::ITexture * m_moon_tonemap
Definition: sky.h:216
video::SColorf m_skycolor_bright_f
Definition: sky.h:188
video::SColor getSkyColor() const
Definition: sky.h:62
bool m_enable_shaders
Definition: sky.h:185
video::SColorf m_cloudcolor_day_f
Definition: sky.h:195
video::SColor getFogColor() const
Definition: sky.h:126
void setFallbackBgColor(video::SColor fallback_bg_color)
Definition: sky.h:96
void clearSkyboxTextures()
Definition: sky.h:114
void overrideColors(video::SColor bgcolor, video::SColor skycolor)
Definition: sky.h:105
virtual void OnRegisterSceneNode()
Definition: sky.cpp:109
bool m_first_update
Definition: sky.h:175
void setFogColor(video::SColor v)
Definition: sky.h:125
void setFogStart(float fog_start)
Definition: sky.h:122
video::SColorf m_bgcolor_bright_f
Definition: sky.h:187
virtual u32 getMaterialCount() const
Definition: sky.h:50
float m_time_brightness
Definition: sky.h:177
video::SColor m_bgcolor
Definition: sky.h:190
void setStarsVisible(bool stars_visible)
Definition: sky.h:83
bool getCloudsVisible() const
Definition: sky.h:89
float m_cloud_brightness
Definition: sky.h:180
video::ITexture * m_sun_tonemap
Definition: sky.h:215
virtual void render()
renders the node.
Definition: sky.cpp:117
void setSunriseTexture(const std::string &sunglow_texture, ITextureSource *tsrc)
Definition: sky.cpp:769
void setFogDistance(s16 fog_distance)
Definition: sky.h:119
void setBodyOrbitTilt(float body_orbit_tilt)
Definition: sky.h:100
bool m_clouds_visible
Definition: sky.h:181
void setSunVisible(bool sun_visible)
Definition: sky.h:67
irr_ptr< scene::SMeshBuffer > m_stars
Definition: sky.h:211
void draw_stars(video::IVideoDriver *driver, float wicked_time_of_day)
Definition: sky.cpp:674
virtual const aabb3f & getBoundingBox() const
Definition: sky.h:46
void setMoonScale(f32 moon_scale)
Definition: sky.h:80
bool m_default_tint
Definition: sky.h:208
bool getMoonVisible() const
Definition: sky.h:77
float getBrightness()
Definition: sky.h:55
bool m_sunlight_seen
Definition: sky.h:178
SkyboxParams m_sky_params
Definition: sky.h:203
video::SColorf m_cloudcolor_f
Definition: sky.h:192
aabb3f m_box
Definition: sky.h:133
void setInClouds(bool clouds)
Definition: sky.h:113
bool getSunVisible() const
Definition: sky.h:68
void setMoonTexture(const std::string &moon_texture, const std::string &moon_tonemap, ITextureSource *tsrc)
Definition: sky.cpp:781
float m_brightness
Definition: sky.h:179
void setSkyColors(const SkyColor &sky_color)
Definition: sky.cpp:871
video::SColorf m_cloudcolor_dawn_f
Definition: sky.h:197
void setSunTexture(const std::string &sun_texture, const std::string &sun_tonemap, ITextureSource *tsrc)
Definition: sky.cpp:737
void setVisible(bool visible)
Definition: sky.h:92
void addTextureToSkybox(const std::string &texture, int material_id, ITextureSource *tsrc)
Definition: sky.cpp:891
u64 m_seed
Definition: sky.h:210
Shared pointer for IrrLicht objects.
Definition: irr_ptr.h:44
core::aabbox3d< f32 > aabb3f
Definition: irr_aabb3d.h:26
core::vector3df v3f
Definition: irr_v3d.h:26
#define rangelim(d, min, max)
Definition: numeric.h:32
#define SKY_MATERIAL_COUNT
Definition: sky.h:30
float getWickedTimeOfDay(float time_of_day)
Definition: sky.cpp:905
Definition: skyparams.h:63
f32 scale
Definition: skyparams.h:67
bool visible
Definition: skyparams.h:64
Definition: skyparams.h:24
Definition: skyparams.h:35
std::vector< std::string > textures
Definition: skyparams.h:40
s16 fog_distance
Definition: skyparams.h:47
video::SColor fog_color
Definition: skyparams.h:49
static constexpr float INVALID_SKYBOX_TILT
Definition: skyparams.h:36
float fog_start
Definition: skyparams.h:48
float body_orbit_tilt
Definition: skyparams.h:46
Definition: skyparams.h:71
video::SColor starcolor
Definition: skyparams.h:74
f32 scale
Definition: skyparams.h:75
bool visible
Definition: skyparams.h:72
f32 day_opacity
Definition: skyparams.h:76
Definition: skyparams.h:53
bool visible
Definition: skyparams.h:54
f32 scale
Definition: skyparams.h:59
bool sunrise_visible
Definition: skyparams.h:58