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