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