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