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

Author Topic: Basic bloom effect help  (Read 10727 times)

0 Members and 1 Guest are viewing this topic.

verilax

  • Newbie
  • *
  • Posts: 2
    • View Profile
Basic bloom effect help
« on: May 06, 2012, 09:06:35 pm »
Hi, I'm trying to implement a bloom effect using shaders on sfml2 but unfortunately, I'm a complete newbie to GLSL and I'm having a lot of trouble figuring out why it's not working at all. I tried to use the shaders posted here by another user and passed in the variables the horizontal and vertical blur shaders required but all that resulted is my sprite becoming invisible. I tried just applying only the horizontal blur and passed in this with the same result (sprite disappears).
    blurH.SetParameter("fadedelta", 0.5f);
    blurH.SetParameter("img", sf::Shader::CurrentTexture);
    blurH.SetParameter("width", 1600.0f);
    blurH.SetParameter("g", 1.5f);
    renderWindow.Draw(outline, blurH);
 

I don't see any GLSL errors in the console so I have no clue what I'm doing wrong. I've been stuck for several days now and it's just so frustrating because I feel like I can't move on to other parts of my game until I can get this working. Would someone be able to share a bit of insight on implementing this or perhaps some working example code?
« Last Edit: May 06, 2012, 09:25:17 pm by Laurent »

Vovosunt

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Basic bloom effect help
« Reply #1 on: May 07, 2012, 11:25:20 am »
I think removing the setParameter "img" might help.
And it's quite peculiar that no errors pop up, usually I have to set the shader with a reference (window.draw(whatever,&shader)) or in the RenderStates.
Also, I think you have to divide the texture coordinates by width, since they are also represented as floats from 0.0 to 1.0 (aka you're asking for a coordinate 1600.0 which is hell knows where)
But I'm not sure, that's how it works on my stuff...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Basic bloom effect help
« Reply #2 on: May 07, 2012, 07:46:24 pm »
Quote
And it's quite peculiar that no errors pop up, usually I have to set the shader with a reference (window.draw(whatever,&shader)) or in the RenderStates.
He's using SFML 1.6.
Laurent Gomila - SFML developer

verilax

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Basic bloom effect help
« Reply #3 on: May 12, 2012, 08:51:52 pm »
Sorry for the huge delay in replying. I'm definitely using SFML2. I just accidentally removed the ampersand when I was retyping the code  :P.

Anyhow, I managed to get bloom working after spending some time writing one up from scratch and using render textures in the process. Still unsure of what went wrong before but it's working now so I don't really care lol. Thanks for the help nonetheless. If anyone's curious, here are the shaders.

Horizontal shader:
Code: [Select]
uniform sampler2D sourceTexture;
uniform float sigma;
uniform float glowMultiplier;
uniform float width;

const int KERNEL_SIZE = 5;
float glow = glowMultiplier / (sigma * sqrt(2.0 * 3.14159));

float blurWeight(float x)
{
    return (glow * exp(-(x*x) / (2.0 * sigma * sigma)));
}

void main()
{
    vec4 color = vec4(0.0);
    vec2 texCoord = gl_TexCoord[0].xy;

    for (int i = -KERNEL_SIZE; i <= KERNEL_SIZE; i++)
    {
        texCoord.x = gl_TexCoord[0].x + (i / width);
        color += texture2D(sourceTexture, texCoord) * blurWeight(i);
    }

    gl_FragColor = color;
}

Vertical shader:
Code: [Select]
uniform sampler2D sourceTexture;
uniform float sigma;
uniform float glowMultiplier;
uniform float height;

const int KERNEL_SIZE = 5;
float glow = glowMultiplier / (sigma * sqrt(2.0 * 3.14159));

float blurWeight(float x)
{
    return (glow * exp(-(x*x) / (2.0 * sigma * sigma)));
}

void main()
{
    vec4 color = vec4(0.0);
    vec2 texCoord = gl_TexCoord[0].xy;

    for (int i = -KERNEL_SIZE; i <= KERNEL_SIZE; i++)
    {
        texCoord.y = gl_TexCoord[0].y + (i / height);
        color += texture2D(sourceTexture, texCoord) * blurWeight(i);
    }

    gl_FragColor = color;
}

And here is what it looks like.

kralo9

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Basic bloom effect help
« Reply #4 on: February 10, 2013, 05:42:56 pm »
I know this post is months old, but how do I render the shaders with latest sfml? I managed to setup sf::Shader with parameters, but I have no clue how to proceed.

I'm totally exhausted. I've got 3 children and no money! Why can't I have no children and 3 money? - Homer S.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Basic bloom effect help
« Reply #5 on: February 10, 2013, 05:45:23 pm »
Pass &shader or sf::RenderStates with .shader set to &shader in either of two overloads of draw in sf::RenderTarget.
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Basic bloom effect help
« Reply #6 on: February 10, 2013, 05:47:54 pm »
Please read the documentation. Everything is explained in the detailed description of the class.

http://www.sfml-dev.org/documentation/2.0/classsf_1_1Shader.php#details
Laurent Gomila - SFML developer

kralo9

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Basic bloom effect help
« Reply #7 on: February 10, 2013, 05:53:52 pm »
There are 2 shaders. Do I call draw twice?
I'm totally exhausted. I've got 3 children and no money! Why can't I have no children and 3 money? - Homer S.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Basic bloom effect help
« Reply #8 on: February 10, 2013, 05:56:41 pm »
If you have vertex and fragment shader then you call draw once and it'll run your vertex shader on vertices and then rasterizer will run your fragment shader, no problem.
But if you have two shaders of one type then yes, you need to call draw twice.
Back to C++ gamedev with SFML in May 2023

PlankSkills

  • Guest
Re: Basic bloom effect help
« Reply #9 on: April 28, 2020, 03:18:31 pm »
If you are still alive from 2012, How did you get the shader working??? I can't get it working.

 

anything