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