Skip to content

Commit

Permalink
shaders: Add a sobel shader for edge detection
Browse files Browse the repository at this point in the history
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
virtuoso committed Oct 13, 2024
1 parent 436feab commit 4edae25
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions demo/ldjam56/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set(SHADERS "ui"
"glyph"
"model"
"contrast"
"sobel"
"combine"
"debug"
"hblur"
Expand Down
4 changes: 3 additions & 1 deletion demo/ldjam56/onehandclap.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ int main(int argc, char **argv, char **envp)
lib_request_shaders("contrast", &scene.shaders);
lib_request_shaders("hblur", &scene.shaders);
lib_request_shaders("vblur", &scene.shaders);
lib_request_shaders("sobel", &scene.shaders);
lib_request_shaders("combine", &scene.shaders);
lib_request_shaders("debug", &scene.shaders);
// lib_request_shaders("terrain", &scene.shaders);
Expand Down Expand Up @@ -429,7 +430,8 @@ int main(int argc, char **argv, char **envp)
pipeline_pass_repeat(bloom_pass, pass, 5);

//struct render_pass *contrast_pass = pipeline_add_pass(main_pl, model_pass, "contrast", false, 0, 0);
pass = pipeline_add_pass(main_pl, model_pass, "combine", false, 0, 0);
pass = pipeline_add_pass(main_pl, model_pass, "sobel", false, 0, 0);
pass = pipeline_add_pass(main_pl, pass, "combine", false, 0, 0);
pipeline_pass_add_source(pass, 2, bloom_pass);

scene.lin_speed = 2.0;
Expand Down
43 changes: 43 additions & 0 deletions shaders/sobel.frag
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 );
}
14 changes: 14 additions & 0 deletions shaders/sobel.vert
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;
}

0 comments on commit 4edae25

Please sign in to comment.