Skip to content

Commit

Permalink
rayleigh: fix per-vertex calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
Mc-Pain committed Dec 23, 2023
1 parent 8e90c63 commit c2e8fa7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 34 deletions.
4 changes: 2 additions & 2 deletions data/shaders/opengl/basesphere_uniforms.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ layout(std140) uniform BaseSphereData {
Eclipse eclipse;
};

#ifdef FRAGMENT_SHADER

// NOTE: you must include attributes.glsl first!

// Common code to calculate the diffuse light term for a planet's surface
Expand All @@ -50,6 +48,8 @@ void CalcPlanetDiffuse(inout vec4 diff, in vec4 color, in vec3 L, in vec3 N, in
diff += color * uneclipsed * 0.5 * clampedCosine;
}

#ifdef FRAGMENT_SHADER

// Common code to calculate the specular light term for a planet's surface
// L: light -> surface vector (normalized)
// N: surface normal
Expand Down
31 changes: 2 additions & 29 deletions data/shaders/opengl/rayleigh_fast.frag
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,14 @@
#include "basesphere_uniforms.glsl"
#include "rayleigh.glsl"

uniform int NumShadows;

in vec4 varyingEyepos;
in vec4 vertexColor;

out vec4 frag_color;

void main(void)
{
float skyNear, skyFar;
vec3 eyenorm = normalize(varyingEyepos.xyz);

sphereEntryExitDist(skyNear, skyFar, geosphereCenter, varyingEyepos.xyz, geosphereRadius * geosphereAtmosTopRad);

// a&b scaled so length of 1.0 means planet surface.
vec3 a = (skyNear * eyenorm - geosphereCenter) * geosphereInvRadius;
vec3 b = (skyFar * eyenorm - geosphereCenter) * geosphereInvRadius;

vec4 atmosDiffuse = vec4(0.0);

#if (NUM_LIGHTS > 0)
vec3 surfaceNorm = normalize(skyNear * eyenorm - geosphereCenter);
for (int i=0; i<NUM_LIGHTS; ++i) {
vec3 lightDir = normalize(vec3(uLight[i].position));

float uneclipsed = clamp(calcUneclipsedSky(eclipse, NumShadows, a, b, lightDir), 0.0, 1.0);

float nDotVP = max(0.0, dot(surfaceNorm, lightDir));
float nnDotVP = max(0.0, dot(surfaceNorm, -lightDir)); //need backlight to increase horizon
atmosDiffuse += uLight[i].diffuse * uneclipsed * 0.5*(nDotVP+0.5*clamp(1.0-nnDotVP*4.0,0.0,1.0) * INV_NUM_LIGHTS);
}
#endif
vec4 color = vec4(vertexColor.rgb, 1.0) * 20;

atmosDiffuse.a = 1.0;
frag_color = (atmosDiffuse *
vec4(vertexColor.rgb, 1.0) * 20
);
frag_color = toSRGB(1 - exp(-color));
}
22 changes: 19 additions & 3 deletions data/shaders/opengl/rayleigh_fast.vert
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "basesphere_uniforms.glsl"
#include "rayleigh.glsl"

uniform int NumShadows;

out vec4 varyingEyepos;
out vec4 vertexColor;

Expand All @@ -18,14 +20,28 @@ void main(void)
vec3 eyenorm = normalize(varyingEyepos.xyz);
vec3 specularHighlight = vec3(0.0);

vec2 skyDist = raySphereIntersect(geosphereCenter, eyenorm, geosphereAtmosTopRad);
vec2 atmosDist = raySphereIntersect(geosphereCenter, eyenorm, geosphereAtmosTopRad);

// a&b scaled so length of 1.0 means planet surface.
vec3 a = atmosDist.x * eyenorm - geosphereCenter;
vec3 b = atmosDist.y * eyenorm - geosphereCenter;

float AU = 149598000000.0;

#if (NUM_LIGHTS > 0)
for (int i=0; i<NUM_LIGHTS; ++i) {
vec3 lightDir = normalize(vec3(uLight[i].position));

vec3 sphereCenter = geosphereCenter * geosphereRadius;
specularHighlight += computeIncidentLight(lightDir, eyenorm, sphereCenter, skyDist) * INV_NUM_LIGHTS;
float uneclipsed = clamp(calcUneclipsedSky(eclipse, NumShadows, a, b, lightDir), 0.0, 1.0);

// Convert from radius-relative to real coordinates
vec3 center = geosphereCenter * geosphereRadius;

vec3 lightPosAU = uLight[i].position.xyz / AU;
float intensity = 1.f / dot(lightPosAU, lightPosAU); // magic to avoid calculating length and then squaring it

specularHighlight += computeIncidentLight(lightDir, eyenorm, center, atmosDist, toLinear(uLight[i].diffuse), uneclipsed) * intensity;

}
#endif

Expand Down

0 comments on commit c2e8fa7

Please sign in to comment.