File: Convolve2DRectTexture1ChannelShader.frag.txt

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (42 lines) | stat: -rw-r--r-- 1,655 bytes parent folder | download | duplicates (7)
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
36
37
38
39
40
41
42
/* Generic 2D convolution fragment shader for 2D rectangle textures.
// OpenGL program has to setup the texture unit 'Kernel' with a lookup
// table texture for the coefficients of the convolution kernel and bind
// texture unit 'Image' with the image to be convoluted. Texture filtering
// mode needs to be GL_NEAREST for defined results! The half width of the
// convolution kernel needs to be provided in the uniform 'KernelHalfWidthX'
// and 'KernelHalfWidthY' for x and y half-width.
//
// Please note that it is more efficient to provide the convolution kernel
// in an array of uniforms or compiled into the shader. This is less flexible
// as it doesn't allow to change kernels on the fly and it only works for a
// rather limited kernel size of say 15 by 15 on Geforce 7000 class hardware,
// but it is significantly faster if it can be used.
//
// (w)2006 by Mario Kleiner. Licensed under MIT license.
*/

#extension GL_ARB_texture_rectangle : enable

uniform float KernelHalfWidthX;
uniform float KernelHalfWidthY;
uniform sampler2DRect Image;
uniform sampler2DRect Kernel;

void main()
{
    float dx, dy;
    float sum = float(0.0);
    float tmp;
    float kernelvalue;

    for (dy = -KernelHalfWidthY; dy <= KernelHalfWidthY; dy++) {
        for (dx = -KernelHalfWidthX; dx <= KernelHalfWidthX; dx++) {
            kernelvalue = texture2DRect(Kernel, vec2(dx + KernelHalfWidthX, dy + KernelHalfWidthY)).r;
            tmp = texture2DRect(Image, gl_TexCoord[0].st + vec2(dx, dy)).r;
            sum += tmp * kernelvalue;
        }
    }

    gl_FragColor.rgb = vec3(sum);
    gl_FragColor.a   = texture2DRect(Image, gl_TexCoord[0].st).a;
}