Line data Source code
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 "irrlichttypes_extrabloated.h"
21 : #include <ISceneNode.h>
22 : #include "camera.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 2 : 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 core::aabbox3d<f32>& getBoundingBox() const;
46 :
47 : // Used by Irrlicht for optimizing rendering
48 0 : virtual video::SMaterial& getMaterial(u32 i)
49 0 : { return m_materials[i]; }
50 :
51 : // Used by Irrlicht for optimizing rendering
52 0 : virtual u32 getMaterialCount() const
53 0 : { return SKY_MATERIAL_COUNT; }
54 :
55 : void update(float m_time_of_day, float time_brightness,
56 : float direct_brightness, bool sunlight_seen, CameraMode cam_mode,
57 : float yaw, float pitch);
58 :
59 1166 : float getBrightness(){ return m_brightness; }
60 :
61 1427244 : video::SColor getBgColor(){
62 1427244 : return m_visible ? m_bgcolor : m_fallback_bg_color;
63 : }
64 1166 : video::SColor getSkyColor(){
65 1166 : return m_visible ? m_skycolor : m_fallback_bg_color;
66 : }
67 :
68 1166 : bool getCloudsVisible(){ return m_clouds_visible && m_visible; }
69 1166 : video::SColorf getCloudColor(){ return m_cloudcolor_f; }
70 :
71 0 : void setVisible(bool visible){ m_visible = visible; }
72 0 : void setFallbackBgColor(const video::SColor &fallback_bg_color){
73 0 : m_fallback_bg_color = fallback_bg_color;
74 0 : }
75 :
76 : private:
77 : core::aabbox3d<f32> Box;
78 : video::SMaterial m_materials[SKY_MATERIAL_COUNT];
79 :
80 : // How much sun & moon transition should affect horizon color
81 6325 : float m_horizon_blend()
82 : {
83 6325 : if (!m_sunlight_seen)
84 0 : return 0;
85 6325 : float x = m_time_of_day >= 0.5 ? (1 - m_time_of_day) * 2 : m_time_of_day * 2;
86 :
87 6325 : if (x <= 0.3)
88 0 : return 0;
89 6325 : if (x <= 0.4) // when the sun and moon are aligned
90 0 : return (x - 0.3) * 10;
91 6325 : if (x <= 0.5)
92 0 : return (0.5 - x) * 10;
93 6325 : return 0;
94 : }
95 :
96 : // Mix two colors by a given amount
97 2530 : video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
98 : {
99 : video::SColor result = video::SColor(
100 2530 : col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor,
101 2530 : col1.getRed() * (1 - factor) + col2.getRed() * factor,
102 2530 : col1.getGreen() * (1 - factor) + col2.getGreen() * factor,
103 10120 : col1.getBlue() * (1 - factor) + col2.getBlue() * factor);
104 2530 : return result;
105 : }
106 1265 : video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
107 : {
108 : video::SColorf result = video::SColorf(
109 1265 : col1.r * (1 - factor) + col2.r * factor,
110 1265 : col1.g * (1 - factor) + col2.g * factor,
111 1265 : col1.b * (1 - factor) + col2.b * factor,
112 5060 : col1.a * (1 - factor) + col2.a * factor);
113 1265 : return result;
114 : }
115 :
116 : bool m_visible;
117 : video::SColor m_fallback_bg_color; // Used when m_visible=false
118 : bool m_first_update;
119 : float m_time_of_day;
120 : float m_time_brightness;
121 : bool m_sunlight_seen;
122 : float m_brightness;
123 : float m_cloud_brightness;
124 : bool m_clouds_visible;
125 : bool m_directional_colored_fog;
126 : video::SColorf m_bgcolor_bright_f;
127 : video::SColorf m_skycolor_bright_f;
128 : video::SColorf m_cloudcolor_bright_f;
129 : video::SColor m_bgcolor;
130 : video::SColor m_skycolor;
131 : video::SColorf m_cloudcolor_f;
132 : v3f m_stars[SKY_STAR_COUNT];
133 : video::S3DVertex m_star_vertices[SKY_STAR_COUNT*4];
134 : video::ITexture* m_sun_texture;
135 : video::ITexture* m_moon_texture;
136 : video::ITexture* m_sun_tonemap;
137 : video::ITexture* m_moon_tonemap;
138 : };
139 :
140 : #endif
141 :
|