Skip to content

Commit

Permalink
Merge pull request pioneerspacesim#6059 from fluffyfreak/cache-atmosp…
Browse files Browse the repository at this point in the history
…herecalculations

Store AtmosphereParameters in BaseSphere, calculate with material setup
  • Loading branch information
fluffyfreak authored Feb 9, 2025
2 parents 3d7881e + 01dd882 commit 1caf5c2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
11 changes: 6 additions & 5 deletions src/BaseSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,22 @@ class BaseSphere {
RefCountedPtr<Graphics::Material> GetSurfaceMaterial() const { return m_surfaceMaterial; }

protected:
virtual void SetUpMaterials() = 0;

// set up shader data for this geosphere's atmosphere
void SetMaterialParameters(const matrix4x4d &t, const float r, const std::vector<Camera::Shadow> &s, const AtmosphereParameters &ap);

const SystemBody *m_sbody;

// all variables for GetHeight(), GetColor()
RefCountedPtr<Terrain> m_terrain;

virtual void SetUpMaterials() = 0;

RefCountedPtr<Graphics::Material> m_surfaceMaterial;
RefCountedPtr<Graphics::Material> m_atmosphereMaterial;

// set up shader data for this geosphere's atmosphere
void SetMaterialParameters(const matrix4x4d &t, const float r, const std::vector<Camera::Shadow> &s, const AtmosphereParameters &ap);

// atmosphere geometry
std::unique_ptr<Graphics::Drawables::Sphere3D> m_atmos;
AtmosphereParameters m_atmosphereParameters;
};

#endif /* _GEOSPHERE_H */
13 changes: 5 additions & 8 deletions src/GasGiant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,15 +635,13 @@ void GasGiant::Render(Graphics::Renderer *renderer, const matrix4x4d &modelView,
SetUpMaterials();

//Update material parameters
//XXX no need to calculate AP every frame
auto ap = GetSystemBody()->CalcAtmosphereParams();
SetMaterialParameters(trans, radius, shadows, ap);
if (ap.atmosDensity > 0.0) {
SetMaterialParameters(trans, radius, shadows, m_atmosphereParameters);
if (m_atmosphereParameters.atmosDensity > 0.0) {
// make atmosphere sphere slightly bigger than required so
// that the edges of the pixel shader atmosphere jizz doesn't
// show ugly polygonal angles
DrawAtmosphereSurface(renderer, trans, campos,
ap.atmosRadius * 1.01,
m_atmosphereParameters.atmosRadius * 1.01,
m_atmosphereMaterial);
}

Expand Down Expand Up @@ -678,7 +676,6 @@ void GasGiant::Render(Graphics::Renderer *renderer, const matrix4x4d &modelView,

void GasGiant::SetUpMaterials()
{

// Request material for this planet, with atmosphere.
// Separate materials for surface and sky.
Graphics::MaterialDescriptor surfDesc;
Expand All @@ -687,8 +684,8 @@ void GasGiant::SetUpMaterials()
surfDesc.textures = 1;

//planetoid with atmosphere
const AtmosphereParameters ap(GetSystemBody()->CalcAtmosphereParams());
assert(ap.atmosDensity > 0.0);
m_atmosphereParameters = GetSystemBody()->CalcAtmosphereParams();
assert(m_atmosphereParameters.atmosDensity > 0.0);
assert(m_surfaceTextureSmall.Valid() || m_surfaceTexture.Valid());

// surface material is solid
Expand Down
12 changes: 5 additions & 7 deletions src/GeoSphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,16 +423,14 @@ void GeoSphere::Render(Graphics::Renderer *renderer, const matrix4x4d &modelView
SetUpMaterials();

//Update material parameters
//XXX no need to calculate AP every frame
auto ap = GetSystemBody()->CalcAtmosphereParams();
SetMaterialParameters(trans, radius, shadows, ap);
SetMaterialParameters(trans, radius, shadows, m_atmosphereParameters);

if (m_atmosphereMaterial.Valid() && ap.atmosDensity > 0.0) {
if (m_atmosphereMaterial.Valid() && m_atmosphereParameters.atmosDensity > 0.0) {
// make atmosphere sphere slightly bigger than required so
// that the edges of the pixel shader atmosphere jizz doesn't
// show ugly polygonal angles
DrawAtmosphereSurface(renderer, trans, campos,
ap.atmosRadius * 1.02,
m_atmosphereParameters.atmosRadius * 1.02,
m_atmosphereMaterial);
}

Expand Down Expand Up @@ -502,6 +500,7 @@ void GeoSphere::Render(Graphics::Renderer *renderer, const matrix4x4d &modelView

void GeoSphere::SetUpMaterials()
{
m_atmosphereParameters = GetSystemBody()->CalcAtmosphereParams();
// normal star has a different setup path than geosphere terrain does
if (GetSystemBody()->GetSuperType() == SystemBody::SUPERTYPE_STAR) {
Graphics::MaterialDescriptor surfDesc;
Expand All @@ -528,9 +527,8 @@ void GeoSphere::SetUpMaterials()
surfDesc.quality &= ~Graphics::HAS_ATMOSPHERE;
} else {
//planetoid with or without atmosphere
const AtmosphereParameters ap(GetSystemBody()->CalcAtmosphereParams());
surfDesc.lighting = true;
if (ap.atmosDensity > 0.0) {
if (m_atmosphereParameters.atmosDensity > 0.0) {
surfDesc.quality |= Graphics::HAS_ATMOSPHERE;
}
}
Expand Down

0 comments on commit 1caf5c2

Please sign in to comment.