Skip to content

Commit 9ecaa1f

Browse files
committed
Implement limb darkening for the Sun
1 parent 47da9e8 commit 9ecaa1f

2 files changed

Lines changed: 36 additions & 13 deletions

File tree

data/shaders/planet.frag

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,30 @@ vec3 linearToSRGB(vec3 lin)
185185

186186
void main()
187187
{
188+
#ifndef IS_MOON
189+
if(sunInfo.w==0.)
190+
{
191+
// We are drawing the Sun
192+
vec4 texColor = texture2D(tex, texc);
193+
texColor.rgb = srgbToLinear(texColor.rgb * sunInfo.rgb);
194+
// Reference: chapter 14.7 "Limb Darkening" in "Allen’s Astrophysical Quantities",
195+
// A.N.Cox (ed.), 4th edition, New York: Springer-Verlag, 2002.
196+
// DOI 10.1007/978-1-4612-1186-0
197+
// The values for u2 and v2 for wavelengths 400nm-800nm were taken, linearly
198+
// interpolated, and integrated against CIE 1931 color matching functions.
199+
// The results were transformed from XYZ to linear sRGB color space.
200+
// We call the results for u2 "a1", and for v2 "a2".
201+
const vec3 a2 = vec3(-0.226988526315793, -0.232934589453355, -0.153026433664999);
202+
const vec3 a1 = vec3(0.848380336865573, 0.937696820066542, 0.981762186155682);
203+
const vec3 a0 = vec3(1) - a1 - a2;
204+
float cosTheta = dot(eyeDirection, normalize(normalVS));
205+
float cosTheta2 = cosTheta*cosTheta;
206+
vec3 limbDarkeningCoef = a0 + a1*cosTheta + a2*cosTheta2;
207+
vec3 color = texColor.rgb * limbDarkeningCoef;
208+
FRAG_COLOR = vec4(linearToSRGB(color), texColor.a);
209+
return;
210+
}
211+
#endif
188212
mediump float final_illumination = 1.0;
189213
#ifdef OREN_NAYAR
190214
mediump float lum = 1.;

src/core/modules/Planet.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3882,10 +3882,13 @@ Planet::RenderData Planet::setCommonShaderUniforms(const StelPainter& painter, Q
38823882
GL(shader->setUniformValue(shaderVars.tex, 0));
38833883
GL(shader->setUniformValue(shaderVars.shadowCount, static_cast<GLint>(data.shadowCandidates.size())));
38843884
GL(shader->setUniformValue(shaderVars.shadowData, data.shadowCandidatesData));
3885-
GL(shader->setUniformValue(shaderVars.sunInfo, static_cast<GLfloat>(data.mTarget[12]),
3886-
static_cast<GLfloat>(data.mTarget[13]),
3887-
static_cast<GLfloat>(data.mTarget[14]),
3888-
static_cast<GLfloat>(sun->getEquatorialRadius())));
3885+
if(this!=sun)
3886+
{
3887+
GL(shader->setUniformValue(shaderVars.sunInfo, static_cast<GLfloat>(data.mTarget[12]),
3888+
static_cast<GLfloat>(data.mTarget[13]),
3889+
static_cast<GLfloat>(data.mTarget[14]),
3890+
static_cast<GLfloat>(sun->getEquatorialRadius())));
3891+
}
38893892
GL(shader->setUniformValue(shaderVars.skyBrightness, lmgr->getAtmosphereAverageLuminance()));
38903893
GL(shader->setUniformValue(shaderVars.poleLat, 1.1f, -0.1f)); // Avoid white objects. poleLat is only used for Mars.
38913894

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

39573960
const SolarSystem* ssm = GETSTELMODULE(SolarSystem);
39583961

3959-
if (this==ssm->getSun())
3960-
{
3961-
texMap->bind();
3962-
//painter->setColor(2, 2, 0.2); // This is now in draw3dModel() to apply extinction
3963-
painter->setArrays(reinterpret_cast<const Vec3f*>(projectedVertexArr.constData()), reinterpret_cast<const Vec2f*>(model.texCoordArr.constData()));
3964-
painter->drawFromArray(StelPainter::Triangles, model.indiceArr.size(), 0, false, model.indiceArr.constData());
3965-
return;
3966-
}
3967-
39683962
//cancel out if shaders are invalid
39693963
if(shaderError)
39703964
return;
@@ -4006,6 +4000,11 @@ void Planet::drawSphere(StelPainter* painter, float screenRd, bool drawOnlyRing)
40064000
GL(shader->bind());
40074001

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

40104009
if (rings!=Q_NULLPTR)
40114010
{

0 commit comments

Comments
 (0)