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

Author Topic: Weird results with blur shader  (Read 2806 times)

0 Members and 1 Guest are viewing this topic.

Debilo

  • Newbie
  • *
  • Posts: 10
    • View Profile
Weird results with blur shader
« on: September 22, 2010, 06:28:15 pm »
Hey guys,
I'm currently trying to get into shader programming a bit. However, I encounter a problem which doesn't appear to make any sense to me.
The shader I'm using is the following:
Code: [Select]
uniform sampler2D tex;

void main()
{
    vec4 sum = vec4(0.0);
    vec2 offx = vec2(1.0, 0.0);
    vec2 offy = vec2(0.0, 1.0);
    sum += texture2D(tex, gl_TexCoord[0].xy - offx - offy);
    sum += texture2D(tex, gl_TexCoord[0].xy - offy);
    sum += texture2D(tex, gl_TexCoord[0].xy + offx - offy);
    sum += texture2D(tex, gl_TexCoord[0].xy - offx);
    sum += texture2D(tex, gl_TexCoord[0].xy);
    sum += texture2D(tex, gl_TexCoord[0].xy + offx);
    sum += texture2D(tex, gl_TexCoord[0].xy + offx + offy);
    sum += texture2D(tex, gl_TexCoord[0].xy + offy);
    sum += texture2D(tex, gl_TexCoord[0].xy - offx + offy);
    gl_FragColor = (sum / 9.0);
}
It's a plain, simple box filter. I can't spot any mistakes in it, which might be owed to the fact that I'm lacking any experience with shader programming.

A minimal example for the C++-code is:
Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow win(sf::VideoMode(800, 600), "Shader");
    sf::Image image;
    image.LoadFromFile("tulips.jpg");
    sf::Shader postEffect;
    postEffect.LoadFromFile("blur.sfx");
    sf::RenderImage renderImage;
    renderImage.Create(win.GetWidth(), win.GetHeight());
    renderImage.Clear();
    renderImage.Draw(sf::Sprite(image));
    renderImage.Display();

    sf::Sprite screen(renderImage.GetImage());

    while (win.IsOpened()) {
        sf::Event e;
        if (win.GetEvent(e)) {
            if (e.Type == sf::Event::KeyPressed && e.Key.Code == sf::Key::Escape) {
                win.Close();
            }
        }
        win.Clear();
        win.Draw(screen, postEffect);
        win.Display();
    }
}

Yet again, I can't see a problem with it. I tried to stick as close to the examples given here (http://www.sfml-dev.org/forum/viewtopic.php?p=11699) as I could. Nevertheless other approaches elicit just the same problem, namely:




I'm using the SFML2.0 binaries for Visual Studio 2010, someone uploaded them here in the forum a while ago. Operating system is Windows 7.
While writing this, it occurs to me that the graphics chip of my notebook is merely an Intel HD Graphics onboard-chip, but that should not pose a problem, I expect.

I hope some of you are able to spot the certainly quite obvious error. I can't, however, so thanks in advance.

Regards,
Debilo

Spodi

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • http://www.netgore.com/
Weird results with blur shader
« Reply #1 on: September 22, 2010, 07:50:59 pm »
At a quick glance, you might want to try using a smaller offset.

Debilo

  • Newbie
  • *
  • Posts: 10
    • View Profile
Weird results with blur shader
« Reply #2 on: September 22, 2010, 07:56:15 pm »
Oh boy, that's what I call awkward! Actually, I'm sitting here laughing, though. Texture coordinates are a great thing, indeed.
Thanks alot, however, I guess that fits into "First think, then write your code"...gosh!

Spodi

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • http://www.netgore.com/
Weird results with blur shader
« Reply #3 on: September 22, 2010, 11:03:03 pm »
No problem, glad it was that simple. :)

Was the purpose of the offset to grab 1 texel in each direction and blur them all together? If so, you might want to instead calculate by using an offset of:

pixelOffset / textureSize

You can get the texture size using textureSize(), but I think that is only in later version of GLSL, so you will have to pass the size as a parameter.

Debilo

  • Newbie
  • *
  • Posts: 10
    • View Profile
Weird results with blur shader
« Reply #4 on: September 24, 2010, 02:38:49 pm »
Yep, just a simple image filter. Thanks for your suggestion, seems useful.