]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/sky.h
Attachments: Fix attachments to temporary removed objects (#8989)
[dragonfireclient.git] / src / client / 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 <array>
22 #include "camera.h"
23 #include "irrlichttypes_extrabloated.h"
24
25 #pragma once
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(s32 id, ITextureSource *tsrc);
38
39         virtual void OnRegisterSceneNode();
40
41         //! renders the node.
42         virtual void render();
43
44         virtual const aabb3f &getBoundingBox() const { return m_box; }
45
46         // Used by Irrlicht for optimizing rendering
47         virtual video::SMaterial &getMaterial(u32 i) { return m_materials[i]; }
48
49         // Used by Irrlicht for optimizing rendering
50         virtual u32 getMaterialCount() const { return SKY_MATERIAL_COUNT; }
51
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);
54
55         float getBrightness() { return m_brightness; }
56
57         const video::SColor &getBgColor() const
58         {
59                 return m_visible ? m_bgcolor : m_fallback_bg_color;
60         }
61
62         const video::SColor &getSkyColor() const
63         {
64                 return m_visible ? m_skycolor : m_fallback_bg_color;
65         }
66
67         bool getCloudsVisible() const { return m_clouds_visible && m_clouds_enabled; }
68         const video::SColorf &getCloudColor() const { return m_cloudcolor_f; }
69
70         void setVisible(bool visible) { m_visible = visible; }
71         // Set only from set_sky API
72         void setCloudsEnabled(bool clouds_enabled) { m_clouds_enabled = clouds_enabled; }
73         void setFallbackBgColor(const video::SColor &fallback_bg_color)
74         {
75                 m_fallback_bg_color = fallback_bg_color;
76         }
77         void overrideColors(const video::SColor &bgcolor, const video::SColor &skycolor)
78         {
79                 m_bgcolor = bgcolor;
80                 m_skycolor = skycolor;
81         }
82         void setBodiesVisible(bool visible) { m_bodies_visible = visible; }
83
84 private:
85         aabb3f m_box;
86         video::SMaterial m_materials[SKY_MATERIAL_COUNT];
87
88         // How much sun & moon transition should affect horizon color
89         float m_horizon_blend()
90         {
91                 if (!m_sunlight_seen)
92                         return 0;
93                 float x = m_time_of_day >= 0.5 ? (1 - m_time_of_day) * 2
94                                                : m_time_of_day * 2;
95
96                 if (x <= 0.3)
97                         return 0;
98                 if (x <= 0.4) // when the sun and moon are aligned
99                         return (x - 0.3) * 10;
100                 if (x <= 0.5)
101                         return (0.5 - x) * 10;
102                 return 0;
103         }
104
105         // Mix two colors by a given amount
106         video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
107         {
108                 video::SColor result = video::SColor(
109                                 col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor,
110                                 col1.getRed() * (1 - factor) + col2.getRed() * factor,
111                                 col1.getGreen() * (1 - factor) + col2.getGreen() * factor,
112                                 col1.getBlue() * (1 - factor) + col2.getBlue() * factor);
113                 return result;
114         }
115         video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
116         {
117                 video::SColorf result =
118                                 video::SColorf(col1.r * (1 - factor) + col2.r * factor,
119                                                 col1.g * (1 - factor) + col2.g * factor,
120                                                 col1.b * (1 - factor) + col2.b * factor,
121                                                 col1.a * (1 - factor) + col2.a * factor);
122                 return result;
123         }
124
125         bool m_visible = true;
126         // Used when m_visible=false
127         video::SColor m_fallback_bg_color = video::SColor(255, 255, 255, 255);
128         bool m_first_update = true;
129         float m_time_of_day;
130         float m_time_brightness;
131         bool m_sunlight_seen;
132         float m_brightness = 0.5f;
133         float m_cloud_brightness = 0.5f;
134         bool m_clouds_visible; // Whether clouds are disabled due to player underground
135         bool m_clouds_enabled = true; // Initialised to true, reset only by set_sky API
136         bool m_directional_colored_fog;
137         bool m_bodies_visible = true; // sun, moon, stars
138         video::SColorf m_bgcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
139         video::SColorf m_skycolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
140         video::SColorf m_cloudcolor_bright_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
141         video::SColor m_bgcolor;
142         video::SColor m_skycolor;
143         video::SColorf m_cloudcolor_f;
144         v3f m_stars[SKY_STAR_COUNT];
145         video::ITexture *m_sun_texture;
146         video::ITexture *m_moon_texture;
147         video::ITexture *m_sun_tonemap;
148         video::ITexture *m_moon_tonemap;
149         void draw_sun(video::IVideoDriver *driver, float sunsize, const video::SColor &suncolor,
150                         const video::SColor &suncolor2, float wicked_time_of_day);
151         void draw_moon(video::IVideoDriver *driver, float moonsize, const video::SColor &mooncolor,
152                         const video::SColor &mooncolor2, float wicked_time_of_day);
153         void draw_sky_body(std::array<video::S3DVertex, 4> &vertices,
154                         float pos_1, float pos_2, const video::SColor &c);
155         void place_sky_body(
156                         std::array<video::S3DVertex, 4> &vertices, float horizon_position,
157                         float day_position);
158 };