-
-
Save icebreaker/43788ab592c98b9dd6bf63a63f7c7d62 to your computer and use it in GitHub Desktop.
GLSL Fragment Shader: Sobel Edge Detection
This file contains hidden or 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
// Sobel Edge Detection Filter | |
// GLSL Fragment Shader | |
// Implementation by Patrick Hebron | |
uniform sampler2D texture; | |
uniform float width; | |
uniform float height; | |
void make_kernel(inout vec4 n[9], sampler2D tex, vec2 coord) | |
{ | |
float w = 1.0 / width; | |
float h = 1.0 / height; | |
n[0] = texture2D(tex, coord + vec2( -w, -h)); | |
n[1] = texture2D(tex, coord + vec2(0.0, -h)); | |
n[2] = texture2D(tex, coord + vec2( w, -h)); | |
n[3] = texture2D(tex, coord + vec2( -w, 0.0)); | |
n[4] = texture2D(tex, coord); | |
n[5] = texture2D(tex, coord + vec2( w, 0.0)); | |
n[6] = texture2D(tex, coord + vec2( -w, h)); | |
n[7] = texture2D(tex, coord + vec2(0.0, h)); | |
n[8] = texture2D(tex, coord + vec2( w, h)); | |
} | |
void main(void) | |
{ | |
vec4 n[9]; | |
make_kernel( n, texture, gl_TexCoord[0].st ); | |
vec4 sobel_edge_h = n[2] + (2.0*n[5]) + n[8] - (n[0] + (2.0*n[3]) + n[6]); | |
vec4 sobel_edge_v = n[0] + (2.0*n[1]) + n[2] - (n[6] + (2.0*n[7]) + n[8]); | |
vec4 sobel = sqrt((sobel_edge_h * sobel_edge_h) + (sobel_edge_v * sobel_edge_v)); | |
gl_FragColor = vec4( 1.0 - sobel.rgb, 1.0 ); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment