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 draw_sun(driver, sunsize, suncolor, suncolor2, wicked_time_of_day);
373 if (wicked_time_of_day < 0.3 || wicked_time_of_day > 0.7) {
374 draw_moon(driver, moonsize, mooncolor, mooncolor2, wicked_time_of_day);
377 // Draw far cloudy fog thing below all horizons in front of sun, moon
379 driver->setMaterial(m_materials[1]);
381 for (u32 j = 0; j < 4; j++) {
382 video::SColor c = cloudyfogcolor;
383 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 0, 1, c, t, t);
384 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 0, 1, c, o, t);
385 vertices[2] = video::S3DVertex( 1, -0.02, -1, 0, 0, 1, c, o, o);
386 vertices[3] = video::S3DVertex(-1, -0.02, -1, 0, 0, 1, c, t, o);
387 for (video::S3DVertex &vertex : vertices) {
392 // Switch from -Z (south) to +X (east)
393 vertex.Pos.rotateXZBy(90);
395 // Switch from -Z (south) to -X (west)
396 vertex.Pos.rotateXZBy(-90);
398 // Switch from -Z (south) to +Z (north)
399 vertex.Pos.rotateXZBy(-180);
401 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
404 // Draw bottom far cloudy fog thing in front of sun, moon and stars
405 video::SColor c = cloudyfogcolor;
406 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 1, 0, c, t, t);
407 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 1, 0, c, o, t);
408 vertices[2] = video::S3DVertex( 1, -1.0, 1, 0, 1, 0, c, o, o);
409 vertices[3] = video::S3DVertex(-1, -1.0, 1, 0, 1, 0, c, t, o);
410 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
415 void Sky::update(float time_of_day, float time_brightness,
416 float direct_brightness, bool sunlight_seen,
417 CameraMode cam_mode, float yaw, float pitch)
419 // Stabilize initial brightness and color values by flooding updates
420 if (m_first_update) {
421 /*dstream<<"First update with time_of_day="<<time_of_day
422 <<" time_brightness="<<time_brightness
423 <<" direct_brightness="<<direct_brightness
424 <<" sunlight_seen="<<sunlight_seen<<std::endl;*/
425 m_first_update = false;
426 for (u32 i = 0; i < 100; i++) {
427 update(time_of_day, time_brightness, direct_brightness,
428 sunlight_seen, cam_mode, yaw, pitch);
433 m_time_of_day = time_of_day;
434 m_time_brightness = time_brightness;
435 m_sunlight_seen = sunlight_seen;
436 m_bodies_visible = true;
438 bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
443 video::SColorf bgcolor_bright_normal_f(170. / 255, 200. / 255, 230. / 255, 1.0);
444 video::SColorf bgcolor_bright_dawn_f(0.666, 200. / 255 * 0.7, 230. / 255 * 0.5, 1.0);
445 video::SColorf bgcolor_bright_dawn_f(0.666, 0.549, 0.220, 1.0);
446 video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.0, 1.0);
447 video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.2, 1.0);
449 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.591, 0.4);
450 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.65, 0.44);
451 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
454 video::SColorf bgcolor_bright_normal_f = video::SColor(255, 155, 193, 240);
455 video::SColorf bgcolor_bright_indoor_f = video::SColor(255, 100, 100, 100);
456 video::SColorf bgcolor_bright_dawn_f = video::SColor(255, 186, 193, 240);
457 video::SColorf bgcolor_bright_night_f = video::SColor(255, 64, 144, 255);
459 video::SColorf skycolor_bright_normal_f = video::SColor(255, 140, 186, 250);
460 video::SColorf skycolor_bright_dawn_f = video::SColor(255, 180, 186, 250);
461 video::SColorf skycolor_bright_night_f = video::SColor(255, 0, 107, 255);
463 // pure white: becomes "diffuse light component" for clouds
464 video::SColorf cloudcolor_bright_normal_f = video::SColor(255, 255, 255, 255);
465 // dawn-factoring version of pure white (note: R is above 1.0)
466 video::SColorf cloudcolor_bright_dawn_f(255.0f/240.0f, 223.0f/240.0f, 191.0f/255.0f);
468 float cloud_color_change_fraction = 0.95;
470 if (std::fabs(time_brightness - m_brightness) < 0.2f) {
471 m_brightness = m_brightness * 0.95 + time_brightness * 0.05;
473 m_brightness = m_brightness * 0.80 + time_brightness * 0.20;
474 cloud_color_change_fraction = 0.0;
477 if (direct_brightness < m_brightness)
478 m_brightness = m_brightness * 0.95 + direct_brightness * 0.05;
480 m_brightness = m_brightness * 0.98 + direct_brightness * 0.02;
483 m_clouds_visible = true;
484 float color_change_fraction = 0.98f;
486 if (is_dawn) { // Dawn
487 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
488 bgcolor_bright_dawn_f, color_change_fraction);
489 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
490 skycolor_bright_dawn_f, color_change_fraction);
491 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
492 cloudcolor_bright_dawn_f, color_change_fraction);
494 if (time_brightness < 0.13f) { // Night
495 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
496 bgcolor_bright_night_f, color_change_fraction);
497 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
498 skycolor_bright_night_f, color_change_fraction);
500 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
501 bgcolor_bright_normal_f, color_change_fraction);
502 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
503 skycolor_bright_normal_f, color_change_fraction);
506 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
507 cloudcolor_bright_normal_f, color_change_fraction);
510 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
511 bgcolor_bright_indoor_f, color_change_fraction);
512 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
513 bgcolor_bright_indoor_f, color_change_fraction);
514 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
515 cloudcolor_bright_normal_f, color_change_fraction);
516 m_clouds_visible = false;
519 video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
520 m_bgcolor = video::SColor(
522 bgcolor_bright.getRed() * m_brightness,
523 bgcolor_bright.getGreen() * m_brightness,
524 bgcolor_bright.getBlue() * m_brightness
527 video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
528 m_skycolor = video::SColor(
530 skycolor_bright.getRed() * m_brightness,
531 skycolor_bright.getGreen() * m_brightness,
532 skycolor_bright.getBlue() * m_brightness
535 // Horizon coloring based on sun and moon direction during sunset and sunrise
536 video::SColor pointcolor = video::SColor(m_bgcolor.getAlpha(), 255, 255, 255);
537 if (m_directional_colored_fog) {
538 if (m_horizon_blend() != 0) {
539 // Calculate hemisphere value from yaw, (inverted in third person front view)
541 if (cam_mode > CAMERA_MODE_THIRD)
543 f32 pointcolor_blend = wrapDegrees_0_360(yaw * dir_factor + 90);
544 if (pointcolor_blend > 180)
545 pointcolor_blend = 360 - pointcolor_blend;
546 pointcolor_blend /= 180;
547 // Bound view angle to determine where transition starts and ends
548 pointcolor_blend = rangelim(1 - pointcolor_blend * 1.375, 0, 1 / 1.375) *
550 // Combine the colors when looking up or down, otherwise turning looks weird
551 pointcolor_blend += (0.5 - pointcolor_blend) *
552 (1 - MYMIN((90 - std::fabs(pitch)) / 90 * 1.5, 1));
553 // Invert direction to match where the sun and moon are rising
554 if (m_time_of_day > 0.5)
555 pointcolor_blend = 1 - pointcolor_blend;
556 // Horizon colors of sun and moon
557 f32 pointcolor_light = rangelim(m_time_brightness * 3, 0.2, 1);
559 video::SColorf pointcolor_sun_f(1, 1, 1, 1);
561 pointcolor_sun_f.r = pointcolor_light *
562 (float)m_materials[3].EmissiveColor.getRed() / 255;
563 pointcolor_sun_f.b = pointcolor_light *
564 (float)m_materials[3].EmissiveColor.getBlue() / 255;
565 pointcolor_sun_f.g = pointcolor_light *
566 (float)m_materials[3].EmissiveColor.getGreen() / 255;
568 pointcolor_sun_f.r = pointcolor_light * 1;
569 pointcolor_sun_f.b = pointcolor_light *
570 (0.25 + (rangelim(m_time_brightness, 0.25, 0.75) - 0.25) * 2 * 0.75);
571 pointcolor_sun_f.g = pointcolor_light * (pointcolor_sun_f.b * 0.375 +
572 (rangelim(m_time_brightness, 0.05, 0.15) - 0.05) * 10 * 0.625);
575 video::SColorf pointcolor_moon_f(0.5 * pointcolor_light,
576 0.6 * pointcolor_light, 0.8 * pointcolor_light, 1);
577 if (m_moon_tonemap) {
578 pointcolor_moon_f.r = pointcolor_light *
579 (float)m_materials[4].EmissiveColor.getRed() / 255;
580 pointcolor_moon_f.b = pointcolor_light *
581 (float)m_materials[4].EmissiveColor.getBlue() / 255;
582 pointcolor_moon_f.g = pointcolor_light *
583 (float)m_materials[4].EmissiveColor.getGreen() / 255;
586 video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
587 video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
588 // Calculate the blend color
589 pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
591 m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
592 m_skycolor = m_mix_scolor(m_skycolor, pointcolor, m_horizon_blend() * 0.25);
595 float cloud_direct_brightness = 0.0f;
597 if (!m_directional_colored_fog) {
598 cloud_direct_brightness = time_brightness;
599 // Boost cloud brightness relative to sky, at dawn, dusk and at night
600 if (time_brightness < 0.7f)
601 cloud_direct_brightness *= 1.3f;
603 cloud_direct_brightness = std::fmin(m_horizon_blend() * 0.15f +
604 m_time_brightness, 1.0f);
605 // Set the same minimum cloud brightness at night
606 if (time_brightness < 0.5f)
607 cloud_direct_brightness = std::fmax(cloud_direct_brightness,
608 time_brightness * 1.3f);
611 cloud_direct_brightness = direct_brightness;
614 m_cloud_brightness = m_cloud_brightness * cloud_color_change_fraction +
615 cloud_direct_brightness * (1.0 - cloud_color_change_fraction);
616 m_cloudcolor_f = video::SColorf(
617 m_cloudcolor_bright_f.r * m_cloud_brightness,
618 m_cloudcolor_bright_f.g * m_cloud_brightness,
619 m_cloudcolor_bright_f.b * m_cloud_brightness,
622 if (m_directional_colored_fog) {
623 m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f,
624 video::SColorf(pointcolor), m_horizon_blend() * 0.25);
628 void Sky::draw_sun(video::IVideoDriver *driver, float sunsize, const video::SColor &suncolor,
629 const video::SColor &suncolor2, float wicked_time_of_day)
630 /* Draw sun in the sky.
631 * driver: Video driver object used to draw
632 * sunsize: the default size of the sun
633 * suncolor: main sun color
634 * suncolor2: second sun color
635 * wicked_time_of_day: current time of day, to know where should be the sun in the sky
638 static const u16 indices[4] = {0, 1, 2, 3};
639 std::array<video::S3DVertex, 4> vertices;
640 if (!m_sun_texture) {
641 driver->setMaterial(m_materials[1]);
642 const float sunsizes[4] = {sunsize * 1.7f, sunsize * 1.2f, sunsize, sunsize * 0.7f};
643 video::SColor c1 = suncolor;
644 video::SColor c2 = suncolor;
645 c1.setAlpha(0.05 * 255);
646 c2.setAlpha(0.15 * 255);
647 const video::SColor colors[4] = {c1, c2, suncolor, suncolor2};
648 for (int i = 0; i < 4; i++) {
649 draw_sky_body(vertices, -sunsizes[i], sunsizes[i], colors[i]);
650 place_sky_body(vertices, 90, wicked_time_of_day * 360 - 90);
651 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
654 driver->setMaterial(m_materials[3]);
655 float d = sunsize * 1.7;
658 c = video::SColor(0, 0, 0, 0);
660 c = video::SColor(255, 255, 255, 255);
661 draw_sky_body(vertices, -d, d, c);
662 place_sky_body(vertices, 90, wicked_time_of_day * 360 - 90);
663 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
668 void Sky::draw_moon(video::IVideoDriver *driver, float moonsize, const video::SColor &mooncolor,
669 const video::SColor &mooncolor2, float wicked_time_of_day)
671 * Draw moon in the sky.
672 * driver: Video driver object used to draw
673 * moonsize: the default size of the moon
674 * mooncolor: main moon color
675 * mooncolor2: second moon color
676 * wicked_time_of_day: current time of day, to know where should be the moon in the sky
679 static const u16 indices[4] = {0, 1, 2, 3};
680 std::array<video::S3DVertex, 4> vertices;
681 if (!m_moon_texture) {
682 driver->setMaterial(m_materials[1]);
683 const float moonsizes_1[4] = {
689 const float moonsizes_2[4] = {
695 video::SColor c1 = mooncolor;
696 video::SColor c2 = mooncolor;
697 c1.setAlpha(0.05 * 255);
698 c2.setAlpha(0.15 * 255);
699 const video::SColor colors[4] = {c1, c2, mooncolor, mooncolor2};
700 for (int i = 0; i < 4; i++) {
701 draw_sky_body(vertices, moonsizes_1[i], moonsizes_2[i], colors[i]);
702 place_sky_body(vertices, -90, wicked_time_of_day * 360 - 90);
703 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
706 driver->setMaterial(m_materials[4]);
707 float d = moonsize * 1.9;
710 c = video::SColor(0, 0, 0, 0);
712 c = video::SColor(255, 255, 255, 255);
713 draw_sky_body(vertices, -d, d, c);
714 place_sky_body(vertices, -90, wicked_time_of_day * 360 - 90);
715 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
720 void Sky::draw_sky_body(std::array<video::S3DVertex, 4> &vertices, float pos_1, float pos_2, const video::SColor &c)
723 * Create an array of vertices with the dimensions specified.
724 * pos_1, pos_2: position of the body's vertices
725 * c: color of the body
730 vertices[0] = video::S3DVertex(pos_1, pos_1, -1, 0, 0, 1, c, t, t);
731 vertices[1] = video::S3DVertex(pos_2, pos_1, -1, 0, 0, 1, c, o, t);
732 vertices[2] = video::S3DVertex(pos_2, pos_2, -1, 0, 0, 1, c, o, o);
733 vertices[3] = video::S3DVertex(pos_1, pos_2, -1, 0, 0, 1, c, t, o);
737 void Sky::place_sky_body(
738 std::array<video::S3DVertex, 4> &vertices, float horizon_position, float day_position)
740 * Place body in the sky.
741 * vertices: The body as a rectangle of 4 vertices
742 * horizon_position: turn the body around the Y axis
743 * day_position: turn the body around the Z axis, to place it depending of the time of the day
746 for (video::S3DVertex &vertex : vertices) {
747 // Body is directed to -Z (south) by default
748 vertex.Pos.rotateXZBy(horizon_position);
749 vertex.Pos.rotateXYBy(day_position);