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