sky.h 4.1 KB

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