Skip to content

Instantly share code, notes, and snippets.

@SmashMaster
SmashMaster / spherecaps.glsl
Created July 23, 2015 05:23
GLSL spherical cap functions
/**
* Projects a sphere with the given radius and distance onto a unit sphere at
* the origin, creating a spherical cap. Returns the angular radius of the cap,
* as well as its surface area as a fraction of the total solid angle.
*/
void sphereCap(float radius, float distance, out float angularRadius, out float solidAngleFrac) {
float apparentRadius = radius/distance;
angularRadius = asin(apparentRadius);
solidAngleFrac = 0.5 - sqrt(1.0 - apparentRadius)*0.5;
}