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