-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
shaders: Add a sobel shader for edge detection
Take an implementation of Sobel edge detection algorithm off the internet and plug it into the main rendering pipeline. Signed-off-by: Alexander Shishkin <[email protected]>
- Loading branch information
Showing
4 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ set(SHADERS "ui" | |
"glyph" | ||
"model" | ||
"contrast" | ||
"sobel" | ||
"combine" | ||
"debug" | ||
"hblur" | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#version 330 | ||
|
||
// Sobel Edge Detection Filter | ||
// GLSL Fragment Shader | ||
// Implementation by Patrick Hebron | ||
// Lifted from https://gist.github.com/Hebali/6ebfc66106459aacee6a9fac029d0115 | ||
|
||
in vec2 pass_tex; | ||
uniform sampler2D model_tex; | ||
uniform float width; | ||
uniform float height; | ||
|
||
layout (location=0) out vec4 FragColor; | ||
|
||
void make_kernel(inout vec4 n[9], sampler2D tex, vec2 coord) | ||
{ | ||
float w = 1.0 / width; | ||
float h = 1.0 / height; | ||
|
||
n[0] = texture(tex, coord + vec2( -w, -h)); | ||
n[1] = texture(tex, coord + vec2(0.0, -h)); | ||
n[2] = texture(tex, coord + vec2( w, -h)); | ||
n[3] = texture(tex, coord + vec2( -w, 0.0)); | ||
n[4] = texture(tex, coord); | ||
n[5] = texture(tex, coord + vec2( w, 0.0)); | ||
n[6] = texture(tex, coord + vec2( -w, h)); | ||
n[7] = texture(tex, coord + vec2(0.0, h)); | ||
n[8] = texture(tex, coord + vec2( w, h)); | ||
} | ||
|
||
void main(void) | ||
{ | ||
vec4 n[9]; | ||
make_kernel(n, model_tex, pass_tex.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)); | ||
|
||
FragColor = n[4]; | ||
if (length(sobel.rgb) > 0.35) | ||
FragColor = vec4(0.0, 0.0, 0.0, 1.0);//vec4( 1.0 - sobel.rgb, 1.0 ); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#version 330 | ||
|
||
in vec3 position; | ||
in vec2 tex; | ||
|
||
uniform mat4 trans; | ||
|
||
out vec2 pass_tex; | ||
|
||
void main() | ||
{ | ||
gl_Position = trans * vec4(position, 1.0); | ||
pass_tex = tex; | ||
} |