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