Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Blur shader not working - Just a grey "cross"  (Read 2606 times)

0 Members and 1 Guest are viewing this topic.

StuntHacks

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Blur shader not working - Just a grey "cross"
« on: April 18, 2016, 03:58:34 pm »
Hi,

I'm trying to apply a blur shader on one of my sprites. But all I get is the texture with a half transparent grey cross layed over it (See  screenshot below). Can someone tell me what I have done wrong?

This is my code that draws the sprite:
shader.setParameter("blur_radius", 20);
shader.setParameter("texture", backgroundTexture);

window.clear(sf::Color::Transparent);
window.draw(backgroundSprite, &shader);
window.display();

And this is my shader:
uniform sampler2D texture;
uniform float blur_radius;

void main()
{
    vec2 offx = vec2(blur_radius, 0.0);
    vec2 offy = vec2(0.0, blur_radius);

    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy)               * 4.0 +
                 texture2D(texture, gl_TexCoord[0].xy - offx)        * 2.0 +
                 texture2D(texture, gl_TexCoord[0].xy + offx)        * 2.0 +
                 texture2D(texture, gl_TexCoord[0].xy - offy)        * 2.0 +
                 texture2D(texture, gl_TexCoord[0].xy + offy)        * 2.0 +
                 texture2D(texture, gl_TexCoord[0].xy - offx - offy) * 1.0 +
                 texture2D(texture, gl_TexCoord[0].xy - offx + offy) * 1.0 +
                 texture2D(texture, gl_TexCoord[0].xy + offx - offy) * 1.0 +
                 texture2D(texture, gl_TexCoord[0].xy + offx + offy) * 1.0;

    gl_FragColor =  gl_Color * (pixel / 16.0);
}

The shader is loaded correctly and shaders are supported on my computer.

And here is the screenshot of the sprite (As you can see, there is no blur):
(click to show/hide)

Thanks,
StuntHacks

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Blur shader not working - Just a grey "cross"
« Reply #1 on: April 19, 2016, 12:58:53 am »
Isn't the range for the components of gl_TexCoord[0] from zero to one?
This would mean your radius of twenty is simply twenty times the size in that dimension.

Try a much lower radius.

If you want twenty texture pixels, it would be: 20/size.
Therefore: offx.x would be 20/width and offy.y would be 20/height
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

StuntHacks

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Blur shader not working - Just a grey "cross"
« Reply #2 on: April 19, 2016, 12:18:07 pm »
I tried to set 0.5 as the blur_radius now. It changes something, but it still isn't a blur. It kinda copies the texture for times, makes them gray and half transparent (See screenshot below).
(click to show/hide)

EDIT: I tried 0.01 now and it seems to work. Still curious why it looks so strange with a bigger radius...

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Blur shader not working - Just a grey "cross"
« Reply #3 on: April 19, 2016, 12:32:57 pm »
gl_TexCoord[0].x ranges from 0.0 to 1.0 (left to right of your texture) (not from 0 to texture-width).

You should divide your blur_radius by your texture-width.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Blur shader not working - Just a grey "cross"
« Reply #4 on: April 19, 2016, 03:47:30 pm »
gl_TexCoord[0].x ranges from 0.0 to 1.0 (left to right of your texture) (not from 0 to texture-width).
Isn't the range for the components of gl_TexCoord[0] from zero to one?
This would mean your radius of twenty is simply twenty times the size in that dimension.

You should divide your blur_radius by your texture-width.
If you want twenty texture pixels, it would be: 20/size.
Therefore: offx.x would be 20/width and offy.y would be 20/height
:P

Still curious why it looks so strange with a bigger radius...
An x offset of 20 is an offset of 20 widths. If your texture is larger than this rectangle, it could be using a different part of the texture. If it's the same size as the rectangle, it's either outside of the range of the texture or, if texture is looping, not moving at all since it loops in widths and you're specifying entire widths.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*