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