mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-01-10 05:32:18 -06:00
Address comments on PR and make code more readable
This commit is contained in:
@@ -270,42 +270,44 @@ void AtmosphereDeferredcaster::deinitialize() {
|
||||
|
||||
void AtmosphereDeferredcaster::update(const UpdateData&) {}
|
||||
|
||||
float AtmosphereDeferredcaster::eclipseShadow(glm::dvec3 position, bool ground)
|
||||
{
|
||||
float AtmosphereDeferredcaster::eclipseShadow(glm::dvec3 position) {
|
||||
// This code is copied from the atmosphere deferred fragment shader
|
||||
// It is used to calculate the eclipse shadow
|
||||
const ShadowRenderingStruct& shadow = _shadowDataArrayCache.front();
|
||||
if (_shadowDataArrayCache.empty() || !shadow.isShadowing) {
|
||||
return 1.0f;
|
||||
return 1.f;
|
||||
}
|
||||
|
||||
glm::dvec3 pc = shadow.casterPositionVec - position;
|
||||
glm::dvec3 scNorm = shadow.sourceCasterVec;
|
||||
glm::dvec3 pcProj = dot(pc, scNorm) * scNorm;
|
||||
glm::dvec3 d = pc - pcProj;
|
||||
const glm::dvec3 positionToCaster = shadow.casterPositionVec - position;
|
||||
const glm::dvec3 sourceToCaster = shadow.sourceCasterVec; // Normalized
|
||||
const glm::dvec3 casterShadow = dot(positionToCaster, sourceToCaster) * sourceToCaster;
|
||||
const glm::dvec3 positionToShadow = positionToCaster - casterShadow;
|
||||
|
||||
float length_d = float(length(d));
|
||||
double lengthPcProj = length(pcProj);
|
||||
float distanceToShadow = static_cast<float>(length(positionToShadow));
|
||||
double shadowLength = length(casterShadow);
|
||||
|
||||
float r_p_pi = float(shadow.rc * (lengthPcProj + shadow.xp) / shadow.xp);
|
||||
float r_u_pi = float(shadow.rc * (shadow.xu - lengthPcProj) / shadow.xu);
|
||||
float radiusPenumbra = static_cast<float>(
|
||||
shadow.radiusCaster * (shadowLength + shadow.penumbra) / shadow.penumbra
|
||||
);
|
||||
float radiusUmbra = static_cast<float>(
|
||||
shadow.radiusCaster * (shadow.umbra - shadowLength) / shadow.umbra
|
||||
);
|
||||
|
||||
if (length_d < r_u_pi) {
|
||||
// umbra
|
||||
// Is the position in the umbra - the fully shaded part
|
||||
if (distanceToShadow < radiusUmbra) {
|
||||
if (_hardShadowsEnabled) {
|
||||
return ground ? 0.2 : 0.5;
|
||||
return 0.5f;
|
||||
}
|
||||
else {
|
||||
// butterworth function
|
||||
return sqrt(r_u_pi / (r_u_pi + pow(length_d, 4.0)));
|
||||
// Smooth the shadow with the butterworth function
|
||||
return sqrt(radiusUmbra / (radiusUmbra + pow(distanceToShadow, 4.f)));
|
||||
}
|
||||
}
|
||||
else if (length_d < r_p_pi) {
|
||||
// penumbra
|
||||
return _hardShadowsEnabled ? 0.5 : length_d / r_p_pi;
|
||||
else if (distanceToShadow < radiusPenumbra) { // In penumbra - partially shaded part
|
||||
return _hardShadowsEnabled ? 0.5f : distanceToShadow / radiusPenumbra;
|
||||
}
|
||||
else {
|
||||
return 1.0;
|
||||
return 1.f;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -430,9 +432,10 @@ void AtmosphereDeferredcaster::preRaycast(const RenderData& data, const Deferred
|
||||
return;
|
||||
}
|
||||
|
||||
const double sourceScale = std::max(glm::compMax(sourceNode->scale()), 1.0);
|
||||
const double casterScale = std::max(glm::compMax(casterNode->scale()), 1.0);
|
||||
|
||||
double sourceScale = std::max(glm::compMax(sourceNode->scale()), 1.0);
|
||||
double casterScale = std::max(glm::compMax(casterNode->scale()), 1.0);
|
||||
double actualSourceRadius = shadowConf.source.second * sourceScale;
|
||||
double actualCasterRadius = shadowConf.caster.second * casterScale;
|
||||
// First we determine if the caster is shadowing the current planet
|
||||
// (all calculations in World Coordinates):
|
||||
glm::dvec3 planetCasterVec = casterPos - data.modelTransform.translation;
|
||||
@@ -442,11 +445,9 @@ void AtmosphereDeferredcaster::preRaycast(const RenderData& data, const Deferred
|
||||
(glm::dot(planetCasterVec, sourceCasterVec) / (scLength * scLength)) *
|
||||
sourceCasterVec;
|
||||
double dTest = glm::length(planetCasterVec - planetCasterProj);
|
||||
double xpTest = shadowConf.caster.second * casterScale *
|
||||
scLength /
|
||||
(shadowConf.source.second * sourceScale +
|
||||
shadowConf.caster.second * casterScale);
|
||||
double rpTest = shadowConf.caster.second * casterScale *
|
||||
double xpTest = actualCasterRadius * scLength /
|
||||
(actualSourceRadius + actualCasterRadius);
|
||||
double rpTest = actualCasterRadius *
|
||||
(glm::length(planetCasterProj) + xpTest) / xpTest;
|
||||
|
||||
double casterDistSun = glm::length(casterPos - sunPosWorld);
|
||||
@@ -462,11 +463,11 @@ void AtmosphereDeferredcaster::preRaycast(const RenderData& data, const Deferred
|
||||
{
|
||||
// The current caster is shadowing the current planet
|
||||
shadow.isShadowing = true;
|
||||
shadow.rs = shadowConf.source.second * sourceScale;
|
||||
shadow.rc = shadowConf.caster.second * casterScale;
|
||||
shadow.radiusSource = actualSourceRadius;
|
||||
shadow.radiusCaster = actualCasterRadius;
|
||||
shadow.sourceCasterVec = glm::normalize(sourceCasterVec);
|
||||
shadow.xp = xpTest;
|
||||
shadow.xu = shadow.rc * scLength / (shadow.rs - shadow.rc);
|
||||
shadow.penumbra = xpTest;
|
||||
shadow.umbra = shadow.radiusCaster * scLength / (shadow.radiusSource - shadow.radiusCaster);
|
||||
shadow.casterPositionVec = casterPos;
|
||||
}
|
||||
_shadowDataArrayCache.push_back(shadow);
|
||||
@@ -483,11 +484,11 @@ void AtmosphereDeferredcaster::preRaycast(const RenderData& data, const Deferred
|
||||
|
||||
if (sd.isShadowing) {
|
||||
std::strcpy(bf, "].xp\0");
|
||||
prg.setUniform(_uniformNameBuffer, sd.xp);
|
||||
prg.setUniform(_uniformNameBuffer, sd.penumbra);
|
||||
std::strcpy(bf, "].xu\0");
|
||||
prg.setUniform(_uniformNameBuffer, sd.xu);
|
||||
prg.setUniform(_uniformNameBuffer, sd.umbra);
|
||||
std::strcpy(bf, "].rc\0");
|
||||
prg.setUniform(_uniformNameBuffer, sd.rc);
|
||||
prg.setUniform(_uniformNameBuffer, sd.radiusCaster);
|
||||
std::strcpy(bf, "].sourceCasterVec\0");
|
||||
prg.setUniform(_uniformNameBuffer, sd.sourceCasterVec);
|
||||
std::strcpy(bf, "].casterPositionVec\0");
|
||||
|
||||
@@ -46,10 +46,10 @@ struct DeferredcastData;
|
||||
struct ShadowConfiguration;
|
||||
|
||||
struct ShadowRenderingStruct {
|
||||
double xu = 0.0;
|
||||
double xp = 0.0;
|
||||
double rs = 0.0;
|
||||
double rc = 0.0;
|
||||
double umbra = 0.0;
|
||||
double penumbra = 0.0;
|
||||
double radiusSource = 0.0;
|
||||
double radiusCaster = 0.0;
|
||||
glm::dvec3 sourceCasterVec = glm::dvec3(0.0);
|
||||
glm::dvec3 casterPositionVec = glm::dvec3(0.0);
|
||||
bool isShadowing = false;
|
||||
@@ -75,7 +75,7 @@ public:
|
||||
void initializeCachedVariables(ghoul::opengl::ProgramObject& program) override;
|
||||
|
||||
void update(const UpdateData&) override;
|
||||
float eclipseShadow(glm::dvec3 position, bool ground);
|
||||
float eclipseShadow(glm::dvec3 position);
|
||||
|
||||
void calculateAtmosphereParameters();
|
||||
|
||||
|
||||
@@ -33,8 +33,8 @@
|
||||
#include <ghoul/misc/profiling.h>
|
||||
#include <openspace/properties/property.h>
|
||||
#include <openspace/rendering/deferredcastermanager.h>
|
||||
#include <math.h>
|
||||
#include <algorithm>
|
||||
#include <math.h>
|
||||
|
||||
namespace {
|
||||
constexpr float KM_TO_M = 1000.f;
|
||||
@@ -273,7 +273,7 @@ documentation::Documentation RenderableAtmosphere::Documentation() {
|
||||
|
||||
RenderableAtmosphere::RenderableAtmosphere(const ghoul::Dictionary& dictionary)
|
||||
: Renderable(dictionary)
|
||||
, _atmosphereHeight(AtmosphereHeightInfo, 60.f, 0.1f, 99.0f)
|
||||
, _atmosphereHeight(AtmosphereHeightInfo, 60.f, 0.1f, 99.f)
|
||||
, _groundAverageReflectance(AverageGroundReflectanceInfo, 0.f, 0.f, 1.f)
|
||||
, _groundRadianceEmission(GroundRadianceEmissionInfo, 0.f, 0.f, 1.f)
|
||||
, _rayleighHeightScale(RayleighHeightScaleInfo, 0.f, 0.1f, 50.f)
|
||||
@@ -502,15 +502,16 @@ void RenderableAtmosphere::updateAtmosphereParameters() {
|
||||
// Calculate atmosphere dimming coefficient
|
||||
void RenderableAtmosphere::setDimmingCoefficient(const glm::dmat4& modelTransform) {
|
||||
// Calculate if the camera is in the atmosphere and if it is in the sunny region
|
||||
glm::dvec3 cameraPos = global::navigationHandler->camera()->positionVec3();
|
||||
glm::dvec3 planetPos = glm::dvec3(modelTransform * glm::dvec4(0.0, 0.0, 0.0, 1.0));
|
||||
const glm::dvec3 cameraPos = global::navigationHandler->camera()->positionVec3();
|
||||
// TODO: change the assumption that the Sun is placed in the origin
|
||||
const glm::dvec3 planetPos = glm::dvec3(modelTransform * glm::dvec4(0.0, 0.0, 0.0, 1.0));
|
||||
const glm::dvec3 normalUnderCamera = glm::normalize(cameraPos - planetPos);
|
||||
const glm::dvec3 vecToSun = glm::normalize(-planetPos);
|
||||
|
||||
float cameraDistance = static_cast<float>(glm::distance(planetPos, cameraPos));
|
||||
glm::dvec3 normalUnderCamera = glm::normalize(cameraPos - planetPos);
|
||||
glm::dvec3 vecToSun = glm::normalize(-planetPos);
|
||||
|
||||
float cameraSunAngle = glm::degrees(static_cast<float>(
|
||||
glm::acos(glm::dot(vecToSun, normalUnderCamera))
|
||||
));
|
||||
float cameraSunAngle = static_cast<float>(
|
||||
glm::degrees(glm::acos(glm::dot(vecToSun, normalUnderCamera))
|
||||
));
|
||||
float sunsetEnd = _atmosphereDimmingSunsetAngle.value().y;
|
||||
|
||||
// If cameraSunAngle is more than 90 degrees, we are in shaded part of globe
|
||||
@@ -520,7 +521,7 @@ void RenderableAtmosphere::setDimmingCoefficient(const glm::dmat4& modelTransfor
|
||||
bool cameraIsInAtmosphere = cameraDistance < atmosphereEdge;
|
||||
|
||||
// Don't fade if camera is not in the sunny part of an atmosphere
|
||||
if (!(cameraIsInAtmosphere && cameraIsInSun)) {
|
||||
if (!cameraIsInAtmosphere || !cameraIsInSun) {
|
||||
return;
|
||||
}
|
||||
// Else we need to fade the objects
|
||||
@@ -534,10 +535,10 @@ void RenderableAtmosphere::setDimmingCoefficient(const glm::dmat4& modelTransfor
|
||||
bool cameraIsInSunset = cameraSunAngle > sunsetStart && cameraIsInSun;
|
||||
|
||||
// See if we are inside of an eclipse shadow
|
||||
float eclipseShadow = _deferredcaster->eclipseShadow(cameraPos, false);
|
||||
bool cameraIsInEclipse = std::abs(eclipseShadow - 1.0f) > glm::epsilon<float>();
|
||||
float eclipseShadow = _deferredcaster->eclipseShadow(cameraPos);
|
||||
bool cameraIsInEclipse = std::abs(eclipseShadow - 1.f) > glm::epsilon<float>();
|
||||
// Invert shadow and multiply with itself to make it more narrow
|
||||
eclipseShadow = std::pow(1.0f - eclipseShadow, 2.0f);
|
||||
eclipseShadow = std::pow(1.f - eclipseShadow, 2.f);
|
||||
float atmosphereDimming = 0.f;
|
||||
|
||||
if (cameraIsInSunset) {
|
||||
@@ -550,7 +551,7 @@ void RenderableAtmosphere::setDimmingCoefficient(const glm::dmat4& modelTransfor
|
||||
// Fading - linear interpolation
|
||||
float fading = (cameraDistance - atmosphereInnerEdge) /
|
||||
atmosphereFadingHeight;
|
||||
atmosphereDimming = std::clamp(eclipseShadow + fading, 0.0f, 1.0f);
|
||||
atmosphereDimming = std::clamp(eclipseShadow + fading, 0.f, 1.f);
|
||||
}
|
||||
else if (cameraIsInFadingRegion) {
|
||||
// Fade with regards to altitude
|
||||
@@ -563,8 +564,7 @@ void RenderableAtmosphere::setDimmingCoefficient(const glm::dmat4& modelTransfor
|
||||
}
|
||||
else {
|
||||
// Camera is below fading region - atmosphere dims objects completely
|
||||
atmosphereDimming = 0.0f;
|
||||
|
||||
atmosphereDimming = 0.f;
|
||||
}
|
||||
// Calculate dimming coefficient for stars, labels etc that are dimmed in the
|
||||
// atmosphere
|
||||
|
||||
Reference in New Issue
Block a user