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