3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
22 #include "irrlichttypes_extrabloated.h"
23 #include <ISceneNode.h>
28 #include "skyparams.h"
30 #define SKY_MATERIAL_COUNT 12
34 // Skybox, rendered with zbuffer turned off, before all other nodes.
35 class Sky : public scene::ISceneNode
39 Sky(s32 id, RenderingEngine *rendering_engine, ITextureSource *tsrc, IShaderSource *ssrc);
41 virtual void OnRegisterSceneNode();
44 virtual void render();
46 virtual const aabb3f &getBoundingBox() const { return m_box; }
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; }
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);
55 float getBrightness() { return m_brightness; }
57 const video::SColor &getBgColor() const
59 return m_visible ? m_bgcolor : m_fallback_bg_color;
62 const video::SColor &getSkyColor() const
64 return m_visible ? m_skycolor : m_fallback_bg_color;
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);
74 v3f getSunDirection();
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; }
81 v3f getMoonDirection();
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; }
89 bool getCloudsVisible() const { return m_clouds_visible && m_clouds_enabled; }
90 const video::SColorf &getCloudColor() const { return m_cloudcolor_f; }
92 void setVisible(bool visible) { m_visible = visible; }
93 // Set only from set_sky API
94 void setCloudsEnabled(bool clouds_enabled) { m_clouds_enabled = clouds_enabled; }
95 void setFallbackBgColor(video::SColor fallback_bg_color)
97 m_fallback_bg_color = fallback_bg_color;
99 void setBodyOrbitTilt(float body_orbit_tilt)
101 m_sky_params.body_orbit_tilt = body_orbit_tilt;
103 void overrideColors(video::SColor bgcolor, video::SColor skycolor)
106 m_skycolor = skycolor;
108 void setSkyColors(const SkyColor &sky_color);
109 void setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
110 const std::string &use_sun_tint);
111 void setInClouds(bool clouds) { m_in_clouds = clouds; }
112 void clearSkyboxTextures() { m_sky_params.textures.clear(); }
113 void addTextureToSkybox(const std::string &texture, int material_id,
114 ITextureSource *tsrc);
115 const video::SColorf &getCurrentStarColor() const { return m_star_color; }
119 video::SMaterial m_materials[SKY_MATERIAL_COUNT];
120 // How much sun & moon transition should affect horizon color
121 float m_horizon_blend()
123 if (!m_sunlight_seen)
125 float x = m_time_of_day >= 0.5 ? (1 - m_time_of_day) * 2
130 if (x <= 0.4) // when the sun and moon are aligned
131 return (x - 0.3) * 10;
133 return (0.5 - x) * 10;
137 // Mix two colors by a given amount
138 static video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
140 video::SColor result = video::SColor(
141 col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor,
142 col1.getRed() * (1 - factor) + col2.getRed() * factor,
143 col1.getGreen() * (1 - factor) + col2.getGreen() * factor,
144 col1.getBlue() * (1 - factor) + col2.getBlue() * factor);
147 static video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
149 video::SColorf result =
150 video::SColorf(col1.r * (1 - factor) + col2.r * factor,
151 col1.g * (1 - factor) + col2.g * factor,
152 col1.b * (1 - factor) + col2.b * factor,
153 col1.a * (1 - factor) + col2.a * factor);
157 bool m_visible = true;
158 // Used when m_visible=false
159 video::SColor m_fallback_bg_color = video::SColor(255, 255, 255, 255);
160 bool m_first_update = true; // Set before the sky is updated for the first time
162 float m_time_brightness;
163 bool m_sunlight_seen;
164 float m_brightness = 0.5f;
165 float m_cloud_brightness = 0.5f;
166 bool m_clouds_visible; // Whether clouds are disabled due to player underground
167 bool m_clouds_enabled = true; // Initialised to true, reset only by set_sky API
168 bool m_directional_colored_fog;
169 bool m_in_clouds = true; // Prevent duplicating bools to remember old values
170 bool m_enable_shaders = false;
172 video::SColorf m_bgcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
173 video::SColorf m_skycolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
174 video::SColorf m_cloudcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
175 video::SColor m_bgcolor;
176 video::SColor m_skycolor;
177 video::SColorf m_cloudcolor_f;
179 // pure white: becomes "diffuse light component" for clouds
180 video::SColorf m_cloudcolor_day_f = video::SColorf(1, 1, 1, 1);
181 // dawn-factoring version of pure white (note: R is above 1.0)
182 video::SColorf m_cloudcolor_dawn_f = video::SColorf(
188 SkyboxParams m_sky_params;
189 SunParams m_sun_params;
190 MoonParams m_moon_params;
191 StarParams m_star_params;
193 bool m_default_tint = true;
196 irr_ptr<scene::SMeshBuffer> m_stars;
197 video::SColorf m_star_color;
199 video::ITexture *m_sun_texture;
200 video::ITexture *m_moon_texture;
201 video::ITexture *m_sun_tonemap;
202 video::ITexture *m_moon_tonemap;
206 void draw_sun(video::IVideoDriver *driver, const video::SColor &suncolor,
207 const video::SColor &suncolor2, float wicked_time_of_day);
208 void draw_moon(video::IVideoDriver *driver, const video::SColor &mooncolor,
209 const video::SColor &mooncolor2, float wicked_time_of_day);
210 void draw_sky_body(std::array<video::S3DVertex, 4> &vertices,
211 float pos_1, float pos_2, const video::SColor &c);
212 void draw_stars(video::IVideoDriver *driver, float wicked_time_of_day);
213 void place_sky_body(std::array<video::S3DVertex, 4> &vertices,
214 float horizon_position, float day_position);
217 // calculates value for sky body positions for the given observed time of day
218 // this is used to draw both Sun/Moon and shadows
219 float getWickedTimeOfDay(float time_of_day);