Skip to content

Commit

Permalink
Implement limb darkening for the Sun
Browse files Browse the repository at this point in the history
  • Loading branch information
10110111 committed Jan 23, 2023
1 parent 47da9e8 commit 91ca578
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
23 changes: 23 additions & 0 deletions data/shaders/planet.frag
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,29 @@ vec3 linearToSRGB(vec3 lin)

void main()
{
#ifndef IS_MOON
if(sunInfo.w==0)
{
// We are drawing the Sun
vec4 texColor = texture2D(tex, texc);
texColor.rgb = srgbToLinear(texColor.rgb * sunInfo.rgb);
// Reference: Arthur N. Cox, "Allen's Astrophysical Quantities", 4th edition,
// chapter 14.7 Limb Darkening.
// The values for u2 and v2 for wavelengths 400nm-800nm were taken, linearly
// interpolated, and integrated against CIE 1931 color matching functions.
// The results were transformed from XYZ to linear sRGB color space.
// We call the results for u2 "a1", and for v2 "a2".
const vec3 a2 = vec3(-0.226988526315793, -0.232934589453355, -0.153026433664999);
const vec3 a1 = vec3(0.848380336865573, 0.937696820066542, 0.981762186155682);
const vec3 a0 = vec3(1) - a1 - a2;
float cosTheta = dot(eyeDirection, normalize(normalVS));
float cosTheta2 = cosTheta*cosTheta;
vec3 limbDarkeningCoef = a0 + a1*cosTheta + a2*cosTheta2;
vec3 color = texColor.rgb * limbDarkeningCoef;
FRAG_COLOR = vec4(linearToSRGB(color), texColor.a);
return;
}
#endif
mediump float final_illumination = 1.0;
#ifdef OREN_NAYAR
mediump float lum = 1.;
Expand Down
25 changes: 12 additions & 13 deletions src/core/modules/Planet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3882,10 +3882,13 @@ Planet::RenderData Planet::setCommonShaderUniforms(const StelPainter& painter, Q
GL(shader->setUniformValue(shaderVars.tex, 0));
GL(shader->setUniformValue(shaderVars.shadowCount, static_cast<GLint>(data.shadowCandidates.size())));
GL(shader->setUniformValue(shaderVars.shadowData, data.shadowCandidatesData));
GL(shader->setUniformValue(shaderVars.sunInfo, static_cast<GLfloat>(data.mTarget[12]),
static_cast<GLfloat>(data.mTarget[13]),
static_cast<GLfloat>(data.mTarget[14]),
static_cast<GLfloat>(sun->getEquatorialRadius())));
if(this!=sun)
{
GL(shader->setUniformValue(shaderVars.sunInfo, static_cast<GLfloat>(data.mTarget[12]),
static_cast<GLfloat>(data.mTarget[13]),
static_cast<GLfloat>(data.mTarget[14]),
static_cast<GLfloat>(sun->getEquatorialRadius())));
}
GL(shader->setUniformValue(shaderVars.skyBrightness, lmgr->getAtmosphereAverageLuminance()));
GL(shader->setUniformValue(shaderVars.poleLat, 1.1f, -0.1f)); // Avoid white objects. poleLat is only used for Mars.

Expand Down Expand Up @@ -3956,15 +3959,6 @@ void Planet::drawSphere(StelPainter* painter, float screenRd, bool drawOnlyRing)

const SolarSystem* ssm = GETSTELMODULE(SolarSystem);

if (this==ssm->getSun())
{
texMap->bind();
//painter->setColor(2, 2, 0.2); // This is now in draw3dModel() to apply extinction
painter->setArrays(reinterpret_cast<const Vec3f*>(projectedVertexArr.constData()), reinterpret_cast<const Vec2f*>(model.texCoordArr.constData()));
painter->drawFromArray(StelPainter::Triangles, model.indiceArr.size(), 0, false, model.indiceArr.constData());
return;
}

//cancel out if shaders are invalid
if(shaderError)
return;
Expand Down Expand Up @@ -4006,6 +4000,11 @@ void Planet::drawSphere(StelPainter* painter, float screenRd, bool drawOnlyRing)
GL(shader->bind());

RenderData rData = setCommonShaderUniforms(*painter,shader,*shaderVars);
if(this==ssm->getSun())
{
const auto color = painter->getColor();
GL(shader->setUniformValue(shaderVars->sunInfo, color[0], color[1], color[2], 0.f));
}

if (rings!=Q_NULLPTR)
{
Expand Down

0 comments on commit 91ca578

Please sign in to comment.