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