Created
July 26, 2012 23:53
-
-
Save RandomEtc/3185293 to your computer and use it in GitHub Desktop.
direction shader for OpenCV flows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uniform sampler2D camTex; | |
uniform sampler2D xTex; | |
uniform sampler2D yTex; | |
// HSL functions from http://code.google.com/p/glmixer/source/browse/trunk/shaders/imageProcessing_fragment.glsl?spec=svn195&r=195 | |
float HueToRGB(in float f1, in float f2, in float hue) | |
{ | |
if (hue < 0.0) | |
hue += 1.0; | |
else if (hue > 1.0) | |
hue -= 1.0; | |
float res; | |
if ((6.0 * hue) < 1.0) | |
res = f1 + (f2 - f1) * 6.0 * hue; | |
else if ((2.0 * hue) < 1.0) | |
res = f2; | |
else if ((3.0 * hue) < 2.0) | |
res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0; | |
else | |
res = f1; | |
return res; | |
} | |
vec3 HSLToRGB(in vec3 hsl) | |
{ | |
vec3 rgb; | |
if (hsl.y == 0.0) | |
rgb = vec3(hsl.z); // Luminance | |
else | |
{ | |
float f1, f2; | |
if (hsl.z < 0.5) | |
f2 = hsl.z * (1.0 + hsl.y); | |
else | |
f2 = (hsl.z + hsl.y) - (hsl.y * hsl.z); | |
f1 = 2.0 * hsl.z - f2; | |
rgb.r = HueToRGB(f1, f2, hsl.x + (1.0/3.0)); | |
rgb.g = HueToRGB(f1, f2, hsl.x); | |
rgb.b = HueToRGB(f1, f2, hsl.x - (1.0/3.0)); | |
} | |
return rgb; | |
} | |
void main() | |
{ | |
float xVal = texture2D( xTex, gl_TexCoord[0].st ).r; | |
float yVal = texture2D( yTex, gl_TexCoord[0].st ).r; | |
// vec3 col = vec3( xVal, yVal, 0.0 ) * 0.5 + 0.5; | |
float angle = (3.1415926536 + atan(yVal, xVal)) / (3.1415926536 * 2.0); | |
vec3 col = HSLToRGB( vec3(angle, length(vec2(xVal, yVal)) / sqrt(2.0), 0.5) ); | |
gl_FragColor.rgb = col; | |
gl_FragColor.a = 1.0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment