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