Luanti 5.11.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 class IImage;
21}
22
23class IShaderSource;
24class ITextureSource;
25
26// Skybox, rendered with zbuffer turned off, before all other nodes.
27class Sky : public scene::ISceneNode
28{
29public:
31 Sky(s32 id, RenderingEngine *rendering_engine, ITextureSource *tsrc, IShaderSource *ssrc);
32
33 virtual void OnRegisterSceneNode();
34
36 virtual void render();
37
38 virtual const aabb3f &getBoundingBox() const { return m_box; }
39
40 // Used by Irrlicht for optimizing rendering
41 virtual video::SMaterial &getMaterial(u32 i) { return m_materials[i]; }
42 virtual u32 getMaterialCount() const { return SKY_MATERIAL_COUNT; }
43
44 void update(float m_time_of_day, float time_brightness, float direct_brightness,
45 bool sunlight_seen, CameraMode cam_mode, float yaw, float pitch);
46
47 float getBrightness() { return m_brightness; }
48
49 video::SColor getBgColor() const
50 {
52 }
53
54 video::SColor getSkyColor() const
55 {
57 }
58
59 void setSunVisible(bool sun_visible) { m_sun_params.visible = sun_visible; }
60 bool getSunVisible() const { return m_sun_params.visible; }
61 void setSunTexture(const std::string &sun_texture,
62 const std::string &sun_tonemap, ITextureSource *tsrc);
63 void setSunScale(f32 sun_scale) { m_sun_params.scale = sun_scale; }
64 void setSunriseVisible(bool glow_visible) { m_sun_params.sunrise_visible = glow_visible; }
65 void setSunriseTexture(const std::string &sunglow_texture, ITextureSource* tsrc);
67
68 void setMoonVisible(bool moon_visible) { m_moon_params.visible = moon_visible; }
69 bool getMoonVisible() const { return m_moon_params.visible; }
70 void setMoonTexture(const std::string &moon_texture,
71 const std::string &moon_tonemap, ITextureSource *tsrc);
72 void setMoonScale(f32 moon_scale) { m_moon_params.scale = moon_scale; }
74
75 void setStarsVisible(bool stars_visible) { m_star_params.visible = stars_visible; }
76 void setStarCount(u16 star_count);
77 void setStarColor(video::SColor star_color) { m_star_params.starcolor = star_color; }
78 void setStarScale(f32 star_scale) { m_star_params.scale = star_scale; updateStars(); }
79 void setStarDayOpacity(f32 day_opacity) { m_star_params.day_opacity = day_opacity; }
80
82 const video::SColorf &getCloudColor() const { return m_cloudcolor_f; }
83
84 void setVisible(bool visible) { m_visible = visible; }
85
86 // Set only from set_sky API
87 void setCloudsEnabled(bool clouds_enabled) { m_clouds_enabled = clouds_enabled; }
88 void setFallbackBgColor(video::SColor fallback_bg_color)
89 {
90 m_fallback_bg_color = fallback_bg_color;
91 }
92 void setBodyOrbitTilt(float body_orbit_tilt)
93 {
94 if (body_orbit_tilt != SkyboxParams::INVALID_SKYBOX_TILT)
95 m_sky_params.body_orbit_tilt = rangelim(body_orbit_tilt, -90.f, 90.f);
96 }
97 void overrideColors(video::SColor bgcolor, video::SColor skycolor)
98 {
99 m_bgcolor = bgcolor;
100 m_skycolor = skycolor;
101 }
102 void setSkyColors(const SkyColor &sky_color);
103 void setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
104 const std::string &use_sun_tint);
105 void setInClouds(bool clouds) { m_in_clouds = clouds; }
107 void addTextureToSkybox(const std::string &texture, int material_id,
108 ITextureSource *tsrc);
109
110 // Note: the Sky class doesn't use these values. It just stores them.
111 void setFogDistance(s16 fog_distance) { m_sky_params.fog_distance = fog_distance; }
113
114 void setFogStart(float fog_start) { m_sky_params.fog_start = fog_start; }
115 float getFogStart() const { return m_sky_params.fog_start; }
116
117 void setFogColor(video::SColor v) { m_sky_params.fog_color = v; }
118 video::SColor getFogColor() const {
119 if (m_sky_params.fog_color.getAlpha() > 0)
120 return m_sky_params.fog_color;
121 return getBgColor();
122 }
123
124private:
127 // How much sun & moon transition should affect horizon color
129 {
130 if (!m_sunlight_seen)
131 return 0;
132 float x = m_time_of_day >= 0.5 ? (1 - m_time_of_day) * 2
133 : m_time_of_day * 2;
134
135 if (x <= 0.3)
136 return 0;
137 if (x <= 0.4) // when the sun and moon are aligned
138 return (x - 0.3) * 10;
139 if (x <= 0.5)
140 return (0.5 - x) * 10;
141 return 0;
142 }
143
144 // Mix two colors by a given amount
145 static video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
146 {
147 video::SColor result = video::SColor(
148 col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor,
149 col1.getRed() * (1 - factor) + col2.getRed() * factor,
150 col1.getGreen() * (1 - factor) + col2.getGreen() * factor,
151 col1.getBlue() * (1 - factor) + col2.getBlue() * factor);
152 return result;
153 }
154 static video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
155 {
156 video::SColorf result =
157 video::SColorf(col1.r * (1 - factor) + col2.r * factor,
158 col1.g * (1 - factor) + col2.g * factor,
159 col1.b * (1 - factor) + col2.b * factor,
160 col1.a * (1 - factor) + col2.a * factor);
161 return result;
162 }
163
164 bool m_visible = true;
165 // Used when m_visible=false
166 video::SColor m_fallback_bg_color = video::SColor(255, 255, 255, 255);
167 bool m_first_update = true; // Set before the sky is updated for the first time
171 float m_brightness = 0.5f;
172 float m_cloud_brightness = 0.5f;
173 bool m_clouds_visible; // Whether clouds are disabled due to player underground
174 bool m_clouds_enabled = true; // Initialised to true, reset only by set_sky API
176 bool m_in_clouds = true; // Prevent duplicating bools to remember old values
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 = nullptr;
205 video::ITexture *m_moon_texture = nullptr;
206 video::IImage *m_sun_tonemap = nullptr;
207 video::IImage *m_moon_tonemap = nullptr;
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:28
bool m_in_clouds
Definition sky.h:176
v3f getSunDirection()
Definition sky.cpp:536
void setCloudsEnabled(bool clouds_enabled)
Definition sky.h:87
void place_sky_body(std::array< video::S3DVertex, 4 > &vertices, float horizon_position, float day_position)
Definition sky.cpp:689
void setStarCount(u16 star_count)
Definition sky.cpp:800
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:312
static video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
Definition sky.h:145
s16 getFogDistance() const
Definition sky.h:112
void setHorizonTint(video::SColor sun_tint, video::SColor moon_tint, const std::string &use_sun_tint)
Definition sky.cpp:860
void draw_sun(video::IVideoDriver *driver, const video::SColor &suncolor, const video::SColor &suncolor2, float wicked_time_of_day)
Definition sky.cpp:546
v3f getMoonDirection()
Definition sky.cpp:541
bool m_directional_colored_fog
Definition sky.h:175
MoonParams m_moon_params
Definition sky.h:196
void setSunriseVisible(bool glow_visible)
Definition sky.h:64
void setMoonVisible(bool moon_visible)
Definition sky.h:68
static video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
Definition sky.h:154
void setSunScale(f32 sun_scale)
Definition sky.h:63
bool m_visible
Definition sky.h:164
virtual video::SMaterial & getMaterial(u32 i)
Definition sky.h:41
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:592
void setStarColor(video::SColor star_color)
Definition sky.h:77
video::SMaterial m_materials[SKY_MATERIAL_COUNT]
Definition sky.h:126
SunParams m_sun_params
Definition sky.h:195
void setStarDayOpacity(f32 day_opacity)
Definition sky.h:79
float m_time_of_day
Definition sky.h:168
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:672
void updateStars()
Definition sky.cpp:809
video::ITexture * m_sun_texture
Definition sky.h:204
video::ITexture * m_moon_texture
Definition sky.h:205
video::IImage * m_sun_tonemap
Definition sky.h:206
const video::SColorf & getCloudColor() const
Definition sky.h:82
void setStarScale(f32 star_scale)
Definition sky.h:78
video::SColor getBgColor() const
Definition sky.h:49
bool m_clouds_enabled
Definition sky.h:174
float m_horizon_blend()
Definition sky.h:128
video::SColor m_fallback_bg_color
Definition sky.h:166
float getFogStart() const
Definition sky.h:115
video::IImage * m_moon_tonemap
Definition sky.h:207
video::SColorf m_skycolor_bright_f
Definition sky.h:179
video::SColor getSkyColor() const
Definition sky.h:54
video::SColorf m_cloudcolor_day_f
Definition sky.h:186
video::SColor getFogColor() const
Definition sky.h:118
void setFallbackBgColor(video::SColor fallback_bg_color)
Definition sky.h:88
void clearSkyboxTextures()
Definition sky.h:106
void overrideColors(video::SColor bgcolor, video::SColor skycolor)
Definition sky.h:97
virtual void OnRegisterSceneNode()
Definition sky.cpp:90
bool m_first_update
Definition sky.h:167
void setFogColor(video::SColor v)
Definition sky.h:117
void setFogStart(float fog_start)
Definition sky.h:114
video::SColorf m_bgcolor_bright_f
Definition sky.h:178
virtual u32 getMaterialCount() const
Definition sky.h:42
float m_time_brightness
Definition sky.h:169
video::SColor m_bgcolor
Definition sky.h:181
void setStarsVisible(bool stars_visible)
Definition sky.h:75
bool getCloudsVisible() const
Definition sky.h:81
float m_cloud_brightness
Definition sky.h:172
virtual void render()
renders the node.
Definition sky.cpp:98
void setSunriseTexture(const std::string &sunglow_texture, ITextureSource *tsrc)
Definition sky.cpp:757
void setFogDistance(s16 fog_distance)
Definition sky.h:111
void setBodyOrbitTilt(float body_orbit_tilt)
Definition sky.h:92
bool m_clouds_visible
Definition sky.h:173
void setSunVisible(bool sun_visible)
Definition sky.h:59
irr_ptr< scene::SMeshBuffer > m_stars
Definition sky.h:202
void draw_stars(video::IVideoDriver *driver, float wicked_time_of_day)
Definition sky.cpp:645
virtual const aabb3f & getBoundingBox() const
Definition sky.h:38
void setMoonScale(f32 moon_scale)
Definition sky.h:72
bool m_default_tint
Definition sky.h:199
bool getMoonVisible() const
Definition sky.h:69
float getBrightness()
Definition sky.h:47
bool m_sunlight_seen
Definition sky.h:170
SkyboxParams m_sky_params
Definition sky.h:194
video::SColorf m_cloudcolor_f
Definition sky.h:183
aabb3f m_box
Definition sky.h:125
Sky(s32 id, RenderingEngine *rendering_engine, ITextureSource *tsrc, IShaderSource *ssrc)
constructor
Definition sky.cpp:46
void setInClouds(bool clouds)
Definition sky.h:105
bool getSunVisible() const
Definition sky.h:60
void setMoonTexture(const std::string &moon_texture, const std::string &moon_tonemap, ITextureSource *tsrc)
Definition sky.cpp:769
float m_brightness
Definition sky.h:171
void setSkyColors(const SkyColor &sky_color)
Definition sky.cpp:855
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:726
void setVisible(bool visible)
Definition sky.h:84
void addTextureToSkybox(const std::string &texture, int material_id, ITextureSource *tsrc)
Definition sky.cpp:875
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:889
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