Skip to content

Commit 848f80b

Browse files
MirceaKitsunesapier
authored and
sapier
committedDec 15, 2013
Directional fog + horizon colors, based on sun & moon positions at sunrise / sunset
1 parent e9e9fd7 commit 848f80b

File tree

3 files changed

+96
-24
lines changed

3 files changed

+96
-24
lines changed
 

Diff for: ‎src/game.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ void the_game(
13181318
*/
13191319

13201320
Sky *sky = NULL;
1321-
sky = new Sky(smgr->getRootSceneNode(), smgr, -1);
1321+
sky = new Sky(smgr->getRootSceneNode(), smgr, -1, client.getEnv().getLocalPlayer());
13221322

13231323
/*
13241324
A copy of the local inventory

Diff for: ‎src/sky.cpp

+56-21
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
#include "main.h" // g_profiler
99
#include "profiler.h"
1010
#include "util/numeric.h" // MYMIN
11+
#include <cmath>
1112

1213
//! constructor
13-
Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id):
14+
Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id, LocalPlayer* player):
1415
scene::ISceneNode(parent, mgr, id),
1516
m_first_update(true),
1617
m_brightness(0.5),
1718
m_cloud_brightness(0.5),
1819
m_bgcolor_bright_f(1,1,1,1),
1920
m_skycolor_bright_f(1,1,1,1),
20-
m_cloudcolor_bright_f(1,1,1,1)
21+
m_cloudcolor_bright_f(1,1,1,1),
22+
m_player(player)
2123
{
2224
setAutomaticCulling(scene::EAC_OFF);
2325
Box.MaxEdge.set(0,0,0);
@@ -431,7 +433,8 @@ void Sky::update(float time_of_day, float time_brightness,
431433
video::SColor(255, 240,240,255);
432434
//video::SColorf cloudcolor_bright_dawn_f(1.0, 0.591, 0.4);
433435
//video::SColorf cloudcolor_bright_dawn_f(1.0, 0.65, 0.44);
434-
video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
436+
//video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
437+
video::SColorf cloudcolor_bright_dawn_f(1.0, 0.875, 0.75);
435438

436439
float cloud_color_change_fraction = 0.95;
437440
if(sunlight_seen){
@@ -470,41 +473,73 @@ void Sky::update(float time_of_day, float time_brightness,
470473
} else {
471474
m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
472475
bgcolor_bright_indoor_f, color_change_fraction);
473-
m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
474-
cloudcolor_bright_normal_f, color_change_fraction);
475476
m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
476477
bgcolor_bright_indoor_f, color_change_fraction);
478+
m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
479+
cloudcolor_bright_normal_f, color_change_fraction);
477480
m_clouds_visible = false;
478481
}
482+
483+
// Horizon coloring based on sun and moon direction during sunset and sunrise
484+
video::SColor pointcolor = video::SColor(255, 255, 255, m_bgcolor.getAlpha());
485+
if (m_horizon_blend() != 0)
486+
{
487+
// calculate hemisphere value from yaw
488+
f32 pointcolor_blend = wrapDegrees_0_360(m_player->getYaw() + 90);
489+
if (pointcolor_blend > 180)
490+
pointcolor_blend = 360 - pointcolor_blend;
491+
pointcolor_blend /= 180;
492+
// bound view angle to determine where transition starts and ends
493+
pointcolor_blend = rangelim(1 - pointcolor_blend * 1.375, 0, 1 / 1.375) * 1.375;
494+
// combine the colors when looking up or down, otherwise turning looks weird
495+
pointcolor_blend += (0.5 - pointcolor_blend) * (1 - MYMIN((90 - std::abs(m_player->getPitch())) / 90 * 1.5, 1));
496+
// invert direction to match where the sun and moon are rising
497+
if (m_time_of_day > 0.5)
498+
pointcolor_blend = 1 - pointcolor_blend;
499+
500+
// horizon colors of sun and moon
501+
f32 pointcolor_light = rangelim(m_time_brightness * 3, 0.2, 1);
502+
video::SColorf pointcolor_sun_f(1, 1, 1, 1);
503+
pointcolor_sun_f.r = pointcolor_light * 1;
504+
pointcolor_sun_f.b = pointcolor_light * (0.25 + (rangelim(m_time_brightness, 0.25, 0.75) - 0.25) * 2 * 0.75);
505+
pointcolor_sun_f.g = pointcolor_light * (pointcolor_sun_f.b * 0.375 + (rangelim(m_time_brightness, 0.05, 0.15) - 0.05) * 10 * 0.625);
506+
video::SColorf pointcolor_moon_f(0.5 * pointcolor_light, 0.6 * pointcolor_light, 0.8 * pointcolor_light, 1);
507+
video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
508+
video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
509+
// calculate the blend color
510+
pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
511+
}
512+
479513
video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
480514
m_bgcolor = video::SColor(
481-
255,
482-
bgcolor_bright.getRed() * m_brightness,
483-
bgcolor_bright.getGreen() * m_brightness,
484-
bgcolor_bright.getBlue() * m_brightness);
485-
515+
255,
516+
bgcolor_bright.getRed() * m_brightness,
517+
bgcolor_bright.getGreen() * m_brightness,
518+
bgcolor_bright.getBlue() * m_brightness);
519+
m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
520+
486521
video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
487522
m_skycolor = video::SColor(
488-
255,
489-
skycolor_bright.getRed() * m_brightness,
490-
skycolor_bright.getGreen() * m_brightness,
491-
skycolor_bright.getBlue() * m_brightness);
492-
523+
255,
524+
skycolor_bright.getRed() * m_brightness,
525+
skycolor_bright.getGreen() * m_brightness,
526+
skycolor_bright.getBlue() * m_brightness);
527+
m_skycolor = m_mix_scolor(m_skycolor, pointcolor, m_horizon_blend() * 0.25);
528+
493529
float cloud_direct_brightness = 0;
494530
if(sunlight_seen){
495-
cloud_direct_brightness = time_brightness;
496-
if(time_brightness >= 0.2 && time_brightness < 0.7)
497-
cloud_direct_brightness *= 1.3;
531+
cloud_direct_brightness = MYMIN(m_horizon_blend() * 0.15 + m_time_brightness, 1);
498532
} else {
499533
cloud_direct_brightness = direct_brightness;
500534
}
501535
m_cloud_brightness = m_cloud_brightness * cloud_color_change_fraction +
502536
cloud_direct_brightness * (1.0 - cloud_color_change_fraction);
503537
m_cloudcolor_f = video::SColorf(
504-
m_cloudcolor_bright_f.getRed() * m_cloud_brightness,
505-
m_cloudcolor_bright_f.getGreen() * m_cloud_brightness,
506-
m_cloudcolor_bright_f.getBlue() * m_cloud_brightness,
538+
m_cloudcolor_bright_f.r * m_cloud_brightness,
539+
m_cloudcolor_bright_f.g * m_cloud_brightness,
540+
m_cloudcolor_bright_f.b * m_cloud_brightness,
507541
1.0);
542+
m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f, video::SColorf(pointcolor), m_horizon_blend() * 0.75);
508543

509544
}
510545

Diff for: ‎src/sky.h

+39-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1919

2020
#include "irrlichttypes_extrabloated.h"
2121
#include <ISceneNode.h>
22+
#include "localplayer.h"
2223

2324
#ifndef SKY_HEADER
2425
#define SKY_HEADER
@@ -31,7 +32,7 @@ class Sky : public scene::ISceneNode
3132
{
3233
public:
3334
//! constructor
34-
Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id);
35+
Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id, LocalPlayer* player);
3536

3637
virtual void OnRegisterSceneNode();
3738

@@ -61,7 +62,42 @@ class Sky : public scene::ISceneNode
6162
private:
6263
core::aabbox3d<f32> Box;
6364
video::SMaterial m_materials[SKY_MATERIAL_COUNT];
64-
65+
66+
// How much sun & moon transition should affect horizon color
67+
float m_horizon_blend()
68+
{
69+
if (!m_sunlight_seen)
70+
return 0;
71+
float x; m_time_of_day >= 0.5 ? x = (1 - m_time_of_day) * 2 : x = m_time_of_day * 2;
72+
if (x <= 0.3)
73+
return 0;
74+
if (x <= 0.4) // when the sun and moon are aligned
75+
return (x - 0.3) * 10;
76+
if (x <= 0.5)
77+
return (0.5 - x) * 10;
78+
return 0;
79+
}
80+
81+
// Mix two colors by a given amount
82+
video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
83+
{
84+
video::SColor result = video::SColor(
85+
col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor,
86+
col1.getRed() * (1 - factor) + col2.getRed() * factor,
87+
col1.getGreen() * (1 - factor) + col2.getGreen() * factor,
88+
col1.getBlue() * (1 - factor) + col2.getBlue() * factor);
89+
return result;
90+
}
91+
video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
92+
{
93+
video::SColorf result = video::SColorf(
94+
col1.r * (1 - factor) + col2.r * factor,
95+
col1.g * (1 - factor) + col2.g * factor,
96+
col1.b * (1 - factor) + col2.b * factor,
97+
col1.a * (1 - factor) + col2.a * factor);
98+
return result;
99+
}
100+
65101
bool m_first_update;
66102
float m_time_of_day;
67103
float m_time_brightness;
@@ -78,6 +114,7 @@ class Sky : public scene::ISceneNode
78114
v3f m_stars[SKY_STAR_COUNT];
79115
u16 m_star_indices[SKY_STAR_COUNT*4];
80116
video::S3DVertex m_star_vertices[SKY_STAR_COUNT*4];
117+
LocalPlayer* m_player;
81118
};
82119

83120
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.