Skip to content

Commit

Permalink
Move phase functions into a library
Browse files Browse the repository at this point in the history
  • Loading branch information
Mc-Pain committed Jan 17, 2024
1 parent 6a663c0 commit 0753efa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
19 changes: 19 additions & 0 deletions data/shaders/opengl/lib.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ struct Surface {
float ambientOcclusion;
};

// Phase functions
// https://www.scratchapixel.com/lessons/procedural-generation-virtual-worlds/simulating-sky/simulating-colors-of-the-sky.html
float miePhaseFunction(const float g, const float mu)
{
/*
* Mie phase function:
*/
return 3.f / (8.f * 3.141592) * ((1.f - g * g) * (1.f + mu * mu)) / ((2.f + g * g) * pow(1.f + g * g - 2.f * g * mu, 1.5f));
}

float rayleighPhaseFunction(const float mu)
{
/*
* Rayleigh phase function:
*/
return 3.f / (16.f * 3.141592) * (1 + mu * mu);
}


// Currently used by: hopefully everything
// Evaluates a standard blinn-phong diffuse+specular, with the addition of a
// light intensity term to scale the lighting contribution based on (pre-calculated)
Expand Down
4 changes: 2 additions & 2 deletions data/shaders/opengl/planetrings.frag
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ void main(void)

// second term: diffuse mie (scattering through ring)
float g = 0.76f;
float phaseThrough = 3.f / (8.f * 3.141592) * ((1.f - g * g) * (1.f + mu * mu)) / ((2.f + g * g) * pow(1.f + g * g - 2.f * g * mu, 1.5f));
float phaseThrough = miePhaseFunction(g, mu);

// third term: reflect mie (imitate albedo >= 1.0)
float muRev = dot(-normalize(vec3(uLight[i].position)), eyenorm);
float phaseReflect = 3.f / (8.f * 3.141592) * ((1.f - g * g) * (1.f + muRev * muRev)) / ((2.f + g * g) * pow(1.f + g * g - 2.f * g * muRev, 1.5f));
float phaseReflect = miePhaseFunction(g, muRev);

col = col + texCol * (diffuse + phaseThrough + phaseReflect) * uLight[i].diffuse;
}
Expand Down

0 comments on commit 0753efa

Please sign in to comment.