GLTextureFilter. How to hook up a the xml correctly to the glsl shader?
in
Contributed Library Questions
•
1 year ago
Dear Forum,
I must admit that I am a complete rooky with shaders.
But for a project i need a blur shader due to performance reasons with variable radius to control the blur intensity.
So I found this nice fragment shader which works smooth with processing 2.0
But I need to stick with Processing 1.5.1 for now.
So by looking at the GLTexture examples I tried to write the correct xml for this.
And calling it all like:
which throws me a bunch of crasy errors:
Fragment shader Blur.glsl compilation:
ERROR: 0:38: Use of undeclared identifier 'textureSampler'
ERROR: 0:44: Use of undeclared identifier 'textureSampler'
ERROR: 0:46: Use of undeclared identifier 'textureSampler'
GLSL program validation:
Validation Failed: Link error:
Program is not successfully linked.
Error in filter parameter blurSize: no valid opengl ID.
Error in filter parameter horizontalPass: no valid opengl ID.
Error in filter parameter sigma: no valid opengl ID.
OpenGL error 1282 at render_triangles in: invalid operation
I am probably doing something fundamently wrong.
But don't know where to look.
So, dear forum, can you point me into the right direction?
I must admit that I am a complete rooky with shaders.
But for a project i need a blur shader due to performance reasons with variable radius to control the blur intensity.
So I found this nice fragment shader which works smooth with processing 2.0
- // Adapted from:
- // http://callumhay.blogspot.com/2010/09/gaussian-blur-shader-glsl.html
- uniform sampler2D textureSampler;
- // The inverse of the texture dimensions along X and Y
- uniform vec2 texcoordOffset;
- varying vec4 vertColor;
- varying vec4 vertTexcoord;
- uniform int blurSize;
- uniform int horizontalPass; // 0 or 1 to indicate vertical or horizontal pass
- uniform float sigma; // The sigma value for the gaussian function: higher value means more blur
- // A good value for 9x9 is around 3 to 5
- // A good value for 7x7 is around 2.5 to 4
- // A good value for 5x5 is around 2 to 3.5
- // ... play around with this based on what you need :)
- const float pi = 3.14159265;
- void main() {
- float numBlurPixelsPerSide = float(blurSize / 2);
- vec2 blurMultiplyVec = 0 < horizontalPass ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
- // Incremental Gaussian Coefficent Calculation (See GPU Gems 3 pp. 877 - 889)
- vec3 incrementalGaussian;
- incrementalGaussian.x = 1.0 / (sqrt(2.0 * pi) * sigma);
- incrementalGaussian.y = exp(-0.5 / (sigma * sigma));
- incrementalGaussian.z = incrementalGaussian.y * incrementalGaussian.y;
- vec4 avgValue = vec4(0.0, 0.0, 0.0, 0.0);
- float coefficientSum = 0.0;
- // Take the central sample first...
- avgValue += texture2D(textureSampler, vertTexcoord.st) * incrementalGaussian.x;
- coefficientSum += incrementalGaussian.x;
- incrementalGaussian.xy *= incrementalGaussian.yz;
- // Go through the remaining 8 vertical samples (4 on each side of the center)
- for (float i = 1.0; i <= numBlurPixelsPerSide; i++) {
- avgValue += texture2D(textureSampler, vertTexcoord.st - i * texcoordOffset *
- blurMultiplyVec) * incrementalGaussian.x;
- avgValue += texture2D(textureSampler, vertTexcoord.st + i * texcoordOffset *
- blurMultiplyVec) * incrementalGaussian.x;
- coefficientSum += 2.0 * incrementalGaussian.x;
- incrementalGaussian.xy *= incrementalGaussian.yz;
- }
- gl_FragColor = avgValue / coefficientSum;
- }
But I need to stick with Processing 1.5.1 for now.
So by looking at the GLTexture examples I tried to write the correct xml for this.
- <filter name="blur">
- <description>Blur filter</description>
- <vertex>SimpleVS.glsl</vertex>
- <fragment>Blur.glsl</fragment>
- <textures input="1" output="1">
- <intexture type="sampler2d" name="textureSampler" label="Texture from which to blur">0</intexture>
- </textures>
- <parameters>
- <parameter type="int" name="blurSize" label="Blur Size">5</parameter>
- <parameter type="int" name="horizontalPass" label="horizontal Pass ">0</parameter>
- <parameter type="float" name="sigma" label="Sigma">2.3</parameter>
- </parameters>
- </filter>
And calling it all like:
- import processing.opengl.*;
- import codeanticode.glgraphics.*;
- size(256, 256, GLConstants.GLGRAPHICS);
- GLTexture tex0 = new GLTexture(this, "lights.jpg");
- GLTexture tex2 = new GLTexture(this, 256, 256);
- GLTextureFilter blurFilter = new GLTextureFilter(this, "Blur.xml");
- blurFilter.setParameterValue("blurSize", 10);
- blurFilter.apply(tex0, tex2);
- image(tex2, 0, 0);
which throws me a bunch of crasy errors:
Fragment shader Blur.glsl compilation:
ERROR: 0:38: Use of undeclared identifier 'textureSampler'
ERROR: 0:44: Use of undeclared identifier 'textureSampler'
ERROR: 0:46: Use of undeclared identifier 'textureSampler'
GLSL program validation:
Validation Failed: Link error:
Program is not successfully linked.
Error in filter parameter blurSize: no valid opengl ID.
Error in filter parameter horizontalPass: no valid opengl ID.
Error in filter parameter sigma: no valid opengl ID.
OpenGL error 1282 at render_triangles in: invalid operation
I am probably doing something fundamently wrong.
But don't know where to look.
So, dear forum, can you point me into the right direction?
1