SFML community forums
Help => Graphics => Topic started by: Debilo 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:
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:
#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:
(http://www15.picfront.org/picture/4gPUTOVs7/thb/output.png) (http://picfront.org/d/7QzG)
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
-
At a quick glance, you might want to try using a smaller offset.
-
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!
-
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.
-
Yep, just a simple image filter. Thanks for your suggestion, seems useful.