Skip to content

Instantly share code, notes, and snippets.

@kosso
Created November 25, 2016 20:58
Show Gist options
  • Save kosso/0d9cb9397e84a4706bb93369be29da7c to your computer and use it in GitHub Desktop.
Save kosso/0d9cb9397e84a4706bb93369be29da7c to your computer and use it in GitHub Desktop.
function __image main(__image src) {
return fish2sphere.apply(src.definition, null, src);
}
////////
kernel vec4 fish2sphere(sampler src)
{
vec2 pfish;
float theta,phi,r;
vec3 psph;
float FOV = 3.141592654; // FOV of the fisheye, eg: 180 degrees
float width = samplerSize(src).x;
float height = samplerSize(src).y;
// Polar angles
//theta = 2.0 * 3.14159265 * (destCoord().x / width - 0.5); // -pi to pi
// phi = 3.14159265 * (destCoord().y / height - 0.5); // -pi/2 to pi/2
theta = 2.0 * 3.14159265 * (samplerCoord(src).x / width - 0.5); // -pi to pi
phi = 3.14159265 * (samplerCoord(src).y / height - 0.5);
// Vector in 3D space
psph.x = cos(phi) * sin(theta);
psph.y = cos(phi) * cos(theta);
psph.z = sin(phi);
// Calculate fisheye angle and radius
theta = atan(psph.z,psph.x);
phi = atan(sqrt(psph.x*psph.x+psph.z*psph.z),psph.y);
r = width * phi / FOV;
// Pixel in fisheye space
pfish.x = 0.5 * width + r * cos(theta);
pfish.y = 0.5 * width + r * sin(theta);
return sample(src, pfish);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment