]> git.lizzy.rs Git - minetest.git/blob - src/sky.cpp
Reduce cloud coloring for colored fog and cloud mode
[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 "tile.h" // getTexturePath
7 #include "noise.h" // easeCurve
8 #include "main.h" // g_profiler
9 #include "profiler.h"
10 #include "util/numeric.h" // MYMIN
11 #include <cmath>
12 #include "settings.h"
13
14 //! constructor
15 Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id, LocalPlayer* player):
16                 scene::ISceneNode(parent, mgr, id),
17                 m_first_update(true),
18                 m_brightness(0.5),
19                 m_cloud_brightness(0.5),
20                 m_bgcolor_bright_f(1,1,1,1),
21                 m_skycolor_bright_f(1,1,1,1),
22                 m_cloudcolor_bright_f(1,1,1,1),
23                 m_player(player)
24 {
25         setAutomaticCulling(scene::EAC_OFF);
26         Box.MaxEdge.set(0,0,0);
27         Box.MinEdge.set(0,0,0);
28
29         // create material
30
31         video::SMaterial mat;
32         mat.Lighting = false;
33         mat.ZBuffer = video::ECFN_NEVER;
34         mat.ZWriteEnable = false;
35         mat.AntiAliasing=0;
36         mat.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
37         mat.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
38         mat.BackfaceCulling = false;
39
40         m_materials[0] = mat;
41
42         m_materials[1] = mat;
43         //m_materials[1].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
44         m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
45
46         m_materials[2] = mat;
47         m_materials[2].setTexture(0, mgr->getVideoDriver()->getTexture(
48                         getTexturePath("sunrisebg.png").c_str()));
49         m_materials[2].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
50         //m_materials[2].MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
51
52         for(u32 i=0; i<SKY_STAR_COUNT; i++){
53                 m_stars[i] = v3f(
54                         myrand_range(-10000,10000),
55                         myrand_range(-10000,10000),
56                         myrand_range(-10000,10000)
57                 );
58                 m_stars[i].normalize();
59         }
60
61         m_directional_colored_fog = g_settings->getBool("directional_colored_fog");
62 }
63
64 void Sky::OnRegisterSceneNode()
65 {
66         if (IsVisible)
67                 SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX);
68
69         scene::ISceneNode::OnRegisterSceneNode();
70 }
71
72 const core::aabbox3d<f32>& Sky::getBoundingBox() const
73 {
74         return Box;
75 }
76
77 //! renders the node.
78 void Sky::render()
79 {
80         video::IVideoDriver* driver = SceneManager->getVideoDriver();
81         scene::ICameraSceneNode* camera = SceneManager->getActiveCamera();
82
83         if (!camera || !driver)
84                 return;
85         
86         ScopeProfiler sp(g_profiler, "Sky::render()", SPT_AVG);
87
88         // draw perspective skybox
89
90         core::matrix4 translate(AbsoluteTransformation);
91         translate.setTranslation(camera->getAbsolutePosition());
92
93         // Draw the sky box between the near and far clip plane
94         const f32 viewDistance = (camera->getNearValue() + camera->getFarValue()) * 0.5f;
95         core::matrix4 scale;
96         scale.setScale(core::vector3df(viewDistance, viewDistance, viewDistance));
97
98         driver->setTransform(video::ETS_WORLD, translate * scale);
99
100         if(m_sunlight_seen)
101         {
102                 float sunsize = 0.07;
103                 video::SColorf suncolor_f(1, 1, 0, 1);
104                 suncolor_f.r = 1;
105                 suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.7+m_time_brightness*(0.5)));
106                 suncolor_f.b = MYMAX(0.0, m_brightness*0.95);
107                 video::SColorf suncolor2_f(1, 1, 1, 1);
108                 suncolor_f.r = 1;
109                 suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.85+m_time_brightness*(0.5)));
110                 suncolor_f.b = MYMAX(0.0, m_brightness);
111
112                 float moonsize = 0.04;
113                 video::SColorf mooncolor_f(0.50, 0.57, 0.65, 1);
114                 video::SColorf mooncolor2_f(0.85, 0.875, 0.9, 1);
115                 
116                 float nightlength = 0.415;
117                 float wn = nightlength / 2;
118                 float wicked_time_of_day = 0;
119                 if(m_time_of_day > wn && m_time_of_day < 1.0 - wn)
120                         wicked_time_of_day = (m_time_of_day - wn)/(1.0-wn*2)*0.5 + 0.25;
121                 else if(m_time_of_day < 0.5)
122                         wicked_time_of_day = m_time_of_day / wn * 0.25;
123                 else
124                         wicked_time_of_day = 1.0 - ((1.0-m_time_of_day) / wn * 0.25);
125                 /*std::cerr<<"time_of_day="<<m_time_of_day<<" -> "
126                                 <<"wicked_time_of_day="<<wicked_time_of_day<<std::endl;*/
127
128                 video::SColor suncolor = suncolor_f.toSColor();
129                 video::SColor suncolor2 = suncolor2_f.toSColor();
130                 video::SColor mooncolor = mooncolor_f.toSColor();
131                 video::SColor mooncolor2 = mooncolor2_f.toSColor();
132
133                 const f32 t = 1.0f;
134                 const f32 o = 0.0f;
135                 static const u16 indices[4] = {0,1,2,3};
136                 video::S3DVertex vertices[4];
137                 
138                 driver->setMaterial(m_materials[1]);
139                 
140                 //video::SColor cloudyfogcolor(255,255,255,255);
141                 video::SColor cloudyfogcolor = m_bgcolor;
142                 //video::SColor cloudyfogcolor = m_bgcolor.getInterpolated(m_skycolor, 0.5);
143                 
144                 // Draw far cloudy fog thing
145                 for(u32 j=0; j<4; j++)
146                 {
147                         video::SColor c = cloudyfogcolor.getInterpolated(m_skycolor, 0.45);
148                         vertices[0] = video::S3DVertex(-1, 0.08,-1, 0,0,1, c, t, t);
149                         vertices[1] = video::S3DVertex( 1, 0.08,-1, 0,0,1, c, o, t);
150                         vertices[2] = video::S3DVertex( 1, 0.12,-1, 0,0,1, c, o, o);
151                         vertices[3] = video::S3DVertex(-1, 0.12,-1, 0,0,1, c, t, o);
152                         for(u32 i=0; i<4; i++){
153                                 if(j==0)
154                                         // Don't switch
155                                         {}
156                                 else if(j==1)
157                                         // Switch from -Z (south) to +X (east)
158                                         vertices[i].Pos.rotateXZBy(90);
159                                 else if(j==2)
160                                         // Switch from -Z (south) to -X (west)
161                                         vertices[i].Pos.rotateXZBy(-90);
162                                 else
163                                         // Switch from -Z (south) to -Z (north)
164                                         vertices[i].Pos.rotateXZBy(-180);
165                         }
166                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
167                 }
168                 for(u32 j=0; j<4; j++)
169                 {
170                         video::SColor c = cloudyfogcolor;
171                         vertices[0] = video::S3DVertex(-1,-1.0,-1, 0,0,1, c, t, t);
172                         vertices[1] = video::S3DVertex( 1,-1.0,-1, 0,0,1, c, o, t);
173                         vertices[2] = video::S3DVertex( 1, 0.08,-1, 0,0,1, c, o, o);
174                         vertices[3] = video::S3DVertex(-1, 0.08,-1, 0,0,1, c, t, o);
175                         for(u32 i=0; i<4; i++){
176                                 if(j==0)
177                                         // Don't switch
178                                         {}
179                                 else if(j==1)
180                                         // Switch from -Z (south) to +X (east)
181                                         vertices[i].Pos.rotateXZBy(90);
182                                 else if(j==2)
183                                         // Switch from -Z (south) to -X (west)
184                                         vertices[i].Pos.rotateXZBy(-90);
185                                 else
186                                         // Switch from -Z (south) to -Z (north)
187                                         vertices[i].Pos.rotateXZBy(-180);
188                         }
189                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
190                 }
191
192                 driver->setMaterial(m_materials[2]);
193
194                 {
195                         float mid1 = 0.25;
196                         float mid = (wicked_time_of_day < 0.5 ? mid1 : (1.0 - mid1));
197                         float a_ = 1.0 - fabs(wicked_time_of_day - mid) * 35.0;
198                         float a = easeCurve(MYMAX(0, MYMIN(1, a_)));
199                         //std::cerr<<"a_="<<a_<<" a="<<a<<std::endl;
200                         video::SColor c(255,255,255,255);
201                         float y = -(1.0 - a) * 0.2;
202                         vertices[0] = video::S3DVertex(-1,-0.05+y,-1, 0,0,1, c, t, t);
203                         vertices[1] = video::S3DVertex( 1,-0.05+y,-1, 0,0,1, c, o, t);
204                         vertices[2] = video::S3DVertex( 1, 0.2+y,-1, 0,0,1, c, o, o);
205                         vertices[3] = video::S3DVertex(-1, 0.2+y,-1, 0,0,1, c, t, o);
206                         for(u32 i=0; i<4; i++){
207                                 if(wicked_time_of_day < 0.5)
208                                         // Switch from -Z (south) to +X (east)
209                                         vertices[i].Pos.rotateXZBy(90);
210                                 else
211                                         // Switch from -Z (south) to -X (west)
212                                         vertices[i].Pos.rotateXZBy(-90);
213                         }
214                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
215                 }
216
217                 driver->setMaterial(m_materials[1]);
218                 
219                 // Draw sun
220                 if(wicked_time_of_day > 0.15 && wicked_time_of_day < 0.85)
221                 {
222                         float d = sunsize * 1.7;
223                         video::SColor c = suncolor;
224                         c.setAlpha(0.05*255);
225                         vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
226                         vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
227                         vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
228                         vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
229                         for(u32 i=0; i<4; i++){
230                                 // Switch from -Z (south) to +X (east)
231                                 vertices[i].Pos.rotateXZBy(90);
232                                 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
233                         }
234                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
235
236                         d = sunsize * 1.2;
237                         c = suncolor;
238                         c.setAlpha(0.15*255);
239                         vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
240                         vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
241                         vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
242                         vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
243                         for(u32 i=0; i<4; i++){
244                                 // Switch from -Z (south) to +X (east)
245                                 vertices[i].Pos.rotateXZBy(90);
246                                 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
247                         }
248                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
249
250                         d = sunsize;
251                         vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, suncolor, t, t);
252                         vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, suncolor, o, t);
253                         vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, suncolor, o, o);
254                         vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, suncolor, t, o);
255                         for(u32 i=0; i<4; i++){
256                                 // Switch from -Z (south) to +X (east)
257                                 vertices[i].Pos.rotateXZBy(90);
258                                 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
259                         }
260                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
261
262                         d = sunsize * 0.7;
263                         vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, suncolor2, t, t);
264                         vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, suncolor2, o, t);
265                         vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, suncolor2, o, o);
266                         vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, suncolor2, t, o);
267                         for(u32 i=0; i<4; i++){
268                                 // Switch from -Z (south) to +X (east)
269                                 vertices[i].Pos.rotateXZBy(90);
270                                 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
271                         }
272                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
273                 }
274                 // Draw moon
275                 if(wicked_time_of_day < 0.3 || wicked_time_of_day > 0.7)
276                 {
277                         float d = moonsize * 1.9;
278                         video::SColor c = mooncolor;
279                         c.setAlpha(0.05*255);
280                         vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
281                         vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
282                         vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
283                         vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
284                         for(u32 i=0; i<4; i++){
285                                 // Switch from -Z (south) to -X (west)
286                                 vertices[i].Pos.rotateXZBy(-90);
287                                 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
288                         }
289                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
290                         
291                         d = moonsize * 1.3;
292                         c = mooncolor;
293                         c.setAlpha(0.15*255);
294                         vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
295                         vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
296                         vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
297                         vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
298                         for(u32 i=0; i<4; i++){
299                                 // Switch from -Z (south) to -X (west)
300                                 vertices[i].Pos.rotateXZBy(-90);
301                                 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
302                         }
303                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
304                         
305                         d = moonsize;
306                         vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, mooncolor, t, t);
307                         vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, mooncolor, o, t);
308                         vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, mooncolor, o, o);
309                         vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, mooncolor, t, o);
310                         for(u32 i=0; i<4; i++){
311                                 // Switch from -Z (south) to -X (west)
312                                 vertices[i].Pos.rotateXZBy(-90);
313                                 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
314                         }
315                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
316                         
317                         float d2 = moonsize * 0.6;
318                         vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, mooncolor2, t, t);
319                         vertices[1] = video::S3DVertex( d2,-d,-1, 0,0,1, mooncolor2, o, t);
320                         vertices[2] = video::S3DVertex( d2, d2,-1, 0,0,1, mooncolor2, o, o);
321                         vertices[3] = video::S3DVertex(-d, d2,-1, 0,0,1, mooncolor2, t, o);
322                         for(u32 i=0; i<4; i++){
323                                 // Switch from -Z (south) to -X (west)
324                                 vertices[i].Pos.rotateXZBy(-90);
325                                 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
326                         }
327                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
328                 }
329                 // Stars
330                 do{
331                         float starbrightness = MYMAX(0, MYMIN(1,
332                                         (0.285 - fabs(wicked_time_of_day < 0.5 ?
333                                         wicked_time_of_day : (1.0 - wicked_time_of_day))) * 10));
334                         float f = starbrightness;
335                         float d = 0.007;
336                         video::SColor starcolor(255, f*90,f*90,f*90);
337                         if(starcolor.getBlue() < m_skycolor.getBlue())
338                                 break;
339                         u16 indices[SKY_STAR_COUNT*4];
340                         video::S3DVertex vertices[SKY_STAR_COUNT*4];
341                         for(u32 i=0; i<SKY_STAR_COUNT; i++){
342                                 indices[i*4+0] = i*4+0;
343                                 indices[i*4+1] = i*4+1;
344                                 indices[i*4+2] = i*4+2;
345                                 indices[i*4+3] = i*4+3;
346                                 v3f p = m_stars[i];
347                                 core::CMatrix4<f32> a;
348                                 a.buildRotateFromTo(v3f(0,1,0), v3f(d,1+d/2,0));
349                                 v3f p1 = p;
350                                 a.rotateVect(p1);
351                                 a.buildRotateFromTo(v3f(0,1,0), v3f(d,1,d));
352                                 v3f p2 = p;
353                                 a.rotateVect(p2);
354                                 a.buildRotateFromTo(v3f(0,1,0), v3f(0,1-d/2,d));
355                                 v3f p3 = p;
356                                 a.rotateVect(p3);
357                                 p.rotateXYBy(wicked_time_of_day * 360 - 90);
358                                 p1.rotateXYBy(wicked_time_of_day * 360 - 90);
359                                 p2.rotateXYBy(wicked_time_of_day * 360 - 90);
360                                 p3.rotateXYBy(wicked_time_of_day * 360 - 90);
361                                 vertices[i*4+0].Pos = p;
362                                 vertices[i*4+0].Color = starcolor;
363                                 vertices[i*4+1].Pos = p1;
364                                 vertices[i*4+1].Color = starcolor;
365                                 vertices[i*4+2].Pos = p2;
366                                 vertices[i*4+2].Color = starcolor;
367                                 vertices[i*4+3].Pos = p3;
368                                 vertices[i*4+3].Color = starcolor;
369                         }
370                         driver->drawVertexPrimitiveList(vertices, SKY_STAR_COUNT*4,
371                                         indices, SKY_STAR_COUNT, video::EVT_STANDARD,
372                                         scene::EPT_QUADS, video::EIT_16BIT);
373                 }while(0);
374                 
375                 for(u32 j=0; j<2; j++)
376                 {
377                         //video::SColor c = m_skycolor;
378                         video::SColor c = cloudyfogcolor;
379                         vertices[0] = video::S3DVertex(-1,-1.0,-1, 0,0,1, c, t, t);
380                         vertices[1] = video::S3DVertex( 1,-1.0,-1, 0,0,1, c, o, t);
381                         vertices[2] = video::S3DVertex( 1,-0.02,-1, 0,0,1, c, o, o);
382                         vertices[3] = video::S3DVertex(-1,-0.02,-1, 0,0,1, c, t, o);
383                         for(u32 i=0; i<4; i++){
384                                 //if(wicked_time_of_day < 0.5)
385                                 if(j==0)
386                                         // Switch from -Z (south) to +X (east)
387                                         vertices[i].Pos.rotateXZBy(90);
388                                 else
389                                         // Switch from -Z (south) to -X (west)
390                                         vertices[i].Pos.rotateXZBy(-90);
391                         }
392                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
393                 }
394         }
395 }
396
397 void Sky::update(float time_of_day, float time_brightness,
398                 float direct_brightness, bool sunlight_seen)
399 {
400         // Stabilize initial brightness and color values by flooding updates
401         if(m_first_update){
402                 /*dstream<<"First update with time_of_day="<<time_of_day
403                                 <<" time_brightness="<<time_brightness
404                                 <<" direct_brightness="<<direct_brightness
405                                 <<" sunlight_seen="<<sunlight_seen<<std::endl;*/
406                 m_first_update = false;
407                 for(u32 i=0; i<100; i++){
408                         update(time_of_day, time_brightness, direct_brightness,
409                                         sunlight_seen);
410                 }
411                 return;
412         }
413
414         m_time_of_day = time_of_day;
415         m_time_brightness = time_brightness;
416         m_sunlight_seen = sunlight_seen;
417         
418         bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
419
420         //video::SColorf bgcolor_bright_normal_f(170./255,200./255,230./255, 1.0);
421         video::SColorf bgcolor_bright_normal_f(155./255,193./255,240./255, 1.0);
422         video::SColorf bgcolor_bright_indoor_f(100./255,100./255,100./255, 1.0);
423         //video::SColorf bgcolor_bright_dawn_f(0.666,200./255*0.7,230./255*0.5,1.0);
424         //video::SColorf bgcolor_bright_dawn_f(0.666,0.549,0.220,1.0);
425         //video::SColorf bgcolor_bright_dawn_f(0.666*1.2,0.549*1.0,0.220*1.0, 1.0);
426         //video::SColorf bgcolor_bright_dawn_f(0.666*1.2,0.549*1.0,0.220*1.2,1.0);
427         video::SColorf bgcolor_bright_dawn_f
428                         (155./255*1.2,193./255,240./255, 1.0);
429
430         video::SColorf skycolor_bright_normal_f =
431                         video::SColor(255, 140, 186, 250);
432         video::SColorf skycolor_bright_dawn_f =
433                         video::SColor(255, 180, 186, 250);
434         
435         video::SColorf cloudcolor_bright_normal_f =
436                         video::SColor(255, 240,240,255);
437         //video::SColorf cloudcolor_bright_dawn_f(1.0, 0.591, 0.4);
438         //video::SColorf cloudcolor_bright_dawn_f(1.0, 0.65, 0.44);
439         //video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
440         video::SColorf cloudcolor_bright_dawn_f(1.0, 0.875, 0.75);
441
442         float cloud_color_change_fraction = 0.95;
443         if(sunlight_seen){
444                 if(fabs(time_brightness - m_brightness) < 0.2){
445                         m_brightness = m_brightness * 0.95 + time_brightness * 0.05;
446                 } else {
447                         m_brightness = m_brightness * 0.80 + time_brightness * 0.20;
448                         cloud_color_change_fraction = 0.0;
449                 }
450         }
451         else{
452                 if(direct_brightness < m_brightness)
453                         m_brightness = m_brightness * 0.95 + direct_brightness * 0.05;
454                 else
455                         m_brightness = m_brightness * 0.98 + direct_brightness * 0.02;
456         }
457         
458         m_clouds_visible = true;
459         float color_change_fraction = 0.98;
460         if(sunlight_seen){
461                 if(is_dawn){
462                         m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
463                                         bgcolor_bright_dawn_f, color_change_fraction);
464                         m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
465                                         skycolor_bright_dawn_f, color_change_fraction);
466                         m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
467                                         cloudcolor_bright_dawn_f, color_change_fraction);
468                 } else {
469                         m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
470                                         bgcolor_bright_normal_f, color_change_fraction);
471                         m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
472                                         skycolor_bright_normal_f, color_change_fraction);
473                         m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
474                                         cloudcolor_bright_normal_f, color_change_fraction);
475                 }
476         } else {
477                 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
478                                 bgcolor_bright_indoor_f, color_change_fraction);
479                 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
480                                 bgcolor_bright_indoor_f, color_change_fraction);
481                 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
482                                 cloudcolor_bright_normal_f, color_change_fraction);
483                 m_clouds_visible = false;
484         }
485
486         // Horizon coloring based on sun and moon direction during sunset and sunrise
487         video::SColor pointcolor = video::SColor(255, 255, 255, m_bgcolor.getAlpha());
488         if (m_directional_colored_fog) {
489                 if (m_horizon_blend() != 0)
490                 {
491                         // calculate hemisphere value from yaw
492                         f32 pointcolor_blend = wrapDegrees_0_360(m_player->getYaw() + 90);
493                         if (pointcolor_blend > 180)
494                                 pointcolor_blend = 360 - pointcolor_blend;
495                         pointcolor_blend /= 180;
496                         // bound view angle to determine where transition starts and ends
497                         pointcolor_blend = rangelim(1 - pointcolor_blend * 1.375, 0, 1 / 1.375) * 1.375;
498                         // combine the colors when looking up or down, otherwise turning looks weird
499                         pointcolor_blend += (0.5 - pointcolor_blend) * (1 - MYMIN((90 - std::abs(m_player->getPitch())) / 90 * 1.5, 1));
500                         // invert direction to match where the sun and moon are rising
501                         if (m_time_of_day > 0.5)
502                                 pointcolor_blend = 1 - pointcolor_blend;
503
504                         // horizon colors of sun and moon
505                         f32 pointcolor_light = rangelim(m_time_brightness * 3, 0.2, 1);
506                         video::SColorf pointcolor_sun_f(1, 1, 1, 1);
507                         pointcolor_sun_f.r = pointcolor_light * 1;
508                         pointcolor_sun_f.b = pointcolor_light * (0.25 + (rangelim(m_time_brightness, 0.25, 0.75) - 0.25) * 2 * 0.75);
509                         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);
510                         video::SColorf pointcolor_moon_f(0.5 * pointcolor_light, 0.6 * pointcolor_light, 0.8 * pointcolor_light, 1);
511                         video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
512                         video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
513                         // calculate the blend color
514                         pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
515                 }
516         }
517
518         video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
519         m_bgcolor = video::SColor(
520                 255,
521                 bgcolor_bright.getRed() * m_brightness,
522                 bgcolor_bright.getGreen() * m_brightness,
523                 bgcolor_bright.getBlue() * m_brightness);
524         if (m_directional_colored_fog) {
525                 m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
526         }
527
528         video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
529         m_skycolor = video::SColor(
530                 255,
531                 skycolor_bright.getRed() * m_brightness,
532                 skycolor_bright.getGreen() * m_brightness,
533                 skycolor_bright.getBlue() * m_brightness);
534         if (m_directional_colored_fog) {
535                 m_skycolor = m_mix_scolor(m_skycolor, pointcolor, m_horizon_blend() * 0.25);
536         }
537
538         float cloud_direct_brightness = 0;
539         if(sunlight_seen) {
540                 if (!m_directional_colored_fog) {
541                         cloud_direct_brightness = time_brightness;
542                         if(time_brightness >= 0.2 && time_brightness < 0.7)
543                                 cloud_direct_brightness *= 1.3;
544                 }
545                 else {
546                         cloud_direct_brightness = MYMIN(m_horizon_blend() * 0.15 + m_time_brightness, 1);
547                 }
548         } else {
549                 cloud_direct_brightness = direct_brightness;
550         }
551         m_cloud_brightness = m_cloud_brightness * cloud_color_change_fraction +
552                         cloud_direct_brightness * (1.0 - cloud_color_change_fraction);
553         m_cloudcolor_f = video::SColorf(
554                         m_cloudcolor_bright_f.r * m_cloud_brightness,
555                         m_cloudcolor_bright_f.g * m_cloud_brightness,
556                         m_cloudcolor_bright_f.b * m_cloud_brightness,
557                         1.0);
558         if (m_directional_colored_fog) {
559                 m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f, video::SColorf(pointcolor), m_horizon_blend() * 0.25);
560         }
561
562 }
563
564