Translated using Weblate (Chinese (Simplified))
[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                                 vertices[0] = video::S3DVertex(-1, -0.02, -1, 0, 0, 1, m_bgcolor, t, t);
256                                 vertices[1] = video::S3DVertex( 1, -0.02, -1, 0, 0, 1, m_bgcolor, o, t);
257                                 vertices[2] = video::S3DVertex( 1, 0.45, -1, 0, 0, 1, m_skycolor, o, o);
258                                 vertices[3] = video::S3DVertex(-1, 0.45, -1, 0, 0, 1, m_skycolor, t, o);
259                                 for (video::S3DVertex &vertex : vertices) {
260                                         if (j == 0)
261                                                 // Don't switch
262                                                 {}
263                                         else if (j == 1)
264                                                 // Switch from -Z (south) to +X (east)
265                                                 vertex.Pos.rotateXZBy(90);
266                                         else if (j == 2)
267                                                 // Switch from -Z (south) to -X (west)
268                                                 vertex.Pos.rotateXZBy(-90);
269                                         else
270                                                 // Switch from -Z (south) to +Z (north)
271                                                 vertex.Pos.rotateXZBy(-180);
272                                 }
273                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
274                         }
275                 }
276
277                 // Draw stars before sun and moon to be behind them
278                 if (m_star_params.visible)
279                         draw_stars(driver, wicked_time_of_day);
280
281                 // Draw sunrise/sunset horizon glow texture
282                 // (textures/base/pack/sunrisebg.png)
283                 if (m_sun_params.sunrise_visible) {
284                         driver->setMaterial(m_materials[2]);
285                         float mid1 = 0.25;
286                         float mid = wicked_time_of_day < 0.5 ? mid1 : (1.0 - mid1);
287                         float a_ = 1.0f - std::fabs(wicked_time_of_day - mid) * 35.0f;
288                         float a = easeCurve(MYMAX(0, MYMIN(1, a_)));
289                         //std::cerr<<"a_="<<a_<<" a="<<a<<std::endl;
290                         video::SColor c(255, 255, 255, 255);
291                         float y = -(1.0 - a) * 0.22;
292                         vertices[0] = video::S3DVertex(-1, -0.05 + y, -1, 0, 0, 1, c, t, t);
293                         vertices[1] = video::S3DVertex( 1, -0.05 + y, -1, 0, 0, 1, c, o, t);
294                         vertices[2] = video::S3DVertex( 1,   0.2 + y, -1, 0, 0, 1, c, o, o);
295                         vertices[3] = video::S3DVertex(-1,   0.2 + y, -1, 0, 0, 1, c, t, o);
296                         for (video::S3DVertex &vertex : vertices) {
297                                 if (wicked_time_of_day < 0.5)
298                                         // Switch from -Z (south) to +X (east)
299                                         vertex.Pos.rotateXZBy(90);
300                                 else
301                                         // Switch from -Z (south) to -X (west)
302                                         vertex.Pos.rotateXZBy(-90);
303                         }
304                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
305                 }
306
307                 // Draw sun
308                 if (m_sun_params.visible)
309                         draw_sun(driver, sunsize, suncolor, suncolor2, wicked_time_of_day);
310
311                 // Draw moon
312                 if (m_moon_params.visible)
313                         draw_moon(driver, moonsize, mooncolor, mooncolor2, wicked_time_of_day);
314
315                 // Draw far cloudy fog thing below all horizons in front of sun, moon
316                 // and stars.
317                 if (m_visible) {
318                         driver->setMaterial(m_materials[1]);
319
320                         for (u32 j = 0; j < 4; j++) {
321                                 video::SColor c = cloudyfogcolor;
322                                 vertices[0] = video::S3DVertex(-1, -1.0,  -1, 0, 0, 1, c, t, t);
323                                 vertices[1] = video::S3DVertex( 1, -1.0,  -1, 0, 0, 1, c, o, t);
324                                 vertices[2] = video::S3DVertex( 1, -0.02, -1, 0, 0, 1, c, o, o);
325                                 vertices[3] = video::S3DVertex(-1, -0.02, -1, 0, 0, 1, c, t, o);
326                                 for (video::S3DVertex &vertex : vertices) {
327                                         if (j == 0)
328                                                 // Don't switch
329                                                 {}
330                                         else if (j == 1)
331                                                 // Switch from -Z (south) to +X (east)
332                                                 vertex.Pos.rotateXZBy(90);
333                                         else if (j == 2)
334                                                 // Switch from -Z (south) to -X (west)
335                                                 vertex.Pos.rotateXZBy(-90);
336                                         else
337                                                 // Switch from -Z (south) to +Z (north)
338                                                 vertex.Pos.rotateXZBy(-180);
339                                 }
340                                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
341                         }
342
343                         // Draw bottom far cloudy fog thing in front of sun, moon and stars
344                         video::SColor c = cloudyfogcolor;
345                         vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 1, 0, c, t, t);
346                         vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 1, 0, c, o, t);
347                         vertices[2] = video::S3DVertex( 1, -1.0, 1, 0, 1, 0, c, o, o);
348                         vertices[3] = video::S3DVertex(-1, -1.0, 1, 0, 1, 0, c, t, o);
349                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
350                 }
351         }
352 }
353
354 void Sky::update(float time_of_day, float time_brightness,
355         float direct_brightness, bool sunlight_seen,
356         CameraMode cam_mode, float yaw, float pitch)
357 {
358         // Stabilize initial brightness and color values by flooding updates
359         if (m_first_update) {
360                 /*dstream<<"First update with time_of_day="<<time_of_day
361                                 <<" time_brightness="<<time_brightness
362                                 <<" direct_brightness="<<direct_brightness
363                                 <<" sunlight_seen="<<sunlight_seen<<std::endl;*/
364                 m_first_update = false;
365                 for (u32 i = 0; i < 100; i++) {
366                         update(time_of_day, time_brightness, direct_brightness,
367                                         sunlight_seen, cam_mode, yaw, pitch);
368                 }
369                 return;
370         }
371
372         m_time_of_day = time_of_day;
373         m_time_brightness = time_brightness;
374         m_sunlight_seen = sunlight_seen;
375         m_in_clouds = false;
376
377         bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
378
379         /*
380         Development colours
381
382         video::SColorf bgcolor_bright_normal_f(170. / 255, 200. / 255, 230. / 255, 1.0);
383         video::SColorf bgcolor_bright_dawn_f(0.666, 200. / 255 * 0.7, 230. / 255 * 0.5, 1.0);
384         video::SColorf bgcolor_bright_dawn_f(0.666, 0.549, 0.220, 1.0);
385         video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.0, 1.0);
386         video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.2, 1.0);
387
388         video::SColorf cloudcolor_bright_dawn_f(1.0, 0.591, 0.4);
389         video::SColorf cloudcolor_bright_dawn_f(1.0, 0.65, 0.44);
390         video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
391         */
392
393         video::SColorf bgcolor_bright_normal_f = m_sky_params.sky_color.day_horizon;
394         video::SColorf bgcolor_bright_indoor_f = m_sky_params.sky_color.indoors;
395         video::SColorf bgcolor_bright_dawn_f = m_sky_params.sky_color.dawn_horizon;
396         video::SColorf bgcolor_bright_night_f = m_sky_params.sky_color.night_horizon;
397
398         video::SColorf skycolor_bright_normal_f = m_sky_params.sky_color.day_sky;
399         video::SColorf skycolor_bright_dawn_f = m_sky_params.sky_color.dawn_sky;
400         video::SColorf skycolor_bright_night_f = m_sky_params.sky_color.night_sky;
401
402         video::SColorf cloudcolor_bright_normal_f = m_cloudcolor_day_f;
403         video::SColorf cloudcolor_bright_dawn_f = m_cloudcolor_dawn_f;
404
405         float cloud_color_change_fraction = 0.95;
406         if (sunlight_seen) {
407                 if (std::fabs(time_brightness - m_brightness) < 0.2f) {
408                         m_brightness = m_brightness * 0.95 + time_brightness * 0.05;
409                 } else {
410                         m_brightness = m_brightness * 0.80 + time_brightness * 0.20;
411                         cloud_color_change_fraction = 0.0;
412                 }
413         } else {
414                 if (direct_brightness < m_brightness)
415                         m_brightness = m_brightness * 0.95 + direct_brightness * 0.05;
416                 else
417                         m_brightness = m_brightness * 0.98 + direct_brightness * 0.02;
418         }
419
420         m_clouds_visible = true;
421         float color_change_fraction = 0.98f;
422         if (sunlight_seen) {
423                 if (is_dawn) { // Dawn
424                         m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
425                                 bgcolor_bright_dawn_f, color_change_fraction);
426                         m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
427                                 skycolor_bright_dawn_f, color_change_fraction);
428                         m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
429                                 cloudcolor_bright_dawn_f, color_change_fraction);
430                 } else {
431                         if (time_brightness < 0.13f) { // Night
432                                 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
433                                         bgcolor_bright_night_f, color_change_fraction);
434                                 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
435                                         skycolor_bright_night_f, color_change_fraction);
436                         } else { // Day
437                                 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
438                                         bgcolor_bright_normal_f, color_change_fraction);
439                                 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
440                                         skycolor_bright_normal_f, color_change_fraction);
441                         }
442
443                         m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
444                                 cloudcolor_bright_normal_f, color_change_fraction);
445                 }
446         } else {
447                 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
448                         bgcolor_bright_indoor_f, color_change_fraction);
449                 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
450                         bgcolor_bright_indoor_f, color_change_fraction);
451                 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
452                         cloudcolor_bright_normal_f, color_change_fraction);
453                 m_clouds_visible = false;
454         }
455
456         video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
457         m_bgcolor = video::SColor(
458                 255,
459                 bgcolor_bright.getRed() * m_brightness,
460                 bgcolor_bright.getGreen() * m_brightness,
461                 bgcolor_bright.getBlue() * m_brightness
462         );
463
464         video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
465         m_skycolor = video::SColor(
466                 255,
467                 skycolor_bright.getRed() * m_brightness,
468                 skycolor_bright.getGreen() * m_brightness,
469                 skycolor_bright.getBlue() * m_brightness
470         );
471
472         // Horizon coloring based on sun and moon direction during sunset and sunrise
473         video::SColor pointcolor = video::SColor(m_bgcolor.getAlpha(), 255, 255, 255);
474         if (m_directional_colored_fog) {
475                 if (m_horizon_blend() != 0) {
476                         // Calculate hemisphere value from yaw, (inverted in third person front view)
477                         s8 dir_factor = 1;
478                         if (cam_mode > CAMERA_MODE_THIRD)
479                                 dir_factor = -1;
480                         f32 pointcolor_blend = wrapDegrees_0_360(yaw * dir_factor + 90);
481                         if (pointcolor_blend > 180)
482                                 pointcolor_blend = 360 - pointcolor_blend;
483                         pointcolor_blend /= 180;
484                         // Bound view angle to determine where transition starts and ends
485                         pointcolor_blend = rangelim(1 - pointcolor_blend * 1.375, 0, 1 / 1.375) *
486                                 1.375;
487                         // Combine the colors when looking up or down, otherwise turning looks weird
488                         pointcolor_blend += (0.5 - pointcolor_blend) *
489                                 (1 - MYMIN((90 - std::fabs(pitch)) / 90 * 1.5, 1));
490                         // Invert direction to match where the sun and moon are rising
491                         if (m_time_of_day > 0.5)
492                                 pointcolor_blend = 1 - pointcolor_blend;
493                         // Horizon colors of sun and moon
494                         f32 pointcolor_light = rangelim(m_time_brightness * 3, 0.2, 1);
495
496                         video::SColorf pointcolor_sun_f(1, 1, 1, 1);
497                         // Use tonemap only if default sun/moon tinting is used
498                         // which keeps previous behaviour.
499                         if (m_sun_tonemap && m_default_tint) {
500                                 pointcolor_sun_f.r = pointcolor_light *
501                                         (float)m_materials[3].EmissiveColor.getRed() / 255;
502                                 pointcolor_sun_f.b = pointcolor_light *
503                                         (float)m_materials[3].EmissiveColor.getBlue() / 255;
504                                 pointcolor_sun_f.g = pointcolor_light *
505                                         (float)m_materials[3].EmissiveColor.getGreen() / 255;
506                         } else if (!m_default_tint) {
507                                 pointcolor_sun_f = m_sky_params.fog_sun_tint;
508                         } else {
509                                 pointcolor_sun_f.r = pointcolor_light * 1;
510                                 pointcolor_sun_f.b = pointcolor_light *
511                                         (0.25 + (rangelim(m_time_brightness, 0.25, 0.75) - 0.25) * 2 * 0.75);
512                                 pointcolor_sun_f.g = pointcolor_light * (pointcolor_sun_f.b * 0.375 +
513                                         (rangelim(m_time_brightness, 0.05, 0.15) - 0.05) * 10 * 0.625);
514                         }
515
516                         video::SColorf pointcolor_moon_f;
517                         if (m_default_tint) {
518                                 pointcolor_moon_f = video::SColorf(
519                                         0.5 * pointcolor_light,
520                                         0.6 * pointcolor_light,
521                                         0.8 * pointcolor_light,
522                                         1
523                                 );
524                         } else {
525                                 pointcolor_moon_f = video::SColorf(
526                                         (m_sky_params.fog_moon_tint.getRed() / 255) * pointcolor_light,
527                                         (m_sky_params.fog_moon_tint.getGreen() / 255) * pointcolor_light,
528                                         (m_sky_params.fog_moon_tint.getBlue() / 255) * pointcolor_light,
529                                         1
530                                 );
531                         }
532                         if (m_moon_tonemap && m_default_tint) {
533                                 pointcolor_moon_f.r = pointcolor_light *
534                                         (float)m_materials[4].EmissiveColor.getRed() / 255;
535                                 pointcolor_moon_f.b = pointcolor_light *
536                                         (float)m_materials[4].EmissiveColor.getBlue() / 255;
537                                 pointcolor_moon_f.g = pointcolor_light *
538                                         (float)m_materials[4].EmissiveColor.getGreen() / 255;
539                         }
540
541                         video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
542                         video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
543                         // Calculate the blend color
544                         pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
545                 }
546                 m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
547                 m_skycolor = m_mix_scolor(m_skycolor, pointcolor, m_horizon_blend() * 0.25);
548         }
549
550         float cloud_direct_brightness = 0.0f;
551         if (sunlight_seen) {
552                 if (!m_directional_colored_fog) {
553                         cloud_direct_brightness = time_brightness;
554                         // Boost cloud brightness relative to sky, at dawn, dusk and at night
555                         if (time_brightness < 0.7f)
556                                 cloud_direct_brightness *= 1.3f;
557                 } else {
558                         cloud_direct_brightness = std::fmin(m_horizon_blend() * 0.15f +
559                                 m_time_brightness, 1.0f);
560                         // Set the same minimum cloud brightness at night
561                         if (time_brightness < 0.5f)
562                                 cloud_direct_brightness = std::fmax(cloud_direct_brightness,
563                                         time_brightness * 1.3f);
564                 }
565         } else {
566                 cloud_direct_brightness = direct_brightness;
567         }
568
569         m_cloud_brightness = m_cloud_brightness * cloud_color_change_fraction +
570                 cloud_direct_brightness * (1.0 - cloud_color_change_fraction);
571         m_cloudcolor_f = video::SColorf(
572                 m_cloudcolor_bright_f.r * m_cloud_brightness,
573                 m_cloudcolor_bright_f.g * m_cloud_brightness,
574                 m_cloudcolor_bright_f.b * m_cloud_brightness,
575                 1.0
576         );
577         if (m_directional_colored_fog) {
578                 m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f,
579                         video::SColorf(pointcolor), m_horizon_blend() * 0.25);
580         }
581 }
582
583 void Sky::draw_sun(video::IVideoDriver *driver, float sunsize, const video::SColor &suncolor,
584         const video::SColor &suncolor2, float wicked_time_of_day)
585         /* Draw sun in the sky.
586          * driver: Video driver object used to draw
587          * sunsize: the default size of the sun
588          * suncolor: main sun color
589          * suncolor2: second sun color
590          * wicked_time_of_day: current time of day, to know where should be the sun in the sky
591          */
592 {
593         static const u16 indices[4] = {0, 1, 2, 3};
594         std::array<video::S3DVertex, 4> vertices;
595         if (!m_sun_texture) {
596                 driver->setMaterial(m_materials[1]);
597                 const float sunsizes[4] = {
598                         (sunsize * 1.7f) * m_sun_params.scale,
599                         (sunsize * 1.2f) * m_sun_params.scale,
600                         (sunsize) * m_sun_params.scale,
601                         (sunsize * 0.7f) * m_sun_params.scale
602                 };
603                 video::SColor c1 = suncolor;
604                 video::SColor c2 = suncolor;
605                 c1.setAlpha(0.05 * 255);
606                 c2.setAlpha(0.15 * 255);
607                 const video::SColor colors[4] = {c1, c2, suncolor, suncolor2};
608                 for (int i = 0; i < 4; i++) {
609                         draw_sky_body(vertices, -sunsizes[i], sunsizes[i], colors[i]);
610                         place_sky_body(vertices, 90, wicked_time_of_day * 360 - 90);
611                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
612                 }
613         } else {
614                 driver->setMaterial(m_materials[3]);
615                 float d = (sunsize * 1.7) * m_sun_params.scale;
616                 video::SColor c;
617                 if (m_sun_tonemap)
618                         c = video::SColor(0, 0, 0, 0);
619                 else
620                         c = video::SColor(255, 255, 255, 255);
621                 draw_sky_body(vertices, -d, d, c);
622                 place_sky_body(vertices, 90, wicked_time_of_day * 360 - 90);
623                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
624         }
625 }
626
627
628 void Sky::draw_moon(video::IVideoDriver *driver, float moonsize, const video::SColor &mooncolor,
629         const video::SColor &mooncolor2, float wicked_time_of_day)
630 /*
631         * Draw moon in the sky.
632         * driver: Video driver object used to draw
633         * moonsize: the default size of the moon
634         * mooncolor: main moon color
635         * mooncolor2: second moon color
636         * wicked_time_of_day: current time of day, to know where should be the moon in
637         * the sky
638         */
639 {
640         static const u16 indices[4] = {0, 1, 2, 3};
641         std::array<video::S3DVertex, 4> vertices;
642         if (!m_moon_texture) {
643                 driver->setMaterial(m_materials[1]);
644                 const float moonsizes_1[4] = {
645                         (-moonsize * 1.9f) * m_moon_params.scale,
646                         (-moonsize * 1.3f) * m_moon_params.scale,
647                         (-moonsize) * m_moon_params.scale,
648                         (-moonsize) * m_moon_params.scale
649                 };
650                 const float moonsizes_2[4] = {
651                         (moonsize * 1.9f) * m_moon_params.scale,
652                         (moonsize * 1.3f) * m_moon_params.scale,
653                         (moonsize) *m_moon_params.scale,
654                         (moonsize * 0.6f) * m_moon_params.scale
655                 };
656                 video::SColor c1 = mooncolor;
657                 video::SColor c2 = mooncolor;
658                 c1.setAlpha(0.05 * 255);
659                 c2.setAlpha(0.15 * 255);
660                 const video::SColor colors[4] = {c1, c2, mooncolor, mooncolor2};
661                 for (int i = 0; i < 4; i++) {
662                         draw_sky_body(vertices, moonsizes_1[i], moonsizes_2[i], colors[i]);
663                         place_sky_body(vertices, -90, wicked_time_of_day * 360 - 90);
664                         driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
665                 }
666         } else {
667                 driver->setMaterial(m_materials[4]);
668                 float d = (moonsize * 1.9) * m_moon_params.scale;
669                 video::SColor c;
670                 if (m_moon_tonemap)
671                         c = video::SColor(0, 0, 0, 0);
672                 else
673                         c = video::SColor(255, 255, 255, 255);
674                 draw_sky_body(vertices, -d, d, c);
675                 place_sky_body(vertices, -90, wicked_time_of_day * 360 - 90);
676                 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
677         }
678 }
679
680 void Sky::draw_stars(video::IVideoDriver * driver, float wicked_time_of_day)
681 {
682         driver->setMaterial(m_materials[1]);
683         // Tune values so that stars first appear just after the sun
684         // disappears over the horizon, and disappear just before the sun
685         // appears over the horizon.
686         // Also tune so that stars are at full brightness from time 20000
687         // to time 4000.
688
689         float tod = wicked_time_of_day < 0.5f ? wicked_time_of_day : (1.0f - wicked_time_of_day);
690         float starbrightness = clamp((0.25f - fabsf(tod)) * 20.0f, 0.0f, 1.0f);
691
692         float f = starbrightness;
693         float d = (0.006 / 2) * m_star_params.scale;
694
695         video::SColor starcolor = m_star_params.starcolor;
696         starcolor.setAlpha(f * m_star_params.starcolor.getAlpha());
697
698         // Stars are only drawn when not fully transparent
699         if (m_star_params.starcolor.getAlpha() < 1)
700                 return;
701 #if ENABLE_GLES
702         u16 *indices = new u16[m_star_params.count * 3];
703         video::S3DVertex *vertices =
704                         new video::S3DVertex[m_star_params.count * 3];
705         for (u32 i = 0; i < m_star_params.count; i++) {
706                 indices[i * 3 + 0] = i * 3 + 0;
707                 indices[i * 3 + 1] = i * 3 + 1;
708                 indices[i * 3 + 2] = i * 3 + 2;
709                 v3f r = m_stars[i];
710                 core::CMatrix4<f32> a;
711                 a.buildRotateFromTo(v3f(0, 1, 0), r);
712                 v3f p = v3f(-d, 1, -d);
713                 v3f p1 = v3f(d, 1, 0);
714                 v3f p2 = v3f(-d, 1, d);
715                 a.rotateVect(p);
716                 a.rotateVect(p1);
717                 a.rotateVect(p2);
718                 p.rotateXYBy(wicked_time_of_day * 360 - 90);
719                 p1.rotateXYBy(wicked_time_of_day * 360 - 90);
720                 p2.rotateXYBy(wicked_time_of_day * 360 - 90);
721                 vertices[i * 3 + 0].Pos = p;
722                 vertices[i * 3 + 0].Color = starcolor;
723                 vertices[i * 3 + 1].Pos = p1;
724                 vertices[i * 3 + 1].Color = starcolor;
725                 vertices[i * 3 + 2].Pos = p2;
726                 vertices[i * 3 + 2].Color = starcolor;
727         }
728         driver->drawIndexedTriangleList(vertices, m_star_params.count * 3,
729                         indices, m_star_params.count);
730         delete[] indices;
731         delete[] vertices;
732 #else
733         u16 *indices = new u16[m_star_params.count * 4];
734         video::S3DVertex *vertices =
735                         new video::S3DVertex[m_star_params.count * 4];
736         for (u32 i = 0; i < m_star_params.count; i++) {
737                 indices[i * 4 + 0] = i * 4 + 0;
738                 indices[i * 4 + 1] = i * 4 + 1;
739                 indices[i * 4 + 2] = i * 4 + 2;
740                 indices[i * 4 + 3] = i * 4 + 3;
741                 v3f r = m_stars[i];
742                 core::CMatrix4<f32> a;
743                 a.buildRotateFromTo(v3f(0, 1, 0), r);
744                 v3f p = v3f(-d, 1, -d);
745                 v3f p1 = v3f(d, 1, -d);
746                 v3f p2 = v3f(d, 1, d);
747                 v3f p3 = v3f(-d, 1, d);
748                 a.rotateVect(p);
749                 a.rotateVect(p1);
750                 a.rotateVect(p2);
751                 a.rotateVect(p3);
752                 p.rotateXYBy(wicked_time_of_day * 360 - 90);
753                 p1.rotateXYBy(wicked_time_of_day * 360 - 90);
754                 p2.rotateXYBy(wicked_time_of_day * 360 - 90);
755                 p3.rotateXYBy(wicked_time_of_day * 360 - 90);
756                 vertices[i * 4 + 0].Pos = p;
757                 vertices[i * 4 + 0].Color = starcolor;
758                 vertices[i * 4 + 1].Pos = p1;
759                 vertices[i * 4 + 1].Color = starcolor;
760                 vertices[i * 4 + 2].Pos = p2;
761                 vertices[i * 4 + 2].Color = starcolor;
762                 vertices[i * 4 + 3].Pos = p3;
763                 vertices[i * 4 + 3].Color = starcolor;
764         }
765         driver->drawVertexPrimitiveList(vertices, m_star_params.count * 4,
766                         indices, m_star_params.count, video::EVT_STANDARD,
767                         scene::EPT_QUADS, video::EIT_16BIT);
768         delete[] indices;
769         delete[] vertices;
770 #endif
771 }
772
773 void Sky::draw_sky_body(std::array<video::S3DVertex, 4> &vertices, float pos_1, float pos_2, const video::SColor &c)
774 {
775         /*
776         * Create an array of vertices with the dimensions specified.
777         * pos_1, pos_2: position of the body's vertices
778         * c: color of the body
779         */
780
781         const f32 t = 1.0f;
782         const f32 o = 0.0f;
783         vertices[0] = video::S3DVertex(pos_1, pos_1, -1, 0, 0, 1, c, t, t);
784         vertices[1] = video::S3DVertex(pos_2, pos_1, -1, 0, 0, 1, c, o, t);
785         vertices[2] = video::S3DVertex(pos_2, pos_2, -1, 0, 0, 1, c, o, o);
786         vertices[3] = video::S3DVertex(pos_1, pos_2, -1, 0, 0, 1, c, t, o);
787 }
788
789
790 void Sky::place_sky_body(
791         std::array<video::S3DVertex, 4> &vertices, float horizon_position, float day_position)
792         /*
793         * Place body in the sky.
794         * vertices: The body as a rectangle of 4 vertices
795         * horizon_position: turn the body around the Y axis
796         * day_position: turn the body around the Z axis, to place it depending of the time of the day
797         */
798 {
799         for (video::S3DVertex &vertex : vertices) {
800                 // Body is directed to -Z (south) by default
801                 vertex.Pos.rotateXZBy(horizon_position);
802                 vertex.Pos.rotateXYBy(day_position);
803         }
804 }
805
806 void Sky::setSunTexture(std::string sun_texture,
807                 std::string sun_tonemap, ITextureSource *tsrc)
808 {
809         // Ignore matching textures (with modifiers) entirely,
810         // but lets at least update the tonemap before hand.
811         m_sun_params.tonemap = sun_tonemap;
812         m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ?
813                 tsrc->getTexture(m_sun_params.tonemap) : NULL;
814         m_materials[3].Lighting = !!m_sun_tonemap;
815
816         if (m_sun_params.texture == sun_texture)
817                 return;
818         m_sun_params.texture = sun_texture;
819
820         if (sun_texture != "") {
821                 // We want to ensure the texture exists first.
822                 m_sun_texture = tsrc->getTextureForMesh(m_sun_params.texture);
823
824                 if (m_sun_texture) {
825                         m_materials[3] = m_materials[0];
826                         m_materials[3].setTexture(0, m_sun_texture);
827                         m_materials[3].MaterialType = video::
828                                 EMT_TRANSPARENT_ALPHA_CHANNEL;
829                         // Disables texture filtering
830                         m_materials[3].setFlag(
831                                 video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
832                         m_materials[3].setFlag(
833                                 video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
834                         m_materials[3].setFlag(
835                                 video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
836                 }
837         } else {
838                 m_sun_texture = nullptr;
839         }
840 }
841
842 void Sky::setSunriseTexture(std::string sunglow_texture,
843                 ITextureSource* tsrc)
844 {
845         // Ignore matching textures (with modifiers) entirely.
846         if (m_sun_params.sunrise == sunglow_texture)
847                 return;
848         m_sun_params.sunrise = sunglow_texture;
849         m_materials[2].setTexture(0, tsrc->getTextureForMesh(
850                 sunglow_texture.empty() ? "sunrisebg.png" : sunglow_texture)
851         );
852 }
853
854 void Sky::setMoonTexture(std::string moon_texture,
855                 std::string moon_tonemap, ITextureSource *tsrc)
856 {
857         // Ignore matching textures (with modifiers) entirely,
858         // but lets at least update the tonemap before hand.
859         m_moon_params.tonemap = moon_tonemap;
860         m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ?
861                 tsrc->getTexture(m_moon_params.tonemap) : NULL;
862         m_materials[4].Lighting = !!m_moon_tonemap;
863
864         if (m_moon_params.texture == moon_texture)
865                 return;
866         m_moon_params.texture = moon_texture;
867
868         if (moon_texture != "") {
869                 // We want to ensure the texture exists first.
870                 m_moon_texture = tsrc->getTextureForMesh(m_moon_params.texture);
871
872                 if (m_moon_texture) {
873                         m_materials[4] = m_materials[0];
874                         m_materials[4].setTexture(0, m_moon_texture);
875                         m_materials[4].MaterialType = video::
876                                 EMT_TRANSPARENT_ALPHA_CHANNEL;
877                         // Disables texture filtering
878                         m_materials[4].setFlag(
879                                 video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
880                         m_materials[4].setFlag(
881                                 video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
882                         m_materials[4].setFlag(
883                                 video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
884                 }
885         } else {
886                 m_moon_texture = nullptr;
887         }
888 }
889
890 void Sky::setStarCount(u16 star_count, bool force_update)
891 {
892         // Allow force updating star count at game init.
893         if (m_star_params.count != star_count || force_update) {
894                 m_star_params.count = star_count;
895                 m_stars.clear();
896                 // Rebuild the stars surrounding the camera
897                 for (u16 i = 0; i < star_count; i++) {
898                         v3f star = v3f(
899                                 myrand_range(-10000, 10000),
900                                 myrand_range(-10000, 10000),
901                                 myrand_range(-10000, 10000)
902                         );
903
904                         star.normalize();
905                         m_stars.emplace_back(star);
906                 }
907         }
908 }
909
910 void Sky::setSkyColors(const SkyColor &sky_color)
911 {
912         m_sky_params.sky_color = sky_color;
913 }
914
915 void Sky::setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
916                 std::string use_sun_tint)
917 {
918         // Change sun and moon tinting:
919         m_sky_params.fog_sun_tint = sun_tint;
920         m_sky_params.fog_moon_tint = moon_tint;
921         // Faster than comparing strings every rendering frame
922         if (use_sun_tint == "default")
923                 m_default_tint = true;
924         else if (use_sun_tint == "custom")
925                 m_default_tint = false;
926         else
927                 m_default_tint = true;
928 }
929
930 void Sky::addTextureToSkybox(std::string texture, int material_id,
931                 ITextureSource *tsrc)
932 {
933         // Sanity check for more than six textures.
934         if (material_id + 5 >= SKY_MATERIAL_COUNT)
935                 return;
936         // Keep a list of texture names handy.
937         m_sky_params.textures.emplace_back(texture);
938         video::ITexture *result = tsrc->getTextureForMesh(texture);
939         m_materials[material_id+5] = m_materials[0];
940         m_materials[material_id+5].setTexture(0, result);
941         m_materials[material_id+5].MaterialType = video::EMT_SOLID;
942 }
943
944 // To be called once at game init to setup default values.
945 void Sky::setSkyDefaults()
946 {
947         SkyboxDefaults sky_defaults;
948         m_sky_params.sky_color = sky_defaults.getSkyColorDefaults();
949         m_sun_params = sky_defaults.getSunDefaults();
950         m_moon_params = sky_defaults.getMoonDefaults();
951         m_star_params = sky_defaults.getStarDefaults();
952 }