Browse Source

Add functions for tracking the sun and moon positions based on latitude, longitude, and time

master
C. J. Howard 4 years ago
parent
commit
b5883e0679
6 changed files with 335 additions and 93 deletions
  1. +1
    -0
      src/game/biome.hpp
  2. +3
    -3
      src/game/bootloader.cpp
  3. +2
    -1
      src/game/states/play-state.cpp
  4. +314
    -85
      src/game/systems/weather-system.cpp
  5. +9
    -4
      src/game/systems/weather-system.hpp
  6. +6
    -0
      src/resources/biome-loader.cpp

+ 1
- 0
src/game/biome.hpp View File

@ -28,6 +28,7 @@ class image;
struct biome
{
std::string name;
float2 coordinates; // Latitude and longitude, in radians
// Terrain
material* terrain_material;

+ 3
- 3
src/game/bootloader.cpp View File

@ -507,7 +507,7 @@ void setup_rendering(game_context* ctx)
ctx->overworld_bloom_pass->set_source_texture(ctx->framebuffer_hdr_color);
ctx->overworld_bloom_pass->set_brightness_threshold(1.0f);
ctx->overworld_bloom_pass->set_blur_iterations(5);
ctx->overworld_bloom_pass->set_enabled(true);
ctx->overworld_bloom_pass->set_enabled(false);
ctx->overworld_final_pass = new ::final_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer(), ctx->resource_manager);
ctx->overworld_final_pass->set_color_texture(ctx->framebuffer_hdr_color);
ctx->overworld_final_pass->set_bloom_texture(ctx->bloom_texture);
@ -1171,7 +1171,7 @@ void setup_controls(game_context* ctx)
(
[ctx, time_scale]()
{
ctx->weather_system->set_time_scale(time_scale * 100.0f);
ctx->weather_system->set_time_scale(time_scale * 500.0f);
}
);
ctx->control_system->get_fast_forward_control()->set_deactivated_callback
@ -1185,7 +1185,7 @@ void setup_controls(game_context* ctx)
(
[ctx, time_scale]()
{
ctx->weather_system->set_time_scale(time_scale * -100.0f);
ctx->weather_system->set_time_scale(time_scale * -500.0f);
}
);
ctx->control_system->get_rewind_control()->set_deactivated_callback

+ 2
- 1
src/game/states/play-state.cpp View File

@ -92,12 +92,13 @@ void play_state_enter(game_context* ctx)
sky_pass->set_sun_angular_radius(ctx->biome->sun_angular_radius);
sky_pass->set_sun_color(ctx->biome->sun_color * ctx->biome->sun_intensity);
ctx->weather_system->set_coordinates(ctx->biome->coordinates);
ctx->weather_system->set_time(2017, 8, 21, 6, 0, 0, -7.0);
ctx->weather_system->set_sky_palette(ctx->biome->sky_palette);
ctx->weather_system->set_sun_palette(ctx->biome->sun_palette);
ctx->weather_system->set_ambient_palette(ctx->biome->ambient_palette);
ctx->weather_system->set_moon_palette(ctx->biome->moon_palette);
ctx->weather_system->set_shadow_palette(ctx->biome->shadow_palette);
ctx->weather_system->set_time_of_day(6.0f * 60.0f * 60.0f);
resource_manager* resource_manager = ctx->resource_manager;
entt::registry& ecs_registry = *ctx->ecs_registry;

+ 314
- 85
src/game/systems/weather-system.cpp View File

@ -28,150 +28,279 @@
#include <cmath>
#include <iostream>
static constexpr float seconds_per_day = 24.0f * 60.0f * 60.0f;
static constexpr double hours_per_day = 24.0;
static constexpr double minutes_per_day = hours_per_day * 60.0;
static constexpr double seconds_per_day = minutes_per_day * 60.0;
weather_system::weather_system(entt::registry& registry):
entity_system(registry),
ambient_light(nullptr),
sun_light(nullptr),
moon_light(nullptr),
shadow_light(nullptr),
sky_pass(nullptr),
shadow_map_pass(nullptr),
material_pass(nullptr),
time_of_day(0.0f),
time_scale(1.0f),
sky_palette(nullptr),
shadow_palette(nullptr),
sun_direction{0.0f, -1.0f, 0.0f}
{}
void weather_system::update(double t, double dt)
/**
*
* @param year Gregorian year
* @param month Month (1 = January, 12 = December)
* @param day Day (1-31)
* @param time Universal time in decimal hours.
*/
static double julian_day(int year, int month, int day, double time)
{
set_time_of_day(time_of_day + dt * time_scale);
if (month < 3)
{
month += 12;
year -= 1;
}
double y = static_cast<double>(year);
double m = static_cast<double>(month);
double d = static_cast<double>(day);
return std::floor(365.25 * y) + std::floor(30.6001 * (m + 1.0)) - 15.0 + 1720996.5 + d + time;
}
void weather_system::set_ambient_light(::ambient_light* light)
void find_sun_ecliptic(double jd, double* longitude, double* latitude, double* distance)
{
this->ambient_light = light;
const double t = (jd - 2451545.0) / 36525.0;
const double m = 6.24 + 628.302 * t;
*longitude = 4.895048 + 628.331951 * t + (0.033417 - 0.000084 * t) * std::sin(m) + 0.000351 * std::sin(m * 2.0);
*latitude = 0.0;
*distance = 1.000140 - (0.016708 - 0.000042 * t) * std::cos(m) - 0.000141 * std::cos(m * 2.0);
}
void weather_system::set_sun_light(directional_light* light)
/**
* Calculates the ecliptic geocentric coordinates of the moon, given a Julian day.
*
* @param[in] jd Julian day.
* @param[out] longitude Ecliptic longitude of the moon, in radians.
* @param[out] latitude Ecliptic latitude of the moon, in radians.
* @param[out] distance Distance to the moon, in Earth radii.
* @return Array containing the ecliptic longitude and latitude of the moon, in radians.
*
* @see A Physically-Based Night Sky Model
*/
void find_moon_ecliptic(double jd, double* longitude, double* latitude, double* distance)
{
sun_light = light;
const double t = (jd - 2451545.0) / 36525.0;
const double l1 = 3.8104 + 8399.7091 * t;
const double m1 = 2.3554 + 8328.6911 * t;
const double m = 6.2300 + 628.3019 * t;
const double d = 5.1985 + 7771.3772 * t;
const double d2 = d * 2.0;
const double f = 1.6280 + 8433.4663 * t;
if (sky_pass)
{
sky_pass->set_sun_light(sun_light);
}
}
*longitude = l1
+ 0.1098 * std::sin(m1)
+ 0.0222 * std::sin(d2 - m1)
+ 0.0115 * std::sin(d2)
+ 0.0037 * std::sin(m1 * 2.0)
- 0.0032 * std::sin(m)
- 0.0020 * std::sin(d2)
+ 0.0010 * std::sin(d2 - m1 * 2.0)
+ 0.0010 * std::sin(d2 - m - m1)
+ 0.0009 * std::sin(d2 + m1)
+ 0.0008 * std::sin(d2 - m)
+ 0.0007 * std::sin(m1 - m)
- 0.0006 * std::sin(d)
- 0.0005 * std::sin(m + m1);
void weather_system::set_moon_light(directional_light* light)
{
moon_light = light;
*latitude = 0.0895 * sin(f)
+ 0.0049 * std::sin(m1 + f)
+ 0.0048 * std::sin(m1 - f)
+ 0.0030 * std::sin(d2 - f)
+ 0.0010 * std::sin(d2 + f - m1)
+ 0.0008 * std::sin(d2 - f - m1)
+ 0.0006 * std::sin(d2 + f);
if (sky_pass)
{
sky_pass->set_moon_light(moon_light);
}
*distance = 1.0 / (0.016593
+ 0.000904 * std::cos(m1)
+ 0.000166 * std::cos(d2 - m1)
+ 0.000137 * std::cos(d2)
+ 0.000049 * std::cos(m1 * 2.0)
+ 0.000015 * std::cos(d2 + m1)
+ 0.000009 * std::cos(d2 - m));
}
void weather_system::set_sky_pass(::sky_pass* pass)
void ecliptic_to_equatorial(double longitude, double latitude, double ecl, double* right_ascension, double* declination)
{
sky_pass = pass;
double eclip_x = std::cos(longitude) * std::cos(latitude);
double eclip_y = std::sin(longitude) * std::cos(latitude);
double eclip_z = std::sin(latitude);
if (sky_pass)
{
sky_pass->set_sun_light(sun_light);
sky_pass->set_moon_light(moon_light);
}
double equat_x = eclip_x;
double equat_y = eclip_y * std::cos(ecl) - eclip_z * std::sin(ecl);
double equat_z = eclip_y * std::sin(ecl) + eclip_z * std::cos(ecl);
*right_ascension = std::atan2(equat_y, equat_x);
*declination = std::atan2(equat_z, sqrt(equat_x * equat_x + equat_y * equat_y));
}
void weather_system::set_shadow_map_pass(::shadow_map_pass* pass)
void equatorial_to_horizontal(double right_ascension, double declination, double lmst, double latitude, double* azimuth, double* elevation)
{
shadow_map_pass = pass;
double hour_angle = lmst - right_ascension;
if (shadow_map_pass)
{
shadow_map_pass->set_light(shadow_light);
}
double x = std::cos(hour_angle) * std::cos(declination);
double y = std::sin(hour_angle) * std::cos(declination);
double z = std::sin(declination);
double horiz_x = x * std::cos(math::half_pi<double> - latitude) - z * std::sin(math::half_pi<double> - latitude);
double horiz_y = y;
double horiz_z = x * std::sin(math::half_pi<double> - latitude) + z * std::cos(math::half_pi<double> - latitude);
*azimuth = std::atan2(horiz_y, horiz_x) + math::pi<double>;
*elevation = std::atan2(horiz_z, std::sqrt(horiz_x * horiz_x + horiz_y * horiz_y));
}
void weather_system::set_material_pass(::material_pass* pass)
{
material_pass = pass;
if (material_pass)
{
material_pass->set_shadow_strength(0.75f);
}
/**
* Calculates the Greenwich mean sidereal time (GMST) from a Julian day.
*
* @param jd Julian day.
* @return GMST, in radians.
*/
static double jd_to_gmst(double jd)
{
return math::wrap_radians<double>(4.894961212 + 6.300388098 * (jd - 2451545.0));
}
void weather_system::set_time_of_day(float t)
{
time_of_day = std::fmod(seconds_per_day + fmod(t, seconds_per_day), seconds_per_day);
weather_system::weather_system(entt::registry& registry):
entity_system(registry),
ambient_light(nullptr),
sun_light(nullptr),
moon_light(nullptr),
shadow_light(nullptr),
sky_pass(nullptr),
shadow_map_pass(nullptr),
material_pass(nullptr),
time_scale(1.0f),
sky_palette(nullptr),
shadow_palette(nullptr),
sun_direction{0.0f, -1.0f, 0.0f},
coordinates{0.0f, 0.0f},
jd(0.0)
{}
void weather_system::update(double t, double dt)
{
jd += (dt * time_scale) / seconds_per_day;
const float latitude = coordinates[0];
const float longitude = coordinates[1];
//sun_azimuth = 0.0f;
//sun_elevation = (time_of_day / seconds_per_day) * math::two_pi<float> - math::half_pi<float>;
// Time correction
double tc = longitude / (math::two_pi<double> / 24.0);
//double pst_tc = -7.0;
double local_jd = jd + tc / 24.0 - 0.5;
double local_time = (local_jd - std::floor(local_jd)) * 24.0;
double hour = local_time;
float hour_angle = math::wrap_radians(time_of_day * (math::two_pi<float> / seconds_per_day) - math::pi<float>);
float declination = math::radians(0.0f);
float latitude = math::radians(0.0f);
sun_elevation = std::asin(std::sin(declination) * std::sin(latitude) + std::cos(declination) * std::cos(hour_angle) * std::cos(latitude));
sun_azimuth = std::acos((std::sin(declination) * std::cos(latitude) - std::cos(declination) * std::cos(hour_angle) * std::sin(latitude)) / std::cos(sun_elevation));
if (hour_angle < 0.0f)
// Calculate equation of time
//float eot_b = (360.0f / 365.0f) * (day_of_year - 81.0f);
//float eot = 9.87f * std::sin(eot_b * 2.0f) - 7.53f * std::cos(eot_b) - 1.5f * std::sin(eot_b);
// Calculate local mean sidereal time (LST)
//double tc = longitude / (math::two_pi<double> / 24.0); // Time correction
//double ut = local_time + tc; // Universal time
double gmst = jd_to_gmst(jd);
double lmst = gmst + longitude;
// Calculate sun position
//float local_solar_time = local_time;// + eot / 60.0f;
/*
float sun_declination = math::radians(23.45f) * std::sin((math::two_pi<float> / 365.0f) * (284.0f + day_of_year));
float sun_hour_angle = math::radians(15.0f) * (local_solar_time - 12.0f);
sun_elevation = std::asin(std::sin(sun_declination) * std::sin(latitude) + std::cos(sun_declination) * std::cos(sun_hour_angle) * std::cos(latitude));
sun_azimuth = std::acos((std::sin(sun_declination) * std::cos(latitude) - std::cos(sun_declination) * std::cos(sun_hour_angle) * std::sin(latitude)) / std::cos(sun_elevation));
if (sun_hour_angle > 0.0f)
sun_azimuth = math::two_pi<float> - sun_azimuth;
*/
//std::cout << "hour angle: " << math::degrees(hour_angle) << std::endl;
//std::cout << "azimuth: " << math::degrees(sun_azimuth) << std::endl;
//std::cout << "time: " << (time_of_day / 60.0f / 60.0f) << std::endl;
// J2000 day
double d = jd - 2451545.0;
math::quaternion<float> sun_azimuth_rotation = math::angle_axis(sun_azimuth, float3{0, 1, 0});
math::quaternion<float> sun_elevation_rotation = math::angle_axis(sun_elevation, float3{-1, 0, 0});
math::quaternion<float> sun_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation);
sun_direction = math::normalize(sun_rotation * float3{0, 0, -1});
// Obliquity of the ecliptic
double ecl = math::radians<double>(23.4393 - 3.563e-7 * d);
// Calculation sun position
double sun_longitude;
double sun_latitude;
double sun_distance;
double sun_right_ascension;
double sun_declination;
double sun_azimuth;
double sun_elevation;
find_sun_ecliptic(jd, &sun_longitude, &sun_latitude, &sun_distance);
ecliptic_to_equatorial(sun_longitude, sun_latitude, ecl, &sun_right_ascension, &sun_declination);
equatorial_to_horizontal(sun_right_ascension, sun_declination, lmst, latitude, &sun_azimuth, &sun_elevation);
// Calculate moon position
double moon_longitude;
double moon_latitude;
double moon_distance;
double moon_right_ascension;
double moon_declination;
double moon_azimuth;
double moon_elevation;
find_moon_ecliptic(jd, &moon_longitude, &moon_latitude, &moon_distance);
ecliptic_to_equatorial(moon_longitude, moon_latitude, ecl, &moon_right_ascension, &moon_declination);
equatorial_to_horizontal(moon_right_ascension, moon_declination, lmst, latitude, &moon_azimuth, &moon_elevation);
/*
std::cout.precision(10);
std::cout << std::fixed;
//std::cout << "gmst: " << math::degrees<double>(gmst) << std::endl;
std::cout << "JD: " << jd << std::endl;
std::cout << "PST: " << pst_time << std::endl;
std::cout << "AZ: " << math::degrees(sun_azimuth) << std::endl;
std::cout << "EL: " << math::degrees(sun_elevation) << std::endl;
std::cout << "DEC: " << math::degrees(sun_declination) << std::endl;
//std::cout << "eOT: " << eot << std::endl;
*/
if (sun_light)
{
math::quaternion<float> sun_azimuth_rotation = math::angle_axis((float)sun_azimuth, float3{0, 1, 0});
math::quaternion<float> sun_elevation_rotation = math::angle_axis((float)sun_elevation, float3{-1, 0, 0});
math::quaternion<float> sun_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation);
sun_direction = math::normalize(sun_rotation * float3{0, 0, -1});
sun_light->set_rotation(sun_rotation);
}
if (moon_light)
{
math::quaternion<float> moon_azimuth_rotation = math::angle_axis(sun_azimuth, float3{0, 1, 0});
math::quaternion<float> moon_elevation_rotation = math::angle_axis(sun_elevation, float3{1, 0, 0});
math::quaternion<float> moon_azimuth_rotation = math::angle_axis((float)moon_azimuth, float3{0, 1, 0});
math::quaternion<float> moon_elevation_rotation = math::angle_axis((float)moon_elevation, float3{-1, 0, 0});
math::quaternion<float> moon_rotation = math::normalize(moon_azimuth_rotation * moon_elevation_rotation);
moon_light->set_rotation(moon_rotation);
}
std::size_t hour_index = static_cast<std::size_t>(hour);
float lerp_factor = hour - std::floor(hour);
if (sky_pass)
{
float hour = time_of_day / (60.0f * 60.0f);
std::size_t hour_index = static_cast<std::size_t>(hour);
const std::array<float4, 4>& gradient0 = sky_gradients[hour_index];
const std::array<float4, 4>& gradient1 = sky_gradients[(hour_index + 1) % sky_gradients.size()];
float t = hour - std::floor(hour);
std::array<float4, 4> gradient;
for (int i = 0; i < 4; ++i)
{
gradient[i] = math::lerp(gradient0[i], gradient1[i], t);
gradient[i] = math::lerp(gradient0[i], gradient1[i], lerp_factor);
}
float3 sun_color0 = sun_colors[hour_index];
float3 sun_color1 = sun_colors[(hour_index + 1) % sun_colors.size()];
float3 sun_color = math::lerp(sun_color0, sun_color1, t);
float3 sun_color = math::lerp(sun_color0, sun_color1, lerp_factor);
float3 moon_color0 = moon_colors[hour_index];
float3 moon_color1 = moon_colors[(hour_index + 1) % moon_colors.size()];
float3 moon_color = math::lerp(moon_color0, moon_color1, t);
float3 moon_color = math::lerp(moon_color0, moon_color1, lerp_factor);
float3 ambient_color0 = ambient_colors[hour_index];
float3 ambient_color1 = ambient_colors[(hour_index + 1) % sun_colors.size()];
float3 ambient_color = math::lerp(ambient_color0, ambient_color1, t);
float3 ambient_color = math::lerp(ambient_color0, ambient_color1, lerp_factor);
sun_light->set_color(sun_color);
@ -181,16 +310,99 @@ void weather_system::set_time_of_day(float t)
ambient_light->set_color(ambient_color);
sky_pass->set_sky_gradient(gradient);
sky_pass->set_time_of_day(time_of_day);
sky_pass->set_time_of_day(hour * 60.0 * 60.0);
}
shadow_light = sun_light;
if (shadow_map_pass)
{
if (sun_elevation < 0.0f)
{
shadow_map_pass->set_light(moon_light);
}
else
{
shadow_map_pass->set_light(sun_light);
}
}
if (material_pass)
{
float shadow_strength0 = shadow_strengths[hour_index];
float shadow_strength1 = shadow_strengths[(hour_index + 1) % shadow_strengths.size()];
float shadow_strength = math::lerp(shadow_strength0, shadow_strength1, lerp_factor);
material_pass->set_shadow_strength(shadow_strength);
}
}
void weather_system::set_coordinates(const float2& coordinates)
{
this->coordinates = coordinates;
}
void weather_system::set_ambient_light(::ambient_light* light)
{
this->ambient_light = light;
}
void weather_system::set_sun_light(directional_light* light)
{
sun_light = light;
if (sky_pass)
{
sky_pass->set_sun_light(sun_light);
}
}
void weather_system::set_moon_light(directional_light* light)
{
moon_light = light;
if (sky_pass)
{
sky_pass->set_moon_light(moon_light);
}
}
void weather_system::set_sky_pass(::sky_pass* pass)
{
sky_pass = pass;
if (sky_pass)
{
sky_pass->set_sun_light(sun_light);
sky_pass->set_moon_light(moon_light);
}
}
void weather_system::set_shadow_map_pass(::shadow_map_pass* pass)
{
shadow_map_pass = pass;
if (shadow_map_pass)
{
shadow_map_pass->set_light(shadow_light);
}
}
void weather_system::set_material_pass(::material_pass* pass)
{
material_pass = pass;
if (material_pass)
{
material_pass->set_shadow_strength(0.75f);
}
}
void weather_system::set_time(int year, int month, int day, int hour, int minute, int second, double tc)
{
double time = ((static_cast<double>(hour) - tc) + ((static_cast<double>(minute) + static_cast<double>(second) / 60.0) / 60.0)) / 24.0;
jd = julian_day(year, month, day, time);
}
void weather_system::set_time_scale(float scale)
{
time_scale = scale;
@ -312,4 +524,21 @@ void weather_system::set_ambient_palette(const ::image* image)
void weather_system::set_shadow_palette(const ::image* image)
{
shadow_palette = image;
if (shadow_palette)
{
unsigned int w = image->get_width();
unsigned int h = image->get_height();
unsigned int c = image->get_channels();
const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
for (unsigned int x = 0; x < w; ++x)
{
unsigned int y = 0;
unsigned int i = y * w * c + x * c;
float r = 1.0f - (static_cast<float>(pixels[i]) / 255.0f);
shadow_strengths.push_back(r);
}
}
}

+ 9
- 4
src/game/systems/weather-system.hpp View File

@ -37,6 +37,8 @@ public:
weather_system(entt::registry& registry);
virtual void update(double t, double dt);
void set_coordinates(const float2& coordinates);
void set_ambient_light(ambient_light* light);
void set_sun_light(directional_light* light);
void set_moon_light(directional_light* light);
@ -44,7 +46,8 @@ public:
void set_shadow_map_pass(::shadow_map_pass* pass);
void set_material_pass(::material_pass* pass);
void set_time_of_day(float t);
/// @param tc Timezone correction, in hours
void set_time(int year, int month, int day, int hour, int minute, int second, double tc);
void set_time_scale(float scale);
void set_sky_palette(const ::image* image);
@ -54,10 +57,11 @@ public:
void set_shadow_palette(const ::image* image);
private:
float time_of_day;
double jd;
float2 coordinates;
float time_scale;
float sun_azimuth;
float sun_elevation;
float3 sun_direction;
ambient_light* ambient_light;
directional_light* sun_light;
@ -74,6 +78,7 @@ private:
std::vector<float3> sun_colors;
std::vector<float3> moon_colors;
std::vector<float3> ambient_colors;
std::vector<float> shadow_strengths;
std::vector<std::array<float4, 4>> sky_gradients;
};

+ 6
- 0
src/resources/biome-loader.cpp View File

@ -70,6 +70,12 @@ biome* resource_loader::load(resource_manager* resource_manager, PHYSFS_F
load_value(&biome->name, json, "name");
float2 coordinates;
if (load_array(&coordinates.x, 2, json, "coordinates"))
{
biome->coordinates = {math::radians(coordinates.x), math::radians(coordinates.y)};
}
if (auto terrain = json.find("terrain"); terrain != json.end())
{
std::string material_filename;

Loading…
Cancel
Save