sky.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irrlichttypes_extrabloated.h"
  18. #include <ISceneNode.h>
  19. #include <array>
  20. #include "camera.h"
  21. #include "irr_ptr.h"
  22. #include "shader.h"
  23. #include "skyparams.h"
  24. #define SKY_MATERIAL_COUNT 12
  25. class ITextureSource;
  26. // Skybox, rendered with zbuffer turned off, before all other nodes.
  27. class Sky : public scene::ISceneNode
  28. {
  29. public:
  30. //! constructor
  31. Sky(s32 id, RenderingEngine *rendering_engine, ITextureSource *tsrc, IShaderSource *ssrc);
  32. virtual void OnRegisterSceneNode();
  33. //! renders the node.
  34. virtual void render();
  35. virtual const aabb3f &getBoundingBox() const { return m_box; }
  36. // Used by Irrlicht for optimizing rendering
  37. virtual video::SMaterial &getMaterial(u32 i) { return m_materials[i]; }
  38. virtual u32 getMaterialCount() const { return SKY_MATERIAL_COUNT; }
  39. void update(float m_time_of_day, float time_brightness, float direct_brightness,
  40. bool sunlight_seen, CameraMode cam_mode, float yaw, float pitch);
  41. float getBrightness() { return m_brightness; }
  42. const video::SColor &getBgColor() const
  43. {
  44. return m_visible ? m_bgcolor : m_fallback_bg_color;
  45. }
  46. const video::SColor &getSkyColor() const
  47. {
  48. return m_visible ? m_skycolor : m_fallback_bg_color;
  49. }
  50. void setSunVisible(bool sun_visible) { m_sun_params.visible = sun_visible; }
  51. bool getSunVisible() const { return m_sun_params.visible; }
  52. void setSunTexture(const std::string &sun_texture,
  53. const std::string &sun_tonemap, ITextureSource *tsrc);
  54. void setSunScale(f32 sun_scale) { m_sun_params.scale = sun_scale; }
  55. void setSunriseVisible(bool glow_visible) { m_sun_params.sunrise_visible = glow_visible; }
  56. void setSunriseTexture(const std::string &sunglow_texture, ITextureSource* tsrc);
  57. void setMoonVisible(bool moon_visible) { m_moon_params.visible = moon_visible; }
  58. bool getMoonVisible() const { return m_moon_params.visible; }
  59. void setMoonTexture(const std::string &moon_texture,
  60. const std::string &moon_tonemap, ITextureSource *tsrc);
  61. void setMoonScale(f32 moon_scale) { m_moon_params.scale = moon_scale; }
  62. void setStarsVisible(bool stars_visible) { m_star_params.visible = stars_visible; }
  63. void setStarCount(u16 star_count);
  64. void setStarColor(video::SColor star_color) { m_star_params.starcolor = star_color; }
  65. void setStarScale(f32 star_scale) { m_star_params.scale = star_scale; updateStars(); }
  66. void setStarDayOpacity(f32 day_opacity) { m_star_params.day_opacity = day_opacity; }
  67. bool getCloudsVisible() const { return m_clouds_visible && m_clouds_enabled; }
  68. const video::SColorf &getCloudColor() const { return m_cloudcolor_f; }
  69. void setVisible(bool visible) { m_visible = visible; }
  70. // Set only from set_sky API
  71. void setCloudsEnabled(bool clouds_enabled) { m_clouds_enabled = clouds_enabled; }
  72. void setFallbackBgColor(video::SColor fallback_bg_color)
  73. {
  74. m_fallback_bg_color = fallback_bg_color;
  75. }
  76. void overrideColors(video::SColor bgcolor, video::SColor skycolor)
  77. {
  78. m_bgcolor = bgcolor;
  79. m_skycolor = skycolor;
  80. }
  81. void setSkyColors(const SkyColor &sky_color);
  82. void setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
  83. const std::string &use_sun_tint);
  84. void setInClouds(bool clouds) { m_in_clouds = clouds; }
  85. void clearSkyboxTextures() { m_sky_params.textures.clear(); }
  86. void addTextureToSkybox(const std::string &texture, int material_id,
  87. ITextureSource *tsrc);
  88. const video::SColorf &getCurrentStarColor() const { return m_star_color; }
  89. float getSkyBodyOrbitTilt() const { return m_sky_body_orbit_tilt; }
  90. private:
  91. aabb3f m_box;
  92. video::SMaterial m_materials[SKY_MATERIAL_COUNT];
  93. // How much sun & moon transition should affect horizon color
  94. float m_horizon_blend()
  95. {
  96. if (!m_sunlight_seen)
  97. return 0;
  98. float x = m_time_of_day >= 0.5 ? (1 - m_time_of_day) * 2
  99. : m_time_of_day * 2;
  100. if (x <= 0.3)
  101. return 0;
  102. if (x <= 0.4) // when the sun and moon are aligned
  103. return (x - 0.3) * 10;
  104. if (x <= 0.5)
  105. return (0.5 - x) * 10;
  106. return 0;
  107. }
  108. // Mix two colors by a given amount
  109. static video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
  110. {
  111. video::SColor result = video::SColor(
  112. col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor,
  113. col1.getRed() * (1 - factor) + col2.getRed() * factor,
  114. col1.getGreen() * (1 - factor) + col2.getGreen() * factor,
  115. col1.getBlue() * (1 - factor) + col2.getBlue() * factor);
  116. return result;
  117. }
  118. static video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
  119. {
  120. video::SColorf result =
  121. video::SColorf(col1.r * (1 - factor) + col2.r * factor,
  122. col1.g * (1 - factor) + col2.g * factor,
  123. col1.b * (1 - factor) + col2.b * factor,
  124. col1.a * (1 - factor) + col2.a * factor);
  125. return result;
  126. }
  127. bool m_visible = true;
  128. // Used when m_visible=false
  129. video::SColor m_fallback_bg_color = video::SColor(255, 255, 255, 255);
  130. bool m_first_update = true; // Set before the sky is updated for the first time
  131. float m_time_of_day;
  132. float m_time_brightness;
  133. bool m_sunlight_seen;
  134. float m_brightness = 0.5f;
  135. float m_cloud_brightness = 0.5f;
  136. bool m_clouds_visible; // Whether clouds are disabled due to player underground
  137. bool m_clouds_enabled = true; // Initialised to true, reset only by set_sky API
  138. bool m_directional_colored_fog;
  139. bool m_in_clouds = true; // Prevent duplicating bools to remember old values
  140. bool m_enable_shaders = false;
  141. float m_sky_body_orbit_tilt = 0.0f;
  142. video::SColorf m_bgcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
  143. video::SColorf m_skycolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
  144. video::SColorf m_cloudcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
  145. video::SColor m_bgcolor;
  146. video::SColor m_skycolor;
  147. video::SColorf m_cloudcolor_f;
  148. // pure white: becomes "diffuse light component" for clouds
  149. video::SColorf m_cloudcolor_day_f = video::SColorf(1, 1, 1, 1);
  150. // dawn-factoring version of pure white (note: R is above 1.0)
  151. video::SColorf m_cloudcolor_dawn_f = video::SColorf(
  152. 255.0f/240.0f,
  153. 223.0f/240.0f,
  154. 191.0f/255.0f
  155. );
  156. SkyboxParams m_sky_params;
  157. SunParams m_sun_params;
  158. MoonParams m_moon_params;
  159. StarParams m_star_params;
  160. bool m_default_tint = true;
  161. u64 m_seed = 0;
  162. irr_ptr<scene::SMeshBuffer> m_stars;
  163. video::SColorf m_star_color;
  164. video::ITexture *m_sun_texture;
  165. video::ITexture *m_moon_texture;
  166. video::ITexture *m_sun_tonemap;
  167. video::ITexture *m_moon_tonemap;
  168. void updateStars();
  169. void draw_sun(video::IVideoDriver *driver, float sunsize, const video::SColor &suncolor,
  170. const video::SColor &suncolor2, float wicked_time_of_day);
  171. void draw_moon(video::IVideoDriver *driver, float moonsize, const video::SColor &mooncolor,
  172. const video::SColor &mooncolor2, float wicked_time_of_day);
  173. void draw_sky_body(std::array<video::S3DVertex, 4> &vertices,
  174. float pos_1, float pos_2, const video::SColor &c);
  175. void draw_stars(video::IVideoDriver *driver, float wicked_time_of_day);
  176. void place_sky_body(std::array<video::S3DVertex, 4> &vertices,
  177. float horizon_position, float day_position);
  178. };
  179. // calculates value for sky body positions for the given observed time of day
  180. // this is used to draw both Sun/Moon and shadows
  181. float getWickedTimeOfDay(float time_of_day);