~gordallott/+junk/Cvis

« back to all changes in this revision

Viewing changes to data/shader_src/sobel.frag

  • Committer: Gordon Allott
  • Date: 2009-07-16 19:10:16 UTC
  • Revision ID: gord@nala-20090716191016-nk6rxnxtysrzpysc
inital import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Sobel filter, basically a way of finding the edges in an image.
 
2
 * tex = source texture
 
3
 * coord = current fragment coordinate
 
4
 * size = vec2(1.0 / width, 1.0 / height)
 
5
 */
 
6
 
 
7
float sobel(sampler2D tex, vec2 coord, vec2 size) {
 
8
  /* computes a sobel value from the surrounding pixels */
 
9
  float stepw, steph;
 
10
  vec2 basecoord = coord;
 
11
  vec4 vert, hori;
 
12
  stepw = size.x;
 
13
  steph = size.y;
 
14
  
 
15
  vert  = texture2D(tex, basecoord + vec2(-step.x, -step.y)) * -1.0;
 
16
  vert += texture2D(tex, basecoord + vec2(-step.x,  0.0   )) * -2.0;
 
17
  vert += texture2D(tex, basecoord + vec2(-step.x, +step.y)) * -1.0;
 
18
  
 
19
  vert += texture2D(tex, basecoord + vec2(+step.x, -step.y)) *  1.0;
 
20
  vert += texture2D(tex, basecoord + vec2(+step.x,  0.0   )) *  2.0;
 
21
  vert += texture2D(tex, basecoord + vec2(+step.x, +step.y)) *  1.0;
 
22
  
 
23
  hori  = texture2D(tex, basecoord + vec2(-step.x, -step.y)) * -1.0;
 
24
  hori += texture2D(tex, basecoord + vec2( 0.0  ,  -step.y)) * -2.0;
 
25
  hori += texture2D(tex, basecoord + vec2(+step.x, -step.y)) * -1.0;
 
26
 
 
27
  hori += texture2D(tex, basecoord + vec2(-step.x, +step.y)) *  1.0;
 
28
  hori += texture2D(tex, basecoord + vec2( 0.0  ,  +step.y)) *  2.0;
 
29
  hori += texture2D(tex, basecoord + vec2(+step.x, +step.y)) *  1.0;
 
30
  
 
31
  /* returns the dot product */
 
32
  return float(sqrt((vert * vert) + (hori * hori)));
 
33
  
 
34
}
 
35