sky.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #include <ISceneNode.h>
  17. #include "camera.h"
  18. #include "irrlichttypes_extrabloated.h"
  19. #pragma once
  20. #define SKY_MATERIAL_COUNT 5
  21. #define SKY_STAR_COUNT 200
  22. class ITextureSource;
  23. // Skybox, rendered with zbuffer turned off, before all other nodes.
  24. class Sky : public scene::ISceneNode
  25. {
  26. public:
  27. //! constructor
  28. Sky(s32 id, ITextureSource *tsrc);
  29. virtual void OnRegisterSceneNode();
  30. //! renders the node.
  31. virtual void render();
  32. virtual const aabb3f &getBoundingBox() const { return m_box; }
  33. // Used by Irrlicht for optimizing rendering
  34. virtual video::SMaterial &getMaterial(u32 i) { return m_materials[i]; }
  35. // Used by Irrlicht for optimizing rendering
  36. virtual u32 getMaterialCount() const { return SKY_MATERIAL_COUNT; }
  37. void update(float m_time_of_day, float time_brightness, float direct_brightness,
  38. bool sunlight_seen, CameraMode cam_mode, float yaw, float pitch);
  39. float getBrightness() { return m_brightness; }
  40. const video::SColor &getBgColor() const
  41. {
  42. return m_visible ? m_bgcolor : m_fallback_bg_color;
  43. }
  44. const video::SColor &getSkyColor() const
  45. {
  46. return m_visible ? m_skycolor : m_fallback_bg_color;
  47. }
  48. bool getCloudsVisible() const { return m_clouds_visible && m_clouds_enabled; }
  49. const video::SColorf &getCloudColor() const { return m_cloudcolor_f; }
  50. void setVisible(bool visible) { m_visible = visible; }
  51. // Set only from set_sky API
  52. void setCloudsEnabled(bool clouds_enabled) { m_clouds_enabled = clouds_enabled; }
  53. void setFallbackBgColor(const video::SColor &fallback_bg_color)
  54. {
  55. m_fallback_bg_color = fallback_bg_color;
  56. }
  57. void overrideColors(const video::SColor &bgcolor, const video::SColor &skycolor)
  58. {
  59. m_bgcolor = bgcolor;
  60. m_skycolor = skycolor;
  61. }
  62. void setBodiesVisible(bool visible) { m_bodies_visible = visible; }
  63. private:
  64. aabb3f m_box;
  65. video::SMaterial m_materials[SKY_MATERIAL_COUNT];
  66. // How much sun & moon transition should affect horizon color
  67. float m_horizon_blend()
  68. {
  69. if (!m_sunlight_seen)
  70. return 0;
  71. float x = m_time_of_day >= 0.5 ? (1 - m_time_of_day) * 2
  72. : m_time_of_day * 2;
  73. if (x <= 0.3)
  74. return 0;
  75. if (x <= 0.4) // when the sun and moon are aligned
  76. return (x - 0.3) * 10;
  77. if (x <= 0.5)
  78. return (0.5 - x) * 10;
  79. return 0;
  80. }
  81. // Mix two colors by a given amount
  82. video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
  83. {
  84. video::SColor result = video::SColor(
  85. col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor,
  86. col1.getRed() * (1 - factor) + col2.getRed() * factor,
  87. col1.getGreen() * (1 - factor) + col2.getGreen() * factor,
  88. col1.getBlue() * (1 - factor) + col2.getBlue() * factor);
  89. return result;
  90. }
  91. video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
  92. {
  93. video::SColorf result =
  94. video::SColorf(col1.r * (1 - factor) + col2.r * factor,
  95. col1.g * (1 - factor) + col2.g * factor,
  96. col1.b * (1 - factor) + col2.b * factor,
  97. col1.a * (1 - factor) + col2.a * factor);
  98. return result;
  99. }
  100. bool m_visible = true;
  101. // Used when m_visible=false
  102. video::SColor m_fallback_bg_color = video::SColor(255, 255, 255, 255);
  103. bool m_first_update = true;
  104. float m_time_of_day;
  105. float m_time_brightness;
  106. bool m_sunlight_seen;
  107. float m_brightness = 0.5f;
  108. float m_cloud_brightness = 0.5f;
  109. bool m_clouds_visible; // Whether clouds are disabled due to player underground
  110. bool m_clouds_enabled = true; // Initialised to true, reset only by set_sky API
  111. bool m_directional_colored_fog;
  112. bool m_bodies_visible = true; // sun, moon, stars
  113. video::SColorf m_bgcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
  114. video::SColorf m_skycolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
  115. video::SColorf m_cloudcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
  116. video::SColor m_bgcolor;
  117. video::SColor m_skycolor;
  118. video::SColorf m_cloudcolor_f;
  119. v3f m_stars[SKY_STAR_COUNT];
  120. video::ITexture *m_sun_texture;
  121. video::ITexture *m_moon_texture;
  122. video::ITexture *m_sun_tonemap;
  123. video::ITexture *m_moon_tonemap;
  124. };