diff --git a/CMakeLists.txt b/CMakeLists.txt index d4aa351..af368e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.7) + option(VERSION_STRING "Project version string" "0.0.0") project(antkeeper VERSION ${VERSION_STRING} LANGUAGES CXX) diff --git a/src/ecs/components/atmosphere-component.hpp b/src/ecs/components/atmosphere-component.hpp new file mode 100644 index 0000000..274d64c --- /dev/null +++ b/src/ecs/components/atmosphere-component.hpp @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2021 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#ifndef ANTKEEPER_ECS_ATMOSPHERE_COMPONENT_HPP +#define ANTKEEPER_ECS_ATMOSPHERE_COMPONENT_HPP + +namespace ecs { + +/// Atmosphere +struct atmosphere_component +{ + /// Rayleigh scale height + double scale_rayleigh; + + /// Mie scale height + double scale_mie; +}; + +} // namespace ecs + +#endif // ANTKEEPER_ECS_ATMOSPHERE_COMPONENT_HPP diff --git a/src/ecs/components/celestial-body-component.hpp b/src/ecs/components/blackbody-component.hpp similarity index 56% rename from src/ecs/components/celestial-body-component.hpp rename to src/ecs/components/blackbody-component.hpp index 51cbc59..193bb8f 100644 --- a/src/ecs/components/celestial-body-component.hpp +++ b/src/ecs/components/blackbody-component.hpp @@ -17,39 +17,18 @@ * along with Antkeeper source code. If not, see . */ -#ifndef ANTKEEPER_ECS_CELESTIAL_BODY_COMPONENT_HPP -#define ANTKEEPER_ECS_CELESTIAL_BODY_COMPONENT_HPP - -#include "physics/orbit/elements.hpp" -#include "utility/fundamental-types.hpp" -#include "math/quaternion-type.hpp" +#ifndef ANTKEEPER_ECS_BLACKBODY_COMPONENT_HPP +#define ANTKEEPER_ECS_BLACKBODY_COMPONENT_HPP namespace ecs { -struct celestial_body_component -{ - physics::orbit::elements orbital_elements; - physics::orbit::elements orbital_rate; - physics::orbit::state orbital_state; - - double3 position; - double3 velocity; - double3 acceleration; - double mass; - double radius; - math::quaternion orientation; -}; - -struct blackbody_radiator +/// Blackbody radiator +struct blackbody_component { + /// Effective temperature, in Kelvin. double temperature; }; -struct diffuse_reflector -{ - double albedo; -}; - } // namespace ecs -#endif // ANTKEEPER_ECS_CELESTIAL_BODY_COMPONENT_HPP +#endif // ANTKEEPER_ECS_BLACKBODY_COMPONENT_HPP diff --git a/src/ecs/components/diffuse-reflector-component.hpp b/src/ecs/components/diffuse-reflector-component.hpp new file mode 100644 index 0000000..6b00845 --- /dev/null +++ b/src/ecs/components/diffuse-reflector-component.hpp @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2021 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#ifndef ANTKEEPER_ECS_DIFFUSE_REFLECTOR_COMPONENT_HPP +#define ANTKEEPER_ECS_DIFFUSE_REFLECTOR_COMPONENT_HPP + +namespace ecs { + +struct diffuse_reflector_component +{ + double albedo; +}; + +} // namespace ecs + +#endif // ANTKEEPER_ECS_DIFFUSE_REFLECTOR_COMPONENT_HPP diff --git a/src/ecs/components/orbit-component.hpp b/src/ecs/components/orbit-component.hpp index 865b07a..efb1c9e 100644 --- a/src/ecs/components/orbit-component.hpp +++ b/src/ecs/components/orbit-component.hpp @@ -21,14 +21,14 @@ #define ANTKEEPER_ECS_ORBIT_COMPONENT_HPP #include "physics/orbit/elements.hpp" +#include "physics/orbit/state.hpp" namespace ecs { struct orbit_component { physics::orbit::elements elements; - physics::orbit::elements rate; - physics::orbit::elements state; + physics::orbit::state state; }; } // namespace ecs diff --git a/src/ecs/systems/astronomy-system.cpp b/src/ecs/systems/astronomy-system.cpp index 9614e39..e1ff177 100644 --- a/src/ecs/systems/astronomy-system.cpp +++ b/src/ecs/systems/astronomy-system.cpp @@ -19,9 +19,10 @@ #include "ecs/systems/astronomy-system.hpp" #include "astro/apparent-size.hpp" -#include "ecs/components/celestial-body-component.hpp" +#include "ecs/components/orbit-component.hpp" +#include "ecs/components/blackbody-component.hpp" +#include "ecs/components/atmosphere-component.hpp" #include "ecs/components/transform-component.hpp" -#include "renderer/passes/sky-pass.hpp" #include "color/color.hpp" #include "physics/orbit/orbit.hpp" #include "physics/time/ut1.hpp" @@ -30,224 +31,165 @@ namespace ecs { -static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0; - astronomy_system::astronomy_system(ecs::registry& registry): entity_system(registry), universal_time(0.0), - days_per_timestep(1.0 / seconds_per_day), - observer_location{0.0, 0.0, 0.0}, - lst(0.0), - obliquity(0.0), - axial_rotation(0.0), - axial_rotation_at_epoch(0.0), - axial_rotation_speed(0.0), - sky_pass(nullptr), - sun_light(nullptr) + time_scale(1.0), + reference_body(entt::null), + reference_body_axial_tilt(0.0), + reference_body_axial_rotation(0.0), + sun_light(nullptr), + sky_pass(nullptr) {} void astronomy_system::update(double t, double dt) { // Add scaled timestep to current time - set_universal_time(universal_time + dt * days_per_timestep); + set_universal_time(universal_time + dt * time_scale); + + // Abort if reference body has not been set + if (reference_body == entt::null) + return; + + // Abort if reference body has no orbit component + if (!registry.has(reference_body)) + return; + + // Update axial rotation of reference body + reference_body_axial_rotation = physics::time::ut1::era(universal_time); + + // Get orbit component of reference body + const auto& reference_orbit = registry.get(reference_body); + + /// Construct reference frame which transforms coordinates from inertial space to reference body BCBF space + inertial_to_bcbf = physics::orbit::inertial::to_bcbf + ( + reference_orbit.state.r, + reference_orbit.elements.i, + reference_body_axial_tilt, + reference_body_axial_rotation + ); - set_universal_time(0.0); + /// Construct reference frame which transforms coordinates from inertial space to reference body topocentric space + inertial_to_topocentric = inertial_to_bcbf * bcbf_to_topocentric; - // Update horizontal (topocentric) positions of intrasolar celestial bodies - registry.view().each( - [&](ecs::entity entity, auto& body, auto& transform) + // Set the transform component translations of orbiting bodies to their topocentric positions + registry.view().each( + [&](ecs::entity entity, auto& orbit, auto& transform) { - double time_correction = observer_location[2] / (math::two_pi / 24.0); - double local_jd = universal_time + time_correction / 24.0 - 0.5; - double local_time = (local_jd - std::floor(local_jd)) * 24.0; - double local_lst = local_time / 24.0f * math::two_pi; + // Transform Cartesian position vector (r) from inertial space to topocentric space + const math::vector3 r_topocentric = inertial_to_topocentric * orbit.state.r; - // Transform orbital position from ecliptic space to horizontal space - //double3 horizontal = ecliptic_to_horizontal * body.orbital_state.r; - double3 horizontal = ecliptic_to_horizontal * double3{1, 0, 0}; - - // Subtract observer's radial distance (planet radius + observer's altitude) - //horizontal.z -= observer_location[0]; - - // Convert Cartesian horizontal coordinates to spherical - double3 spherical = geom::cartesian::to_spherical(horizontal); - - // Find angular radius - double angular_radius = astro::find_angular_radius(body.radius, spherical[0]); + // Update local transform + transform.local.translation = math::type_cast(r_topocentric); + }); + + // Get atmosphere component of reference body, if any + if (registry.has(reference_body)) + { + const ecs::atmosphere_component& atmosphere = registry.get(reference_body); + } + + if (sun_light != nullptr) + { + const math::vector3 sun_position_inertial = {0, 0, 0}; + const math::vector3 sun_forward_inertial = math::normalize(reference_orbit.state.r - sun_position_inertial); + const math::vector3 sun_up_inertial = {0, 0, 1}; - // Transform into local coordinates - const double3x3 horizontal_to_local = math::rotate_x(-math::half_pi) * math::rotate_z(-math::half_pi); + // Transform sun position, forward, and up vectors into topocentric space + const math::vector3 sun_position_topocentric = inertial_to_topocentric * sun_position_inertial; + const math::vector3 sun_forward_topocentric = inertial_to_topocentric.rotation * sun_forward_inertial; + const math::vector3 sun_up_topocentric = inertial_to_topocentric.rotation * sun_up_inertial; - double3 translation = horizontal_to_local * horizontal; - double3x3 rotation = horizontal_to_local * ecliptic_to_horizontal; + // Update sun light transform + sun_light->set_translation(math::type_cast(sun_position_topocentric)); + sun_light->set_rotation + ( + math::look_rotation + ( + math::type_cast(sun_forward_topocentric), + math::type_cast(sun_up_topocentric) + ) + ); + // Convert sun topocentric Cartesian coordinates to spherical coordinates + math::vector3 sun_az_el = geom::cartesian::to_spherical(ezs_to_sez * sun_position_topocentric); + sun_az_el.z = math::pi - sun_az_el.z; - // Set local transform of transform component - transform.local.translation = math::type_cast(translation); - transform.local.rotation = math::normalize(math::type_cast(math::quaternion_cast(rotation))); - transform.local.scale = math::type_cast(double3{body.radius, body.radius, body.radius}); + //std::cout << "el: " << math::degrees(sun_az_el.y) << "; az: " << math::degrees(sun_az_el.z) << std::endl; - if (sun_light != nullptr) - { - const double universal_time_cy = universal_time * 2.7397e-5; - const double3 solar_system_barycenter = {0, 0, 0}; - - physics::orbit::elements earth_elements; - earth_elements.a = 1.00000261 + 0.00000562 * universal_time_cy; - earth_elements.e = 0.01671123 + -0.00004392 * universal_time_cy; - - earth_elements.i = math::radians(-0.00001531) + math::radians(-0.01294668) * universal_time_cy; - earth_elements.raan = 0.0; - - const double earth_elements_mean_longitude = math::radians(100.46457166) + math::radians(35999.37244981) * universal_time_cy; - const double earth_elements_longitude_perihelion = math::radians(102.93768193) + math::radians(0.32327364) * universal_time_cy; - - earth_elements.w = earth_elements_longitude_perihelion - earth_elements.raan; - earth_elements.ta = earth_elements_mean_longitude - earth_elements_longitude_perihelion; - - - // Calculate semi-minor axis, b - double b = physics::orbit::derive_semiminor_axis(earth_elements.a, earth_elements.e); - - // Solve Kepler's equation for eccentric anomaly (E) - double ea = physics::orbit::kepler_ea(earth_elements.e, earth_elements.ta, 10, 1e-6); - - // Calculate radial distance, r; and true anomaly, v - double xv = earth_elements.a * (std::cos(ea) - earth_elements.e); - double yv = b * std::sin(ea); - double r = std::sqrt(xv * xv + yv * yv); - double ta = std::atan2(yv, xv); - - // Position of the body in perifocal space - const math::vector3 earth_position_pqw = math::quaternion::rotate_z(ta) * math::vector3{r, 0, 0}; - - - const double earth_axial_tilt = math::radians(23.45); - const double earth_axial_rotation = physics::time::ut1::era(universal_time); - const double earth_radius_au = 4.2635e-5; - - const double observer_altitude = earth_radius_au; - const double observer_latitude = math::radians(0.0); - const double observer_longitude = math::radians(0.0); - - const physics::frame earth_inertial_to_pqw = physics::orbit::inertial::to_perifocal(solar_system_barycenter, earth_elements.raan, earth_elements.i, earth_elements.w); - const math::vector3 earth_position_inertial = earth_inertial_to_pqw.inverse() * earth_position_pqw; - - const math::vector3 sun_position_intertial = math::vector3{0, 0, 0}; - - const physics::frame earth_inertial_to_bci = physics::orbit::inertial::to_bci(earth_position_inertial, earth_elements.i, earth_axial_tilt); - const physics::frame earth_inertial_to_bcbf = physics::orbit::inertial::to_bcbf(earth_position_inertial, earth_elements.i, earth_axial_tilt, earth_axial_rotation); - const physics::frame earth_bcbf_to_topo = physics::orbit::bcbf::to_topocentric(observer_altitude, observer_latitude, observer_longitude); - - const math::vector3 sun_position_earth_bci = earth_inertial_to_bci * sun_position_intertial; - const math::vector3 sun_position_earth_bcbf = earth_inertial_to_bcbf * sun_position_intertial; - const math::vector3 sun_position_earth_topo = earth_bcbf_to_topo * sun_position_earth_bcbf; - - const math::vector3 sun_radec = geom::cartesian::to_spherical(sun_position_earth_bci); - const math::vector3 sun_azel = geom::cartesian::to_spherical(sun_position_earth_topo); - - const double sun_az = sun_azel.z; - const double sun_el = sun_azel.y; - - double sun_ra = sun_radec.z; - const double sun_dec = sun_radec.y; - - if (sun_ra < 0.0) - sun_ra += math::two_pi; - - std::cout << "ra: " << (sun_ra / math::two_pi * 24.0) << "; dec: " << math::degrees(sun_dec) << std::endl; - std::cout << "az: " << math::degrees(math::pi - sun_az) << "; el: " << math::degrees(sun_el) << std::endl; - - - float az = spherical.z; - float el = spherical.y; - - if (az < 0.0f) - az += math::two_pi; - - //std::cout << "local: " << translation << std::endl; - //std::cout << "az: " << math::degrees(az) << "; "; - //std::cout << "el: " << math::degrees(el) << std::endl; - - math::quaternion sun_azimuth_rotation = math::angle_axis(static_cast(spherical.z), float3{0, 1, 0}); - math::quaternion sun_elevation_rotation = math::angle_axis(static_cast(spherical.y), float3{1, 0, 0}); - math::quaternion sun_az_el_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation); - - // Set sun color - float cct = 3000.0f + std::sin(spherical.y) * 5000.0f; - float3 color_xyz = color::cct::to_xyz(cct); - float3 color_acescg = color::xyz::to_acescg(color_xyz); - - sun_light->set_color(color_acescg); - - // Set sun intensity (in lux) - float intensity = std::max(0.0, std::sin(spherical.y) * 108000.0f); - sun_light->set_intensity(intensity); - - - //sun_light->set_translation({0, 500, 0}); - sun_light->set_translation(transform.local.translation); - //sun_light->set_rotation(transform.local.rotation); - //sun_light->set_rotation(sun_az_el_rotation); - //sun_light->set_rotation(sun_elevation_rotation); - sun_light->set_rotation(math::look_rotation(math::normalize(-transform.local.translation), {0, 0, -1})); - - if (this->sky_pass) - { - this->sky_pass->set_sun_coordinates(transform.local.rotation * float3{0, 0, -1}, {static_cast(spherical.z), static_cast(spherical.y)}); - } - } - }); + // Calculate sun color + float cct = 3000.0f + std::sin(sun_az_el.y) * 5000.0f; + float3 color_xyz = color::cct::to_xyz(cct); + float3 color_acescg = color::xyz::to_acescg(color_xyz); + sun_light->set_color(color_acescg); + + // Calculate sun intensity (in lux) + const float illuminance_zenith = 108000.0f; + float illuminance = std::max(0.0, std::sin(sun_az_el.y) * illuminance_zenith); + sun_light->set_intensity(illuminance); + } - if (sky_pass) + if (sky_pass != nullptr) { - // Calculate local time - double time_correction = observer_location[2] / (math::two_pi / 24.0); - double local_jd = universal_time + time_correction / 24.0 - 0.5; - double local_time = (local_jd - std::floor(local_jd)) * 24.0; + sky_pass->set_topocentric_frame + ( + physics::frame + { + math::type_cast(inertial_to_topocentric.translation), + math::type_cast(inertial_to_topocentric.rotation) + } + ); - sky_pass->set_time_of_day(local_time); + sky_pass->set_sun_object(sun_light); } } void astronomy_system::set_universal_time(double time) { universal_time = time; - update_axial_rotation(); } void astronomy_system::set_time_scale(double scale) { - days_per_timestep = scale / seconds_per_day; + time_scale = scale; } -void astronomy_system::set_observer_location(const double3& location) -{ - observer_location = location; - update_sidereal_time(); -} - -void astronomy_system::set_obliquity(double angle) +void astronomy_system::set_reference_body(ecs::entity entity) { - obliquity = angle; - update_ecliptic_to_horizontal(); + reference_body = entity; } -void astronomy_system::set_axial_rotation_speed(double speed) +void astronomy_system::set_reference_body_axial_tilt(double angle) { - axial_rotation_speed = speed; - update_axial_rotation(); + reference_body_axial_tilt = angle; } -void astronomy_system::set_axial_rotation_at_epoch(double angle) -{ - axial_rotation_at_epoch = angle; - update_axial_rotation(); -} - -void astronomy_system::set_sky_pass(::sky_pass* pass) +void astronomy_system::set_observer_location(const double3& location) { - sky_pass = pass; + observer_location = location; + + // Construct reference frame which transforms coordinates from SEZ to EZS + sez_to_ezs = physics::frame + { + {0, 0, 0}, + math::normalize + ( + math::quaternion::rotate_x(-math::half_pi) * + math::quaternion::rotate_z(-math::half_pi) + ) + }; + + // Construct reference frame which transforms coordinates from EZS to SEZ + ezs_to_sez = sez_to_ezs.inverse(); + + // Construct reference frame which transforms coordinates from BCBF space to topocentric space + bcbf_to_topocentric = physics::orbit::bcbf::to_topocentric + ( + observer_location[0], // Radial distance + observer_location[1], // Latitude + observer_location[2] // Longitude + ) * sez_to_ezs; } void astronomy_system::set_sun_light(scene::directional_light* light) @@ -255,21 +197,9 @@ void astronomy_system::set_sun_light(scene::directional_light* light) sun_light = light; } -void astronomy_system::update_axial_rotation() -{ - axial_rotation = math::wrap_radians(axial_rotation_at_epoch + universal_time * axial_rotation_speed); - update_sidereal_time(); -} - -void astronomy_system::update_sidereal_time() -{ - lst = math::wrap_radians(axial_rotation + observer_location[2]); - update_ecliptic_to_horizontal(); -} - -void astronomy_system::update_ecliptic_to_horizontal() +void astronomy_system::set_sky_pass(::sky_pass* pass) { - //ecliptic_to_horizontal = coordinates::rectangular::ecliptic::to_horizontal(obliquity, observer_location[1], lst); + this->sky_pass = pass; } } // namespace ecs diff --git a/src/ecs/systems/astronomy-system.hpp b/src/ecs/systems/astronomy-system.hpp index 9dbfa91..c9c657b 100644 --- a/src/ecs/systems/astronomy-system.hpp +++ b/src/ecs/systems/astronomy-system.hpp @@ -24,8 +24,8 @@ #include "ecs/entity.hpp" #include "scene/directional-light.hpp" #include "utility/fundamental-types.hpp" - -class sky_pass; +#include "physics/frame.hpp" +#include "renderer/passes/sky-pass.hpp" namespace ecs { @@ -61,58 +61,45 @@ public: void set_time_scale(double scale); /** - * Sets the location of the observer. + * Sets the reference body, from which observations are taking place. * - * @param location Spherical coordinates of the observer, in the ISO order of radial distance, polar angle (radians), and azimuthal angle (radians). + * @param entity Entity of the reference body. */ - void set_observer_location(const double3& location); + void set_reference_body(ecs::entity entity); /** - * Sets the obliquity of the ecliptic, a.k.a. axial tilt. + * Sets the axial tilt of the reference body. * - * @param angle Angle between the planet's rotational axis and its orbital axis, in radians. + * @param angle Angle between the reference body's rotational axis and its orbital axis, in radians. */ - void set_obliquity(double angle); + void set_reference_body_axial_tilt(double angle); /** - * Sets the rotational speed of the observer's planet. + * Sets the location of the observer using spherical coordinates in BCBF space. * - * @param speed Rotational speed, in radians per day. + * @param location Spherical coordinates of the observer, in reference body BCBF space, in the ISO order of radial distance, polar angle (radians), and azimuthal angle (radians). */ - void set_axial_rotation_speed(double speed); + void set_observer_location(const double3& location); - /** - * Sets the axial rotation of the observer's planet when the universal time is `0.0`. - * - * @param angle Axial rotation angle, in radians. - */ - void set_axial_rotation_at_epoch(double angle); + void set_sun_light(scene::directional_light* light); void set_sky_pass(sky_pass* pass); - void set_sun_light(scene::directional_light* light); - private: - /// Updates the axial rotation angle - void update_axial_rotation(); - - /// Updates the local sidereal time (LST) and dependent variables - void update_sidereal_time(); - - /// Updates the ecliptic-to-horizontal transformation matrix - void update_ecliptic_to_horizontal(); - double universal_time; - double days_per_timestep; - double lst; - double axial_rotation_at_epoch; - double axial_rotation_speed; - double axial_rotation; + double time_scale; + ecs::entity reference_body; + double reference_body_axial_tilt; + double reference_body_axial_rotation; double3 observer_location; - double obliquity; - double3x3 ecliptic_to_horizontal; - sky_pass* sky_pass; scene::directional_light* sun_light; + sky_pass* sky_pass; + + physics::frame inertial_to_bcbf; + physics::frame bcbf_to_topocentric; + physics::frame inertial_to_topocentric; + physics::frame sez_to_ezs; + physics::frame ezs_to_sez; }; } // namespace ecs diff --git a/src/ecs/systems/orbit-system.cpp b/src/ecs/systems/orbit-system.cpp new file mode 100644 index 0000000..9fcd627 --- /dev/null +++ b/src/ecs/systems/orbit-system.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2021 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#include "ecs/systems/orbit-system.hpp" +#include "ecs/components/orbit-component.hpp" +#include "ecs/entity.hpp" +#include "physics/orbit/orbit.hpp" + +namespace ecs { + +orbit_system::orbit_system(ecs::registry& registry): + entity_system(registry), + universal_time(0.0), + time_scale(1.0), + ke_iterations(10), + ke_tolerance(1e-6) +{} + +void orbit_system::update(double t, double dt) +{ + // Add scaled timestep to current time + set_universal_time(universal_time + dt * time_scale); + + // Update the orbital state of orbiting bodies + registry.view().each( + [&](ecs::entity entity, auto& orbit) + { + // Calculate semi-minor axis (b) + const double b = physics::orbit::derive_semiminor_axis(orbit.elements.a, orbit.elements.e); + + // Solve Kepler's equation for eccentric anomaly (E) + const double ea = physics::orbit::kepler_ea(orbit.elements.e, orbit.elements.ta, ke_iterations, ke_tolerance); + + // Calculate radial distance and true anomaly (nu) + const double xv = orbit.elements.a * (std::cos(ea) - orbit.elements.e); + const double yv = b * std::sin(ea); + const double distance = std::sqrt(xv * xv + yv * yv); + const double ta = std::atan2(yv, xv); + + // Calculate Cartesian position (r) in perifocal space + const math::vector3 r_perifocal = math::quaternion::rotate_z(ta) * math::vector3{distance, 0, 0}; + + /// @TODO Calculate Cartesian velocity (v) in perifocal space + //const math::vector3 v_perifocal = ... + + // Construct perifocal to inertial reference frame + const physics::frame perifocal_to_inertial = physics::orbit::inertial::to_perifocal + ( + {0, 0, 0}, + orbit.elements.raan, + orbit.elements.i, + orbit.elements.w + ).inverse(); + + // Transform orbital state vectors from perifocal space to the parent inertial space + const math::vector3 r_inertial = perifocal_to_inertial.transform(r_perifocal); + //const math::vector3 v_inertial = perifocal_frame.transform(v_perifocal); + + // Update orbital state of component + orbit.state.r = r_inertial; + //orbit.state.v = v_inertial; + }); +} + +void orbit_system::set_universal_time(double time) +{ + universal_time = time; +} + +void orbit_system::set_time_scale(double scale) +{ + time_scale = scale; +} + +} // namespace ecs diff --git a/src/ecs/systems/solar-system.hpp b/src/ecs/systems/orbit-system.hpp similarity index 86% rename from src/ecs/systems/solar-system.hpp rename to src/ecs/systems/orbit-system.hpp index dce98bf..ad6725b 100644 --- a/src/ecs/systems/solar-system.hpp +++ b/src/ecs/systems/orbit-system.hpp @@ -26,16 +26,16 @@ namespace ecs { /** - * Updates positions, velocities, and rotations of intrasolar celestial bodies. + * Updates the Cartesian position and velocity of orbiting bodies given their Keplerian orbital elements and the current time. */ -class solar_system: +class orbit_system: public entity_system { public: - solar_system(ecs::registry& registry); + orbit_system(ecs::registry& registry); /** - * Scales then adds the timestep `dt` to the current time, then recalculates the positions of celestial bodies. + * Scales then adds the timestep `dt` to the current time, then recalculates the positions of orbiting bodies. * * @param t Time, in seconds. * @param dt Delta time, in seconds. @@ -58,9 +58,9 @@ public: private: double universal_time; - double days_per_timestep; - double ke_tolerance; + double time_scale; std::size_t ke_iterations; + double ke_tolerance; }; } // namespace ecs diff --git a/src/ecs/systems/solar-system.cpp b/src/ecs/systems/solar-system.cpp deleted file mode 100644 index 59966ab..0000000 --- a/src/ecs/systems/solar-system.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 2021 Christopher J. Howard - * - * This file is part of Antkeeper source code. - * - * Antkeeper source code is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Antkeeper source code is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Antkeeper source code. If not, see . - */ - -#include "ecs/systems/solar-system.hpp" -#include "ecs/components/celestial-body-component.hpp" -#include "ecs/entity.hpp" - -namespace ecs { - -static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0; - -solar_system::solar_system(ecs::registry& registry): - entity_system(registry), - universal_time(0.0), - days_per_timestep(1.0 / seconds_per_day), - ke_tolerance(1e-6), - ke_iterations(10) -{} - -void solar_system::update(double t, double dt) -{ - // Add scaled timestep to current time - set_universal_time(universal_time + dt * days_per_timestep); - - // Update orbital state of intrasolar celestial bodies - registry.view().each( - [&](ecs::entity entity, auto& body) - { - auto elements = body.orbital_elements; - elements.a += body.orbital_rate.a * universal_time; - elements.e += body.orbital_rate.e * universal_time; - elements.w += body.orbital_rate.w * universal_time; - elements.ta += body.orbital_rate.ta * universal_time; - elements.i += body.orbital_rate.i * universal_time; - elements.raan += body.orbital_rate.raan * universal_time; - - // Calculate ecliptic orbital position - //body.orbital_state.r = astro::orbital_elements_to_ecliptic(elements, ke_tolerance, ke_iterations); - }); -} - -void solar_system::set_universal_time(double time) -{ - universal_time = time; -} - -void solar_system::set_time_scale(double scale) -{ - days_per_timestep = scale / seconds_per_day; -} - -} // namespace ecs diff --git a/src/ecs/systems/weather-system.cpp b/src/ecs/systems/weather-system.cpp deleted file mode 100644 index 0cf1729..0000000 --- a/src/ecs/systems/weather-system.cpp +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2021 Christopher J. Howard - * - * This file is part of Antkeeper source code. - * - * Antkeeper source code is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Antkeeper source code is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Antkeeper source code. If not, see . - */ - -#include "ecs/systems/weather-system.hpp" -#include "scene/directional-light.hpp" -#include "scene/ambient-light.hpp" -#include "renderer/passes/sky-pass.hpp" -#include "renderer/passes/shadow-map-pass.hpp" -#include "renderer/passes/material-pass.hpp" -#include - -namespace ecs { - -static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0; - -weather_system::weather_system(ecs::registry& registry): - entity_system(registry), - sky_pass(nullptr), - shadow_map_pass(nullptr), - material_pass(nullptr), - universal_time(0.0), - days_per_timestep(1.0 / seconds_per_day) -{} - -void weather_system::update(double t, double dt) -{ - // Add scaled timestep to current time - set_universal_time(universal_time + dt * days_per_timestep); -} - -void weather_system::set_sky_pass(::sky_pass* pass) -{ - sky_pass = pass; -} - -void weather_system::set_shadow_map_pass(::shadow_map_pass* pass) -{ - shadow_map_pass = pass; -} - -void weather_system::set_material_pass(::material_pass* pass) -{ - material_pass = pass; -} - -void weather_system::set_universal_time(double time) -{ - universal_time = time; -} - -void weather_system::set_time_scale(double scale) -{ - days_per_timestep = scale / seconds_per_day; -} - -} // namespace ecs diff --git a/src/ecs/systems/weather-system.hpp b/src/ecs/systems/weather-system.hpp deleted file mode 100644 index 3e90f9f..0000000 --- a/src/ecs/systems/weather-system.hpp +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2021 Christopher J. Howard - * - * This file is part of Antkeeper source code. - * - * Antkeeper source code is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Antkeeper source code is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Antkeeper source code. If not, see . - */ - -#ifndef ANTKEEPER_ECS_WEATHER_SYSTEM_HPP -#define ANTKEEPER_ECS_WEATHER_SYSTEM_HPP - -#include "entity-system.hpp" -#include "utility/fundamental-types.hpp" - -class sky_pass; -class shadow_map_pass; -class material_pass; -class ambient_light; -class directional_light; -class image; - -namespace ecs { - -class weather_system: - public entity_system -{ -public: - weather_system(ecs::registry& registry); - virtual void update(double t, double dt); - - /** - * - * @param latitude Latitude, in radians. - * @param longitude Longitude, in radians. - * @param altitude Altitude, in radians. - */ - void set_location(float latitude, float longitude, float altitude); - - void set_sky_pass(::sky_pass* pass); - void set_shadow_map_pass(::shadow_map_pass* pass); - void set_material_pass(::material_pass* pass); - - /** - * Sets the current universal time. - * - * @param time Universal time, in days. - */ - void set_universal_time(double time); - - /** - * Sets the factor by which the timestep `dt` will be scaled before being added to the current universal time. - * - * @param scale Factor by which to scale the timestep. - */ - void set_time_scale(double scale); - -private: - static void load_palette(std::vector* palette, const ::image* image, unsigned int row); - static float3 interpolate_gradient(const std::vector& gradient, float position); - - double universal_time; - double days_per_timestep; - sky_pass* sky_pass; - shadow_map_pass* shadow_map_pass; - material_pass* material_pass; -}; - -} // namespace ecs - -#endif // ANTKEEPER_ECS_WEATHER_SYSTEM_HPP diff --git a/src/game/bootloader.cpp b/src/game/bootloader.cpp index d9e7eac..209cdf6 100644 --- a/src/game/bootloader.cpp +++ b/src/game/bootloader.cpp @@ -73,9 +73,8 @@ #include "ecs/systems/spatial-system.hpp" #include "ecs/systems/tracking-system.hpp" #include "ecs/systems/painting-system.hpp" -#include "ecs/systems/weather-system.hpp" #include "ecs/systems/astronomy-system.hpp" -#include "ecs/systems/solar-system.hpp" +#include "ecs/systems/orbit-system.hpp" #include "ecs/components/marker-component.hpp" #include "ecs/commands.hpp" #include "utility/paths.hpp" @@ -98,6 +97,8 @@ #include #include "utility/timestamp.hpp" +static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0; + static void parse_options(game_context* ctx, int argc, char** argv); static void setup_resources(game_context* ctx); static void load_config(game_context* ctx); @@ -857,21 +858,11 @@ void setup_systems(game_context* ctx) ctx->painting_system = new ecs::painting_system(*ctx->ecs_registry, event_dispatcher, ctx->resource_manager); ctx->painting_system->set_scene(ctx->overworld_scene); - // Setup weather system - ctx->weather_system = new ecs::weather_system(*ctx->ecs_registry); - ctx->weather_system->set_sky_pass(ctx->overworld_sky_pass); - ctx->weather_system->set_shadow_map_pass(ctx->overworld_shadow_map_pass); - ctx->weather_system->set_material_pass(ctx->overworld_material_pass); - // Setup solar system - ctx->solar_system = new ecs::solar_system(*ctx->ecs_registry); + ctx->orbit_system = new ecs::orbit_system(*ctx->ecs_registry); // Setup astronomy system ctx->astronomy_system = new ecs::astronomy_system(*ctx->ecs_registry); - ctx->astronomy_system->set_obliquity(math::radians(23.4365472133)); - ctx->astronomy_system->set_axial_rotation_speed(math::radians(360.9856)); - ctx->astronomy_system->set_axial_rotation_at_epoch(0.0); - ctx->astronomy_system->set_sky_pass(ctx->overworld_sky_pass); // Set time scale float time_scale = 60.0f; @@ -879,9 +870,9 @@ void setup_systems(game_context* ctx) { time_scale = ctx->config->get("time_scale"); } - ctx->weather_system->set_time_scale(time_scale); - ctx->solar_system->set_time_scale(time_scale); - ctx->astronomy_system->set_time_scale(time_scale); + + ctx->orbit_system->set_time_scale(time_scale / seconds_per_day); + ctx->astronomy_system->set_time_scale(time_scale / seconds_per_day); // Setup render system ctx->render_system = new ecs::render_system(*ctx->ecs_registry); @@ -1180,36 +1171,32 @@ void setup_controls(game_context* ctx) ( [ctx, time_scale]() { - ctx->weather_system->set_time_scale(time_scale * 100.0f); - ctx->solar_system->set_time_scale(time_scale * 100.0f); - ctx->astronomy_system->set_time_scale(time_scale * 100.0f); + ctx->orbit_system->set_time_scale(time_scale * 100.0f / seconds_per_day); + ctx->astronomy_system->set_time_scale(time_scale * 100.0f / seconds_per_day); } ); ctx->control_system->get_fast_forward_control()->set_deactivated_callback ( [ctx, time_scale]() { - ctx->weather_system->set_time_scale(time_scale); - ctx->solar_system->set_time_scale(time_scale); - ctx->astronomy_system->set_time_scale(time_scale); + ctx->orbit_system->set_time_scale(time_scale / seconds_per_day); + ctx->astronomy_system->set_time_scale(time_scale / seconds_per_day); } ); ctx->control_system->get_rewind_control()->set_activated_callback ( [ctx, time_scale]() { - ctx->weather_system->set_time_scale(time_scale * -100.0f); - ctx->solar_system->set_time_scale(time_scale * -100.0f); - ctx->astronomy_system->set_time_scale(time_scale * -100.0f); + ctx->orbit_system->set_time_scale(time_scale * -100.0f / seconds_per_day); + ctx->astronomy_system->set_time_scale(time_scale * -100.0f / seconds_per_day); } ); ctx->control_system->get_rewind_control()->set_deactivated_callback ( [ctx, time_scale]() { - ctx->weather_system->set_time_scale(time_scale); - ctx->solar_system->set_time_scale(time_scale); - ctx->astronomy_system->set_time_scale(time_scale); + ctx->orbit_system->set_time_scale(time_scale / seconds_per_day); + ctx->astronomy_system->set_time_scale(time_scale / seconds_per_day); } ); @@ -1268,13 +1255,12 @@ void setup_callbacks(game_context* ctx) ctx->camera_system->update(t, dt); ctx->tool_system->update(t, dt); - ctx->solar_system->update(t, dt); + ctx->orbit_system->update(t, dt); ctx->astronomy_system->update(t, dt); ctx->spatial_system->update(t, dt); ctx->constraint_system->update(t, dt); ctx->tracking_system->update(t, dt); ctx->painting_system->update(t, dt); - ctx->weather_system->update(t, dt); //(*ctx->focal_point_tween)[1] = ctx->orbit_cam->get_focal_point(); diff --git a/src/game/game-context.hpp b/src/game/game-context.hpp index 0221e2c..b9169ce 100644 --- a/src/game/game-context.hpp +++ b/src/game/game-context.hpp @@ -82,9 +82,8 @@ namespace ecs class spatial_system; class tracking_system; class painting_system; - class weather_system; class astronomy_system; - class solar_system; + class orbit_system; class behavior_system; class collision_system; class constraint_system; @@ -248,9 +247,8 @@ struct game_context ecs::spatial_system* spatial_system; ecs::tracking_system* tracking_system; ecs::painting_system* painting_system; - ecs::weather_system* weather_system; ecs::astronomy_system* astronomy_system; - ecs::solar_system* solar_system; + ecs::orbit_system* orbit_system; // Game biome* biome; diff --git a/src/game/states/play-state.cpp b/src/game/states/play-state.cpp index 15e9ce2..75ca1df 100644 --- a/src/game/states/play-state.cpp +++ b/src/game/states/play-state.cpp @@ -32,7 +32,7 @@ #include "ecs/components/transform-component.hpp" #include "ecs/components/camera-follow-component.hpp" #include "ecs/components/orbit-component.hpp" -#include "ecs/components/celestial-body-component.hpp" +#include "ecs/components/blackbody-component.hpp" #include "ecs/components/light-component.hpp" #include "ecs/commands.hpp" #include "game/game-context.hpp" @@ -56,8 +56,7 @@ #include "ecs/systems/camera-system.hpp" #include "ecs/systems/render-system.hpp" #include "ecs/systems/tool-system.hpp" -#include "ecs/systems/weather-system.hpp" -#include "ecs/systems/solar-system.hpp" +#include "ecs/systems/orbit-system.hpp" #include "ecs/systems/astronomy-system.hpp" #include "game/biome.hpp" #include "utility/fundamental-types.hpp" @@ -100,53 +99,58 @@ void play_state_enter(game_context* ctx) sky_pass->set_observer_location(4.26352e-5, ctx->biome->location[0], ctx->biome->location[1]); sky_pass->set_moon_angular_radius(math::radians(1.0f)); sky_pass->set_sun_angular_radius(math::radians(1.0f)); - sky_pass->set_sun_coordinates({0, 1, 0}, {0, 3.1415f / 2.0f}); - sky_pass->set_moon_coordinates({1, 0, 0}, {0, 0}); - sky_pass->set_moon_rotation(math::identity_quaternion); sky_pass->set_sky_gradient(resource_manager->load("sky-gradient.tex"), resource_manager->load("sky-gradient2.tex")); - - - scene::ambient_light* ambient = new scene::ambient_light(); - ambient->set_color({1, 1, 1}); - ambient->set_intensity(0.0f); - ambient->update_tweens(); - ctx->overworld_scene->add_object(ambient); - - - // Create sun + auto sun_entity = ecs_registry.create(); { - ecs::celestial_body_component sun_body; - sun_body.orbital_elements.a = 1.0; - sun_body.orbital_elements.e = 0.016709; - sun_body.orbital_elements.w = math::radians(282.9404); - sun_body.orbital_elements.ta = math::radians(356.0470); - sun_body.orbital_elements.i = 0.0; - sun_body.orbital_elements.raan = 0.0; + ecs::orbit_component orbit; + orbit.elements.a = 0.0; + orbit.elements.e = 0.0; + orbit.elements.i = math::radians(0.0); + orbit.elements.raan = math::radians(0.0); + orbit.elements.w = math::radians(0.0); + orbit.elements.ta = math::radians(0.0); + + ecs::blackbody_component blackbody; + blackbody.temperature = 5772.0; - sun_body.orbital_rate.a = 0.0; - sun_body.orbital_rate.e = -1.151e-9; - sun_body.orbital_rate.w = math::radians(4.70935e-5); - sun_body.orbital_rate.ta = math::radians(0.9856002585); - sun_body.orbital_rate.i = 0.0; - sun_body.orbital_rate.raan = 0.0; + ecs::transform_component transform; + transform.local = math::identity_transform; + transform.warp = true; - ecs::transform_component sun_transform; - sun_transform.local = math::identity_transform; - sun_transform.warp = true; + ecs_registry.assign(sun_entity, orbit); + ecs_registry.assign(sun_entity, blackbody); + ecs_registry.assign(sun_entity, transform); + } + + // Create Earth + auto earth_entity = ecs_registry.create(); + { + ecs::orbit_component orbit; + orbit.elements.a = 1.00000261; + orbit.elements.e = 0.01671123; + orbit.elements.i = math::radians(-0.00001531); + orbit.elements.raan = math::radians(0.0); + const double longitude_periapsis = math::radians(102.93768193); + orbit.elements.w = longitude_periapsis - orbit.elements.raan; + orbit.elements.ta = math::radians(100.46457166) - longitude_periapsis; - auto sun_entity = ecs_registry.create(); - ecs_registry.assign(sun_entity, sun_transform); - ecs_registry.assign(sun_entity, sun_body); + ecs::transform_component transform; + transform.local = math::identity_transform; + transform.warp = true; - //ctx->astronomy_system->set_sun(sun_entity); + ecs_registry.assign(earth_entity, orbit); + ecs_registry.assign(earth_entity, transform); } - scene::directional_light* sun = new scene::directional_light(); + scene::ambient_light* ambient = new scene::ambient_light(); + ambient->set_color({1, 1, 1}); + ambient->set_intensity(0.0f); + ambient->update_tweens(); + ctx->overworld_scene->add_object(ambient); - //float3 sun_color = math::type_cast(astro::blackbody(6000.0)); // NOTE: this is linear sRGB, should be ACEScg - //sun->set_color(sun_color); + scene::directional_light* sun = new scene::directional_light(); sun->set_intensity(1000.0f); sun->set_light_texture(resource_manager->load("forest-gobo.tex")); sun->set_light_texture_scale({2000, 2000}); @@ -156,20 +160,18 @@ void play_state_enter(game_context* ctx) ctx->overworld_scene->add_object(sun); ctx->overworld_shadow_map_pass->set_light(sun); + // Set universal time + const double universal_time = 0.0; + ctx->astronomy_system->set_universal_time(universal_time); + ctx->orbit_system->set_universal_time(universal_time); - ctx->weather_system->set_universal_time(0.0); - - ctx->solar_system->set_universal_time(0.0); - - ctx->astronomy_system->set_observer_location(double3{4.26352e-5, math::radians(10.0f), 0.0f}); - ctx->astronomy_system->set_universal_time(0.0); - ctx->astronomy_system->set_obliquity(math::radians(23.4393)); - ctx->astronomy_system->set_axial_rotation_at_epoch(math::radians(280.4606)); - ctx->astronomy_system->set_axial_rotation_speed(math::radians(360.9856)); + // Set astronomy system observation parameters + const double earth_radius_au = 4.2635e-5; + ctx->astronomy_system->set_reference_body(earth_entity); + ctx->astronomy_system->set_reference_body_axial_tilt(math::radians(23.4393)); + ctx->astronomy_system->set_observer_location(double3{4.26352e-5, math::radians(0.0f), math::radians(0.0f)}); ctx->astronomy_system->set_sun_light(sun); - - - + ctx->astronomy_system->set_sky_pass(ctx->overworld_sky_pass); // Load entity archetypes ecs::archetype* ant_hill_archetype = resource_manager->load("ant-hill.ent"); @@ -429,4 +431,3 @@ void play_state_exit(game_context* ctx) logger->pop_task(EXIT_SUCCESS); } - diff --git a/src/physics/frame.hpp b/src/physics/frame.hpp index a368152..f10803e 100644 --- a/src/physics/frame.hpp +++ b/src/physics/frame.hpp @@ -20,7 +20,7 @@ #ifndef ANTKEEPER_PHYSICS_FRAME_HPP #define ANTKEEPER_PHYSICS_FRAME_HPP -#include "utility/fundamental-types.hpp" +#include "math/math.hpp" #include namespace physics { @@ -112,8 +112,8 @@ frame frame::transform(const frame& f) const { return frame { - transform(f.translation), - math::normalize(rotation * f.rotation) + f.transform(translation), + math::normalize(f.rotation * rotation) }; } diff --git a/src/renderer/passes/sky-pass.cpp b/src/renderer/passes/sky-pass.cpp index 652bba2..9918161 100644 --- a/src/renderer/passes/sky-pass.cpp +++ b/src/renderer/passes/sky-pass.cpp @@ -42,6 +42,7 @@ #include "math/interpolation.hpp" #include "geom/cartesian.hpp" #include "geom/spherical.hpp" +#include "physics/orbit/orbit.hpp" #include #include #include @@ -65,12 +66,11 @@ sky_pass::sky_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffe time_tween(nullptr), time_of_day_tween(0.0, math::lerp), julian_day_tween(0.0, math::lerp), - sun_position_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp), - sun_az_el_tween(float2{0.0f, 0.0f}, math::lerp), - moon_position_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp), - moon_az_el_tween(float2{0.0f, 0.0f}, math::lerp), horizon_color_tween(float3{0.0f, 0.0f, 0.0f}, math::lerp), - zenith_color_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp) + zenith_color_tween(float3{1.0f, 1.0f, 1.0f}, math::lerp), + topocentric_frame_translation({0, 0, 0}, math::lerp), + topocentric_frame_rotation(math::quaternion::identity(), math::nlerp), + sun_object(nullptr) { // Load star catalog string_table* star_catalog = resource_manager->load("stars.csv"); @@ -110,7 +110,11 @@ sky_pass::sky_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffe dec = math::wrap_radians(math::radians(dec)); // Transform spherical equatorial coordinates to rectangular equatorial coordinates - double3 position = geom::spherical::to_cartesian(double3{1.0, dec, ra}); + double3 position_bci = geom::spherical::to_cartesian(double3{1.0, dec, ra}); + + // Transform coordinates from equatorial space to inertial space + physics::frame bci_to_inertial = physics::orbit::inertial::to_bci({0, 0, 0}, 0.0, math::radians(23.4393)).inverse(); + double3 position_inertial = bci_to_inertial * position_bci; // Convert color index to color temperature double cct = color::index::bv_to_cct(bv_color); @@ -131,9 +135,9 @@ sky_pass::sky_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffe double3 scaled_color = color_acescg * vmag_lux; // Build vertex - *(star_vertex++) = static_cast(position.x); - *(star_vertex++) = static_cast(position.y); - *(star_vertex++) = static_cast(position.z); + *(star_vertex++) = static_cast(position_inertial.x); + *(star_vertex++) = static_cast(position_inertial.y); + *(star_vertex++) = static_cast(position_inertial.z); *(star_vertex++) = static_cast(scaled_color.x); *(star_vertex++) = static_cast(scaled_color.y); *(star_vertex++) = static_cast(scaled_color.z); @@ -198,13 +202,26 @@ void sky_pass::render(render_context* context) const float time_of_day = time_of_day_tween.interpolate(context->alpha); float julian_day = julian_day_tween.interpolate(context->alpha); - float3 sun_position = sun_position_tween.interpolate(context->alpha); - float2 sun_az_el = sun_az_el_tween.interpolate(context->alpha); - float3 moon_position = moon_position_tween.interpolate(context->alpha); - float2 moon_az_el = moon_az_el_tween.interpolate(context->alpha); float3 horizon_color = horizon_color_tween.interpolate(context->alpha); float3 zenith_color = zenith_color_tween.interpolate(context->alpha); + // Construct tweened inertial to topocentric frame + physics::frame topocentric_frame = + { + topocentric_frame_translation.interpolate(context->alpha), + topocentric_frame_rotation.interpolate(context->alpha) + }; + + // Get topocentric space sun position + float3 sun_position = {0, 0, 0}; + if (sun_object != nullptr) + { + sun_position = math::normalize(sun_object->get_transform_tween().interpolate(context->alpha).translation); + } + + // Get topocentric space moon position + float3 moon_position = {0, 0, 0}; + // Draw sky model { rasterizer->use_program(*sky_shader_program); @@ -234,12 +251,8 @@ void sky_pass::render(render_context* context) const observer_location_input->upload(observer_location); if (sun_position_input) sun_position_input->upload(sun_position); - if (sun_az_el_input) - sun_az_el_input->upload(sun_az_el); if (moon_position_input) moon_position_input->upload(moon_position); - if (moon_az_el_input) - moon_az_el_input->upload(moon_az_el); if (julian_day_input) julian_day_input->upload(julian_day); if (cos_moon_angular_radius_input) @@ -255,7 +268,7 @@ void sky_pass::render(render_context* context) const } // Draw moon model - if (moon_az_el[1] >= -moon_angular_radius) + if (moon_position.y >= -moon_angular_radius) { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE); @@ -265,7 +278,7 @@ void sky_pass::render(render_context* context) const math::transform moon_transform; moon_transform.translation = moon_position * -moon_distance; - moon_transform.rotation = moon_rotation; + moon_transform.rotation = math::quaternion::identity(); moon_transform.scale = {moon_radius, moon_radius, moon_radius}; model = math::matrix_cast(moon_transform); @@ -310,7 +323,10 @@ void sky_pass::render(render_context* context) const //star_transform.rotation = math::normalize(math::type_cast(math::quaternion_cast(rotation))); //star_transform.rotation = math::identity_quaternion; //star_transform.scale = {star_distance, star_distance, star_distance}; - //model = math::matrix_cast(star_transform); + //model = math::matrix_cast(star_transform); + + model = topocentric_frame.matrix(); + model = math::scale(model, {star_distance, star_distance, star_distance}); model_view = view * model; @@ -363,9 +379,7 @@ void sky_pass::set_sky_model(const model* model) sky_gradient2_input = sky_shader_program->get_input("sky_gradient2"); observer_location_input = sky_shader_program->get_input("observer_location"); sun_position_input = sky_shader_program->get_input("sun_position"); - sun_az_el_input = sky_shader_program->get_input("sun_az_el"); moon_position_input = sky_shader_program->get_input("moon_position"); - moon_az_el_input = sky_shader_program->get_input("moon_az_el"); julian_day_input = sky_shader_program->get_input("julian_day"); cos_moon_angular_radius_input = sky_shader_program->get_input("cos_moon_angular_radius"); cos_sun_angular_radius_input = sky_shader_program->get_input("cos_sun_angular_radius"); @@ -418,13 +432,11 @@ void sky_pass::set_moon_model(const model* model) void sky_pass::update_tweens() { julian_day_tween.update(); - sun_position_tween.update(); - sun_az_el_tween.update(); - moon_position_tween.update(); - moon_az_el_tween.update(); time_of_day_tween.update(); horizon_color_tween.update(); zenith_color_tween.update(); + topocentric_frame_translation.update(); + topocentric_frame_rotation.update(); } void sky_pass::set_time_of_day(float time) @@ -458,23 +470,6 @@ void sky_pass::set_observer_location(float altitude, float latitude, float longi observer_location = {altitude, latitude, longitude}; } -void sky_pass::set_sun_coordinates(const float3& position, const float2& az_el) -{ - sun_position_tween[1] = position; - sun_az_el_tween[1] = az_el; -} - -void sky_pass::set_moon_coordinates(const float3& position, const float2& az_el) -{ - moon_position_tween[1] = position; - moon_az_el_tween[1] = az_el; -} - -void sky_pass::set_moon_rotation(const math::quaternion& rotation) -{ - moon_rotation = rotation; -} - void sky_pass::set_moon_angular_radius(float radius) { moon_angular_radius = radius; @@ -487,6 +482,17 @@ void sky_pass::set_sun_angular_radius(float radius) cos_sun_angular_radius = std::cos(sun_angular_radius); } +void sky_pass::set_topocentric_frame(const physics::frame& frame) +{ + topocentric_frame_translation[1] = frame.translation; + topocentric_frame_rotation[1] = frame.rotation; +} + +void sky_pass::set_sun_object(const scene::object_base* object) +{ + sun_object = object; +} + void sky_pass::set_horizon_color(const float3& color) { horizon_color_tween[1] = color; diff --git a/src/renderer/passes/sky-pass.hpp b/src/renderer/passes/sky-pass.hpp index 335eb22..5b0819d 100644 --- a/src/renderer/passes/sky-pass.hpp +++ b/src/renderer/passes/sky-pass.hpp @@ -32,6 +32,8 @@ #include "gl/vertex-array.hpp" #include "gl/texture-2d.hpp" #include "gl/drawing-mode.hpp" +#include "physics/frame.hpp" +#include "scene/object.hpp" class resource_manager; class model; @@ -61,12 +63,12 @@ public: void set_julian_day(float jd); void set_observer_location(float altitude, float latitude, float longitude); - void set_sun_coordinates(const float3& position, const float2& az_el); - void set_moon_coordinates(const float3& position, const float2& az_el); - void set_moon_rotation(const math::quaternion& rotation); void set_moon_angular_radius(float radius); void set_sun_angular_radius(float radius); + + void set_topocentric_frame(const physics::frame& frame); + void set_sun_object(const scene::object_base* object); private: virtual void handle_event(const mouse_moved_event& event); @@ -81,9 +83,7 @@ private: const gl::shader_input* time_of_day_input; const gl::shader_input* observer_location_input; const gl::shader_input* sun_position_input; - const gl::shader_input* sun_az_el_input; const gl::shader_input* moon_position_input; - const gl::shader_input* moon_az_el_input; const gl::shader_input* blue_noise_map_input; const gl::shader_input* julian_day_input; const gl::shader_input* cos_sun_angular_radius_input; @@ -131,19 +131,18 @@ private: const tween* time_tween; tween time_of_day_tween; tween julian_day_tween; - tween sun_position_tween; - tween sun_az_el_tween; - tween moon_position_tween; - tween moon_az_el_tween; tween horizon_color_tween; tween zenith_color_tween; - math::quaternion moon_rotation; + tween topocentric_frame_translation; + tween> topocentric_frame_rotation; float moon_angular_radius; float cos_moon_angular_radius; float sun_angular_radius; float cos_sun_angular_radius; + + const scene::object_base* sun_object; }; #endif // ANTKEEPER_SKY_PASS_HPP