Sky API: Rename *_tint to fog_*_tint for consistency
[oweals/minetest.git] / src / client / sky.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "sky.h"
21 #include "ITexture.h"
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
28 #include "profiler.h"
29 #include "util/numeric.h"
30 #include <cmath>
31 #include "client/renderingengine.h"
32 #include "settings.h"
33 #include "camera.h" // CameraModes
34 #include "config.h"
35 using namespace irr::core;
36
37 Sky::Sky(s32 id, ITextureSource *tsrc) :
38                 scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
39                         RenderingEngine::get_scene_manager(), id)
40 {
41         setAutomaticCulling(scene::EAC_OFF);
42         m_box.MaxEdge.set(0, 0, 0);
43         m_box.MinEdge.set(0, 0, 0);
44
45         // Create material
46
47         video::SMaterial mat;
48         mat.Lighting = false;
49 #if ENABLE_GLES
50         mat.ZBuffer = video::ECFN_DISABLED;
51 #else
52         mat.ZBuffer = video::ECFN_NEVER;
53 #endif
54         mat.ZWriteEnable = false;
55         mat.AntiAliasing = 0;
56         mat.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
57         mat.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
58         mat.BackfaceCulling = false;
59
60         m_materials[0] = mat;
61
62         m_materials[1] = mat;
63         //m_materials[1].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
64         m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
65
66         m_materials[2] = mat;
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;
70
71         // Ensures that sun and moon textures and tonemaps are correct.
72         setSkyDefaults();
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;
81
82         if (m_sun_texture) {
83                 m_materials[3] = mat;
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
91                 if (m_sun_tonemap)
92                         m_materials[3].Lighting = true;
93         }
94         if (m_moon_texture) {
95                 m_materials[4] = mat;
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
103                 if (m_moon_tonemap)
104                         m_materials[4].Lighting = true;
105         }
106
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;
111         }
112         m_directional_colored_fog = g_settings->getBool("directional_colored_fog");
113         setStarCount(1000, true);
114 }
115
116 void Sky::OnRegisterSceneNode()
117 {
118         if (IsVisible)
119                 SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX);
120
121         scene::ISceneNode::OnRegisterSceneNode();
122 }
123
124 void Sky::render()
125 {
126         video::IVideoDriver *driver = SceneManager->getVideoDriver();
127         scene::ICameraSceneNode *camera = SceneManager->getActiveCamera();
128
129         if (!camera || !driver)
130                 return;
131
132         ScopeProfiler sp(g_profiler, "Sky::render()", SPT_AVG);
133
134         // Draw perspective skybox
135
136         core::matrix4 translate(AbsoluteTransformation);
137         translate.setTranslation(camera->getAbsolutePosition());
138
139         // Draw the sky box between the near and far clip plane
140         const f32 viewDistance = (camera->getNearValue() + camera->getFarValue()) * 0.5f;
141         core::matrix4 scale;
142         scale.setScale(core::vector3df(viewDistance, viewDistance, viewDistance));
143
144         driver->setTransform(video::ETS_WORLD, translate * scale);
145
146         if (m_sunlight_seen) {
147                 float sunsize = 0.07;
148                 video::SColorf suncolor_f(1, 1, 0, 1);
149                 //suncolor_f.r = 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.
157                 suncolor_f.r = 1;
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);
160
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);
164
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;
172                 else
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;*/
176
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();
181
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;
184
185                 if (m_sun_tonemap) {
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;
192                 }
193
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;
201                 }
202
203                 const f32 t = 1.0f;
204                 const f32 o = 0.0f;
205                 static const u16 indices[4] = {0, 1, 2, 3};
206                 video::S3DVertex vertices[4];
207
208                 driver->setMaterial(m_materials[1]);
209
210                 video::SColor cloudyfogcolor = m_bgcolor;
211
212                 // Abort rendering if we're in the clouds.
213                 // Stops rendering a pure white hole in the bottom of the skybox.
214                 if (m_in_clouds)
215                         return;
216
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);
245                                         }
246                                 }
247                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
248                         }
249                 }
250
251                 // Draw far cloudy fog thing blended with skycolor
252                 if (m_visible) {
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) {
261                                         if (j == 0)
262                                                 // Don't switch
263                                                 {}
264                                         else if (j == 1)
265                                                 // Switch from -Z (south) to +X (east)
266                                                 vertex.Pos.rotateXZBy(90);
267                                         else if (j == 2)
268                                                 // Switch from -Z (south) to -X (west)
269                                                 vertex.Pos.rotateXZBy(-90);
270                                         else
271                                                 // Switch from -Z (south) to +Z (north)
272                                                 vertex.Pos.rotateXZBy(-180);
273                                 }
274                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
275                         }
276
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) {
285                                         if (j == 0)
286                                                 // Don't switch
287                                                 {}
288                                         else if (j == 1)
289                                                 // Switch from -Z (south) to +X (east)
290                                                 vertex.Pos.rotateXZBy(90);
291                                         else if (j == 2)
292                                                 // Switch from -Z (south) to -X (west)
293                                                 vertex.Pos.rotateXZBy(-90);
294                                         else
295                                                 // Switch from -Z (south) to +Z (north)
296                                                 vertex.Pos.rotateXZBy(-180);
297                                 }
298                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
299                         }
300                 }
301
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);
305
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]);
310                         float mid1 = 0.25;
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);
325                                 else
326                                         // Switch from -Z (south) to -X (west)
327                                         vertex.Pos.rotateXZBy(-90);
328                         }
329                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
330                 }
331
332                 // Draw sun
333                 if (m_sun_params.visible)
334                         draw_sun(driver, sunsize, suncolor, suncolor2, wicked_time_of_day);
335
336                 // Draw moon
337                 if (m_moon_params.visible)
338                         draw_moon(driver, moonsize, mooncolor, mooncolor2, wicked_time_of_day);
339
340                 // Draw far cloudy fog thing below all horizons in front of sun, moon
341                 // and stars.
342                 if (m_visible) {
343                         driver->setMaterial(m_materials[1]);
344
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) {
352                                         if (j == 0)
353                                                 // Don't switch
354                                                 {}
355                                         else if (j == 1)
356                                                 // Switch from -Z (south) to +X (east)
357                                                 vertex.Pos.rotateXZBy(90);
358                                         else if (j == 2)
359                                                 // Switch from -Z (south) to -X (west)
360                                                 vertex.Pos.rotateXZBy(-90);
361                                         else
362                                                 // Switch from -Z (south) to +Z (north)
363                                                 vertex.Pos.rotateXZBy(-180);
364                                 }
365                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
366                         }
367
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);
375                 }
376         }
377 }
378
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)
382 {
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);
393                 }
394                 return;
395         }
396
397         m_time_of_day = time_of_day;
398         m_time_brightness = time_brightness;
399         m_sunlight_seen = sunlight_seen;
400         m_in_clouds = false;
401
402         bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
403
404         /*
405         Development colours
406
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);
412
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);
416         */
417
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;
422
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;
426
427         video::SColorf cloudcolor_bright_normal_f = m_cloudcolor_day_f;
428         video::SColorf cloudcolor_bright_dawn_f = m_cloudcolor_dawn_f;
429
430         float cloud_color_change_fraction = 0.95;
431         if (sunlight_seen) {
432                 if (std::fabs(time_brightness - m_brightness) < 0.2f) {
433                         m_brightness = m_brightness * 0.95 + time_brightness * 0.05;
434                 } else {
435                         m_brightness = m_brightness * 0.80 + time_brightness * 0.20;
436                         cloud_color_change_fraction = 0.0;
437                 }
438         } else {
439                 if (direct_brightness < m_brightness)
440                         m_brightness = m_brightness * 0.95 + direct_brightness * 0.05;
441                 else
442                         m_brightness = m_brightness * 0.98 + direct_brightness * 0.02;
443         }
444
445         m_clouds_visible = true;
446         float color_change_fraction = 0.98f;
447         if (sunlight_seen) {
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);
455                 } else {
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);
461                         } else { // Day
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);
466                         }
467
468                         m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
469                                 cloudcolor_bright_normal_f, color_change_fraction);
470                 }
471         } else {
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;
479         }
480
481         video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
482         m_bgcolor = video::SColor(
483                 255,
484                 bgcolor_bright.getRed() * m_brightness,
485                 bgcolor_bright.getGreen() * m_brightness,
486                 bgcolor_bright.getBlue() * m_brightness
487         );
488
489         video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
490         m_skycolor = video::SColor(
491                 255,
492                 skycolor_bright.getRed() * m_brightness,
493                 skycolor_bright.getGreen() * m_brightness,
494                 skycolor_bright.getBlue() * m_brightness
495         );
496
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)
502                         s8 dir_factor = 1;
503                         if (cam_mode > CAMERA_MODE_THIRD)
504                                 dir_factor = -1;
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) *
511                                 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);
520
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.fog_sun_tint;
533                         } else {
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);
539                         }
540
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,
547                                         1
548                                 );
549                         } else {
550                                 pointcolor_moon_f = video::SColorf(
551                                         (m_sky_params.fog_moon_tint.getRed() / 255) * pointcolor_light,
552                                         (m_sky_params.fog_moon_tint.getGreen() / 255) * pointcolor_light,
553                                         (m_sky_params.fog_moon_tint.getBlue() / 255) * pointcolor_light,
554                                         1
555                                 );
556                         }
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;
564                         }
565
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);
570                 }
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);
573         }
574
575         float cloud_direct_brightness = 0.0f;
576         if (sunlight_seen) {
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;
582                 } else {
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);
589                 }
590         } else {
591                 cloud_direct_brightness = direct_brightness;
592         }
593
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,
600                 1.0
601         );
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);
605         }
606 }
607
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
616          */
617 {
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
627                 };
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);
637                 }
638         } else {
639                 driver->setMaterial(m_materials[3]);
640                 float d = (sunsize * 1.7) * m_sun_params.scale;
641                 video::SColor c;
642                 if (m_sun_tonemap)
643                         c = video::SColor(0, 0, 0, 0);
644                 else
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);
649         }
650 }
651
652
653 void Sky::draw_moon(video::IVideoDriver *driver, float moonsize, const video::SColor &mooncolor,
654         const video::SColor &mooncolor2, float wicked_time_of_day)
655 /*
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
662         * the sky
663         */
664 {
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
674                 };
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
680                 };
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);
690                 }
691         } else {
692                 driver->setMaterial(m_materials[4]);
693                 float d = (moonsize * 1.9) * m_moon_params.scale;
694                 video::SColor c;
695                 if (m_moon_tonemap)
696                         c = video::SColor(0, 0, 0, 0);
697                 else
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);
702         }
703 }
704
705 void Sky::draw_stars(video::IVideoDriver * driver, float wicked_time_of_day)
706 {
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
712         // to time 4000.
713
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);
716
717         float f = starbrightness;
718         float d = (0.006 / 2) * m_star_params.scale;
719
720         video::SColor starcolor = m_star_params.starcolor;
721         starcolor.setAlpha(f * m_star_params.starcolor.getAlpha());
722
723         // Stars are only drawn when not fully transparent
724         if (m_star_params.starcolor.getAlpha() < 1)
725                 return;
726 #if ENABLE_GLES
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;
734                 v3f r = m_stars[i];
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);
740                 a.rotateVect(p);
741                 a.rotateVect(p1);
742                 a.rotateVect(p2);
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;
752         }
753         driver->drawIndexedTriangleList(vertices, m_star_params.count * 3,
754                         indices, m_star_params.count);
755         delete[] indices;
756         delete[] vertices;
757 #else
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;
766                 v3f r = m_stars[i];
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);
773                 a.rotateVect(p);
774                 a.rotateVect(p1);
775                 a.rotateVect(p2);
776                 a.rotateVect(p3);
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;
789         }
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);
793         delete[] indices;
794         delete[] vertices;
795 #endif
796 }
797
798 void Sky::draw_sky_body(std::array<video::S3DVertex, 4> &vertices, float pos_1, float pos_2, const video::SColor &c)
799 {
800         /*
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
804         */
805
806         const f32 t = 1.0f;
807         const f32 o = 0.0f;
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);
812 }
813
814
815 void Sky::place_sky_body(
816         std::array<video::S3DVertex, 4> &vertices, float horizon_position, float day_position)
817         /*
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
822         */
823 {
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);
828         }
829 }
830
831 void Sky::setSunTexture(std::string sun_texture,
832                 std::string sun_tonemap, ITextureSource *tsrc)
833 {
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;
840
841         if (m_sun_params.texture == sun_texture)
842                 return;
843         m_sun_params.texture = sun_texture;
844
845         if (sun_texture != "") {
846                 // We want to ensure the texture exists first.
847                 m_sun_texture = tsrc->getTextureForMesh(m_sun_params.texture);
848
849                 if (m_sun_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);
861                 }
862         } else {
863                 m_sun_texture = nullptr;
864         }
865 }
866
867 void Sky::setSunriseTexture(std::string sunglow_texture,
868                 ITextureSource* tsrc)
869 {
870         // Ignore matching textures (with modifiers) entirely.
871         if (m_sun_params.sunrise == sunglow_texture)
872                 return;
873         m_sun_params.sunrise = sunglow_texture;
874         m_materials[2].setTexture(0, tsrc->getTextureForMesh(
875                 sunglow_texture.empty() ? "sunrisebg.png" : sunglow_texture)
876         );
877 }
878
879 void Sky::setMoonTexture(std::string moon_texture,
880                 std::string moon_tonemap, ITextureSource *tsrc)
881 {
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;
888
889         if (m_moon_params.texture == moon_texture)
890                 return;
891         m_moon_params.texture = moon_texture;
892
893         if (moon_texture != "") {
894                 // We want to ensure the texture exists first.
895                 m_moon_texture = tsrc->getTextureForMesh(m_moon_params.texture);
896
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);
909                 }
910         } else {
911                 m_moon_texture = nullptr;
912         }
913 }
914
915 void Sky::setStarCount(u16 star_count, bool force_update)
916 {
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;
920                 m_stars.clear();
921                 // Rebuild the stars surrounding the camera
922                 for (u16 i = 0; i < star_count; i++) {
923                         v3f star = v3f(
924                                 myrand_range(-10000, 10000),
925                                 myrand_range(-10000, 10000),
926                                 myrand_range(-10000, 10000)
927                         );
928
929                         star.normalize();
930                         m_stars.emplace_back(star);
931                 }
932         }
933 }
934
935 void Sky::setSkyColors(const SkyboxParams sky)
936 {
937         m_sky_params.sky_color = sky.sky_color;
938 }
939
940 void Sky::setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
941                 std::string use_sun_tint)
942 {
943         // Change sun and moon tinting:
944         m_sky_params.fog_sun_tint = sun_tint;
945         m_sky_params.fog_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;
951         else
952                 m_default_tint = true;
953 }
954
955 void Sky::addTextureToSkybox(std::string texture, int material_id,
956                 ITextureSource *tsrc)
957 {
958         // Sanity check for more than six textures.
959         if (material_id + 5 >= SKY_MATERIAL_COUNT)
960                 return;
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;
967 }
968
969 // To be called once at game init to setup default values.
970 void Sky::setSkyDefaults()
971 {
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();
977 }