Skip to content

Instantly share code, notes, and snippets.

@Endox
Created February 10, 2013 18:21
Show Gist options
  • Save Endox/4750473 to your computer and use it in GitHub Desktop.
Save Endox/4750473 to your computer and use it in GitHub Desktop.
Oculus Rift lens distortion correction fragment shader snippet in GLSL
// Exact distortion parameters (a, b, c) are not known yet, these are just placeholers
float a = 0.20f;
float b = 0.00f;
float c = 0.00f;
float d = 1 - (a + b + c);
// Calculate the source location "radius" (distance from the centre of the viewport)
// fragPos - xy position of the current fragment (destination) in NDC space [-1 1]^2
float destR = length(fragPos);
float srcR = a * pow(destR,4) + b * pow(destR,3) + c * pow(destR,2) + d * destR;
// Calculate the source vector (radial)
vec2 correctedRadial = normalize(fragPos) * srcR;
// Transform the coordinates (from [-1,1]^2 to [0, 1]^2)
vec2 uv = (correctedRadial/2.0f) + vec2(0.5f);
// Sample the texture at the source location
fragColor = texture(tex, uv);
@TonderaiZR
Copy link

Hello is this full code?

@Endox
Copy link
Author

Endox commented Jun 4, 2013

Hi, sorry for replying so late. It's not a full code, just a snippet from the fragment shader. It will work if you declare and bind those variables correctly (fragPos, fragColor and tex).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment