~gordallott/+junk/Cvis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* Sobel filter, basically a way of finding the edges in an image.
 * tex = source texture
 * coord = current fragment coordinate
 * size = vec2(1.0 / width, 1.0 / height)
 */

float sobel(sampler2D tex, vec2 coord, vec2 size) {
  /* computes a sobel value from the surrounding pixels */
  float stepw, steph;
  vec2 basecoord = coord;
  vec4 vert, hori;
  stepw = size.x;
  steph = size.y;
  
  vert  = texture2D(tex, basecoord + vec2(-step.x, -step.y)) * -1.0;
  vert += texture2D(tex, basecoord + vec2(-step.x,  0.0   )) * -2.0;
  vert += texture2D(tex, basecoord + vec2(-step.x, +step.y)) * -1.0;
  
  vert += texture2D(tex, basecoord + vec2(+step.x, -step.y)) *  1.0;
  vert += texture2D(tex, basecoord + vec2(+step.x,  0.0   )) *  2.0;
  vert += texture2D(tex, basecoord + vec2(+step.x, +step.y)) *  1.0;
  
  hori  = texture2D(tex, basecoord + vec2(-step.x, -step.y)) * -1.0;
  hori += texture2D(tex, basecoord + vec2( 0.0  ,  -step.y)) * -2.0;
  hori += texture2D(tex, basecoord + vec2(+step.x, -step.y)) * -1.0;

  hori += texture2D(tex, basecoord + vec2(-step.x, +step.y)) *  1.0;
  hori += texture2D(tex, basecoord + vec2( 0.0  ,  +step.y)) *  2.0;
  hori += texture2D(tex, basecoord + vec2(+step.x, +step.y)) *  1.0;
  
  /* returns the dot product */
  return float(sqrt((vert * vert) + (hori * hori)));
  
}