3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "IVideoDriver.h"
22 #include "ISceneManager.h"
23 #include "ICameraSceneNode.h"
24 #include "S3DVertex.h"
25 #include "client/tile.h"
26 #include "noise.h" // easeCurve
28 #include "util/numeric.h"
30 #include "client/renderingengine.h"
32 #include "camera.h" // CameraModes
35 Sky::Sky(s32 id, ITextureSource *tsrc):
36 scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
37 RenderingEngine::get_scene_manager(), id)
39 setAutomaticCulling(scene::EAC_OFF);
40 m_box.MaxEdge.set(0, 0, 0);
41 m_box.MinEdge.set(0, 0, 0);
48 mat.ZBuffer = video::ECFN_DISABLED;
50 mat.ZBuffer = video::ECFN_NEVER;
52 mat.ZWriteEnable = false;
54 mat.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
55 mat.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
56 mat.BackfaceCulling = false;
61 //m_materials[1].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
62 m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
65 m_materials[2].setTexture(0, tsrc->getTextureForMesh("sunrisebg.png"));
66 m_materials[2].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
67 //m_materials[2].MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
69 m_sun_texture = tsrc->isKnownSourceImage("sun.png") ?
70 tsrc->getTextureForMesh("sun.png") : NULL;
71 m_moon_texture = tsrc->isKnownSourceImage("moon.png") ?
72 tsrc->getTextureForMesh("moon.png") : NULL;
73 m_sun_tonemap = tsrc->isKnownSourceImage("sun_tonemap.png") ?
74 tsrc->getTexture("sun_tonemap.png") : NULL;
75 m_moon_tonemap = tsrc->isKnownSourceImage("moon_tonemap.png") ?
76 tsrc->getTexture("moon_tonemap.png") : NULL;
80 m_materials[3].setTexture(0, m_sun_texture);
81 m_materials[3].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
83 m_materials[3].Lighting = true;
88 m_materials[4].setTexture(0, m_moon_texture);
89 m_materials[4].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
91 m_materials[4].Lighting = true;
94 for (v3f &star : m_stars) {
96 myrand_range(-10000, 10000),
97 myrand_range(-10000, 10000),
98 myrand_range(-10000, 10000)
103 m_directional_colored_fog = g_settings->getBool("directional_colored_fog");
107 void Sky::OnRegisterSceneNode()
110 SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX);
112 scene::ISceneNode::OnRegisterSceneNode();
121 video::IVideoDriver* driver = SceneManager->getVideoDriver();
122 scene::ICameraSceneNode* camera = SceneManager->getActiveCamera();
124 if (!camera || !driver)
127 ScopeProfiler sp(g_profiler, "Sky::render()", SPT_AVG);
129 // Draw perspective skybox
131 core::matrix4 translate(AbsoluteTransformation);
132 translate.setTranslation(camera->getAbsolutePosition());
134 // Draw the sky box between the near and far clip plane
135 const f32 viewDistance = (camera->getNearValue() + camera->getFarValue()) * 0.5f;
137 scale.setScale(core::vector3df(viewDistance, viewDistance, viewDistance));
139 driver->setTransform(video::ETS_WORLD, translate * scale);
141 if (m_sunlight_seen) {
142 float sunsize = 0.07;
143 video::SColorf suncolor_f(1, 1, 0, 1);
145 //suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.7 + m_time_brightness * 0.5));
146 //suncolor_f.b = MYMAX(0.0, m_brightness * 0.95);
147 video::SColorf suncolor2_f(1, 1, 1, 1);
148 // The values below were probably meant to be suncolor2_f instead of a
149 // reassignment of suncolor_f. However, the resulting colour was chosen
150 // and is our long-running classic colour. So preserve, but comment-out
151 // the unnecessary first assignments above.
153 suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.85 + m_time_brightness * 0.5));
154 suncolor_f.b = MYMAX(0.0, m_brightness);
156 float moonsize = 0.04;
157 video::SColorf mooncolor_f(0.50, 0.57, 0.65, 1);
158 video::SColorf mooncolor2_f(0.85, 0.875, 0.9, 1);
160 float nightlength = 0.415;
161 float wn = nightlength / 2;
162 float wicked_time_of_day = 0;
163 if (m_time_of_day > wn && m_time_of_day < 1.0 - wn)
164 wicked_time_of_day = (m_time_of_day - wn) / (1.0 - wn * 2) * 0.5 + 0.25;
165 else if (m_time_of_day < 0.5)
166 wicked_time_of_day = m_time_of_day / wn * 0.25;
168 wicked_time_of_day = 1.0 - ((1.0 - m_time_of_day) / wn * 0.25);
169 /*std::cerr<<"time_of_day="<<m_time_of_day<<" -> "
170 <<"wicked_time_of_day="<<wicked_time_of_day<<std::endl;*/
172 video::SColor suncolor = suncolor_f.toSColor();
173 video::SColor suncolor2 = suncolor2_f.toSColor();
174 video::SColor mooncolor = mooncolor_f.toSColor();
175 video::SColor mooncolor2 = mooncolor2_f.toSColor();
177 // Calculate offset normalized to the X dimension of a 512x1 px tonemap
178 float offset = (1.0 - fabs(sin((m_time_of_day - 0.5) * irr::core::PI))) * 511;
181 u8 * texels = (u8 *)m_sun_tonemap->lock();
182 video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
183 video::SColor texel_color (255, texel->getRed(),
184 texel->getGreen(), texel->getBlue());
185 m_sun_tonemap->unlock();
186 m_materials[3].EmissiveColor = texel_color;
189 if (m_moon_tonemap) {
190 u8 * texels = (u8 *)m_moon_tonemap->lock();
191 video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
192 video::SColor texel_color (255, texel->getRed(),
193 texel->getGreen(), texel->getBlue());
194 m_moon_tonemap->unlock();
195 m_materials[4].EmissiveColor = texel_color;
200 static const u16 indices[4] = {0, 1, 2, 3};
201 video::S3DVertex vertices[4];
203 driver->setMaterial(m_materials[1]);
205 video::SColor cloudyfogcolor = m_bgcolor;
207 // Draw far cloudy fog thing blended with skycolor
208 for (u32 j = 0; j < 4; j++) {
209 video::SColor c = cloudyfogcolor.getInterpolated(m_skycolor, 0.45);
210 vertices[0] = video::S3DVertex(-1, 0.08, -1, 0, 0, 1, c, t, t);
211 vertices[1] = video::S3DVertex( 1, 0.08, -1, 0, 0, 1, c, o, t);
212 vertices[2] = video::S3DVertex( 1, 0.12, -1, 0, 0, 1, c, o, o);
213 vertices[3] = video::S3DVertex(-1, 0.12, -1, 0, 0, 1, c, t, o);
214 for (video::S3DVertex &vertex : vertices) {
219 // Switch from -Z (south) to +X (east)
220 vertex.Pos.rotateXZBy(90);
222 // Switch from -Z (south) to -X (west)
223 vertex.Pos.rotateXZBy(-90);
225 // Switch from -Z (south) to +Z (north)
226 vertex.Pos.rotateXZBy(-180);
228 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
231 // Draw far cloudy fog thing at and below all horizons
232 for (u32 j = 0; j < 4; j++) {
233 video::SColor c = cloudyfogcolor;
234 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 0, 1, c, t, t);
235 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 0, 1, c, o, t);
236 vertices[2] = video::S3DVertex( 1, 0.08, -1, 0, 0, 1, c, o, o);
237 vertices[3] = video::S3DVertex(-1, 0.08, -1, 0, 0, 1, c, t, o);
238 for (video::S3DVertex &vertex : vertices) {
243 // Switch from -Z (south) to +X (east)
244 vertex.Pos.rotateXZBy(90);
246 // Switch from -Z (south) to -X (west)
247 vertex.Pos.rotateXZBy(-90);
249 // Switch from -Z (south) to +Z (north)
250 vertex.Pos.rotateXZBy(-180);
252 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
255 // If sun, moon and stars are (temporarily) disabled, abort here
256 if (!m_bodies_visible)
259 // Draw stars before sun and moon to be behind them
261 driver->setMaterial(m_materials[1]);
262 // Tune values so that stars first appear just after the sun
263 // disappears over the horizon, and disappear just before the sun
264 // appears over the horizon.
265 // Also tune so that stars are at full brightness from time 20000 to
267 float starbrightness = MYMAX(0, MYMIN(1,
268 (0.25 - fabs(wicked_time_of_day < 0.5 ?
269 wicked_time_of_day : (1.0 - wicked_time_of_day))) * 20));
270 float f = starbrightness;
272 video::SColor starcolor(255, f * 90, f * 90, f * 90);
273 // Stars are only drawn when brighter than skycolor
274 if (starcolor.getBlue() < m_skycolor.getBlue())
277 u16 indices[SKY_STAR_COUNT * 3];
278 video::S3DVertex vertices[SKY_STAR_COUNT * 3];
279 for (u32 i = 0; i < SKY_STAR_COUNT; i++) {
280 indices[i * 3 + 0] = i * 3 + 0;
281 indices[i * 3 + 1] = i * 3 + 1;
282 indices[i * 3 + 2] = i * 3 + 2;
284 core::CMatrix4<f32> a;
285 a.buildRotateFromTo(v3f(0, 1, 0), r);
286 v3f p = v3f(-d, 1, -d);
287 v3f p1 = v3f(d, 1, 0);
288 v3f p2 = v3f(-d, 1, d);
292 p.rotateXYBy(wicked_time_of_day * 360 - 90);
293 p1.rotateXYBy(wicked_time_of_day * 360 - 90);
294 p2.rotateXYBy(wicked_time_of_day * 360 - 90);
295 vertices[i * 3 + 0].Pos = p;
296 vertices[i * 3 + 0].Color = starcolor;
297 vertices[i * 3 + 1].Pos = p1;
298 vertices[i * 3 + 1].Color = starcolor;
299 vertices[i * 3 + 2].Pos = p2;
300 vertices[i * 3 + 2].Color = starcolor;
302 driver->drawIndexedTriangleList(vertices, SKY_STAR_COUNT * 3,
303 indices, SKY_STAR_COUNT);
305 u16 indices[SKY_STAR_COUNT * 4];
306 video::S3DVertex vertices[SKY_STAR_COUNT * 4];
307 for (u32 i = 0; i < SKY_STAR_COUNT; i++) {
308 indices[i * 4 + 0] = i * 4 + 0;
309 indices[i * 4 + 1] = i * 4 + 1;
310 indices[i * 4 + 2] = i * 4 + 2;
311 indices[i * 4 + 3] = i * 4 + 3;
313 core::CMatrix4<f32> a;
314 a.buildRotateFromTo(v3f(0, 1, 0), r);
315 v3f p = v3f(-d, 1, -d);
316 v3f p1 = v3f( d, 1, -d);
317 v3f p2 = v3f( d, 1, d);
318 v3f p3 = v3f(-d, 1, d);
323 p.rotateXYBy(wicked_time_of_day * 360 - 90);
324 p1.rotateXYBy(wicked_time_of_day * 360 - 90);
325 p2.rotateXYBy(wicked_time_of_day * 360 - 90);
326 p3.rotateXYBy(wicked_time_of_day * 360 - 90);
327 vertices[i * 4 + 0].Pos = p;
328 vertices[i * 4 + 0].Color = starcolor;
329 vertices[i * 4 + 1].Pos = p1;
330 vertices[i * 4 + 1].Color = starcolor;
331 vertices[i * 4 + 2].Pos = p2;
332 vertices[i * 4 + 2].Color = starcolor;
333 vertices[i * 4 + 3].Pos = p3;
334 vertices[i * 4 + 3].Color = starcolor;
336 driver->drawVertexPrimitiveList(vertices, SKY_STAR_COUNT * 4,
337 indices, SKY_STAR_COUNT, video::EVT_STANDARD,
338 scene::EPT_QUADS, video::EIT_16BIT);
342 // Draw sunrise/sunset horizon glow texture (textures/base/pack/sunrisebg.png)
344 driver->setMaterial(m_materials[2]);
346 float mid = wicked_time_of_day < 0.5 ? mid1 : (1.0 - mid1);
347 float a_ = 1.0f - std::fabs(wicked_time_of_day - mid) * 35.0f;
348 float a = easeCurve(MYMAX(0, MYMIN(1, a_)));
349 //std::cerr<<"a_="<<a_<<" a="<<a<<std::endl;
350 video::SColor c(255, 255, 255, 255);
351 float y = -(1.0 - a) * 0.22;
352 vertices[0] = video::S3DVertex(-1, -0.05 + y, -1, 0, 0, 1, c, t, t);
353 vertices[1] = video::S3DVertex( 1, -0.05 + y, -1, 0, 0, 1, c, o, t);
354 vertices[2] = video::S3DVertex( 1, 0.2 + y, -1, 0, 0, 1, c, o, o);
355 vertices[3] = video::S3DVertex(-1, 0.2 + y, -1, 0, 0, 1, c, t, o);
356 for (video::S3DVertex &vertex : vertices) {
357 if (wicked_time_of_day < 0.5)
358 // Switch from -Z (south) to +X (east)
359 vertex.Pos.rotateXZBy(90);
361 // Switch from -Z (south) to -X (west)
362 vertex.Pos.rotateXZBy(-90);
364 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
368 if (wicked_time_of_day > 0.15 && wicked_time_of_day < 0.85) {
369 if (!m_sun_texture) {
370 driver->setMaterial(m_materials[1]);
371 float d = sunsize * 1.7;
372 video::SColor c = suncolor;
373 c.setAlpha(0.05 * 255);
374 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
375 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
376 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
377 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
378 for (video::S3DVertex &vertex : vertices) {
379 // Switch from -Z (south) to +X (east)
380 vertex.Pos.rotateXZBy(90);
381 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
383 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
387 c.setAlpha(0.15 * 255);
388 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
389 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
390 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
391 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
392 for (video::S3DVertex &vertex : vertices) {
393 // Switch from -Z (south) to +X (east)
394 vertex.Pos.rotateXZBy(90);
395 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
397 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
400 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, suncolor, t, t);
401 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, suncolor, o, t);
402 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, suncolor, o, o);
403 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, suncolor, t, o);
404 for (video::S3DVertex &vertex : vertices) {
405 // Switch from -Z (south) to +X (east)
406 vertex.Pos.rotateXZBy(90);
407 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
409 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
412 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, suncolor2, t, t);
413 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, suncolor2, o, t);
414 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, suncolor2, o, o);
415 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, suncolor2, t, o);
416 for (video::S3DVertex &vertex : vertices) {
417 // Switch from -Z (south) to +X (east)
418 vertex.Pos.rotateXZBy(90);
419 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
421 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
423 driver->setMaterial(m_materials[3]);
424 float d = sunsize * 1.7;
427 c = video::SColor (0, 0, 0, 0);
429 c = video::SColor (255, 255, 255, 255);
430 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
431 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
432 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
433 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
434 for (video::S3DVertex &vertex : vertices) {
435 // Switch from -Z (south) to +X (east)
436 vertex.Pos.rotateXZBy(90);
437 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
439 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
444 if (wicked_time_of_day < 0.3 || wicked_time_of_day > 0.7) {
445 if (!m_moon_texture) {
446 driver->setMaterial(m_materials[1]);
447 float d = moonsize * 1.9;
448 video::SColor c = mooncolor;
449 c.setAlpha(0.05 * 255);
450 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
451 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
452 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
453 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
454 for (video::S3DVertex &vertex : vertices) {
455 // Switch from -Z (south) to -X (west)
456 vertex.Pos.rotateXZBy(-90);
457 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
459 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
463 c.setAlpha(0.15 * 255);
464 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
465 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
466 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
467 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
468 for (video::S3DVertex &vertex : vertices) {
469 // Switch from -Z (south) to -X (west)
470 vertex.Pos.rotateXZBy(-90);
471 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
473 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
476 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, mooncolor, t, t);
477 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, mooncolor, o, t);
478 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, mooncolor, o, o);
479 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, mooncolor, t, o);
480 for (video::S3DVertex &vertex : vertices) {
481 // Switch from -Z (south) to -X (west)
482 vertex.Pos.rotateXZBy(-90);
483 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
485 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
487 float d2 = moonsize * 0.6;
488 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, mooncolor2, t, t);
489 vertices[1] = video::S3DVertex( d2,-d, -1, 0, 0, 1, mooncolor2, o, t);
490 vertices[2] = video::S3DVertex( d2, d2, -1, 0, 0, 1, mooncolor2, o, o);
491 vertices[3] = video::S3DVertex(-d, d2, -1, 0, 0, 1, mooncolor2, t, o);
492 for (video::S3DVertex &vertex : vertices) {
493 // Switch from -Z (south) to -X (west)
494 vertex.Pos.rotateXZBy(-90);
495 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
497 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
499 driver->setMaterial(m_materials[4]);
500 float d = moonsize * 1.9;
503 c = video::SColor (0, 0, 0, 0);
505 c = video::SColor (255, 255, 255, 255);
506 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
507 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
508 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
509 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
510 for (video::S3DVertex &vertex : vertices) {
511 // Switch from -Z (south) to -X (west)
512 vertex.Pos.rotateXZBy(-90);
513 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
515 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
519 // Draw far cloudy fog thing below all horizons in front of sun, moon
521 driver->setMaterial(m_materials[1]);
523 for (u32 j = 0; j < 4; j++) {
524 video::SColor c = cloudyfogcolor;
525 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 0, 1, c, t, t);
526 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 0, 1, c, o, t);
527 vertices[2] = video::S3DVertex( 1, -0.02, -1, 0, 0, 1, c, o, o);
528 vertices[3] = video::S3DVertex(-1, -0.02, -1, 0, 0, 1, c, t, o);
529 for (video::S3DVertex &vertex : vertices) {
534 // Switch from -Z (south) to +X (east)
535 vertex.Pos.rotateXZBy(90);
537 // Switch from -Z (south) to -X (west)
538 vertex.Pos.rotateXZBy(-90);
540 // Switch from -Z (south) to +Z (north)
541 vertex.Pos.rotateXZBy(-180);
543 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
546 // Draw bottom far cloudy fog thing in front of sun, moon and stars
547 video::SColor c = cloudyfogcolor;
548 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 1, 0, c, t, t);
549 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 1, 0, c, o, t);
550 vertices[2] = video::S3DVertex( 1, -1.0, 1, 0, 1, 0, c, o, o);
551 vertices[3] = video::S3DVertex(-1, -1.0, 1, 0, 1, 0, c, t, o);
552 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
557 void Sky::update(float time_of_day, float time_brightness,
558 float direct_brightness, bool sunlight_seen,
559 CameraMode cam_mode, float yaw, float pitch)
561 // Stabilize initial brightness and color values by flooding updates
562 if (m_first_update) {
563 /*dstream<<"First update with time_of_day="<<time_of_day
564 <<" time_brightness="<<time_brightness
565 <<" direct_brightness="<<direct_brightness
566 <<" sunlight_seen="<<sunlight_seen<<std::endl;*/
567 m_first_update = false;
568 for (u32 i = 0; i < 100; i++) {
569 update(time_of_day, time_brightness, direct_brightness,
570 sunlight_seen, cam_mode, yaw, pitch);
575 m_time_of_day = time_of_day;
576 m_time_brightness = time_brightness;
577 m_sunlight_seen = sunlight_seen;
578 m_bodies_visible = true;
580 bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
585 video::SColorf bgcolor_bright_normal_f(170. / 255, 200. / 255, 230. / 255, 1.0);
586 video::SColorf bgcolor_bright_dawn_f(0.666, 200. / 255 * 0.7, 230. / 255 * 0.5, 1.0);
587 video::SColorf bgcolor_bright_dawn_f(0.666, 0.549, 0.220, 1.0);
588 video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.0, 1.0);
589 video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.2, 1.0);
591 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.591, 0.4);
592 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.65, 0.44);
593 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
596 video::SColorf bgcolor_bright_normal_f = video::SColor(255, 155, 193, 240);
597 video::SColorf bgcolor_bright_indoor_f = video::SColor(255, 100, 100, 100);
598 video::SColorf bgcolor_bright_dawn_f = video::SColor(255, 186, 193, 240);
599 video::SColorf bgcolor_bright_night_f = video::SColor(255, 64, 144, 255);
601 video::SColorf skycolor_bright_normal_f = video::SColor(255, 140, 186, 250);
602 video::SColorf skycolor_bright_dawn_f = video::SColor(255, 180, 186, 250);
603 video::SColorf skycolor_bright_night_f = video::SColor(255, 0, 107, 255);
605 // pure white: becomes "diffuse light component" for clouds
606 video::SColorf cloudcolor_bright_normal_f = video::SColor(255, 255, 255, 255);
607 // dawn-factoring version of pure white (note: R is above 1.0)
608 video::SColorf cloudcolor_bright_dawn_f(255.0f/240.0f, 223.0f/240.0f, 191.0f/255.0f);
610 float cloud_color_change_fraction = 0.95;
612 if (std::fabs(time_brightness - m_brightness) < 0.2f) {
613 m_brightness = m_brightness * 0.95 + time_brightness * 0.05;
615 m_brightness = m_brightness * 0.80 + time_brightness * 0.20;
616 cloud_color_change_fraction = 0.0;
619 if (direct_brightness < m_brightness)
620 m_brightness = m_brightness * 0.95 + direct_brightness * 0.05;
622 m_brightness = m_brightness * 0.98 + direct_brightness * 0.02;
625 m_clouds_visible = true;
626 float color_change_fraction = 0.98f;
628 if (is_dawn) { // Dawn
629 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
630 bgcolor_bright_dawn_f, color_change_fraction);
631 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
632 skycolor_bright_dawn_f, color_change_fraction);
633 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
634 cloudcolor_bright_dawn_f, color_change_fraction);
636 if (time_brightness < 0.13f) { // Night
637 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
638 bgcolor_bright_night_f, color_change_fraction);
639 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
640 skycolor_bright_night_f, color_change_fraction);
642 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
643 bgcolor_bright_normal_f, color_change_fraction);
644 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
645 skycolor_bright_normal_f, color_change_fraction);
648 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
649 cloudcolor_bright_normal_f, color_change_fraction);
652 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
653 bgcolor_bright_indoor_f, color_change_fraction);
654 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
655 bgcolor_bright_indoor_f, color_change_fraction);
656 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
657 cloudcolor_bright_normal_f, color_change_fraction);
658 m_clouds_visible = false;
661 video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
662 m_bgcolor = video::SColor(
664 bgcolor_bright.getRed() * m_brightness,
665 bgcolor_bright.getGreen() * m_brightness,
666 bgcolor_bright.getBlue() * m_brightness
669 video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
670 m_skycolor = video::SColor(
672 skycolor_bright.getRed() * m_brightness,
673 skycolor_bright.getGreen() * m_brightness,
674 skycolor_bright.getBlue() * m_brightness
677 // Horizon coloring based on sun and moon direction during sunset and sunrise
678 video::SColor pointcolor = video::SColor(m_bgcolor.getAlpha(), 255, 255, 255);
679 if (m_directional_colored_fog) {
680 if (m_horizon_blend() != 0) {
681 // Calculate hemisphere value from yaw, (inverted in third person front view)
683 if (cam_mode > CAMERA_MODE_THIRD)
685 f32 pointcolor_blend = wrapDegrees_0_360(yaw * dir_factor + 90);
686 if (pointcolor_blend > 180)
687 pointcolor_blend = 360 - pointcolor_blend;
688 pointcolor_blend /= 180;
689 // Bound view angle to determine where transition starts and ends
690 pointcolor_blend = rangelim(1 - pointcolor_blend * 1.375, 0, 1 / 1.375) *
692 // Combine the colors when looking up or down, otherwise turning looks weird
693 pointcolor_blend += (0.5 - pointcolor_blend) *
694 (1 - MYMIN((90 - std::fabs(pitch)) / 90 * 1.5, 1));
695 // Invert direction to match where the sun and moon are rising
696 if (m_time_of_day > 0.5)
697 pointcolor_blend = 1 - pointcolor_blend;
698 // Horizon colors of sun and moon
699 f32 pointcolor_light = rangelim(m_time_brightness * 3, 0.2, 1);
701 video::SColorf pointcolor_sun_f(1, 1, 1, 1);
703 pointcolor_sun_f.r = pointcolor_light *
704 (float)m_materials[3].EmissiveColor.getRed() / 255;
705 pointcolor_sun_f.b = pointcolor_light *
706 (float)m_materials[3].EmissiveColor.getBlue() / 255;
707 pointcolor_sun_f.g = pointcolor_light *
708 (float)m_materials[3].EmissiveColor.getGreen() / 255;
710 pointcolor_sun_f.r = pointcolor_light * 1;
711 pointcolor_sun_f.b = pointcolor_light *
712 (0.25 + (rangelim(m_time_brightness, 0.25, 0.75) - 0.25) * 2 * 0.75);
713 pointcolor_sun_f.g = pointcolor_light * (pointcolor_sun_f.b * 0.375 +
714 (rangelim(m_time_brightness, 0.05, 0.15) - 0.05) * 10 * 0.625);
717 video::SColorf pointcolor_moon_f(0.5 * pointcolor_light,
718 0.6 * pointcolor_light, 0.8 * pointcolor_light, 1);
719 if (m_moon_tonemap) {
720 pointcolor_moon_f.r = pointcolor_light *
721 (float)m_materials[4].EmissiveColor.getRed() / 255;
722 pointcolor_moon_f.b = pointcolor_light *
723 (float)m_materials[4].EmissiveColor.getBlue() / 255;
724 pointcolor_moon_f.g = pointcolor_light *
725 (float)m_materials[4].EmissiveColor.getGreen() / 255;
728 video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
729 video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
730 // Calculate the blend color
731 pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
733 m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
734 m_skycolor = m_mix_scolor(m_skycolor, pointcolor, m_horizon_blend() * 0.25);
737 float cloud_direct_brightness = 0.0f;
739 if (!m_directional_colored_fog) {
740 cloud_direct_brightness = time_brightness;
741 // Boost cloud brightness relative to sky, at dawn, dusk and at night
742 if (time_brightness < 0.7f)
743 cloud_direct_brightness *= 1.3f;
745 cloud_direct_brightness = std::fmin(m_horizon_blend() * 0.15f +
746 m_time_brightness, 1.0f);
747 // Set the same minimum cloud brightness at night
748 if (time_brightness < 0.5f)
749 cloud_direct_brightness = std::fmax(cloud_direct_brightness,
750 time_brightness * 1.3f);
753 cloud_direct_brightness = direct_brightness;
756 m_cloud_brightness = m_cloud_brightness * cloud_color_change_fraction +
757 cloud_direct_brightness * (1.0 - cloud_color_change_fraction);
758 m_cloudcolor_f = video::SColorf(
759 m_cloudcolor_bright_f.r * m_cloud_brightness,
760 m_cloudcolor_bright_f.g * m_cloud_brightness,
761 m_cloudcolor_bright_f.b * m_cloud_brightness,
764 if (m_directional_colored_fog) {
765 m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f,
766 video::SColorf(pointcolor), m_horizon_blend() * 0.25);