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