Instantly share code, notes, and snippets.

What would you like to do?
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);

This comment has been minimized.

Copy link

Tondy commented Apr 21, 2013

Hello is this full code?


This comment has been minimized.

Copy link

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