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
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 // Draw bottom far cloudy fog thing
256 video::SColor c = cloudyfogcolor;
257 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 1, 0, c, t, t);
258 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 1, 0, c, o, t);
259 vertices[2] = video::S3DVertex( 1, -1.0, 1, 0, 1, 0, c, o, o);
260 vertices[3] = video::S3DVertex(-1, -1.0, 1, 0, 1, 0, c, t, o);
261 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
263 // If sun, moon and stars are (temporarily) disabled, abort here
264 if (!m_bodies_visible)
267 driver->setMaterial(m_materials[2]);
269 // Draw sunrise/sunset horizon glow texture (textures/base/pack/sunrisebg.png)
272 float mid = wicked_time_of_day < 0.5 ? mid1 : (1.0 - mid1);
273 float a_ = 1.0f - std::fabs(wicked_time_of_day - mid) * 35.0f;
274 float a = easeCurve(MYMAX(0, MYMIN(1, a_)));
275 //std::cerr<<"a_="<<a_<<" a="<<a<<std::endl;
276 video::SColor c(255, 255, 255, 255);
277 float y = -(1.0 - a) * 0.22;
278 vertices[0] = video::S3DVertex(-1, -0.05 + y, -1, 0, 0, 1, c, t, t);
279 vertices[1] = video::S3DVertex( 1, -0.05 + y, -1, 0, 0, 1, c, o, t);
280 vertices[2] = video::S3DVertex( 1, 0.2 + y, -1, 0, 0, 1, c, o, o);
281 vertices[3] = video::S3DVertex(-1, 0.2 + y, -1, 0, 0, 1, c, t, o);
282 for (video::S3DVertex &vertex : vertices) {
283 if (wicked_time_of_day < 0.5)
284 // Switch from -Z (south) to +X (east)
285 vertex.Pos.rotateXZBy(90);
287 // Switch from -Z (south) to -X (west)
288 vertex.Pos.rotateXZBy(-90);
290 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
295 driver->setMaterial(m_materials[1]);
296 float starbrightness = MYMAX(0, MYMIN(1,
297 (0.285 - fabs(wicked_time_of_day < 0.5 ?
298 wicked_time_of_day : (1.0 - wicked_time_of_day))) * 10));
299 float f = starbrightness;
301 video::SColor starcolor(255, f * 90, f * 90, f * 90);
302 if (starcolor.getBlue() < m_skycolor.getBlue())
305 u16 indices[SKY_STAR_COUNT * 3];
306 video::S3DVertex vertices[SKY_STAR_COUNT * 3];
307 for (u32 i = 0; i < SKY_STAR_COUNT; i++) {
308 indices[i * 3 + 0] = i * 3 + 0;
309 indices[i * 3 + 1] = i * 3 + 1;
310 indices[i * 3 + 2] = i * 3 + 2;
312 core::CMatrix4<f32> a;
313 a.buildRotateFromTo(v3f(0, 1, 0), r);
314 v3f p = v3f(-d, 1, -d);
315 v3f p1 = v3f(d, 1, 0);
316 v3f p2 = v3f(-d, 1, d);
320 p.rotateXYBy(wicked_time_of_day * 360 - 90);
321 p1.rotateXYBy(wicked_time_of_day * 360 - 90);
322 p2.rotateXYBy(wicked_time_of_day * 360 - 90);
323 vertices[i * 3 + 0].Pos = p;
324 vertices[i * 3 + 0].Color = starcolor;
325 vertices[i * 3 + 1].Pos = p1;
326 vertices[i * 3 + 1].Color = starcolor;
327 vertices[i * 3 + 2].Pos = p2;
328 vertices[i * 3 + 2].Color = starcolor;
330 driver->drawIndexedTriangleList(vertices, SKY_STAR_COUNT * 3,
331 indices, SKY_STAR_COUNT);
333 u16 indices[SKY_STAR_COUNT * 4];
334 video::S3DVertex vertices[SKY_STAR_COUNT * 4];
335 for (u32 i = 0; i < SKY_STAR_COUNT; i++) {
336 indices[i * 4 + 0] = i * 4 + 0;
337 indices[i * 4 + 1] = i * 4 + 1;
338 indices[i * 4 + 2] = i * 4 + 2;
339 indices[i * 4 + 3] = i * 4 + 3;
341 core::CMatrix4<f32> a;
342 a.buildRotateFromTo(v3f(0, 1, 0), r);
343 v3f p = v3f(-d, 1, -d);
344 v3f p1 = v3f( d, 1, -d);
345 v3f p2 = v3f( d, 1, d);
346 v3f p3 = v3f(-d, 1, d);
351 p.rotateXYBy(wicked_time_of_day * 360 - 90);
352 p1.rotateXYBy(wicked_time_of_day * 360 - 90);
353 p2.rotateXYBy(wicked_time_of_day * 360 - 90);
354 p3.rotateXYBy(wicked_time_of_day * 360 - 90);
355 vertices[i * 4 + 0].Pos = p;
356 vertices[i * 4 + 0].Color = starcolor;
357 vertices[i * 4 + 1].Pos = p1;
358 vertices[i * 4 + 1].Color = starcolor;
359 vertices[i * 4 + 2].Pos = p2;
360 vertices[i * 4 + 2].Color = starcolor;
361 vertices[i * 4 + 3].Pos = p3;
362 vertices[i * 4 + 3].Color = starcolor;
364 driver->drawVertexPrimitiveList(vertices, SKY_STAR_COUNT * 4,
365 indices, SKY_STAR_COUNT, video::EVT_STANDARD,
366 scene::EPT_QUADS, video::EIT_16BIT);
371 if (wicked_time_of_day > 0.15 && wicked_time_of_day < 0.85) {
372 if (!m_sun_texture) {
373 driver->setMaterial(m_materials[1]);
374 float d = sunsize * 1.7;
375 video::SColor c = suncolor;
376 c.setAlpha(0.05 * 255);
377 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
378 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
379 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
380 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
381 for (video::S3DVertex &vertex : vertices) {
382 // Switch from -Z (south) to +X (east)
383 vertex.Pos.rotateXZBy(90);
384 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
386 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
390 c.setAlpha(0.15 * 255);
391 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
392 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
393 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
394 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
395 for (video::S3DVertex &vertex : vertices) {
396 // Switch from -Z (south) to +X (east)
397 vertex.Pos.rotateXZBy(90);
398 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
400 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
403 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, suncolor, t, t);
404 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, suncolor, o, t);
405 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, suncolor, o, o);
406 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, suncolor, t, o);
407 for (video::S3DVertex &vertex : vertices) {
408 // Switch from -Z (south) to +X (east)
409 vertex.Pos.rotateXZBy(90);
410 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
412 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
415 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, suncolor2, t, t);
416 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, suncolor2, o, t);
417 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, suncolor2, o, o);
418 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, suncolor2, t, o);
419 for (video::S3DVertex &vertex : vertices) {
420 // Switch from -Z (south) to +X (east)
421 vertex.Pos.rotateXZBy(90);
422 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
424 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
426 driver->setMaterial(m_materials[3]);
427 float d = sunsize * 1.7;
430 c = video::SColor (0, 0, 0, 0);
432 c = video::SColor (255, 255, 255, 255);
433 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
434 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
435 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
436 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
437 for (video::S3DVertex &vertex : vertices) {
438 // Switch from -Z (south) to +X (east)
439 vertex.Pos.rotateXZBy(90);
440 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
442 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
447 if (wicked_time_of_day < 0.3 || wicked_time_of_day > 0.7) {
448 if (!m_moon_texture) {
449 driver->setMaterial(m_materials[1]);
450 float d = moonsize * 1.9;
451 video::SColor c = mooncolor;
452 c.setAlpha(0.05 * 255);
453 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
454 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
455 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
456 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
457 for (video::S3DVertex &vertex : vertices) {
458 // Switch from -Z (south) to -X (west)
459 vertex.Pos.rotateXZBy(-90);
460 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
462 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
466 c.setAlpha(0.15 * 255);
467 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
468 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
469 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
470 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
471 for (video::S3DVertex &vertex : vertices) {
472 // Switch from -Z (south) to -X (west)
473 vertex.Pos.rotateXZBy(-90);
474 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
476 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
479 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, mooncolor, t, t);
480 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, mooncolor, o, t);
481 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, mooncolor, o, o);
482 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, mooncolor, t, o);
483 for (video::S3DVertex &vertex : vertices) {
484 // Switch from -Z (south) to -X (west)
485 vertex.Pos.rotateXZBy(-90);
486 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
488 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
490 float d2 = moonsize * 0.6;
491 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, mooncolor2, t, t);
492 vertices[1] = video::S3DVertex( d2,-d, -1, 0, 0, 1, mooncolor2, o, t);
493 vertices[2] = video::S3DVertex( d2, d2, -1, 0, 0, 1, mooncolor2, o, o);
494 vertices[3] = video::S3DVertex(-d, d2, -1, 0, 0, 1, mooncolor2, t, o);
495 for (video::S3DVertex &vertex : vertices) {
496 // Switch from -Z (south) to -X (west)
497 vertex.Pos.rotateXZBy(-90);
498 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
500 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
502 driver->setMaterial(m_materials[4]);
503 float d = moonsize * 1.9;
506 c = video::SColor (0, 0, 0, 0);
508 c = video::SColor (255, 255, 255, 255);
509 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
510 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
511 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
512 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
513 for (video::S3DVertex &vertex : vertices) {
514 // Switch from -Z (south) to -X (west)
515 vertex.Pos.rotateXZBy(-90);
516 vertex.Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
518 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
522 // Draw far cloudy fog thing below east and west horizons
523 for (u32 j = 0; j < 2; 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) {
530 //if (wicked_time_of_day < 0.5)
532 // Switch from -Z (south) to +X (east)
533 vertex.Pos.rotateXZBy(90);
535 // Switch from -Z (south) to -X (west)
536 vertex.Pos.rotateXZBy(-90);
538 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
544 void Sky::update(float time_of_day, float time_brightness,
545 float direct_brightness, bool sunlight_seen,
546 CameraMode cam_mode, float yaw, float pitch)
548 // Stabilize initial brightness and color values by flooding updates
549 if (m_first_update) {
550 /*dstream<<"First update with time_of_day="<<time_of_day
551 <<" time_brightness="<<time_brightness
552 <<" direct_brightness="<<direct_brightness
553 <<" sunlight_seen="<<sunlight_seen<<std::endl;*/
554 m_first_update = false;
555 for (u32 i = 0; i < 100; i++) {
556 update(time_of_day, time_brightness, direct_brightness,
557 sunlight_seen, cam_mode, yaw, pitch);
562 m_time_of_day = time_of_day;
563 m_time_brightness = time_brightness;
564 m_sunlight_seen = sunlight_seen;
565 m_bodies_visible = true;
567 bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
572 video::SColorf bgcolor_bright_normal_f(170. / 255, 200. / 255, 230. / 255, 1.0);
573 video::SColorf bgcolor_bright_dawn_f(0.666, 200. / 255 * 0.7, 230. / 255 * 0.5, 1.0);
574 video::SColorf bgcolor_bright_dawn_f(0.666, 0.549, 0.220, 1.0);
575 video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.0, 1.0);
576 video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.2, 1.0);
578 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.591, 0.4);
579 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.65, 0.44);
580 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
583 video::SColorf bgcolor_bright_normal_f = video::SColor(255, 155, 193, 240);
584 video::SColorf bgcolor_bright_indoor_f = video::SColor(255, 100, 100, 100);
585 video::SColorf bgcolor_bright_dawn_f = video::SColor(255, 186, 193, 240);
586 video::SColorf bgcolor_bright_night_f = video::SColor(255, 64, 144, 255);
588 video::SColorf skycolor_bright_normal_f = video::SColor(255, 140, 186, 250);
589 video::SColorf skycolor_bright_dawn_f = video::SColor(255, 180, 186, 250);
590 video::SColorf skycolor_bright_night_f = video::SColor(255, 0, 107, 255);
592 // pure white: becomes "diffuse light component" for clouds
593 video::SColorf cloudcolor_bright_normal_f = video::SColor(255, 255, 255, 255);
594 // dawn-factoring version of pure white (note: R is above 1.0)
595 video::SColorf cloudcolor_bright_dawn_f(255.0f/240.0f, 223.0f/240.0f, 191.0f/255.0f);
597 float cloud_color_change_fraction = 0.95;
599 if (std::fabs(time_brightness - m_brightness) < 0.2f) {
600 m_brightness = m_brightness * 0.95 + time_brightness * 0.05;
602 m_brightness = m_brightness * 0.80 + time_brightness * 0.20;
603 cloud_color_change_fraction = 0.0;
606 if (direct_brightness < m_brightness)
607 m_brightness = m_brightness * 0.95 + direct_brightness * 0.05;
609 m_brightness = m_brightness * 0.98 + direct_brightness * 0.02;
612 m_clouds_visible = true;
613 float color_change_fraction = 0.98;
615 if (is_dawn) { // Dawn
616 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
617 bgcolor_bright_dawn_f, color_change_fraction);
618 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
619 skycolor_bright_dawn_f, color_change_fraction);
620 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
621 cloudcolor_bright_dawn_f, color_change_fraction);
623 if (time_brightness < 0.07) { // Night
624 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
625 bgcolor_bright_night_f, color_change_fraction);
626 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
627 skycolor_bright_night_f, color_change_fraction);
629 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
630 bgcolor_bright_normal_f, color_change_fraction);
631 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
632 skycolor_bright_normal_f, color_change_fraction);
635 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
636 cloudcolor_bright_normal_f, color_change_fraction);
639 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
640 bgcolor_bright_indoor_f, color_change_fraction);
641 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
642 bgcolor_bright_indoor_f, color_change_fraction);
643 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
644 cloudcolor_bright_normal_f, color_change_fraction);
645 m_clouds_visible = false;
648 video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
649 m_bgcolor = video::SColor(
651 bgcolor_bright.getRed() * m_brightness,
652 bgcolor_bright.getGreen() * m_brightness,
653 bgcolor_bright.getBlue() * m_brightness
656 video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
657 m_skycolor = video::SColor(
659 skycolor_bright.getRed() * m_brightness,
660 skycolor_bright.getGreen() * m_brightness,
661 skycolor_bright.getBlue() * m_brightness
664 // Horizon coloring based on sun and moon direction during sunset and sunrise
665 video::SColor pointcolor = video::SColor(m_bgcolor.getAlpha(), 255, 255, 255);
666 if (m_directional_colored_fog) {
667 if (m_horizon_blend() != 0) {
668 // Calculate hemisphere value from yaw, (inverted in third person front view)
670 if (cam_mode > CAMERA_MODE_THIRD)
672 f32 pointcolor_blend = wrapDegrees_0_360(yaw * dir_factor + 90);
673 if (pointcolor_blend > 180)
674 pointcolor_blend = 360 - pointcolor_blend;
675 pointcolor_blend /= 180;
676 // Bound view angle to determine where transition starts and ends
677 pointcolor_blend = rangelim(1 - pointcolor_blend * 1.375, 0, 1 / 1.375) *
679 // Combine the colors when looking up or down, otherwise turning looks weird
680 pointcolor_blend += (0.5 - pointcolor_blend) *
681 (1 - MYMIN((90 - std::fabs(pitch)) / 90 * 1.5, 1));
682 // Invert direction to match where the sun and moon are rising
683 if (m_time_of_day > 0.5)
684 pointcolor_blend = 1 - pointcolor_blend;
685 // Horizon colors of sun and moon
686 f32 pointcolor_light = rangelim(m_time_brightness * 3, 0.2, 1);
688 video::SColorf pointcolor_sun_f(1, 1, 1, 1);
690 pointcolor_sun_f.r = pointcolor_light *
691 (float)m_materials[3].EmissiveColor.getRed() / 255;
692 pointcolor_sun_f.b = pointcolor_light *
693 (float)m_materials[3].EmissiveColor.getBlue() / 255;
694 pointcolor_sun_f.g = pointcolor_light *
695 (float)m_materials[3].EmissiveColor.getGreen() / 255;
697 pointcolor_sun_f.r = pointcolor_light * 1;
698 pointcolor_sun_f.b = pointcolor_light *
699 (0.25 + (rangelim(m_time_brightness, 0.25, 0.75) - 0.25) * 2 * 0.75);
700 pointcolor_sun_f.g = pointcolor_light * (pointcolor_sun_f.b * 0.375 +
701 (rangelim(m_time_brightness, 0.05, 0.15) - 0.05) * 10 * 0.625);
704 video::SColorf pointcolor_moon_f(0.5 * pointcolor_light,
705 0.6 * pointcolor_light, 0.8 * pointcolor_light, 1);
706 if (m_moon_tonemap) {
707 pointcolor_moon_f.r = pointcolor_light *
708 (float)m_materials[4].EmissiveColor.getRed() / 255;
709 pointcolor_moon_f.b = pointcolor_light *
710 (float)m_materials[4].EmissiveColor.getBlue() / 255;
711 pointcolor_moon_f.g = pointcolor_light *
712 (float)m_materials[4].EmissiveColor.getGreen() / 255;
715 video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
716 video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
717 // Calculate the blend color
718 pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
720 m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
721 m_skycolor = m_mix_scolor(m_skycolor, pointcolor, m_horizon_blend() * 0.25);
724 float cloud_direct_brightness = 0.0f;
726 if (!m_directional_colored_fog) {
727 cloud_direct_brightness = time_brightness;
728 // Boost cloud brightness relative to sky, at dawn, dusk and at night
729 if (time_brightness < 0.7f)
730 cloud_direct_brightness *= 1.3f;
732 cloud_direct_brightness = std::fmin(m_horizon_blend() * 0.15f +
733 m_time_brightness, 1.0f);
734 // Set the same minimum cloud brightness at night
735 if (time_brightness < 0.5f)
736 cloud_direct_brightness = std::fmax(cloud_direct_brightness,
737 time_brightness * 1.3f);
740 cloud_direct_brightness = direct_brightness;
743 m_cloud_brightness = m_cloud_brightness * cloud_color_change_fraction +
744 cloud_direct_brightness * (1.0 - cloud_color_change_fraction);
745 m_cloudcolor_f = video::SColorf(
746 m_cloudcolor_bright_f.r * m_cloud_brightness,
747 m_cloudcolor_bright_f.g * m_cloud_brightness,
748 m_cloudcolor_bright_f.b * m_cloud_brightness,
751 if (m_directional_colored_fog) {
752 m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f,
753 video::SColorf(pointcolor), m_horizon_blend() * 0.25);