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.
22 #include "IVideoDriver.h"
23 #include "ISceneManager.h"
24 #include "ICameraSceneNode.h"
25 #include "S3DVertex.h"
26 #include "client/tile.h"
27 #include "noise.h" // easeCurve
29 #include "util/numeric.h"
31 #include "client/renderingengine.h"
33 #include "camera.h" // CameraModes
35 using namespace irr::core;
37 Sky::Sky(s32 id, ITextureSource *tsrc) :
38 scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
39 RenderingEngine::get_scene_manager(), id)
41 setAutomaticCulling(scene::EAC_OFF);
42 m_box.MaxEdge.set(0, 0, 0);
43 m_box.MinEdge.set(0, 0, 0);
50 mat.ZBuffer = video::ECFN_DISABLED;
52 mat.ZBuffer = video::ECFN_NEVER;
54 mat.ZWriteEnable = false;
56 mat.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
57 mat.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
58 mat.BackfaceCulling = false;
63 //m_materials[1].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
64 m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
67 m_materials[2].setTexture(0, tsrc->getTextureForMesh("sunrisebg.png"));
68 m_materials[2].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
69 //m_materials[2].MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
71 // Ensures that sun and moon textures and tonemaps are correct.
73 m_sun_texture = tsrc->isKnownSourceImage(m_sun_params.texture) ?
74 tsrc->getTextureForMesh(m_sun_params.texture) : NULL;
75 m_moon_texture = tsrc->isKnownSourceImage(m_moon_params.texture) ?
76 tsrc->getTextureForMesh(m_moon_params.texture) : NULL;
77 m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ?
78 tsrc->getTexture(m_sun_params.tonemap) : NULL;
79 m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ?
80 tsrc->getTexture(m_moon_params.tonemap) : NULL;
84 m_materials[3].setTexture(0, m_sun_texture);
85 m_materials[3].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
86 // Disables texture filtering
87 m_materials[3].setFlag(video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
88 m_materials[3].setFlag(video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
89 m_materials[3].setFlag(video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
90 // Use tonemaps if available
92 m_materials[3].Lighting = true;
96 m_materials[4].setTexture(0, m_moon_texture);
97 m_materials[4].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
98 // Disables texture filtering
99 m_materials[4].setFlag(video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
100 m_materials[4].setFlag(video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
101 m_materials[4].setFlag(video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
102 // Use tonemaps if available
104 m_materials[4].Lighting = true;
107 for (int i = 5; i < 11; i++) {
108 m_materials[i] = mat;
109 m_materials[i].Lighting = true;
110 m_materials[i].MaterialType = video::EMT_SOLID;
112 m_directional_colored_fog = g_settings->getBool("directional_colored_fog");
113 setStarCount(1000, true);
116 void Sky::OnRegisterSceneNode()
119 SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX);
121 scene::ISceneNode::OnRegisterSceneNode();
126 video::IVideoDriver *driver = SceneManager->getVideoDriver();
127 scene::ICameraSceneNode *camera = SceneManager->getActiveCamera();
129 if (!camera || !driver)
132 ScopeProfiler sp(g_profiler, "Sky::render()", SPT_AVG);
134 // Draw perspective skybox
136 core::matrix4 translate(AbsoluteTransformation);
137 translate.setTranslation(camera->getAbsolutePosition());
139 // Draw the sky box between the near and far clip plane
140 const f32 viewDistance = (camera->getNearValue() + camera->getFarValue()) * 0.5f;
142 scale.setScale(core::vector3df(viewDistance, viewDistance, viewDistance));
144 driver->setTransform(video::ETS_WORLD, translate * scale);
146 if (m_sunlight_seen) {
147 float sunsize = 0.07;
148 video::SColorf suncolor_f(1, 1, 0, 1);
150 //suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.7 + m_time_brightness * 0.5));
151 //suncolor_f.b = MYMAX(0.0, m_brightness * 0.95);
152 video::SColorf suncolor2_f(1, 1, 1, 1);
153 // The values below were probably meant to be suncolor2_f instead of a
154 // reassignment of suncolor_f. However, the resulting colour was chosen
155 // and is our long-running classic colour. So preserve, but comment-out
156 // the unnecessary first assignments above.
158 suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.85 + m_time_brightness * 0.5));
159 suncolor_f.b = MYMAX(0.0, m_brightness);
161 float moonsize = 0.04;
162 video::SColorf mooncolor_f(0.50, 0.57, 0.65, 1);
163 video::SColorf mooncolor2_f(0.85, 0.875, 0.9, 1);
165 float nightlength = 0.415;
166 float wn = nightlength / 2;
167 float wicked_time_of_day = 0;
168 if (m_time_of_day > wn && m_time_of_day < 1.0 - wn)
169 wicked_time_of_day = (m_time_of_day - wn) / (1.0 - wn * 2) * 0.5 + 0.25;
170 else if (m_time_of_day < 0.5)
171 wicked_time_of_day = m_time_of_day / wn * 0.25;
173 wicked_time_of_day = 1.0 - ((1.0 - m_time_of_day) / wn * 0.25);
174 /*std::cerr<<"time_of_day="<<m_time_of_day<<" -> "
175 <<"wicked_time_of_day="<<wicked_time_of_day<<std::endl;*/
177 video::SColor suncolor = suncolor_f.toSColor();
178 video::SColor suncolor2 = suncolor2_f.toSColor();
179 video::SColor mooncolor = mooncolor_f.toSColor();
180 video::SColor mooncolor2 = mooncolor2_f.toSColor();
182 // Calculate offset normalized to the X dimension of a 512x1 px tonemap
183 float offset = (1.0 - fabs(sin((m_time_of_day - 0.5) * irr::core::PI))) * 511;
186 u8 * texels = (u8 *)m_sun_tonemap->lock();
187 video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
188 video::SColor texel_color (255, texel->getRed(),
189 texel->getGreen(), texel->getBlue());
190 m_sun_tonemap->unlock();
191 m_materials[3].EmissiveColor = texel_color;
194 if (m_moon_tonemap) {
195 u8 * texels = (u8 *)m_moon_tonemap->lock();
196 video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
197 video::SColor texel_color (255, texel->getRed(),
198 texel->getGreen(), texel->getBlue());
199 m_moon_tonemap->unlock();
200 m_materials[4].EmissiveColor = texel_color;
205 static const u16 indices[4] = {0, 1, 2, 3};
206 video::S3DVertex vertices[4];
208 driver->setMaterial(m_materials[1]);
210 video::SColor cloudyfogcolor = m_bgcolor;
212 // Abort rendering if we're in the clouds.
213 // Stops rendering a pure white hole in the bottom of the skybox.
217 // Draw the six sided skybox,
218 if (m_sky_params.textures.size() == 6) {
219 for (u32 j = 5; j < 11; j++) {
220 video::SColor c(255, 255, 255, 255);
221 driver->setMaterial(m_materials[j]);
222 // Use 1.05 rather than 1.0 to avoid colliding with the
223 // sun, moon and stars, as this is a background skybox.
224 vertices[0] = video::S3DVertex(-1.05, -1.05, -1.05, 0, 0, 1, c, t, t);
225 vertices[1] = video::S3DVertex( 1.05, -1.05, -1.05, 0, 0, 1, c, o, t);
226 vertices[2] = video::S3DVertex( 1.05, 1.05, -1.05, 0, 0, 1, c, o, o);
227 vertices[3] = video::S3DVertex(-1.05, 1.05, -1.05, 0, 0, 1, c, t, o);
228 for (video::S3DVertex &vertex : vertices) {
229 if (j == 5) { // Top texture
230 vertex.Pos.rotateYZBy(90);
231 vertex.Pos.rotateXZBy(90);
232 } else if (j == 6) { // Bottom texture
233 vertex.Pos.rotateYZBy(-90);
234 vertex.Pos.rotateXZBy(90);
235 } else if (j == 7) { // Left texture
236 vertex.Pos.rotateXZBy(90);
237 } else if (j == 8) { // Right texture
238 vertex.Pos.rotateXZBy(-90);
239 } else if (j == 9) { // Front texture, do nothing
240 // Irrlicht doesn't like it when vertexes are left
241 // alone and not rotated for some reason.
242 vertex.Pos.rotateXZBy(0);
243 } else {// Back texture
244 vertex.Pos.rotateXZBy(180);
247 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
251 // Draw far cloudy fog thing blended with skycolor
253 driver->setMaterial(m_materials[1]);
254 for (u32 j = 0; j < 4; j++) {
255 video::SColor c = cloudyfogcolor.getInterpolated(m_skycolor, 0.45);
256 vertices[0] = video::S3DVertex(-1, 0.08, -1, 0, 0, 1, c, t, t);
257 vertices[1] = video::S3DVertex( 1, 0.08, -1, 0, 0, 1, c, o, t);
258 vertices[2] = video::S3DVertex( 1, 0.12, -1, 0, 0, 1, c, o, o);
259 vertices[3] = video::S3DVertex(-1, 0.12, -1, 0, 0, 1, c, t, o);
260 for (video::S3DVertex &vertex : vertices) {
265 // Switch from -Z (south) to +X (east)
266 vertex.Pos.rotateXZBy(90);
268 // Switch from -Z (south) to -X (west)
269 vertex.Pos.rotateXZBy(-90);
271 // Switch from -Z (south) to +Z (north)
272 vertex.Pos.rotateXZBy(-180);
274 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
277 // Draw far cloudy fog thing at and below all horizons
278 for (u32 j = 0; j < 4; j++) {
279 video::SColor c = cloudyfogcolor;
280 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 0, 1, c, t, t);
281 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 0, 1, c, o, t);
282 vertices[2] = video::S3DVertex( 1, 0.08, -1, 0, 0, 1, c, o, o);
283 vertices[3] = video::S3DVertex(-1, 0.08, -1, 0, 0, 1, c, t, o);
284 for (video::S3DVertex &vertex : vertices) {
289 // Switch from -Z (south) to +X (east)
290 vertex.Pos.rotateXZBy(90);
292 // Switch from -Z (south) to -X (west)
293 vertex.Pos.rotateXZBy(-90);
295 // Switch from -Z (south) to +Z (north)
296 vertex.Pos.rotateXZBy(-180);
298 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
302 // Draw stars before sun and moon to be behind them
303 if (m_star_params.visible)
304 draw_stars(driver, wicked_time_of_day);
306 // Draw sunrise/sunset horizon glow texture
307 // (textures/base/pack/sunrisebg.png)
308 if (m_sun_params.sunrise_visible) {
309 driver->setMaterial(m_materials[2]);
311 float mid = wicked_time_of_day < 0.5 ? mid1 : (1.0 - mid1);
312 float a_ = 1.0f - std::fabs(wicked_time_of_day - mid) * 35.0f;
313 float a = easeCurve(MYMAX(0, MYMIN(1, a_)));
314 //std::cerr<<"a_="<<a_<<" a="<<a<<std::endl;
315 video::SColor c(255, 255, 255, 255);
316 float y = -(1.0 - a) * 0.22;
317 vertices[0] = video::S3DVertex(-1, -0.05 + y, -1, 0, 0, 1, c, t, t);
318 vertices[1] = video::S3DVertex( 1, -0.05 + y, -1, 0, 0, 1, c, o, t);
319 vertices[2] = video::S3DVertex( 1, 0.2 + y, -1, 0, 0, 1, c, o, o);
320 vertices[3] = video::S3DVertex(-1, 0.2 + y, -1, 0, 0, 1, c, t, o);
321 for (video::S3DVertex &vertex : vertices) {
322 if (wicked_time_of_day < 0.5)
323 // Switch from -Z (south) to +X (east)
324 vertex.Pos.rotateXZBy(90);
326 // Switch from -Z (south) to -X (west)
327 vertex.Pos.rotateXZBy(-90);
329 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
333 if (m_sun_params.visible)
334 draw_sun(driver, sunsize, suncolor, suncolor2, wicked_time_of_day);
337 if (m_moon_params.visible)
338 draw_moon(driver, moonsize, mooncolor, mooncolor2, wicked_time_of_day);
340 // Draw far cloudy fog thing below all horizons in front of sun, moon
343 driver->setMaterial(m_materials[1]);
345 for (u32 j = 0; j < 4; j++) {
346 video::SColor c = cloudyfogcolor;
347 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 0, 1, c, t, t);
348 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 0, 1, c, o, t);
349 vertices[2] = video::S3DVertex( 1, -0.02, -1, 0, 0, 1, c, o, o);
350 vertices[3] = video::S3DVertex(-1, -0.02, -1, 0, 0, 1, c, t, o);
351 for (video::S3DVertex &vertex : vertices) {
356 // Switch from -Z (south) to +X (east)
357 vertex.Pos.rotateXZBy(90);
359 // Switch from -Z (south) to -X (west)
360 vertex.Pos.rotateXZBy(-90);
362 // Switch from -Z (south) to +Z (north)
363 vertex.Pos.rotateXZBy(-180);
365 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
368 // Draw bottom far cloudy fog thing in front of sun, moon and stars
369 video::SColor c = cloudyfogcolor;
370 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 1, 0, c, t, t);
371 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 1, 0, c, o, t);
372 vertices[2] = video::S3DVertex( 1, -1.0, 1, 0, 1, 0, c, o, o);
373 vertices[3] = video::S3DVertex(-1, -1.0, 1, 0, 1, 0, c, t, o);
374 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
379 void Sky::update(float time_of_day, float time_brightness,
380 float direct_brightness, bool sunlight_seen,
381 CameraMode cam_mode, float yaw, float pitch)
383 // Stabilize initial brightness and color values by flooding updates
384 if (m_first_update) {
385 /*dstream<<"First update with time_of_day="<<time_of_day
386 <<" time_brightness="<<time_brightness
387 <<" direct_brightness="<<direct_brightness
388 <<" sunlight_seen="<<sunlight_seen<<std::endl;*/
389 m_first_update = false;
390 for (u32 i = 0; i < 100; i++) {
391 update(time_of_day, time_brightness, direct_brightness,
392 sunlight_seen, cam_mode, yaw, pitch);
397 m_time_of_day = time_of_day;
398 m_time_brightness = time_brightness;
399 m_sunlight_seen = sunlight_seen;
402 bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
407 video::SColorf bgcolor_bright_normal_f(170. / 255, 200. / 255, 230. / 255, 1.0);
408 video::SColorf bgcolor_bright_dawn_f(0.666, 200. / 255 * 0.7, 230. / 255 * 0.5, 1.0);
409 video::SColorf bgcolor_bright_dawn_f(0.666, 0.549, 0.220, 1.0);
410 video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.0, 1.0);
411 video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.2, 1.0);
413 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.591, 0.4);
414 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.65, 0.44);
415 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
418 video::SColorf bgcolor_bright_normal_f = m_sky_params.sky_color.day_horizon;
419 video::SColorf bgcolor_bright_indoor_f = m_sky_params.sky_color.indoors;
420 video::SColorf bgcolor_bright_dawn_f = m_sky_params.sky_color.dawn_horizon;
421 video::SColorf bgcolor_bright_night_f = m_sky_params.sky_color.night_horizon;
423 video::SColorf skycolor_bright_normal_f = m_sky_params.sky_color.day_sky;
424 video::SColorf skycolor_bright_dawn_f = m_sky_params.sky_color.dawn_sky;
425 video::SColorf skycolor_bright_night_f = m_sky_params.sky_color.night_sky;
427 video::SColorf cloudcolor_bright_normal_f = m_cloudcolor_day_f;
428 video::SColorf cloudcolor_bright_dawn_f = m_cloudcolor_dawn_f;
430 float cloud_color_change_fraction = 0.95;
432 if (std::fabs(time_brightness - m_brightness) < 0.2f) {
433 m_brightness = m_brightness * 0.95 + time_brightness * 0.05;
435 m_brightness = m_brightness * 0.80 + time_brightness * 0.20;
436 cloud_color_change_fraction = 0.0;
439 if (direct_brightness < m_brightness)
440 m_brightness = m_brightness * 0.95 + direct_brightness * 0.05;
442 m_brightness = m_brightness * 0.98 + direct_brightness * 0.02;
445 m_clouds_visible = true;
446 float color_change_fraction = 0.98f;
448 if (is_dawn) { // Dawn
449 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
450 bgcolor_bright_dawn_f, color_change_fraction);
451 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
452 skycolor_bright_dawn_f, color_change_fraction);
453 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
454 cloudcolor_bright_dawn_f, color_change_fraction);
456 if (time_brightness < 0.13f) { // Night
457 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
458 bgcolor_bright_night_f, color_change_fraction);
459 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
460 skycolor_bright_night_f, color_change_fraction);
462 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
463 bgcolor_bright_normal_f, color_change_fraction);
464 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
465 skycolor_bright_normal_f, color_change_fraction);
468 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
469 cloudcolor_bright_normal_f, color_change_fraction);
472 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
473 bgcolor_bright_indoor_f, color_change_fraction);
474 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
475 bgcolor_bright_indoor_f, color_change_fraction);
476 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
477 cloudcolor_bright_normal_f, color_change_fraction);
478 m_clouds_visible = false;
481 video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
482 m_bgcolor = video::SColor(
484 bgcolor_bright.getRed() * m_brightness,
485 bgcolor_bright.getGreen() * m_brightness,
486 bgcolor_bright.getBlue() * m_brightness
489 video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
490 m_skycolor = video::SColor(
492 skycolor_bright.getRed() * m_brightness,
493 skycolor_bright.getGreen() * m_brightness,
494 skycolor_bright.getBlue() * m_brightness
497 // Horizon coloring based on sun and moon direction during sunset and sunrise
498 video::SColor pointcolor = video::SColor(m_bgcolor.getAlpha(), 255, 255, 255);
499 if (m_directional_colored_fog) {
500 if (m_horizon_blend() != 0) {
501 // Calculate hemisphere value from yaw, (inverted in third person front view)
503 if (cam_mode > CAMERA_MODE_THIRD)
505 f32 pointcolor_blend = wrapDegrees_0_360(yaw * dir_factor + 90);
506 if (pointcolor_blend > 180)
507 pointcolor_blend = 360 - pointcolor_blend;
508 pointcolor_blend /= 180;
509 // Bound view angle to determine where transition starts and ends
510 pointcolor_blend = rangelim(1 - pointcolor_blend * 1.375, 0, 1 / 1.375) *
512 // Combine the colors when looking up or down, otherwise turning looks weird
513 pointcolor_blend += (0.5 - pointcolor_blend) *
514 (1 - MYMIN((90 - std::fabs(pitch)) / 90 * 1.5, 1));
515 // Invert direction to match where the sun and moon are rising
516 if (m_time_of_day > 0.5)
517 pointcolor_blend = 1 - pointcolor_blend;
518 // Horizon colors of sun and moon
519 f32 pointcolor_light = rangelim(m_time_brightness * 3, 0.2, 1);
521 video::SColorf pointcolor_sun_f(1, 1, 1, 1);
522 // Use tonemap only if default sun/moon tinting is used
523 // which keeps previous behaviour.
524 if (m_sun_tonemap && m_default_tint) {
525 pointcolor_sun_f.r = pointcolor_light *
526 (float)m_materials[3].EmissiveColor.getRed() / 255;
527 pointcolor_sun_f.b = pointcolor_light *
528 (float)m_materials[3].EmissiveColor.getBlue() / 255;
529 pointcolor_sun_f.g = pointcolor_light *
530 (float)m_materials[3].EmissiveColor.getGreen() / 255;
531 } else if (!m_default_tint) {
532 pointcolor_sun_f = m_sky_params.sun_tint;
534 pointcolor_sun_f.r = pointcolor_light * 1;
535 pointcolor_sun_f.b = pointcolor_light *
536 (0.25 + (rangelim(m_time_brightness, 0.25, 0.75) - 0.25) * 2 * 0.75);
537 pointcolor_sun_f.g = pointcolor_light * (pointcolor_sun_f.b * 0.375 +
538 (rangelim(m_time_brightness, 0.05, 0.15) - 0.05) * 10 * 0.625);
541 video::SColorf pointcolor_moon_f;
542 if (m_default_tint) {
543 pointcolor_moon_f = video::SColorf(
544 0.5 * pointcolor_light,
545 0.6 * pointcolor_light,
546 0.8 * pointcolor_light,
550 pointcolor_moon_f = video::SColorf(
551 (m_sky_params.moon_tint.getRed() / 255) * pointcolor_light,
552 (m_sky_params.moon_tint.getGreen() / 255) * pointcolor_light,
553 (m_sky_params.moon_tint.getBlue() / 255) * pointcolor_light,
557 if (m_moon_tonemap && m_default_tint) {
558 pointcolor_moon_f.r = pointcolor_light *
559 (float)m_materials[4].EmissiveColor.getRed() / 255;
560 pointcolor_moon_f.b = pointcolor_light *
561 (float)m_materials[4].EmissiveColor.getBlue() / 255;
562 pointcolor_moon_f.g = pointcolor_light *
563 (float)m_materials[4].EmissiveColor.getGreen() / 255;
566 video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
567 video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
568 // Calculate the blend color
569 pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
571 m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
572 m_skycolor = m_mix_scolor(m_skycolor, pointcolor, m_horizon_blend() * 0.25);
575 float cloud_direct_brightness = 0.0f;
577 if (!m_directional_colored_fog) {
578 cloud_direct_brightness = time_brightness;
579 // Boost cloud brightness relative to sky, at dawn, dusk and at night
580 if (time_brightness < 0.7f)
581 cloud_direct_brightness *= 1.3f;
583 cloud_direct_brightness = std::fmin(m_horizon_blend() * 0.15f +
584 m_time_brightness, 1.0f);
585 // Set the same minimum cloud brightness at night
586 if (time_brightness < 0.5f)
587 cloud_direct_brightness = std::fmax(cloud_direct_brightness,
588 time_brightness * 1.3f);
591 cloud_direct_brightness = direct_brightness;
594 m_cloud_brightness = m_cloud_brightness * cloud_color_change_fraction +
595 cloud_direct_brightness * (1.0 - cloud_color_change_fraction);
596 m_cloudcolor_f = video::SColorf(
597 m_cloudcolor_bright_f.r * m_cloud_brightness,
598 m_cloudcolor_bright_f.g * m_cloud_brightness,
599 m_cloudcolor_bright_f.b * m_cloud_brightness,
602 if (m_directional_colored_fog) {
603 m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f,
604 video::SColorf(pointcolor), m_horizon_blend() * 0.25);
608 void Sky::draw_sun(video::IVideoDriver *driver, float sunsize, const video::SColor &suncolor,
609 const video::SColor &suncolor2, float wicked_time_of_day)
610 /* Draw sun in the sky.
611 * driver: Video driver object used to draw
612 * sunsize: the default size of the sun
613 * suncolor: main sun color
614 * suncolor2: second sun color
615 * wicked_time_of_day: current time of day, to know where should be the sun in the sky
618 static const u16 indices[4] = {0, 1, 2, 3};
619 std::array<video::S3DVertex, 4> vertices;
620 if (!m_sun_texture) {
621 driver->setMaterial(m_materials[1]);
622 const float sunsizes[4] = {
623 (sunsize * 1.7f) * m_sun_params.scale,
624 (sunsize * 1.2f) * m_sun_params.scale,
625 (sunsize) * m_sun_params.scale,
626 (sunsize * 0.7f) * m_sun_params.scale
628 video::SColor c1 = suncolor;
629 video::SColor c2 = suncolor;
630 c1.setAlpha(0.05 * 255);
631 c2.setAlpha(0.15 * 255);
632 const video::SColor colors[4] = {c1, c2, suncolor, suncolor2};
633 for (int i = 0; i < 4; i++) {
634 draw_sky_body(vertices, -sunsizes[i], sunsizes[i], colors[i]);
635 place_sky_body(vertices, 90, wicked_time_of_day * 360 - 90);
636 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
639 driver->setMaterial(m_materials[3]);
640 float d = (sunsize * 1.7) * m_sun_params.scale;
643 c = video::SColor(0, 0, 0, 0);
645 c = video::SColor(255, 255, 255, 255);
646 draw_sky_body(vertices, -d, d, c);
647 place_sky_body(vertices, 90, wicked_time_of_day * 360 - 90);
648 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
653 void Sky::draw_moon(video::IVideoDriver *driver, float moonsize, const video::SColor &mooncolor,
654 const video::SColor &mooncolor2, float wicked_time_of_day)
656 * Draw moon in the sky.
657 * driver: Video driver object used to draw
658 * moonsize: the default size of the moon
659 * mooncolor: main moon color
660 * mooncolor2: second moon color
661 * wicked_time_of_day: current time of day, to know where should be the moon in
665 static const u16 indices[4] = {0, 1, 2, 3};
666 std::array<video::S3DVertex, 4> vertices;
667 if (!m_moon_texture) {
668 driver->setMaterial(m_materials[1]);
669 const float moonsizes_1[4] = {
670 (-moonsize * 1.9f) * m_moon_params.scale,
671 (-moonsize * 1.3f) * m_moon_params.scale,
672 (-moonsize) * m_moon_params.scale,
673 (-moonsize) * m_moon_params.scale
675 const float moonsizes_2[4] = {
676 (moonsize * 1.9f) * m_moon_params.scale,
677 (moonsize * 1.3f) * m_moon_params.scale,
678 (moonsize) *m_moon_params.scale,
679 (moonsize * 0.6f) * m_moon_params.scale
681 video::SColor c1 = mooncolor;
682 video::SColor c2 = mooncolor;
683 c1.setAlpha(0.05 * 255);
684 c2.setAlpha(0.15 * 255);
685 const video::SColor colors[4] = {c1, c2, mooncolor, mooncolor2};
686 for (int i = 0; i < 4; i++) {
687 draw_sky_body(vertices, moonsizes_1[i], moonsizes_2[i], colors[i]);
688 place_sky_body(vertices, -90, wicked_time_of_day * 360 - 90);
689 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
692 driver->setMaterial(m_materials[4]);
693 float d = (moonsize * 1.9) * m_moon_params.scale;
696 c = video::SColor(0, 0, 0, 0);
698 c = video::SColor(255, 255, 255, 255);
699 draw_sky_body(vertices, -d, d, c);
700 place_sky_body(vertices, -90, wicked_time_of_day * 360 - 90);
701 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
705 void Sky::draw_stars(video::IVideoDriver * driver, float wicked_time_of_day)
707 driver->setMaterial(m_materials[1]);
708 // Tune values so that stars first appear just after the sun
709 // disappears over the horizon, and disappear just before the sun
710 // appears over the horizon.
711 // Also tune so that stars are at full brightness from time 20000
714 float tod = wicked_time_of_day < 0.5f ? wicked_time_of_day : (1.0f - wicked_time_of_day);
715 float starbrightness = clamp((0.25f - fabsf(tod)) * 20.0f, 0.0f, 1.0f);
717 float f = starbrightness;
718 float d = (0.006 / 2) * m_star_params.scale;
720 video::SColor starcolor = m_star_params.starcolor;
721 starcolor.setAlpha(f * m_star_params.starcolor.getAlpha());
723 // Stars are only drawn when not fully transparent
724 if (m_star_params.starcolor.getAlpha() < 1)
727 u16 *indices = new u16[m_star_params.count * 3];
728 video::S3DVertex *vertices =
729 new video::S3DVertex[m_star_params.count * 3];
730 for (u32 i = 0; i < m_star_params.count; i++) {
731 indices[i * 3 + 0] = i * 3 + 0;
732 indices[i * 3 + 1] = i * 3 + 1;
733 indices[i * 3 + 2] = i * 3 + 2;
735 core::CMatrix4<f32> a;
736 a.buildRotateFromTo(v3f(0, 1, 0), r);
737 v3f p = v3f(-d, 1, -d);
738 v3f p1 = v3f(d, 1, 0);
739 v3f p2 = v3f(-d, 1, d);
743 p.rotateXYBy(wicked_time_of_day * 360 - 90);
744 p1.rotateXYBy(wicked_time_of_day * 360 - 90);
745 p2.rotateXYBy(wicked_time_of_day * 360 - 90);
746 vertices[i * 3 + 0].Pos = p;
747 vertices[i * 3 + 0].Color = starcolor;
748 vertices[i * 3 + 1].Pos = p1;
749 vertices[i * 3 + 1].Color = starcolor;
750 vertices[i * 3 + 2].Pos = p2;
751 vertices[i * 3 + 2].Color = starcolor;
753 driver->drawIndexedTriangleList(vertices, m_star_params.count * 3,
754 indices, m_star_params.count);
758 u16 *indices = new u16[m_star_params.count * 4];
759 video::S3DVertex *vertices =
760 new video::S3DVertex[m_star_params.count * 4];
761 for (u32 i = 0; i < m_star_params.count; i++) {
762 indices[i * 4 + 0] = i * 4 + 0;
763 indices[i * 4 + 1] = i * 4 + 1;
764 indices[i * 4 + 2] = i * 4 + 2;
765 indices[i * 4 + 3] = i * 4 + 3;
767 core::CMatrix4<f32> a;
768 a.buildRotateFromTo(v3f(0, 1, 0), r);
769 v3f p = v3f(-d, 1, -d);
770 v3f p1 = v3f(d, 1, -d);
771 v3f p2 = v3f(d, 1, d);
772 v3f p3 = v3f(-d, 1, d);
777 p.rotateXYBy(wicked_time_of_day * 360 - 90);
778 p1.rotateXYBy(wicked_time_of_day * 360 - 90);
779 p2.rotateXYBy(wicked_time_of_day * 360 - 90);
780 p3.rotateXYBy(wicked_time_of_day * 360 - 90);
781 vertices[i * 4 + 0].Pos = p;
782 vertices[i * 4 + 0].Color = starcolor;
783 vertices[i * 4 + 1].Pos = p1;
784 vertices[i * 4 + 1].Color = starcolor;
785 vertices[i * 4 + 2].Pos = p2;
786 vertices[i * 4 + 2].Color = starcolor;
787 vertices[i * 4 + 3].Pos = p3;
788 vertices[i * 4 + 3].Color = starcolor;
790 driver->drawVertexPrimitiveList(vertices, m_star_params.count * 4,
791 indices, m_star_params.count, video::EVT_STANDARD,
792 scene::EPT_QUADS, video::EIT_16BIT);
798 void Sky::draw_sky_body(std::array<video::S3DVertex, 4> &vertices, float pos_1, float pos_2, const video::SColor &c)
801 * Create an array of vertices with the dimensions specified.
802 * pos_1, pos_2: position of the body's vertices
803 * c: color of the body
808 vertices[0] = video::S3DVertex(pos_1, pos_1, -1, 0, 0, 1, c, t, t);
809 vertices[1] = video::S3DVertex(pos_2, pos_1, -1, 0, 0, 1, c, o, t);
810 vertices[2] = video::S3DVertex(pos_2, pos_2, -1, 0, 0, 1, c, o, o);
811 vertices[3] = video::S3DVertex(pos_1, pos_2, -1, 0, 0, 1, c, t, o);
815 void Sky::place_sky_body(
816 std::array<video::S3DVertex, 4> &vertices, float horizon_position, float day_position)
818 * Place body in the sky.
819 * vertices: The body as a rectangle of 4 vertices
820 * horizon_position: turn the body around the Y axis
821 * day_position: turn the body around the Z axis, to place it depending of the time of the day
824 for (video::S3DVertex &vertex : vertices) {
825 // Body is directed to -Z (south) by default
826 vertex.Pos.rotateXZBy(horizon_position);
827 vertex.Pos.rotateXYBy(day_position);
831 void Sky::setSunTexture(std::string sun_texture,
832 std::string sun_tonemap, ITextureSource *tsrc)
834 // Ignore matching textures (with modifiers) entirely,
835 // but lets at least update the tonemap before hand.
836 m_sun_params.tonemap = sun_tonemap;
837 m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ?
838 tsrc->getTexture(m_sun_params.tonemap) : NULL;
839 m_materials[3].Lighting = !!m_sun_tonemap;
841 if (m_sun_params.texture == sun_texture)
843 m_sun_params.texture = sun_texture;
845 if (sun_texture != "") {
846 // We want to ensure the texture exists first.
847 m_sun_texture = tsrc->getTextureForMesh(m_sun_params.texture);
850 m_materials[3] = m_materials[0];
851 m_materials[3].setTexture(0, m_sun_texture);
852 m_materials[3].MaterialType = video::
853 EMT_TRANSPARENT_ALPHA_CHANNEL;
854 // Disables texture filtering
855 m_materials[3].setFlag(
856 video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
857 m_materials[3].setFlag(
858 video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
859 m_materials[3].setFlag(
860 video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
863 m_sun_texture = nullptr;
867 void Sky::setSunriseTexture(std::string sunglow_texture,
868 ITextureSource* tsrc)
870 // Ignore matching textures (with modifiers) entirely.
871 if (m_sun_params.sunrise == sunglow_texture)
873 m_sun_params.sunrise = sunglow_texture;
874 m_materials[2].setTexture(0, tsrc->getTextureForMesh(
875 sunglow_texture.empty() ? "sunrisebg.png" : sunglow_texture)
879 void Sky::setMoonTexture(std::string moon_texture,
880 std::string moon_tonemap, ITextureSource *tsrc)
882 // Ignore matching textures (with modifiers) entirely,
883 // but lets at least update the tonemap before hand.
884 m_moon_params.tonemap = moon_tonemap;
885 m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ?
886 tsrc->getTexture(m_moon_params.tonemap) : NULL;
887 m_materials[4].Lighting = !!m_moon_tonemap;
889 if (m_moon_params.texture == moon_texture)
891 m_moon_params.texture = moon_texture;
893 if (moon_texture != "") {
894 // We want to ensure the texture exists first.
895 m_moon_texture = tsrc->getTextureForMesh(m_moon_params.texture);
897 if (m_moon_texture) {
898 m_materials[4] = m_materials[0];
899 m_materials[4].setTexture(0, m_moon_texture);
900 m_materials[4].MaterialType = video::
901 EMT_TRANSPARENT_ALPHA_CHANNEL;
902 // Disables texture filtering
903 m_materials[4].setFlag(
904 video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
905 m_materials[4].setFlag(
906 video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
907 m_materials[4].setFlag(
908 video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
911 m_moon_texture = nullptr;
915 void Sky::setStarCount(u16 star_count, bool force_update)
917 // Allow force updating star count at game init.
918 if (m_star_params.count != star_count || force_update) {
919 m_star_params.count = star_count;
921 // Rebuild the stars surrounding the camera
922 for (u16 i = 0; i < star_count; i++) {
924 myrand_range(-10000, 10000),
925 myrand_range(-10000, 10000),
926 myrand_range(-10000, 10000)
930 m_stars.emplace_back(star);
935 void Sky::setSkyColors(const SkyboxParams sky)
937 m_sky_params.sky_color = sky.sky_color;
940 void Sky::setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
941 std::string use_sun_tint)
943 // Change sun and moon tinting:
944 m_sky_params.sun_tint = sun_tint;
945 m_sky_params.moon_tint = moon_tint;
946 // Faster than comparing strings every rendering frame
947 if (use_sun_tint == "default")
948 m_default_tint = true;
949 else if (use_sun_tint == "custom")
950 m_default_tint = false;
952 m_default_tint = true;
955 void Sky::addTextureToSkybox(std::string texture, int material_id,
956 ITextureSource *tsrc)
958 // Sanity check for more than six textures.
959 if (material_id + 5 >= SKY_MATERIAL_COUNT)
961 // Keep a list of texture names handy.
962 m_sky_params.textures.emplace_back(texture);
963 video::ITexture *result = tsrc->getTextureForMesh(texture);
964 m_materials[material_id+5] = m_materials[0];
965 m_materials[material_id+5].setTexture(0, result);
966 m_materials[material_id+5].MaterialType = video::EMT_SOLID;
969 // To be called once at game init to setup default values.
970 void Sky::setSkyDefaults()
972 SkyboxDefaults sky_defaults;
973 m_sky_params.sky_color = sky_defaults.getSkyColorDefaults();
974 m_sun_params = sky_defaults.getSunDefaults();
975 m_moon_params = sky_defaults.getMoonDefaults();
976 m_star_params = sky_defaults.getStarDefaults();