]> git.lizzy.rs Git - minetest.git/blob - src/sky.cpp
Add clouds API
[minetest.git] / src / sky.cpp
1 #include "sky.h"
2 #include "IVideoDriver.h"
3 #include "ISceneManager.h"
4 #include "ICameraSceneNode.h"
5 #include "S3DVertex.h"
6 #include "client/tile.h"
7 #include "noise.h"  // easeCurve
8 #include "profiler.h"
9 #include "util/numeric.h"
10 #include <cmath>
11 #include "settings.h"
12 #include "camera.h"  // CameraModes
13
14
15 Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id,
16                 ITextureSource *tsrc):
17                 scene::ISceneNode(parent, mgr, id),
18                 m_visible(true),
19                 m_fallback_bg_color(255, 255, 255, 255),
20                 m_first_update(true),
21                 m_brightness(0.5),
22                 m_cloud_brightness(0.5),
23                 m_bgcolor_bright_f(1, 1, 1, 1),
24                 m_skycolor_bright_f(1, 1, 1, 1),
25                 m_cloudcolor_bright_f(1, 1, 1, 1)
26 {
27         setAutomaticCulling(scene::EAC_OFF);
28         m_box.MaxEdge.set(0, 0, 0);
29         m_box.MinEdge.set(0, 0, 0);
30
31         // Create material
32
33         video::SMaterial mat;
34         mat.Lighting = false;
35         mat.ZBuffer = video::ECFN_NEVER;
36         mat.ZWriteEnable = false;
37         mat.AntiAliasing = 0;
38         mat.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
39         mat.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
40         mat.BackfaceCulling = false;
41
42         m_materials[0] = mat;
43
44         m_materials[1] = mat;
45         //m_materials[1].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
46         m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
47
48         m_materials[2] = mat;
49         m_materials[2].setTexture(0, tsrc->getTextureForMesh("sunrisebg.png"));
50         m_materials[2].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
51         //m_materials[2].MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
52
53         m_sun_texture = tsrc->isKnownSourceImage("sun.png") ?
54                 tsrc->getTextureForMesh("sun.png") : NULL;
55         m_moon_texture = tsrc->isKnownSourceImage("moon.png") ?
56                 tsrc->getTextureForMesh("moon.png") : NULL;
57         m_sun_tonemap = tsrc->isKnownSourceImage("sun_tonemap.png") ?
58                 tsrc->getTexture("sun_tonemap.png") : NULL;
59         m_moon_tonemap = tsrc->isKnownSourceImage("moon_tonemap.png") ?
60                 tsrc->getTexture("moon_tonemap.png") : NULL;
61
62         if (m_sun_texture) {
63                 m_materials[3] = mat;
64                 m_materials[3].setTexture(0, m_sun_texture);
65                 m_materials[3].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
66                 if (m_sun_tonemap)
67                         m_materials[3].Lighting = true;
68         }
69
70         if (m_moon_texture) {
71                 m_materials[4] = mat;
72                 m_materials[4].setTexture(0, m_moon_texture);
73                 m_materials[4].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
74                 if (m_moon_tonemap)
75                         m_materials[4].Lighting = true;
76         }
77
78         for (u32 i = 0; i < SKY_STAR_COUNT; i++) {
79                 m_stars[i] = v3f(
80                         myrand_range(-10000, 10000),
81                         myrand_range(-10000, 10000),
82                         myrand_range(-10000, 10000)
83                 );
84                 m_stars[i].normalize();
85         }
86
87         m_directional_colored_fog = g_settings->getBool("directional_colored_fog");
88 }
89
90
91 void Sky::OnRegisterSceneNode()
92 {
93         if (IsVisible)
94                 SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX);
95
96         scene::ISceneNode::OnRegisterSceneNode();
97 }
98
99
100 void Sky::render()
101 {
102         if (!m_visible)
103                 return;
104
105         video::IVideoDriver* driver = SceneManager->getVideoDriver();
106         scene::ICameraSceneNode* camera = SceneManager->getActiveCamera();
107
108         if (!camera || !driver)
109                 return;
110         
111         ScopeProfiler sp(g_profiler, "Sky::render()", SPT_AVG);
112
113         // Draw perspective skybox
114
115         core::matrix4 translate(AbsoluteTransformation);
116         translate.setTranslation(camera->getAbsolutePosition());
117
118         // Draw the sky box between the near and far clip plane
119         const f32 viewDistance = (camera->getNearValue() + camera->getFarValue()) * 0.5f;
120         core::matrix4 scale;
121         scale.setScale(core::vector3df(viewDistance, viewDistance, viewDistance));
122
123         driver->setTransform(video::ETS_WORLD, translate * scale);
124
125         if (m_sunlight_seen) {
126                 float sunsize = 0.07;
127                 video::SColorf suncolor_f(1, 1, 0, 1);
128                 suncolor_f.r = 1;
129                 suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.7 + m_time_brightness * 0.5));
130                 suncolor_f.b = MYMAX(0.0, m_brightness * 0.95);
131                 video::SColorf suncolor2_f(1, 1, 1, 1);
132                 suncolor_f.r = 1;
133                 suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.85 + m_time_brightness * 0.5));
134                 suncolor_f.b = MYMAX(0.0, m_brightness);
135
136                 float moonsize = 0.04;
137                 video::SColorf mooncolor_f(0.50, 0.57, 0.65, 1);
138                 video::SColorf mooncolor2_f(0.85, 0.875, 0.9, 1);
139                 
140                 float nightlength = 0.415;
141                 float wn = nightlength / 2;
142                 float wicked_time_of_day = 0;
143                 if (m_time_of_day > wn && m_time_of_day < 1.0 - wn)
144                         wicked_time_of_day = (m_time_of_day - wn) / (1.0 - wn * 2) * 0.5 + 0.25;
145                 else if (m_time_of_day < 0.5)
146                         wicked_time_of_day = m_time_of_day / wn * 0.25;
147                 else
148                         wicked_time_of_day = 1.0 - ((1.0 - m_time_of_day) / wn * 0.25);
149                 /*std::cerr<<"time_of_day="<<m_time_of_day<<" -> "
150                                 <<"wicked_time_of_day="<<wicked_time_of_day<<std::endl;*/
151
152                 video::SColor suncolor = suncolor_f.toSColor();
153                 video::SColor suncolor2 = suncolor2_f.toSColor();
154                 video::SColor mooncolor = mooncolor_f.toSColor();
155                 video::SColor mooncolor2 = mooncolor2_f.toSColor();
156
157                 // Calculate offset normalized to the X dimension of a 512x1 px tonemap
158                 float offset = (1.0 - fabs(sin((m_time_of_day - 0.5) * irr::core::PI))) * 511;
159
160                 if (m_sun_tonemap) {
161                         u8 * texels = (u8 *)m_sun_tonemap->lock();
162                         video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
163                         video::SColor texel_color (255, texel->getRed(),
164                                 texel->getGreen(), texel->getBlue());
165                         m_sun_tonemap->unlock();
166                         m_materials[3].EmissiveColor = texel_color;
167                 }
168
169                 if (m_moon_tonemap) {
170                         u8 * texels = (u8 *)m_moon_tonemap->lock();
171                         video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
172                         video::SColor texel_color (255, texel->getRed(),
173                                 texel->getGreen(), texel->getBlue());
174                         m_moon_tonemap->unlock();
175                         m_materials[4].EmissiveColor = texel_color;
176                 }
177
178                 const f32 t = 1.0f;
179                 const f32 o = 0.0f;
180                 static const u16 indices[4] = {0, 1, 2, 3};
181                 video::S3DVertex vertices[4];
182                 
183                 driver->setMaterial(m_materials[1]);
184                 
185                 video::SColor cloudyfogcolor = m_bgcolor;
186                 
187                 // Draw far cloudy fog thing blended with skycolor
188                 for (u32 j = 0; j < 4; j++) {
189                         video::SColor c = cloudyfogcolor.getInterpolated(m_skycolor, 0.45);
190                         vertices[0] = video::S3DVertex(-1, 0.08, -1, 0, 0, 1, c, t, t);
191                         vertices[1] = video::S3DVertex( 1, 0.08, -1, 0, 0, 1, c, o, t);
192                         vertices[2] = video::S3DVertex( 1, 0.12, -1, 0, 0, 1, c, o, o);
193                         vertices[3] = video::S3DVertex(-1, 0.12, -1, 0, 0, 1, c, t, o);
194                         for (u32 i = 0; i < 4; i++) {
195                                 if (j == 0)
196                                         // Don't switch
197                                         {}
198                                 else if (j == 1)
199                                         // Switch from -Z (south) to +X (east)
200                                         vertices[i].Pos.rotateXZBy(90);
201                                 else if (j == 2)
202                                         // Switch from -Z (south) to -X (west)
203                                         vertices[i].Pos.rotateXZBy(-90);
204                                 else
205                                         // Switch from -Z (south) to +Z (north)
206                                         vertices[i].Pos.rotateXZBy(-180);
207                         }
208                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
209                 }
210
211                 // Draw far cloudy fog thing
212                 for (u32 j = 0; j < 4; j++) {
213                         video::SColor c = cloudyfogcolor;
214                         vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 0, 1, c, t, t);
215                         vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 0, 1, c, o, t);
216                         vertices[2] = video::S3DVertex( 1, 0.08, -1, 0, 0, 1, c, o, o);
217                         vertices[3] = video::S3DVertex(-1, 0.08, -1, 0, 0, 1, c, t, o);
218                         for (u32 i = 0; i < 4; i++) {
219                                 if (j == 0)
220                                         // Don't switch
221                                         {}
222                                 else if (j == 1)
223                                         // Switch from -Z (south) to +X (east)
224                                         vertices[i].Pos.rotateXZBy(90);
225                                 else if (j == 2)
226                                         // Switch from -Z (south) to -X (west)
227                                         vertices[i].Pos.rotateXZBy(-90);
228                                 else
229                                         // Switch from -Z (south) to +Z (north)
230                                         vertices[i].Pos.rotateXZBy(-180);
231                         }
232                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
233                 }
234
235                 // Draw bottom far cloudy fog thing
236                 video::SColor c = cloudyfogcolor;
237                 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 1, 0, c, t, t);
238                 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 1, 0, c, o, t);
239                 vertices[2] = video::S3DVertex( 1, -1.0, 1, 0, 1, 0, c, o, o);
240                 vertices[3] = video::S3DVertex(-1, -1.0, 1, 0, 1, 0, c, t, o);
241                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
242
243                 driver->setMaterial(m_materials[2]);
244
245                 // Draw sunrise/sunset horizon glow texture (textures/base/pack/sunrisebg.png)
246                 {
247                         float mid1 = 0.25;
248                         float mid = wicked_time_of_day < 0.5 ? mid1 : (1.0 - mid1);
249                         float a_ = 1.0 - fabs(wicked_time_of_day - mid) * 35.0;
250                         float a = easeCurve(MYMAX(0, MYMIN(1, a_)));
251                         //std::cerr<<"a_="<<a_<<" a="<<a<<std::endl;
252                         video::SColor c(255, 255, 255, 255);
253                         float y = -(1.0 - a) * 0.22;
254                         vertices[0] = video::S3DVertex(-1, -0.05 + y, -1, 0, 0, 1, c, t, t);
255                         vertices[1] = video::S3DVertex( 1, -0.05 + y, -1, 0, 0, 1, c, o, t);
256                         vertices[2] = video::S3DVertex( 1,   0.2 + y, -1, 0, 0, 1, c, o, o);
257                         vertices[3] = video::S3DVertex(-1,   0.2 + y, -1, 0, 0, 1, c, t, o);
258                         for (u32 i = 0; i < 4; i++) {
259                                 if (wicked_time_of_day < 0.5)
260                                         // Switch from -Z (south) to +X (east)
261                                         vertices[i].Pos.rotateXZBy(90);
262                                 else
263                                         // Switch from -Z (south) to -X (west)
264                                         vertices[i].Pos.rotateXZBy(-90);
265                         }
266                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
267                 }
268
269                 // Draw sun
270                 if (wicked_time_of_day > 0.15 && wicked_time_of_day < 0.85) {
271                         if (!m_sun_texture) {
272                                 driver->setMaterial(m_materials[1]);
273                                 float d = sunsize * 1.7;
274                                 video::SColor c = suncolor;
275                                 c.setAlpha(0.05 * 255);
276                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
277                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
278                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, c, o, o);
279                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, c, t, o);
280                                 for (u32 i = 0; i < 4; i++) {
281                                         // Switch from -Z (south) to +X (east)
282                                         vertices[i].Pos.rotateXZBy(90);
283                                         vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
284                                 }
285                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
286
287                                 d = sunsize * 1.2;
288                                 c = suncolor;
289                                 c.setAlpha(0.15 * 255);
290                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
291                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
292                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, c, o, o);
293                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, c, t, o);
294                                 for (u32 i = 0; i < 4; i++) {
295                                         // Switch from -Z (south) to +X (east)
296                                         vertices[i].Pos.rotateXZBy(90);
297                                         vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
298                                 }
299                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
300
301                                 d = sunsize;
302                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, suncolor, t, t);
303                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, suncolor, o, t);
304                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, suncolor, o, o);
305                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, suncolor, t, o);
306                                 for (u32 i = 0; i < 4; i++) {
307                                         // Switch from -Z (south) to +X (east)
308                                         vertices[i].Pos.rotateXZBy(90);
309                                         vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
310                                 }
311                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
312
313                                 d = sunsize * 0.7;
314                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, suncolor2, t, t);
315                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, suncolor2, o, t);
316                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, suncolor2, o, o);
317                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, suncolor2, t, o);
318                                 for (u32 i = 0; i < 4; i++) {
319                                         // Switch from -Z (south) to +X (east)
320                                         vertices[i].Pos.rotateXZBy(90);
321                                         vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
322                                 }
323                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
324                         } else {
325                                 driver->setMaterial(m_materials[3]);
326                                 float d = sunsize * 1.7;
327                                 video::SColor c;
328                                 if (m_sun_tonemap)
329                                         c = video::SColor (0, 0, 0, 0);
330                                 else
331                                         c = video::SColor (255, 255, 255, 255);
332                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
333                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
334                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, c, o, o);
335                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, c, t, o);
336                                 for(u32 i = 0; i < 4; i++) {
337                                         // Switch from -Z (south) to +X (east)
338                                         vertices[i].Pos.rotateXZBy(90);
339                                         vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
340                                 }
341                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
342                         }
343                 }
344
345                 // Draw moon
346                 if (wicked_time_of_day < 0.3 || wicked_time_of_day > 0.7) {
347                         if (!m_moon_texture) {
348                                 driver->setMaterial(m_materials[1]);
349                                 float d = moonsize * 1.9;
350                                 video::SColor c = mooncolor;
351                                 c.setAlpha(0.05 * 255);
352                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
353                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
354                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, c, o, o);
355                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, c, t, o);
356                                 for (u32 i = 0; i < 4; i++) {
357                                         // Switch from -Z (south) to -X (west)
358                                         vertices[i].Pos.rotateXZBy(-90);
359                                         vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
360                                 }
361                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
362                         
363                                 d = moonsize * 1.3;
364                                 c = mooncolor;
365                                 c.setAlpha(0.15 * 255);
366                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
367                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
368                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, c, o, o);
369                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, c, t, o);
370                                 for (u32 i = 0; i < 4; i++) {
371                                         // Switch from -Z (south) to -X (west)
372                                         vertices[i].Pos.rotateXZBy(-90);
373                                         vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
374                                 }
375                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
376
377                                 d = moonsize;
378                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, mooncolor, t, t);
379                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, mooncolor, o, t);
380                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, mooncolor, o, o);
381                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, mooncolor, t, o);
382                                 for (u32 i = 0; i < 4; i++) {
383                                         // Switch from -Z (south) to -X (west)
384                                         vertices[i].Pos.rotateXZBy(-90);
385                                         vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
386                                 }
387                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
388
389                                 float d2 = moonsize * 0.6;
390                                 vertices[0] = video::S3DVertex(-d, -d,  -1, 0, 0, 1, mooncolor2, t, t);
391                                 vertices[1] = video::S3DVertex( d2,-d,  -1, 0, 0, 1, mooncolor2, o, t);
392                                 vertices[2] = video::S3DVertex( d2, d2, -1, 0, 0, 1, mooncolor2, o, o);
393                                 vertices[3] = video::S3DVertex(-d,  d2, -1, 0, 0, 1, mooncolor2, t, o);
394                                 for (u32 i = 0; i < 4; i++) {
395                                         // Switch from -Z (south) to -X (west)
396                                         vertices[i].Pos.rotateXZBy(-90);
397                                         vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
398                                 }
399                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
400                         } else {
401                                 driver->setMaterial(m_materials[4]);
402                                 float d = moonsize * 1.9;
403                                 video::SColor c;
404                                 if (m_moon_tonemap)
405                                         c = video::SColor (0, 0, 0, 0);
406                                 else
407                                         c = video::SColor (255, 255, 255, 255);
408                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
409                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
410                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, c, o, o);
411                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, c, t, o);
412                                 for (u32 i = 0; i < 4; i++) {
413                                         // Switch from -Z (south) to -X (west)
414                                         vertices[i].Pos.rotateXZBy(-90);
415                                         vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
416                                 }
417                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
418                         }
419                 }
420
421                 // Draw stars
422                 driver->setMaterial(m_materials[1]);
423                 do {
424                         float starbrightness = MYMAX(0, MYMIN(1,
425                                 (0.285 - fabs(wicked_time_of_day < 0.5 ?
426                                 wicked_time_of_day : (1.0 - wicked_time_of_day))) * 10));
427                         float f = starbrightness;
428                         float d = 0.007;
429                         video::SColor starcolor(255, f * 90, f * 90, f * 90);
430                         if (starcolor.getBlue() < m_skycolor.getBlue())
431                                 break;
432                         u16 indices[SKY_STAR_COUNT * 4];
433                         video::S3DVertex vertices[SKY_STAR_COUNT * 4];
434                         for (u32 i = 0; i < SKY_STAR_COUNT; i++) {
435                                 indices[i * 4 + 0] = i * 4 + 0;
436                                 indices[i * 4 + 1] = i * 4 + 1;
437                                 indices[i * 4 + 2] = i * 4 + 2;
438                                 indices[i * 4 + 3] = i * 4 + 3;
439                                 v3f p = m_stars[i];
440                                 core::CMatrix4<f32> a;
441                                 a.buildRotateFromTo(v3f(0, 1, 0), v3f(d, 1 + d / 2, 0));
442                                 v3f p1 = p;
443                                 a.rotateVect(p1);
444                                 a.buildRotateFromTo(v3f(0, 1, 0), v3f(d, 1, d));
445                                 v3f p2 = p;
446                                 a.rotateVect(p2);
447                                 a.buildRotateFromTo(v3f(0, 1, 0), v3f(0, 1 - d / 2, d));
448                                 v3f p3 = p;
449                                 a.rotateVect(p3);
450                                 p.rotateXYBy(wicked_time_of_day * 360 - 90);
451                                 p1.rotateXYBy(wicked_time_of_day * 360 - 90);
452                                 p2.rotateXYBy(wicked_time_of_day * 360 - 90);
453                                 p3.rotateXYBy(wicked_time_of_day * 360 - 90);
454                                 vertices[i * 4 + 0].Pos = p;
455                                 vertices[i * 4 + 0].Color = starcolor;
456                                 vertices[i * 4 + 1].Pos = p1;
457                                 vertices[i * 4 + 1].Color = starcolor;
458                                 vertices[i * 4 + 2].Pos = p2;
459                                 vertices[i * 4 + 2].Color = starcolor;
460                                 vertices[i * 4 + 3].Pos = p3;
461                                 vertices[i * 4 + 3].Color = starcolor;
462                         }
463                         driver->drawVertexPrimitiveList(vertices, SKY_STAR_COUNT * 4,
464                                 indices, SKY_STAR_COUNT, video::EVT_STANDARD,
465                                 scene::EPT_QUADS, video::EIT_16BIT);
466                 } while(0);
467                 
468                 // Draw far cloudy fog thing below east and west horizons
469                 for (u32 j = 0; j < 2; j++) {
470                         video::SColor c = cloudyfogcolor;
471                         vertices[0] = video::S3DVertex(-1, -1.0,  -1, 0, 0, 1, c, t, t);
472                         vertices[1] = video::S3DVertex( 1, -1.0,  -1, 0, 0, 1, c, o, t);
473                         vertices[2] = video::S3DVertex( 1, -0.02, -1, 0, 0, 1, c, o, o);
474                         vertices[3] = video::S3DVertex(-1, -0.02, -1, 0, 0, 1, c, t, o);
475                         for (u32 i = 0; i < 4; i++) {
476                                 //if (wicked_time_of_day < 0.5)
477                                 if (j == 0)
478                                         // Switch from -Z (south) to +X (east)
479                                         vertices[i].Pos.rotateXZBy(90);
480                                 else
481                                         // Switch from -Z (south) to -X (west)
482                                         vertices[i].Pos.rotateXZBy(-90);
483                         }
484                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
485                 }
486         }
487 }
488
489
490 void Sky::update(float time_of_day, float time_brightness,
491                 float direct_brightness, bool sunlight_seen,
492                 CameraMode cam_mode, float yaw, float pitch)
493 {
494         // Stabilize initial brightness and color values by flooding updates
495         if (m_first_update) {
496                 /*dstream<<"First update with time_of_day="<<time_of_day
497                                 <<" time_brightness="<<time_brightness
498                                 <<" direct_brightness="<<direct_brightness
499                                 <<" sunlight_seen="<<sunlight_seen<<std::endl;*/
500                 m_first_update = false;
501                 for (u32 i = 0; i < 100; i++) {
502                         update(time_of_day, time_brightness, direct_brightness,
503                                 sunlight_seen, cam_mode, yaw, pitch);
504                 }
505                 return;
506         }
507
508         m_time_of_day = time_of_day;
509         m_time_brightness = time_brightness;
510         m_sunlight_seen = sunlight_seen;
511         
512         bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
513
514         /*
515         Development colours
516
517         video::SColorf bgcolor_bright_normal_f(170. / 255, 200. / 255, 230. / 255, 1.0);
518         video::SColorf bgcolor_bright_dawn_f(0.666, 200. / 255 * 0.7, 230. / 255 * 0.5, 1.0);
519         video::SColorf bgcolor_bright_dawn_f(0.666, 0.549, 0.220, 1.0);
520         video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.0, 1.0);
521         video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.2, 1.0);
522
523         video::SColorf cloudcolor_bright_dawn_f(1.0, 0.591, 0.4);
524         video::SColorf cloudcolor_bright_dawn_f(1.0, 0.65, 0.44);
525         video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
526         */
527
528         video::SColorf bgcolor_bright_normal_f = video::SColor(255, 155, 193, 240);
529         video::SColorf bgcolor_bright_indoor_f = video::SColor(255, 100, 100, 100);
530         video::SColorf bgcolor_bright_dawn_f = video::SColor(255, 186, 193, 240);
531         video::SColorf bgcolor_bright_night_f = video::SColor(255, 64, 144, 255);
532
533         video::SColorf skycolor_bright_normal_f = video::SColor(255, 140, 186, 250);
534         video::SColorf skycolor_bright_dawn_f = video::SColor(255, 180, 186, 250);
535         video::SColorf skycolor_bright_night_f = video::SColor(255, 0, 107, 255);
536         
537         // pure white: becomes "diffuse light component" for clouds
538         video::SColorf cloudcolor_bright_normal_f = video::SColor(255, 255, 255, 255);
539         // dawn-factoring version of pure white (note: R is above 1.0)
540         video::SColorf cloudcolor_bright_dawn_f(255.0f/240.0f, 223.0f/240.0f, 191.0f/255.0f);
541
542         float cloud_color_change_fraction = 0.95;
543         if (sunlight_seen) {
544                 if (fabs(time_brightness - m_brightness) < 0.2) {
545                         m_brightness = m_brightness * 0.95 + time_brightness * 0.05;
546                 } else {
547                         m_brightness = m_brightness * 0.80 + time_brightness * 0.20;
548                         cloud_color_change_fraction = 0.0;
549                 }
550         } else {
551                 if (direct_brightness < m_brightness)
552                         m_brightness = m_brightness * 0.95 + direct_brightness * 0.05;
553                 else
554                         m_brightness = m_brightness * 0.98 + direct_brightness * 0.02;
555         }
556         
557         m_clouds_visible = true;
558         float color_change_fraction = 0.98;
559         if (sunlight_seen) {
560                 if (is_dawn) {  // Dawn
561                         m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
562                                 bgcolor_bright_dawn_f, color_change_fraction);
563                         m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
564                                 skycolor_bright_dawn_f, color_change_fraction);
565                         m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
566                                 cloudcolor_bright_dawn_f, color_change_fraction);
567                 } else {
568                         if (time_brightness < 0.07) {  // Night
569                                 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
570                                         bgcolor_bright_night_f, color_change_fraction);
571                                 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
572                                         skycolor_bright_night_f, color_change_fraction);
573                         } else {  // Day
574                                 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
575                                         bgcolor_bright_normal_f, color_change_fraction);
576                                 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
577                                         skycolor_bright_normal_f, color_change_fraction);
578                         }
579
580                         m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
581                                 cloudcolor_bright_normal_f, color_change_fraction);
582                 }
583         } else {
584                 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
585                         bgcolor_bright_indoor_f, color_change_fraction);
586                 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
587                         bgcolor_bright_indoor_f, color_change_fraction);
588                 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
589                         cloudcolor_bright_normal_f, color_change_fraction);
590                 m_clouds_visible = false;
591         }
592
593         video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
594         m_bgcolor = video::SColor(
595                 255,
596                 bgcolor_bright.getRed() * m_brightness,
597                 bgcolor_bright.getGreen() * m_brightness,
598                 bgcolor_bright.getBlue() * m_brightness
599         );
600
601         video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
602         m_skycolor = video::SColor(
603                 255,
604                 skycolor_bright.getRed() * m_brightness,
605                 skycolor_bright.getGreen() * m_brightness,
606                 skycolor_bright.getBlue() * m_brightness
607         );
608
609         // Horizon coloring based on sun and moon direction during sunset and sunrise
610         video::SColor pointcolor = video::SColor(255, 255, 255, m_bgcolor.getAlpha());
611         if (m_directional_colored_fog) {
612                 if (m_horizon_blend() != 0) {
613                         // Calculate hemisphere value from yaw, (inverted in third person front view)
614                         s8 dir_factor = 1;
615                         if (cam_mode > CAMERA_MODE_THIRD)
616                                 dir_factor = -1;
617                         f32 pointcolor_blend = wrapDegrees_0_360(yaw * dir_factor + 90);
618                         if (pointcolor_blend > 180)
619                                 pointcolor_blend = 360 - pointcolor_blend;
620                         pointcolor_blend /= 180;
621                         // Bound view angle to determine where transition starts and ends
622                         pointcolor_blend = rangelim(1 - pointcolor_blend * 1.375, 0, 1 / 1.375) *
623                                 1.375;
624                         // Combine the colors when looking up or down, otherwise turning looks weird
625                         pointcolor_blend += (0.5 - pointcolor_blend) *
626                                 (1 - MYMIN((90 - std::fabs(pitch)) / 90 * 1.5, 1));
627                         // Invert direction to match where the sun and moon are rising
628                         if (m_time_of_day > 0.5)
629                                 pointcolor_blend = 1 - pointcolor_blend;
630                         // Horizon colors of sun and moon
631                         f32 pointcolor_light = rangelim(m_time_brightness * 3, 0.2, 1);
632
633                         video::SColorf pointcolor_sun_f(1, 1, 1, 1);
634                         if (m_sun_tonemap) {
635                                 pointcolor_sun_f.r = pointcolor_light *
636                                         (float)m_materials[3].EmissiveColor.getRed() / 255;
637                                 pointcolor_sun_f.b = pointcolor_light *
638                                         (float)m_materials[3].EmissiveColor.getBlue() / 255;
639                                 pointcolor_sun_f.g = pointcolor_light *
640                                         (float)m_materials[3].EmissiveColor.getGreen() / 255;
641                         } else {
642                                 pointcolor_sun_f.r = pointcolor_light * 1;
643                                 pointcolor_sun_f.b = pointcolor_light *
644                                         (0.25 + (rangelim(m_time_brightness, 0.25, 0.75) - 0.25) * 2 * 0.75);
645                                 pointcolor_sun_f.g = pointcolor_light * (pointcolor_sun_f.b * 0.375 +
646                                         (rangelim(m_time_brightness, 0.05, 0.15) - 0.05) * 10 * 0.625);
647                         }
648
649                         video::SColorf pointcolor_moon_f(0.5 * pointcolor_light,
650                                 0.6 * pointcolor_light, 0.8 * pointcolor_light, 1);
651                         if (m_moon_tonemap) {
652                                 pointcolor_moon_f.r = pointcolor_light *
653                                         (float)m_materials[4].EmissiveColor.getRed() / 255;
654                                 pointcolor_moon_f.b = pointcolor_light *
655                                         (float)m_materials[4].EmissiveColor.getBlue() / 255;
656                                 pointcolor_moon_f.g = pointcolor_light *
657                                         (float)m_materials[4].EmissiveColor.getGreen() / 255;
658                         }
659
660                         video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
661                         video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
662                         // Calculate the blend color
663                         pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
664                 }
665                 m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
666                 m_skycolor = m_mix_scolor(m_skycolor, pointcolor, m_horizon_blend() * 0.25);
667         }
668
669         float cloud_direct_brightness = 0;
670         if (sunlight_seen) {
671                 if (!m_directional_colored_fog) {
672                         cloud_direct_brightness = time_brightness;
673                         if (time_brightness >= 0.2 && time_brightness < 0.7)
674                                 cloud_direct_brightness *= 1.3;
675                 } else {
676                         cloud_direct_brightness = MYMIN(m_horizon_blend() * 0.15 +
677                                 m_time_brightness, 1);
678                 }
679         } else {
680                 cloud_direct_brightness = direct_brightness;
681         }
682
683         m_cloud_brightness = m_cloud_brightness * cloud_color_change_fraction +
684                 cloud_direct_brightness * (1.0 - cloud_color_change_fraction);
685         m_cloudcolor_f = video::SColorf(
686                 m_cloudcolor_bright_f.r * m_cloud_brightness,
687                 m_cloudcolor_bright_f.g * m_cloud_brightness,
688                 m_cloudcolor_bright_f.b * m_cloud_brightness,
689                 1.0
690         );
691         if (m_directional_colored_fog) {
692                 m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f,
693                         video::SColorf(pointcolor), m_horizon_blend() * 0.25);
694         }
695 }