Browse Source

Separate atmospheric scattering-related functions out of astronomy system and into new atmosphere system

master
C. J. Howard 1 week ago
parent
commit
6f4d7471f2
9 changed files with 146 additions and 46 deletions
  1. +1
    -0
      CMakeLists.txt
  2. +3
    -39
      src/ecs/systems/astronomy-system.cpp
  3. +0
    -6
      src/ecs/systems/astronomy-system.hpp
  4. +80
    -0
      src/ecs/systems/atmosphere-system.cpp
  5. +53
    -0
      src/ecs/systems/atmosphere-system.hpp
  6. +5
    -0
      src/game/bootloader.cpp
  7. +2
    -0
      src/game/game-context.hpp
  8. +1
    -1
      src/game/states/play-state.cpp
  9. +1
    -0
      src/physics/atmosphere.hpp

+ 1
- 0
CMakeLists.txt View File

@ -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)

+ 3
- 39
src/ecs/systems/astronomy-system.cpp View File

@ -59,14 +59,7 @@ astronomy_system::astronomy_system(ecs::registry& registry):
reference_atmosphere(nullptr),
sun_light(nullptr),
sky_pass(nullptr)
{
// RGB wavelengths determined by matching wavelengths to XYZ, transforming XYZ to ACEScg, then selecting the max wavelengths for R, G, and B.
rgb_wavelengths_nm = {602.224, 541.069, 448.143};
rgb_wavelengths_m = rgb_wavelengths_nm * 1e-9;
registry.on_construct<ecs::atmosphere_component>().connect<&astronomy_system::on_atmosphere_construct>(this);
registry.on_replace<ecs::atmosphere_component>().connect<&astronomy_system::on_atmosphere_replace>(this);
}
{}
void astronomy_system::update(double t, double dt)
{
@ -93,8 +86,8 @@ void astronomy_system::update(double t, double dt)
inertial_to_topocentric = inertial_to_bcbf * bcbf_to_topocentric;
// Set the transform component translations of orbiting bodies to their topocentric positions
registry.view<orbit_component, transform_component>().each(
[&](ecs::entity entity, const auto& orbit, auto& transform)
registry.view<celestial_body_component, orbit_component, transform_component>().each(
[&](ecs::entity entity, const auto& celestial_body, const auto& orbit, auto& transform)
{
// Transform Cartesian position vector (r) from inertial space to topocentric space
const math::vector3<double> r_topocentric = inertial_to_topocentric * orbit.state.r;
@ -276,33 +269,4 @@ void astronomy_system::set_sky_pass(::sky_pass* pass)
this->sky_pass = pass;
}
void astronomy_system::on_atmosphere_construct(ecs::registry& registry, ecs::entity entity, ecs::atmosphere_component& atmosphere)
{
on_atmosphere_replace(registry, entity, atmosphere);
}
void astronomy_system::on_atmosphere_replace(ecs::registry& registry, ecs::entity entity, ecs::atmosphere_component& atmosphere)
{
// Calculate polarization factors
const double rayleigh_polarization = physics::atmosphere::polarization(atmosphere.index_of_refraction, atmosphere.rayleigh_density);
const double mie_polarization = physics::atmosphere::polarization(atmosphere.index_of_refraction, atmosphere.mie_density);
// Calculate Rayleigh scattering coefficients
atmosphere.rayleigh_scattering =
{
physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.x, atmosphere.rayleigh_density, rayleigh_polarization),
physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.y, atmosphere.rayleigh_density, rayleigh_polarization),
physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.z, atmosphere.rayleigh_density, rayleigh_polarization)
};
// Calculate Mie scattering coefficients
const double mie_scattering = physics::atmosphere::scattering_mie(atmosphere.mie_density, mie_polarization);
atmosphere.mie_scattering =
{
mie_scattering,
mie_scattering,
mie_scattering
};
}
} // namespace ecs

+ 0
- 6
src/ecs/systems/astronomy-system.hpp View File

@ -82,15 +82,9 @@ public:
void set_sky_pass(sky_pass* pass);
private:
void on_atmosphere_construct(ecs::registry& registry, ecs::entity entity, ecs::atmosphere_component& atmosphere);
void on_atmosphere_replace(ecs::registry& registry, ecs::entity entity, ecs::atmosphere_component& atmosphere);
double universal_time;
double time_scale;
double3 rgb_wavelengths_nm;
double3 rgb_wavelengths_m;
ecs::entity reference_entity;
const ecs::orbit_component* reference_orbit;
const ecs::celestial_body_component* reference_body;

+ 80
- 0
src/ecs/systems/atmosphere-system.cpp View File

@ -0,0 +1,80 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "ecs/systems/atmosphere-system.hpp"
#include "physics/atmosphere.hpp"
namespace ecs {
atmosphere_system::atmosphere_system(ecs::registry& registry):
entity_system(registry)
{
// RGB wavelengths determined by matching wavelengths to XYZ, transforming XYZ to ACEScg, then selecting the max wavelengths for R, G, and B.
rgb_wavelengths_nm = {602.224, 541.069, 448.143};
rgb_wavelengths_m = rgb_wavelengths_nm * 1e-9;
registry.on_construct<ecs::atmosphere_component>().connect<&atmosphere_system::on_atmosphere_construct>(this);
registry.on_replace<ecs::atmosphere_component>().connect<&atmosphere_system::on_atmosphere_replace>(this);
}
void atmosphere_system::update(double t, double dt)
{}
void atmosphere_system::update_coefficients(ecs::entity entity)
{
// Abort if entity has no atmosphere component
if (!registry.has<atmosphere_component>(entity))
return;
// Get atmosphere component of the entity
atmosphere_component& atmosphere = registry.get<atmosphere_component>(entity);
// Calculate polarization factors
const double rayleigh_polarization = physics::atmosphere::polarization(atmosphere.index_of_refraction, atmosphere.rayleigh_density);
const double mie_polarization = physics::atmosphere::polarization(atmosphere.index_of_refraction, atmosphere.mie_density);
// Calculate Rayleigh scattering coefficients
atmosphere.rayleigh_scattering =
{
physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.x, atmosphere.rayleigh_density, rayleigh_polarization),
physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.y, atmosphere.rayleigh_density, rayleigh_polarization),
physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.z, atmosphere.rayleigh_density, rayleigh_polarization)
};
// Calculate Mie scattering coefficients
const double mie_scattering = physics::atmosphere::scattering_mie(atmosphere.mie_density, mie_polarization);
atmosphere.mie_scattering =
{
mie_scattering,
mie_scattering,
mie_scattering
};
}
void atmosphere_system::on_atmosphere_construct(ecs::registry& registry, ecs::entity entity, ecs::atmosphere_component& atmosphere)
{
update_coefficients(entity);
}
void atmosphere_system::on_atmosphere_replace(ecs::registry& registry, ecs::entity entity, ecs::atmosphere_component& atmosphere)
{
update_coefficients(entity);
}
} // namespace ecs

+ 53
- 0
src/ecs/systems/atmosphere-system.hpp View File

@ -0,0 +1,53 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_ECS_ATMOSPHERE_SYSTEM_HPP
#define ANTKEEPER_ECS_ATMOSPHERE_SYSTEM_HPP
#include "entity-system.hpp"
#include "ecs/entity.hpp"
#include "utility/fundamental-types.hpp"
#include "ecs/components/atmosphere-component.hpp"
namespace ecs {
/**
* Updates variables related to atmospheric scattering.
*/
class atmosphere_system:
public entity_system
{
public:
atmosphere_system(ecs::registry& registry);
virtual void update(double t, double dt);
private:
void update_coefficients(ecs::entity entity);
void on_atmosphere_construct(ecs::registry& registry, ecs::entity entity, ecs::atmosphere_component& atmosphere);
void on_atmosphere_replace(ecs::registry& registry, ecs::entity entity, ecs::atmosphere_component& atmosphere);
double3 rgb_wavelengths_nm;
double3 rgb_wavelengths_m;
};
} // namespace ecs
#endif // ANTKEEPER_ECS_ATMOSPHERE_SYSTEM_HPP

+ 5
- 0
src/game/bootloader.cpp View File

@ -75,6 +75,7 @@
#include "ecs/systems/painting-system.hpp"
#include "ecs/systems/astronomy-system.hpp"
#include "ecs/systems/blackbody-system.hpp"
#include "ecs/systems/atmosphere-system.hpp"
#include "ecs/systems/orbit-system.hpp"
#include "ecs/components/marker-component.hpp"
#include "ecs/commands.hpp"
@ -864,6 +865,9 @@ void setup_systems(game_context* ctx)
// Setup blackbody system
ctx->blackbody_system = new ecs::blackbody_system(*ctx->ecs_registry);
// Setup atmosphere system
ctx->atmosphere_system = new ecs::atmosphere_system(*ctx->ecs_registry);
// Setup astronomy system
ctx->astronomy_system = new ecs::astronomy_system(*ctx->ecs_registry);
@ -1260,6 +1264,7 @@ void setup_callbacks(game_context* ctx)
ctx->orbit_system->update(t, dt);
ctx->blackbody_system->update(t, dt);
ctx->atmosphere_system->update(t, dt);
ctx->astronomy_system->update(t, dt);
ctx->spatial_system->update(t, dt);
ctx->constraint_system->update(t, dt);

+ 2
- 0
src/game/game-context.hpp View File

@ -84,6 +84,7 @@ namespace ecs
class painting_system;
class astronomy_system;
class blackbody_system;
class atmosphere_system;
class orbit_system;
class behavior_system;
class collision_system;
@ -249,6 +250,7 @@ struct game_context
ecs::tracking_system* tracking_system;
ecs::painting_system* painting_system;
ecs::blackbody_system* blackbody_system;
ecs::atmosphere_system* atmosphere_system;
ecs::astronomy_system* astronomy_system;
ecs::orbit_system* orbit_system;

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

@ -112,7 +112,7 @@ void play_state_enter(game_context* ctx)
orbit.elements.ta = math::radians(0.0);
ecs::blackbody_component blackbody;
blackbody.temperature = 5777.0;
blackbody.temperature = 5778.0;
ecs::transform_component transform;
transform.local = math::identity_transform<float>;

+ 1
- 0
src/physics/atmosphere.hpp View File

@ -21,6 +21,7 @@
#define ANTKEEPER_PHYSICS_ATMOSPHERE_HPP
#include "physics/constants.hpp"
#include "math/constants.hpp"
#include <cmath>
namespace physics {

Loading…
Cancel
Save