Fix player rotations (#7941)
[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
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                 // Draw bottom far cloudy fog thing
256                 video::SColor c = cloudyfogcolor;
257                 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 1, 0, c, t, t);
258                 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 1, 0, c, o, t);
259                 vertices[2] = video::S3DVertex( 1, -1.0, 1, 0, 1, 0, c, o, o);
260                 vertices[3] = video::S3DVertex(-1, -1.0, 1, 0, 1, 0, c, t, o);
261                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
262
263                 // If sun, moon and stars are (temporarily) disabled, abort here
264                 if (!m_bodies_visible)
265                         return;
266
267                 driver->setMaterial(m_materials[2]);
268
269                 // Draw sunrise/sunset horizon glow texture (textures/base/pack/sunrisebg.png)
270                 {
271                         float mid1 = 0.25;
272                         float mid = wicked_time_of_day < 0.5 ? mid1 : (1.0 - mid1);
273                         float a_ = 1.0f - std::fabs(wicked_time_of_day - mid) * 35.0f;
274                         float a = easeCurve(MYMAX(0, MYMIN(1, a_)));
275                         //std::cerr<<"a_="<<a_<<" a="<<a<<std::endl;
276                         video::SColor c(255, 255, 255, 255);
277                         float y = -(1.0 - a) * 0.22;
278                         vertices[0] = video::S3DVertex(-1, -0.05 + y, -1, 0, 0, 1, c, t, t);
279                         vertices[1] = video::S3DVertex( 1, -0.05 + y, -1, 0, 0, 1, c, o, t);
280                         vertices[2] = video::S3DVertex( 1,   0.2 + y, -1, 0, 0, 1, c, o, o);
281                         vertices[3] = video::S3DVertex(-1,   0.2 + y, -1, 0, 0, 1, c, t, o);
282                         for (video::S3DVertex &vertex : vertices) {
283                                 if (wicked_time_of_day < 0.5)
284                                         // Switch from -Z (south) to +X (east)
285                                         vertex.Pos.rotateXZBy(90);
286                                 else
287                                         // Switch from -Z (south) to -X (west)
288                                         vertex.Pos.rotateXZBy(-90);
289                         }
290                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
291                 }
292
293                 // Draw sun
294                 if (wicked_time_of_day > 0.15 && wicked_time_of_day < 0.85) {
295                         if (!m_sun_texture) {
296                                 driver->setMaterial(m_materials[1]);
297                                 float d = sunsize * 1.7;
298                                 video::SColor c = suncolor;
299                                 c.setAlpha(0.05 * 255);
300                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
301                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
302                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, c, o, o);
303                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, c, t, o);
304                                 for (video::S3DVertex &vertex : vertices) {
305                                         // Switch from -Z (south) to +X (east)
306                                         vertex.Pos.rotateXZBy(90);
307                                         vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
308                                 }
309                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
310
311                                 d = sunsize * 1.2;
312                                 c = suncolor;
313                                 c.setAlpha(0.15 * 255);
314                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
315                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
316                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, c, o, o);
317                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, c, t, o);
318                                 for (video::S3DVertex &vertex : vertices) {
319                                         // Switch from -Z (south) to +X (east)
320                                         vertex.Pos.rotateXZBy(90);
321                                         vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
322                                 }
323                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
324
325                                 d = sunsize;
326                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, suncolor, t, t);
327                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, suncolor, o, t);
328                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, suncolor, o, o);
329                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, suncolor, t, o);
330                                 for (video::S3DVertex &vertex : vertices) {
331                                         // Switch from -Z (south) to +X (east)
332                                         vertex.Pos.rotateXZBy(90);
333                                         vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
334                                 }
335                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
336
337                                 d = sunsize * 0.7;
338                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, suncolor2, t, t);
339                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, suncolor2, o, t);
340                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, suncolor2, o, o);
341                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, suncolor2, t, o);
342                                 for (video::S3DVertex &vertex : vertices) {
343                                         // Switch from -Z (south) to +X (east)
344                                         vertex.Pos.rotateXZBy(90);
345                                         vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
346                                 }
347                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
348                         } else {
349                                 driver->setMaterial(m_materials[3]);
350                                 float d = sunsize * 1.7;
351                                 video::SColor c;
352                                 if (m_sun_tonemap)
353                                         c = video::SColor (0, 0, 0, 0);
354                                 else
355                                         c = video::SColor (255, 255, 255, 255);
356                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
357                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
358                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, c, o, o);
359                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, c, t, o);
360                                 for (video::S3DVertex &vertex : vertices) {
361                                         // Switch from -Z (south) to +X (east)
362                                         vertex.Pos.rotateXZBy(90);
363                                         vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
364                                 }
365                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
366                         }
367                 }
368
369                 // Draw stars before moon to be behind the moon
370                 do {
371                         driver->setMaterial(m_materials[1]);
372                         // Tune values so that stars first appear just after the sun
373                         // disappears over the horizon, and disappear just before the sun 
374                         // appears over the horizon.
375                         // Also tune so that stars are at full brightness from time 20000 to 
376                         // time 4000.
377                         float starbrightness = MYMAX(0, MYMIN(1,
378                                 (0.25 - fabs(wicked_time_of_day < 0.5 ?
379                                 wicked_time_of_day : (1.0 - wicked_time_of_day))) * 20));
380                         float f = starbrightness;
381                         float d = 0.007 / 2;
382                         video::SColor starcolor(255, f * 90, f * 90, f * 90);
383                         // Stars are only drawn when brighter than skycolor
384                         if (starcolor.getBlue() < m_skycolor.getBlue())
385                                 break;
386 #ifdef __ANDROID__
387                         u16 indices[SKY_STAR_COUNT * 3];
388                         video::S3DVertex vertices[SKY_STAR_COUNT * 3];
389                         for (u32 i = 0; i < SKY_STAR_COUNT; i++) {
390                                 indices[i * 3 + 0] = i * 3 + 0;
391                                 indices[i * 3 + 1] = i * 3 + 1;
392                                 indices[i * 3 + 2] = i * 3 + 2;
393                                 v3f r = m_stars[i];
394                                 core::CMatrix4<f32> a;
395                                 a.buildRotateFromTo(v3f(0, 1, 0), r);
396                                 v3f p = v3f(-d, 1, -d);
397                                 v3f p1 = v3f(d, 1, 0);
398                                 v3f p2 = v3f(-d, 1, d);
399                                 a.rotateVect(p);
400                                 a.rotateVect(p1);
401                                 a.rotateVect(p2);
402                                 p.rotateXYBy(wicked_time_of_day * 360 - 90);
403                                 p1.rotateXYBy(wicked_time_of_day * 360 - 90);
404                                 p2.rotateXYBy(wicked_time_of_day * 360 - 90);
405                                 vertices[i * 3 + 0].Pos = p;
406                                 vertices[i * 3 + 0].Color = starcolor;
407                                 vertices[i * 3 + 1].Pos = p1;
408                                 vertices[i * 3 + 1].Color = starcolor;
409                                 vertices[i * 3 + 2].Pos = p2;
410                                 vertices[i * 3 + 2].Color = starcolor;
411                         }
412                         driver->drawIndexedTriangleList(vertices, SKY_STAR_COUNT * 3,
413                                         indices, SKY_STAR_COUNT);
414 #else
415                         u16 indices[SKY_STAR_COUNT * 4];
416                         video::S3DVertex vertices[SKY_STAR_COUNT * 4];
417                         for (u32 i = 0; i < SKY_STAR_COUNT; i++) {
418                                 indices[i * 4 + 0] = i * 4 + 0;
419                                 indices[i * 4 + 1] = i * 4 + 1;
420                                 indices[i * 4 + 2] = i * 4 + 2;
421                                 indices[i * 4 + 3] = i * 4 + 3;
422                                 v3f r = m_stars[i];
423                                 core::CMatrix4<f32> a;
424                                 a.buildRotateFromTo(v3f(0, 1, 0), r);
425                                 v3f p = v3f(-d, 1, -d);
426                                 v3f p1 = v3f( d, 1, -d);
427                                 v3f p2 = v3f( d, 1, d);
428                                 v3f p3 = v3f(-d, 1, d);
429                                 a.rotateVect(p);
430                                 a.rotateVect(p1);
431                                 a.rotateVect(p2);
432                                 a.rotateVect(p3);
433                                 p.rotateXYBy(wicked_time_of_day * 360 - 90);
434                                 p1.rotateXYBy(wicked_time_of_day * 360 - 90);
435                                 p2.rotateXYBy(wicked_time_of_day * 360 - 90);
436                                 p3.rotateXYBy(wicked_time_of_day * 360 - 90);
437                                 vertices[i * 4 + 0].Pos = p;
438                                 vertices[i * 4 + 0].Color = starcolor;
439                                 vertices[i * 4 + 1].Pos = p1;
440                                 vertices[i * 4 + 1].Color = starcolor;
441                                 vertices[i * 4 + 2].Pos = p2;
442                                 vertices[i * 4 + 2].Color = starcolor;
443                                 vertices[i * 4 + 3].Pos = p3;
444                                 vertices[i * 4 + 3].Color = starcolor;
445                         }
446                         driver->drawVertexPrimitiveList(vertices, SKY_STAR_COUNT * 4,
447                                 indices, SKY_STAR_COUNT, video::EVT_STANDARD,
448                                 scene::EPT_QUADS, video::EIT_16BIT);
449 #endif
450                 } while(false);
451
452                 // Draw moon
453                 if (wicked_time_of_day < 0.3 || wicked_time_of_day > 0.7) {
454                         if (!m_moon_texture) {
455                                 driver->setMaterial(m_materials[1]);
456                                 float d = moonsize * 1.9;
457                                 video::SColor c = mooncolor;
458                                 c.setAlpha(0.05 * 255);
459                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
460                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
461                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, c, o, o);
462                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, c, t, o);
463                                 for (video::S3DVertex &vertex : vertices) {
464                                         // Switch from -Z (south) to -X (west)
465                                         vertex.Pos.rotateXZBy(-90);
466                                         vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
467                                 }
468                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
469
470                                 d = moonsize * 1.3;
471                                 c = mooncolor;
472                                 c.setAlpha(0.15 * 255);
473                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
474                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
475                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, c, o, o);
476                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, c, t, o);
477                                 for (video::S3DVertex &vertex : vertices) {
478                                         // Switch from -Z (south) to -X (west)
479                                         vertex.Pos.rotateXZBy(-90);
480                                         vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
481                                 }
482                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
483
484                                 d = moonsize;
485                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, mooncolor, t, t);
486                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, mooncolor, o, t);
487                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, mooncolor, o, o);
488                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, mooncolor, t, o);
489                                 for (video::S3DVertex &vertex : vertices) {
490                                         // Switch from -Z (south) to -X (west)
491                                         vertex.Pos.rotateXZBy(-90);
492                                         vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
493                                 }
494                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
495
496                                 float d2 = moonsize * 0.6;
497                                 vertices[0] = video::S3DVertex(-d, -d,  -1, 0, 0, 1, mooncolor2, t, t);
498                                 vertices[1] = video::S3DVertex( d2,-d,  -1, 0, 0, 1, mooncolor2, o, t);
499                                 vertices[2] = video::S3DVertex( d2, d2, -1, 0, 0, 1, mooncolor2, o, o);
500                                 vertices[3] = video::S3DVertex(-d,  d2, -1, 0, 0, 1, mooncolor2, t, o);
501                                 for (video::S3DVertex &vertex : vertices) {
502                                         // Switch from -Z (south) to -X (west)
503                                         vertex.Pos.rotateXZBy(-90);
504                                         vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
505                                 }
506                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
507                         } else {
508                                 driver->setMaterial(m_materials[4]);
509                                 float d = moonsize * 1.9;
510                                 video::SColor c;
511                                 if (m_moon_tonemap)
512                                         c = video::SColor (0, 0, 0, 0);
513                                 else
514                                         c = video::SColor (255, 255, 255, 255);
515                                 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
516                                 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
517                                 vertices[2] = video::S3DVertex( d,  d, -1, 0, 0, 1, c, o, o);
518                                 vertices[3] = video::S3DVertex(-d,  d, -1, 0, 0, 1, c, t, o);
519                                 for (video::S3DVertex &vertex : vertices) {
520                                         // Switch from -Z (south) to -X (west)
521                                         vertex.Pos.rotateXZBy(-90);
522                                         vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
523                                 }
524                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
525                         }
526                 }
527
528                 // Draw far cloudy fog thing below East and West horizons.
529                 // These act as horizons that the sun and moon rise and set over.
530                 driver->setMaterial(m_materials[1]);
531
532                 for (u32 j = 0; j < 2; j++) {
533                         video::SColor c = cloudyfogcolor;
534                         vertices[0] = video::S3DVertex(-1, -1.0,  -1, 0, 0, 1, c, t, t);
535                         vertices[1] = video::S3DVertex( 1, -1.0,  -1, 0, 0, 1, c, o, t);
536                         vertices[2] = video::S3DVertex( 1, -0.02, -1, 0, 0, 1, c, o, o);
537                         vertices[3] = video::S3DVertex(-1, -0.02, -1, 0, 0, 1, c, t, o);
538                         for (video::S3DVertex &vertex : vertices) {
539                                 //if (wicked_time_of_day < 0.5)
540                                 if (j == 0)
541                                         // Switch from -Z (south) to +X (east)
542                                         vertex.Pos.rotateXZBy(90);
543                                 else
544                                         // Switch from -Z (south) to -X (west)
545                                         vertex.Pos.rotateXZBy(-90);
546                         }
547                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
548                 }
549         }
550 }
551
552
553 void Sky::update(float time_of_day, float time_brightness,
554                 float direct_brightness, bool sunlight_seen,
555                 CameraMode cam_mode, float yaw, float pitch)
556 {
557         // Stabilize initial brightness and color values by flooding updates
558         if (m_first_update) {
559                 /*dstream<<"First update with time_of_day="<<time_of_day
560                                 <<" time_brightness="<<time_brightness
561                                 <<" direct_brightness="<<direct_brightness
562                                 <<" sunlight_seen="<<sunlight_seen<<std::endl;*/
563                 m_first_update = false;
564                 for (u32 i = 0; i < 100; i++) {
565                         update(time_of_day, time_brightness, direct_brightness,
566                                 sunlight_seen, cam_mode, yaw, pitch);
567                 }
568                 return;
569         }
570
571         m_time_of_day = time_of_day;
572         m_time_brightness = time_brightness;
573         m_sunlight_seen = sunlight_seen;
574         m_bodies_visible = true;
575
576         bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
577
578         /*
579         Development colours
580
581         video::SColorf bgcolor_bright_normal_f(170. / 255, 200. / 255, 230. / 255, 1.0);
582         video::SColorf bgcolor_bright_dawn_f(0.666, 200. / 255 * 0.7, 230. / 255 * 0.5, 1.0);
583         video::SColorf bgcolor_bright_dawn_f(0.666, 0.549, 0.220, 1.0);
584         video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.0, 1.0);
585         video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.2, 1.0);
586
587         video::SColorf cloudcolor_bright_dawn_f(1.0, 0.591, 0.4);
588         video::SColorf cloudcolor_bright_dawn_f(1.0, 0.65, 0.44);
589         video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
590         */
591
592         video::SColorf bgcolor_bright_normal_f = video::SColor(255, 155, 193, 240);
593         video::SColorf bgcolor_bright_indoor_f = video::SColor(255, 100, 100, 100);
594         video::SColorf bgcolor_bright_dawn_f = video::SColor(255, 186, 193, 240);
595         video::SColorf bgcolor_bright_night_f = video::SColor(255, 64, 144, 255);
596
597         video::SColorf skycolor_bright_normal_f = video::SColor(255, 140, 186, 250);
598         video::SColorf skycolor_bright_dawn_f = video::SColor(255, 180, 186, 250);
599         video::SColorf skycolor_bright_night_f = video::SColor(255, 0, 107, 255);
600
601         // pure white: becomes "diffuse light component" for clouds
602         video::SColorf cloudcolor_bright_normal_f = video::SColor(255, 255, 255, 255);
603         // dawn-factoring version of pure white (note: R is above 1.0)
604         video::SColorf cloudcolor_bright_dawn_f(255.0f/240.0f, 223.0f/240.0f, 191.0f/255.0f);
605
606         float cloud_color_change_fraction = 0.95;
607         if (sunlight_seen) {
608                 if (std::fabs(time_brightness - m_brightness) < 0.2f) {
609                         m_brightness = m_brightness * 0.95 + time_brightness * 0.05;
610                 } else {
611                         m_brightness = m_brightness * 0.80 + time_brightness * 0.20;
612                         cloud_color_change_fraction = 0.0;
613                 }
614         } else {
615                 if (direct_brightness < m_brightness)
616                         m_brightness = m_brightness * 0.95 + direct_brightness * 0.05;
617                 else
618                         m_brightness = m_brightness * 0.98 + direct_brightness * 0.02;
619         }
620
621         m_clouds_visible = true;
622         float color_change_fraction = 0.98f;
623         if (sunlight_seen) {
624                 if (is_dawn) { // Dawn
625                         m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
626                                 bgcolor_bright_dawn_f, color_change_fraction);
627                         m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
628                                 skycolor_bright_dawn_f, color_change_fraction);
629                         m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
630                                 cloudcolor_bright_dawn_f, color_change_fraction);
631                 } else {
632                         if (time_brightness < 0.13f) { // Night
633                                 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
634                                         bgcolor_bright_night_f, color_change_fraction);
635                                 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
636                                         skycolor_bright_night_f, color_change_fraction);
637                         } else { // Day
638                                 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
639                                         bgcolor_bright_normal_f, color_change_fraction);
640                                 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
641                                         skycolor_bright_normal_f, color_change_fraction);
642                         }
643
644                         m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
645                                 cloudcolor_bright_normal_f, color_change_fraction);
646                 }
647         } else {
648                 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
649                         bgcolor_bright_indoor_f, color_change_fraction);
650                 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
651                         bgcolor_bright_indoor_f, color_change_fraction);
652                 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
653                         cloudcolor_bright_normal_f, color_change_fraction);
654                 m_clouds_visible = false;
655         }
656
657         video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
658         m_bgcolor = video::SColor(
659                 255,
660                 bgcolor_bright.getRed() * m_brightness,
661                 bgcolor_bright.getGreen() * m_brightness,
662                 bgcolor_bright.getBlue() * m_brightness
663         );
664
665         video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
666         m_skycolor = video::SColor(
667                 255,
668                 skycolor_bright.getRed() * m_brightness,
669                 skycolor_bright.getGreen() * m_brightness,
670                 skycolor_bright.getBlue() * m_brightness
671         );
672
673         // Horizon coloring based on sun and moon direction during sunset and sunrise
674         video::SColor pointcolor = video::SColor(m_bgcolor.getAlpha(), 255, 255, 255);
675         if (m_directional_colored_fog) {
676                 if (m_horizon_blend() != 0) {
677                         // Calculate hemisphere value from yaw, (inverted in third person front view)
678                         s8 dir_factor = 1;
679                         if (cam_mode > CAMERA_MODE_THIRD)
680                                 dir_factor = -1;
681                         f32 pointcolor_blend = wrapDegrees_0_360(yaw * dir_factor + 90);
682                         if (pointcolor_blend > 180)
683                                 pointcolor_blend = 360 - pointcolor_blend;
684                         pointcolor_blend /= 180;
685                         // Bound view angle to determine where transition starts and ends
686                         pointcolor_blend = rangelim(1 - pointcolor_blend * 1.375, 0, 1 / 1.375) *
687                                 1.375;
688                         // Combine the colors when looking up or down, otherwise turning looks weird
689                         pointcolor_blend += (0.5 - pointcolor_blend) *
690                                 (1 - MYMIN((90 - std::fabs(pitch)) / 90 * 1.5, 1));
691                         // Invert direction to match where the sun and moon are rising
692                         if (m_time_of_day > 0.5)
693                                 pointcolor_blend = 1 - pointcolor_blend;
694                         // Horizon colors of sun and moon
695                         f32 pointcolor_light = rangelim(m_time_brightness * 3, 0.2, 1);
696
697                         video::SColorf pointcolor_sun_f(1, 1, 1, 1);
698                         if (m_sun_tonemap) {
699                                 pointcolor_sun_f.r = pointcolor_light *
700                                         (float)m_materials[3].EmissiveColor.getRed() / 255;
701                                 pointcolor_sun_f.b = pointcolor_light *
702                                         (float)m_materials[3].EmissiveColor.getBlue() / 255;
703                                 pointcolor_sun_f.g = pointcolor_light *
704                                         (float)m_materials[3].EmissiveColor.getGreen() / 255;
705                         } else {
706                                 pointcolor_sun_f.r = pointcolor_light * 1;
707                                 pointcolor_sun_f.b = pointcolor_light *
708                                         (0.25 + (rangelim(m_time_brightness, 0.25, 0.75) - 0.25) * 2 * 0.75);
709                                 pointcolor_sun_f.g = pointcolor_light * (pointcolor_sun_f.b * 0.375 +
710                                         (rangelim(m_time_brightness, 0.05, 0.15) - 0.05) * 10 * 0.625);
711                         }
712
713                         video::SColorf pointcolor_moon_f(0.5 * pointcolor_light,
714                                 0.6 * pointcolor_light, 0.8 * pointcolor_light, 1);
715                         if (m_moon_tonemap) {
716                                 pointcolor_moon_f.r = pointcolor_light *
717                                         (float)m_materials[4].EmissiveColor.getRed() / 255;
718                                 pointcolor_moon_f.b = pointcolor_light *
719                                         (float)m_materials[4].EmissiveColor.getBlue() / 255;
720                                 pointcolor_moon_f.g = pointcolor_light *
721                                         (float)m_materials[4].EmissiveColor.getGreen() / 255;
722                         }
723
724                         video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
725                         video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
726                         // Calculate the blend color
727                         pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
728                 }
729                 m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
730                 m_skycolor = m_mix_scolor(m_skycolor, pointcolor, m_horizon_blend() * 0.25);
731         }
732
733         float cloud_direct_brightness = 0.0f;
734         if (sunlight_seen) {
735                 if (!m_directional_colored_fog) {
736                         cloud_direct_brightness = time_brightness;
737                         // Boost cloud brightness relative to sky, at dawn, dusk and at night
738                         if (time_brightness < 0.7f)
739                                 cloud_direct_brightness *= 1.3f;
740                 } else {
741                         cloud_direct_brightness = std::fmin(m_horizon_blend() * 0.15f +
742                                 m_time_brightness, 1.0f);
743                         // Set the same minimum cloud brightness at night
744                         if (time_brightness < 0.5f)
745                                 cloud_direct_brightness = std::fmax(cloud_direct_brightness,
746                                         time_brightness * 1.3f);
747                 }
748         } else {
749                 cloud_direct_brightness = direct_brightness;
750         }
751
752         m_cloud_brightness = m_cloud_brightness * cloud_color_change_fraction +
753                 cloud_direct_brightness * (1.0 - cloud_color_change_fraction);
754         m_cloudcolor_f = video::SColorf(
755                 m_cloudcolor_bright_f.r * m_cloud_brightness,
756                 m_cloudcolor_bright_f.g * m_cloud_brightness,
757                 m_cloudcolor_bright_f.b * m_cloud_brightness,
758                 1.0
759         );
760         if (m_directional_colored_fog) {
761                 m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f,
762                         video::SColorf(pointcolor), m_horizon_blend() * 0.25);
763         }
764 }