PDA

View Full Version : Gaussian Blur on texture



devdept
06-20-2008, 03:16 AM
Hi All,

I have a texture generated on the fly (via FBO) with the model shadow. This is a black and transparent texture with - of course - many stair steps between (on/off) pixels.

I was thiking to add Gaussian Blur on the whole bitmap. I found the algorithm you see below but it works pixel by pixel.

Now the questions are:

1) Does OpenGL provide some built in algorithm to do this?
2) What is the fastest method to change pixel color on a texture?
3) Does some other different approach exist to obtain the same result?

Thanks so much as usually!

Alberto


#define gauss_width 7

sumr=0;
sumg=0;
sumb=0;

int gauss_fact[gauss_width]={1,6,15,20,15,6,1};
int gauss_sum=64;

for(i=1;i<temp->w-1;i++){
for(j=1;j<temp->h-1;j++){
sumr=0;
sumg=0;
sumb=0;
for(k=0;k<gauss_width;k++){
color=getpixel(temp,i-((gauss_width-1)>>1)+k,j);
r=getr32(color);
g=getg32(color);
b=getb32(color);
sumr+=r*gauss_fact[k];
sumg+=g*gauss_fact[k];
sumb+=b*gauss_fact[k];

}
putpixel(temp1,i,j,makecol(sumr/gauss_sum,sumg/gauss_sum,
sumb/gauss_sum));
}
}

for(i=1;i<temp->w-1;i++){
for(j=1;j<temp->h-1;j++){
sumr=0;
sumg=0;
sumb=0;
for(k=0;k<gauss_width;k++){
color=getpixel(temp1,i,j-((gauss_width-1)>>1)+k);
r=getr32(color);
g=getg32(color);
b=getb32(color);

sumr+=r*gauss_fact[k];
sumg+=g*gauss_fact[k];
sumb+=b*gauss_fact[k];
}
sumr/=gauss_sum;
sumg/=gauss_sum;
sumb/=gauss_sum;

putpixel(temp2,i,j,makecol(sumr,sumg,sumb));
}
}

yooyo
06-20-2008, 03:26 AM
As you can see in that algorithm.. do the same.. but you cant write (setpixel) into the same texture. So.. do the ping-pong... create another texture (same size as original) and do horizontal blur. Then use that new texture as source for vertical blur.


fbo1 is original fbo
fbo2 is temporary fbo

fbo2 = horizontal_blur(fbo1) // step 1.. read from fbo1, write to fbo2
fbo1 = vertical_blur(fbo2) // step 2.. read from fbo2, write to fbo1

now, fbo1 contains blurred image.


Even more, you can repeat step1 and step2 several times to blur more.

dletozeun
06-20-2008, 04:01 AM
I think this would be useful too:
http://www.gamasutra.com/features/20010209/evans_02.htm

devdept
06-20-2008, 05:00 AM
Thanks guy,

I now have the doubt that using this extension "EXT_convolution" I can do it via hardware. Do you know if it is true?

Thanks.

Alberto

Seth Hoffert
06-20-2008, 05:56 AM
In my experience some/most of the imaging extensions (including convolution) are not HW accelerated.

devdept
06-20-2008, 06:26 AM
No I am certain, the best way to do what we need are Shaders. Does anybody know a good 'basic' tutorial on the Shaders based convulution filter?

Thanks.

Alberto

Vexator
06-20-2008, 07:54 AM
/////////////////////////////////////////////////
// 7x1 gaussian blur fragment shader
/////////////////////////////////////////////////

varying vec2 v_Coordinates;

uniform vec2 u_Scale;
uniform sampler2D u_Texture0;

const vec2 gaussFilter[7] =
{
-3.0, 0.015625,
-2.0, 0.09375,
-1.0, 0.234375,
0.0, 0.3125,
1.0, 0.234375,
2.0, 0.09375,
3.0, 0.015625
};

void main()
{
vec4 color = 0.0;
for( int i = 0; i < 7; i++ )
{
color += texture2D( u_Texture0, vec2( v_Coordinates.x+gaussFilter[i].x*u_Scale.x, v_Coordinates.y+gaussFilter[i].x*u_Scale.y ) )*gaussFilter[i].y;
}

gl_FragColor = color;
}

u_Scale is vec2(0, 1.0/height ) for vertical blur and vec2(1.0/width, 0 ) for horizontal blur.

devdept
06-22-2008, 06:16 AM
Thanks a lot vexator,

You all Shaders expert use shader program only code samples but for us old OpenGL programmers the rest is still not so easy to add.

Can you please add also the gl calls to make this shader to run?

Thanks so much,

Alberto

dletozeun
06-22-2008, 07:30 AM
You will need a good glsl tutorial:
http://www.lighthouse3d.com/opengl/glsl/

devdept
07-29-2008, 02:46 PM
Hi Vexator,


Please help me to make this shader to work.

What are variables we need to initialize?

How can we perform the blur in both directions?


Thanks,

Alberto

febingeorge
09-18-2012, 02:59 AM
what is width and height here?
If my image size 720X480 does it mean width is 720 and height is 480.
Or is it the size of the window

febingeorge
09-18-2012, 07:10 AM
I got answer for the above question.
But now I have another question, in the earlier example of a fragment shader it uses 7x1 matrix.
What if I want to use 7x7 matrix?

febingeorge
09-18-2012, 07:12 AM
Hi Vexator I have one question, in the earlier example of a fragment shader it uses 7x1 matrix.
What if I want to use 7x7 matrix? Do I have to use a nested loop with u_Scale = vec2(1.0/width, 1.0/height )