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