Skip to content

Commit

Permalink
s/baricStep/scaleHeight
Browse files Browse the repository at this point in the history
  • Loading branch information
Mc-Pain committed Jan 18, 2024
1 parent bc16cca commit 8423738
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion data/shaders/opengl/basesphere_uniforms.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ layout(std140) uniform BaseSphereData {
vec4 atmosColor;
vec3 coefficientsR; // coefficients for approximating the Rayleigh contribution
vec3 coefficientsM; // coefficients for approximating the Mie contribution
vec2 baricStep; // height for (R, M) in km, at which density will be reduced by e
vec2 scaleHeight; // height for (R, M) in km, at which density will be reduced by e

// Eclipse data
Eclipse eclipse;
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/opengl/rayleigh.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void scatter(out vec2 density, const in vec3 orig, const in vec3 center)
{
float height = height(orig, center);

density = -height / baricStep;
density = -height / scaleHeight;

// earth atmospheric density: 1.225 kg/m^3, divided by 1e5
// 1/1.225e-5 = 81632.65306
Expand Down
4 changes: 2 additions & 2 deletions src/BaseSphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct BaseSphereDataBlock {
Color4f atmosColor;
alignas(16) vector3f coefficientsR;
alignas(16) vector3f coefficientsM;
alignas(16) vector2f baricStep;
alignas(16) vector2f scaleHeight;

// Eclipse struct data
alignas(16) vector3f shadowCentreX;
Expand Down Expand Up @@ -102,7 +102,7 @@ void BaseSphere::SetMaterialParameters(const matrix4x4d &trans, const float radi
matData.atmosColor = ap.atmosCol.ToColor4f();
matData.coefficientsR = ap.rayleighCoefficients;
matData.coefficientsM = ap.mieCoefficients;
matData.baricStep = ap.baricStep;
matData.scaleHeight = ap.scaleHeight;

// we handle up to three shadows at a time
auto it = shadows.cbegin(), itEnd = shadows.cend();
Expand Down
2 changes: 1 addition & 1 deletion src/galaxy/AtmosphereParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct AtmosphereParameters {
vector3d center;
vector3f rayleighCoefficients;
vector3f mieCoefficients;
vector2f baricStep;
vector2f scaleHeight;
};

#endif // ATMOSPHEREPARAMETERS_H_INCLUDED
14 changes: 7 additions & 7 deletions src/galaxy/SystemBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ bool SystemBody::IsScoopable() const
// planet radius, height of atmosphere (not its radius), length of perpendicular between ray and planet center, lapse rate at which density fades out by rate of e
// this function is used in GetCoefficients
// all input units are in km
double SystemBody::ComputeDensity(const double radius, const double atmosphereHeight, const double h, const double baricStep) const
double SystemBody::ComputeDensity(const double radius, const double atmosphereHeight, const double h, const double scaleHeight) const
{
int numSamples = 16;
double totalHeight = radius + atmosphereHeight;
Expand All @@ -281,7 +281,7 @@ double SystemBody::ComputeDensity(const double radius, const double atmosphereHe
for (int i = 0; i < numSamples; ++i) {
double t = tmin + step * (i + 0.5);
double h = sqrt(minHeight * minHeight + t * t) - radius;
density += step * exp(-h / baricStep);
density += step * exp(-h / scaleHeight);
}

return density;
Expand All @@ -290,20 +290,20 @@ double SystemBody::ComputeDensity(const double radius, const double atmosphereHe
// these coefficients are to be passed into shaders, meant to accelerate scattering computation per-pixel
// instead of sampling each one (which I suspect is VERY SLOW)
// all input units are in km
vector3f SystemBody::GetCoefficients(const double radius, const double atmHeight, const double baricStep) const
vector3f SystemBody::GetCoefficients(const double radius, const double atmHeight, const double scaleHeight) const
{
float k, b, c;

// compute full out-scattering densities at 0 and 1 km heights
// k = density at 0 km
// b = log(density0km / density1km) - log is used to multiply it by actual height
k = ComputeDensity(radius, atmHeight, 0.f, baricStep);
b = log(k / ComputeDensity(radius, atmHeight, 1.f, baricStep));
k = ComputeDensity(radius, atmHeight, 0.f, scaleHeight);
b = log(k / ComputeDensity(radius, atmHeight, 1.f, scaleHeight));

// compute c - erf coefficient, which adjusts slope of erf near t=0 to match actual in-scattering
float erf1_minus_erf0 = 0.421463;
float sHeight = sqrt(radius * radius + 1.f) - radius;
float c1 = exp(-sHeight / baricStep);
float c1 = exp(-sHeight / scaleHeight);
float c2 = k * erf1_minus_erf0;
c = c1 / c2;
return vector3f(k, b, c);
Expand Down Expand Up @@ -371,7 +371,7 @@ AtmosphereParameters SystemBody::CalcAtmosphereParams() const
const float atmosHeight_in_km = radiusPlanet_in_km * (params.atmosRadius - 1);
params.rayleighCoefficients = GetCoefficients(radiusPlanet_in_km, atmosHeight_in_km, atmosScaleHeight);
params.mieCoefficients = GetCoefficients(radiusPlanet_in_km, atmosHeight_in_km, atmosScaleHeight / 6.66); // 7994 / 1200 = 6.61
params.baricStep = vector2f(atmosScaleHeight, atmosScaleHeight / 6.66);
params.scaleHeight = vector2f(atmosScaleHeight, atmosScaleHeight / 6.66);

return params;
}
Expand Down
4 changes: 2 additions & 2 deletions src/galaxy/SystemBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ class SystemBody : public RefCounted, public SystemBodyType, protected SystemBod
double GetAtmDensity(double altitude, double pressure) const;

// for rayleigh scattering
double ComputeDensity(const double radius, const double atmosphereHeight, const double h, const double baricStep) const;
vector3f GetCoefficients(const double radius, const double atmHeight, const double baricStep) const;
double ComputeDensity(const double radius, const double atmosphereHeight, const double h, const double scaleHeight) const;
vector3f GetCoefficients(const double radius, const double atmHeight, const double scaleHeight) const;

AtmosphereParameters CalcAtmosphereParams() const;

Expand Down

0 comments on commit 8423738

Please sign in to comment.