Browse Source

Optimize ephemeris position calculations, calculate starlight illuminance from catalog

master
C. J. Howard 2 years ago
parent
commit
3be0096615
5 changed files with 58 additions and 15 deletions
  1. +13
    -2
      src/entity/systems/astronomy.cpp
  2. +4
    -0
      src/entity/systems/astronomy.hpp
  3. +21
    -2
      src/entity/systems/orbit.cpp
  4. +6
    -0
      src/entity/systems/orbit.hpp
  5. +14
    -11
      src/game/world.cpp

+ 13
- 2
src/entity/systems/astronomy.cpp View File

@ -66,7 +66,8 @@ astronomy::astronomy(entity::registry& registry):
moon_light(nullptr), moon_light(nullptr),
camera(nullptr), camera(nullptr),
sky_pass(nullptr), sky_pass(nullptr),
exposure_offset(0.0)
exposure_offset(0.0),
starlight_illuminance(0.0)
{ {
// Construct transformation which transforms coordinates from ENU to EUS // Construct transformation which transforms coordinates from ENU to EUS
enu_to_eus = math::transformation::se3<double> enu_to_eus = math::transformation::se3<double>
@ -79,6 +80,12 @@ astronomy::astronomy(entity::registry& registry):
registry.on_replace<entity::component::celestial_body>().connect<&astronomy::on_celestial_body_replace>(this); registry.on_replace<entity::component::celestial_body>().connect<&astronomy::on_celestial_body_replace>(this);
} }
astronomy::~astronomy()
{
registry.on_construct<entity::component::celestial_body>().disconnect<&astronomy::on_celestial_body_construct>(this);
registry.on_replace<entity::component::celestial_body>().disconnect<&astronomy::on_celestial_body_replace>(this);
}
void astronomy::update(double t, double dt) void astronomy::update(double t, double dt)
{ {
double total_illuminance = 0.0; double total_illuminance = 0.0;
@ -271,7 +278,6 @@ void astronomy::update(double t, double dt)
sky_light_illuminance += sky_illuminance; sky_light_illuminance += sky_illuminance;
// Add starlight illuminance to sky light illuminance // Add starlight illuminance to sky light illuminance
const double starlight_illuminance = 0.0002;
sky_light_illuminance += starlight_illuminance; sky_light_illuminance += starlight_illuminance;
// Add sky light illuminance to total illuminance // Add sky light illuminance to total illuminance
@ -478,6 +484,11 @@ void astronomy::set_exposure_offset(float offset)
exposure_offset = offset; exposure_offset = offset;
} }
void astronomy::set_starlight_illuminance(double illuminance)
{
starlight_illuminance = illuminance;
}
void astronomy::set_sky_pass(::render::sky_pass* pass) void astronomy::set_sky_pass(::render::sky_pass* pass)
{ {
this->sky_pass = pass; this->sky_pass = pass;

+ 4
- 0
src/entity/systems/astronomy.hpp View File

@ -43,6 +43,7 @@ class astronomy:
{ {
public: public:
astronomy(entity::registry& registry); astronomy(entity::registry& registry);
~astronomy();
/** /**
* 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 celestial bodies.
@ -86,6 +87,8 @@ public:
void set_camera(scene::camera* camera); void set_camera(scene::camera* camera);
void set_exposure_offset(float offset); void set_exposure_offset(float offset);
void set_starlight_illuminance(double illuminance);
inline float get_exposure_offset() const { return exposure_offset; }; inline float get_exposure_offset() const { return exposure_offset; };
void set_sky_pass(::render::sky_pass* pass); void set_sky_pass(::render::sky_pass* pass);
@ -115,6 +118,7 @@ private:
scene::camera* camera; scene::camera* camera;
::render::sky_pass* sky_pass; ::render::sky_pass* sky_pass;
float exposure_offset; float exposure_offset;
double starlight_illuminance;
}; };
} // namespace system } // namespace system

+ 21
- 2
src/entity/systems/orbit.cpp View File

@ -29,7 +29,16 @@ orbit::orbit(entity::registry& registry):
ephemeris(nullptr), ephemeris(nullptr),
time(0.0), time(0.0),
time_scale(1.0) time_scale(1.0)
{}
{
registry.on_construct<entity::component::orbit>().connect<&orbit::on_orbit_construct>(this);
registry.on_replace<entity::component::orbit>().connect<&orbit::on_orbit_replace>(this);
}
orbit::~orbit()
{
registry.on_construct<entity::component::orbit>().disconnect<&orbit::on_orbit_construct>(this);
registry.on_replace<entity::component::orbit>().disconnect<&orbit::on_orbit_replace>(this);
}
void orbit::update(double t, double dt) void orbit::update(double t, double dt)
{ {
@ -40,7 +49,7 @@ void orbit::update(double t, double dt)
return; return;
// Calculate positions of ephemeris items, in meters // Calculate positions of ephemeris items, in meters
for (std::size_t i = 0; i < ephemeris->size(); ++i)
for (int i: ephemeris_indices)
positions[i] = (*ephemeris)[i].position(time) * 1000.0; positions[i] = (*ephemeris)[i].position(time) * 1000.0;
// Propagate orbits // Propagate orbits
@ -75,5 +84,15 @@ void orbit::set_time_scale(double scale)
time_scale = scale; time_scale = scale;
} }
void orbit::on_orbit_construct(entity::registry& registry, entity::id entity_id, entity::component::orbit& component)
{
ephemeris_indices.insert(component.ephemeris_index);
}
void orbit::on_orbit_replace(entity::registry& registry, entity::id entity_id, entity::component::orbit& component)
{
ephemeris_indices.insert(component.ephemeris_index);
}
} // namespace system } // namespace system
} // namespace entity } // namespace entity

+ 6
- 0
src/entity/systems/orbit.hpp View File

@ -25,6 +25,7 @@
#include "entity/id.hpp" #include "entity/id.hpp"
#include "entity/components/orbit.hpp" #include "entity/components/orbit.hpp"
#include "physics/orbit/ephemeris.hpp" #include "physics/orbit/ephemeris.hpp"
#include <unordered_set>
namespace entity { namespace entity {
namespace system { namespace system {
@ -37,6 +38,7 @@ class orbit:
{ {
public: public:
orbit(entity::registry& registry); orbit(entity::registry& registry);
~orbit();
/** /**
* Scales then adds the timestep `dt` to the current time, then recalculates the positions of orbiting bodies. * Scales then adds the timestep `dt` to the current time, then recalculates the positions of orbiting bodies.
@ -68,10 +70,14 @@ public:
void set_ephemeris(const physics::orbit::ephemeris<double>* ephemeris); void set_ephemeris(const physics::orbit::ephemeris<double>* ephemeris);
private: private:
void on_orbit_construct(entity::registry& registry, entity::id entity_id, entity::component::orbit& component);
void on_orbit_replace(entity::registry& registry, entity::id entity_id, entity::component::orbit& component);
const physics::orbit::ephemeris<double>* ephemeris; const physics::orbit::ephemeris<double>* ephemeris;
double time; double time;
double time_scale; double time_scale;
std::vector<double3> positions; std::vector<double3> positions;
std::unordered_set<int> ephemeris_indices;
}; };
} // namespace system } // namespace system

+ 14
- 11
src/game/world.cpp View File

@ -75,6 +75,7 @@ void create_stars(game::context& ctx)
float* star_vertex_data = new float[star_count * star_vertex_size]; float* star_vertex_data = new float[star_count * star_vertex_size];
float* star_vertex = star_vertex_data; float* star_vertex = star_vertex_data;
// Init starlight illuminance
double starlight_illuminance = 0.0; double starlight_illuminance = 0.0;
// Build star catalog vertex data // Build star catalog vertex data
@ -100,8 +101,6 @@ void create_stars(game::context& ctx)
continue; continue;
} }
starlight_illuminance += physics::light::vmag::to_illuminance(vmag);
// Convert right ascension and declination from degrees to radians // Convert right ascension and declination from degrees to radians
ra = math::wrap_radians(math::radians(ra)); ra = math::wrap_radians(math::radians(ra));
dec = math::wrap_radians(math::radians(dec)); dec = math::wrap_radians(math::radians(dec));
@ -109,6 +108,9 @@ void create_stars(game::context& ctx)
// Convert ICRF coordinates from spherical to Cartesian // Convert ICRF coordinates from spherical to Cartesian
double3 position = physics::orbit::frame::bci::cartesian(double3{1.0, dec, ra}); double3 position = physics::orbit::frame::bci::cartesian(double3{1.0, dec, ra});
// Convert apparent magnitude to brightness factor relative to a 0th magnitude star
double brightness = physics::light::vmag::to_brightness(vmag);
// Convert color index to color temperature // Convert color index to color temperature
double cct = color::index::bv_to_cct(bv_color); double cct = color::index::bv_to_cct(bv_color);
@ -118,24 +120,22 @@ void create_stars(game::context& ctx)
// Transform XYZ color to ACEScg colorspace // Transform XYZ color to ACEScg colorspace
double3 color_acescg = color::xyz::to_acescg(color_xyz); double3 color_acescg = color::xyz::to_acescg(color_xyz);
// Convert apparent magnitude to brightness factor relative to a 0th magnitude star
double brightness = physics::light::vmag::to_brightness(vmag);
// Scale color by relative brightness // Scale color by relative brightness
double3 scaled_color = color_acescg * brightness;
color_acescg *= brightness;
// Build vertex // Build vertex
*(star_vertex++) = static_cast<float>(position.x); *(star_vertex++) = static_cast<float>(position.x);
*(star_vertex++) = static_cast<float>(position.y); *(star_vertex++) = static_cast<float>(position.y);
*(star_vertex++) = static_cast<float>(position.z); *(star_vertex++) = static_cast<float>(position.z);
*(star_vertex++) = static_cast<float>(scaled_color.x);
*(star_vertex++) = static_cast<float>(scaled_color.y);
*(star_vertex++) = static_cast<float>(scaled_color.z);
*(star_vertex++) = static_cast<float>(color_acescg.x);
*(star_vertex++) = static_cast<float>(color_acescg.y);
*(star_vertex++) = static_cast<float>(color_acescg.z);
*(star_vertex++) = static_cast<float>(brightness); *(star_vertex++) = static_cast<float>(brightness);
// Convert apparent magnitude to illuminance and add to total starlight
starlight_illuminance += physics::light::vmag::to_illuminance(vmag);
} }
std::cout << "TOTAL STARLIGHT: " << starlight_illuminance << std::endl;
// Unload star catalog // Unload star catalog
ctx.resource_manager->unload("stars.csv"); ctx.resource_manager->unload("stars.csv");
@ -188,6 +188,9 @@ void create_stars(game::context& ctx)
// Pass stars model to sky pass // Pass stars model to sky pass
ctx.sky_pass->set_stars_model(stars_model); ctx.sky_pass->set_stars_model(stars_model);
// Pass starlight illuminance to astronomy system
ctx.astronomy_system->set_starlight_illuminance(starlight_illuminance);
} }
void create_sun(game::context& ctx) void create_sun(game::context& ctx)

Loading…
Cancel
Save