# OpenGL Gaussian Blur Filter

This topic is 4645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Tell me please, where I can find detailed description of how to implement Gaussian Blur Filter in OpenGL

##### Share on other sites
Do you know how to use pixel shaders ?

If so, search for "Image Processing on the GPU" on Google. I am sure you can find many resources. There is also a Chapter dedicated to Image Processing on the GPU in the book "GPU Gems". Hope this helps!

##### Share on other sites
dimensionX
Yes, I have some experience writing shaders. I googled many times with different queries, but I couldn't find any tutorials about Gaussian Blur. Everything I find are many Demos, and nothing but demos.
And, unfortunately, I don't have GPU Gems book.

Some tutorial links would be very nice.

##### Share on other sites
Did the demos you see have source code? If just source code isn't good enough, then ignore this since I didn't write a tutorial but I have source code available in Cg and GLSL of an anisotropic diffusion filter. (anisotropic diffusion is a generalization of Gaussian blurring).

To convert this to Gaussian blurring (isotropic diffusion):
In the GLSL version, file anisotropic_diffusion.glsl change the line:

vec4 c = exp(-(dif*dif)/k_squared);

to:

vec4 c = vec4(1.0f, 1.0f, 1.0f, 1.0f);

In the Cg version, file anisotropic_diffusion.cg change the line:

float4 c = exp(-(dif*dif)/k_squared);

to:

float4 c = (1.0f).xxxx;

The Cg version might complain since k_squared will no longer be used. If it does just set k_squared to be a huge number when it's sent from the host application and leave the shader as is.

[Edited by - atcdevil on January 30, 2007 11:14:33 AM]

##### Share on other sites
atcdevil

Thanx for help.

<code>
float sample = texRECT(previous_step, gl_TexCoord[0].st).r;
vec4 neighbors = vec4(texRECT(previous_step, gl_TexCoord[1].st).r,
texRECT(previous_step, gl_TexCoord[2].st).r,
texRECT(previous_step, gl_TexCoord[3].st).r,
texRECT(previous_step, gl_TexCoord[4].st).r);
</code>

I just don't undestand, what you pass in texcoords (0-4).
Can you explain me?

##### Share on other sites
Take a look at compute_locations.glsl, which computes the texture coordinate which tje anisotropic_diffusion.glsl fragment shader use.

// Compute Positions
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord0 + vec4(1.0, 0.0, 0.0, 0.0); //east
gl_TexCoord[2] = gl_MultiTexCoord0 + vec4(-1.0, 0.0, 0.0, 0.0); //west
gl_TexCoord[3] = gl_MultiTexCoord0 + vec4(0.0, -1.0, 0.0, 0.0); //north
gl_TexCoord[4] = gl_MultiTexCoord0 + vec4(0.0, 1.0, 0.0, 0.0); //south
gl_Position = ftransform();
}

Also the code which draws a quad in the first place:
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f((float)width,0); glVertex2f((float)width, 0.0f);
glTexCoord2f((float)width, (float)height); glVertex2f((float)width, (float)height);
glTexCoord2f(0.0f, (float)height); glVertex2f(0.0f, (float)height);
glEnd();

I'm drawing a full-screen quad. Pretend that what I'm actually doing is exactly texture mapping the image onto that quad (with no blurring). If you used the 0th texture coordinate you would get an exact copy of the image. If you used the first coordinate you would get the image shifted by 1 pixel to the right. If you used the second coordinate you would get the image shifted by 1 pixel to the left. If you used the third coordinate you would get the image shifted by 1 pixel up. If you used the fourth coordinate you would get the image shifted by 1 pixel down.

For example if the current fragment is at (2,2)
texcoord0 = (2,2)
texcoord1 = (3,2)
texcoord1 = (1,2)
texcoord1 = (2,1)
texcoord1 = (2,3)

##### Share on other sites
atcdevil
Thanx for such cool explanation!
Now I understand, how to implemen blur. I still don't understand those formulas, but I see the concept.

Btw, why you use only red color component for blurring?

##### Share on other sites
Quote:
 Original post by glJackdimensionXYes, I have some experience writing shaders. I googled many times with different queries, but I couldn't find any tutorials about Gaussian Blur. Everything I find are many Demos, and nothing but demos.And, unfortunately, I don't have GPU Gems book. Some tutorial links would be very nice.

---------------------

uniform sampler2DRect rectTexture;

void main()
{
vec4 color = texture2DRect(rectTexture, gl_FragCoord.st);

color += texture2DRect(rectTexture, vec2(gl_FragCoord.s + 1, gl_FragCoord.t));
color += texture2DRect(rectTexture, vec2(gl_FragCoord.s + 1, gl_FragCoord.t + 1));
color += texture2DRect(rectTexture, vec2(gl_FragCoord.s, gl_FragCoord.t + 1));

gl_FragColor = color * 0.25;
}

This is simply a 2x2 averaging filter. To do a 3x3 Gaussian just plug-in the weights.

Also Keep in mind that gl_FragCoord.st is not at integer locations. The lower left window coordinate (gl_FragCoord) is (0.5, 0.5). There is a 0.5 offset. Now if you have mentioned the texture sampling a GL_LINEAR be aware of the 0.5 offsets.

GL_LINEAR works as follows:

s = floor(texCoord.s - 0.5); NOT s = floor(texCoord.s); as we usually do in C.
t = floor(texCoord.t - 0.5);

This -0.5 subtraction is to compensate for the 0.5 offset in the window coordinate. The reason for mentioning this is, if you have computed a texture coordinate as (1.2, 1.7) we would expect the lowe left corner to be (1, 1) but as per opengl's GL_LINEAR the lower left corner will be (0 = floor(1.2-0.5), 1 = floor(1.7-0.5)). Do you notice the difference. To overcome this just add 0.5 to the texture coordinates before sampling the textures i.e texture2DRect(rectTexture, texCoord.st + 0.5); While sampling OpenGL will subtract the 0.5 we have added and the result would be (1 = floor(1.2+0.5-0.5), 1 = floor(1.7+0.5-0.5))

Hope this information is useful to all those who are doing image processing on the GPU.

##### Share on other sites
Quote:
 Original post by glJackatcdevilBtw, why you use only red color component for blurring?

It's not really the red component. My textures only have 1 component, so it's just a grayscale value.

• ### Similar Content

• By xhcao
Does sync be needed to read texture content after access texture image in compute shader?
My simple code is as below,
glUseProgram(program.get());
glBindImageTexture(0, texture[0], 0, GL_FALSE, 3, GL_READ_ONLY, GL_R32UI);
glBindImageTexture(1, texture[1], 0, GL_FALSE, 4, GL_WRITE_ONLY, GL_R32UI);
glDispatchCompute(1, 1, 1);
// Does sync be needed here?
glUseProgram(0);
GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[1], 0);
glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);

Compute shader is very simple, imageLoad content from texture[0], and imageStore content to texture[1]. Does need to sync after dispatchCompute?

• My question: is it possible to transform multiple angular velocities so that they can be reinserted as one? My research is below:

• I have this code below in both my vertex and fragment shader, however when I request glGetUniformLocation("Lights[0].diffuse") or "Lights[0].attenuation", it returns -1. It will only give me a valid uniform location if I actually use the diffuse/attenuation variables in the VERTEX shader. Because I use position in the vertex shader, it always returns a valid uniform location. I've read that I can share uniforms across both vertex and fragment, but I'm confused what this is even compiling to if this is the case.

#define NUM_LIGHTS 2
struct Light
{
vec3 position;
vec3 diffuse;
float attenuation;
};
uniform Light Lights[NUM_LIGHTS];

• By pr033r
Hello,
I have a Bachelor project on topic "Implenet 3D Boid's algorithm in OpenGL". All OpenGL issues works fine for me, all rendering etc. But when I started implement the boid's algorithm it was getting worse and worse. I read article (http://natureofcode.com/book/chapter-6-autonomous-agents/) inspirate from another code (here: https://github.com/jyanar/Boids/tree/master/src) but it still doesn't work like in tutorials and videos. For example the main problem: when I apply Cohesion (one of three main laws of boids) it makes some "cycling knot". Second, when some flock touch to another it scary change the coordination or respawn in origin (x: 0, y:0. z:0). Just some streng things.
I followed many tutorials, change a try everything but it isn't so smooth, without lags like in another videos. I really need your help.
My code (optimalizing branch): https://github.com/pr033r/BachelorProject/tree/Optimalizing
Exe file (if you want to look) and models folder (for those who will download the sources):
http://leteckaposta.cz/367190436
Thanks for any help...

• By Andrija
I am currently trying to implement shadow mapping into my project , but although i can render my depth map to the screen and it looks okay , when i sample it with shadowCoords there is no shadow.
Here is my light space matrix calculation
mat4x4 lightViewMatrix; vec3 sun_pos = {SUN_OFFSET * the_sun->direction[0], SUN_OFFSET * the_sun->direction[1], SUN_OFFSET * the_sun->direction[2]}; mat4x4_look_at(lightViewMatrix,sun_pos,player->pos,up); mat4x4_mul(lightSpaceMatrix,lightProjMatrix,lightViewMatrix); I will tweak the values for the size and frustum of the shadow map, but for now i just want to draw shadows around the player position
the_sun->direction is a normalized vector so i multiply it by a constant to get the position.
player->pos is the camera position in world space
the light projection matrix is calculated like this:
uniform mat4 light_space_matrix; void main() { gl_Position = light_space_matrix * transfMatrix * vec4(position, 1.0f); } Shadow fragment shader:
out float fragDepth; void main() { fragDepth = gl_FragCoord.z; } I am using deferred rendering so i have all my world positions in the g_positions buffer
get_shadow_fac(light_space_matrix * vec4(position,1.0)); Where position is the value i got from sampling the g_position buffer
Here is my depth texture (i know it will produce low quality shadows but i just want to get it working for now):
sorry because of the compression , the black smudges are trees ... https://i.stack.imgur.com/T43aK.jpg
EDIT: Depth texture attachment:
glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT24,fbo->width,fbo->height,0,GL_DEPTH_COMPONENT,GL_FLOAT,NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, fbo->depthTexture, 0);

• 13
• 16
• 10
• 17
• 9