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

Author Topic: How to do shaders on the frame buffer correctly?  (Read 3100 times)

0 Members and 1 Guest are viewing this topic.

Rob92

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
How to do shaders on the frame buffer correctly?
« on: February 27, 2016, 08:00:03 pm »
I want to apply a fragment shader to the screen, so I can manipulate the frame buffer.

At the moment I am doing it like this:

if (m_frameBufferStates.shader != nullptr)
{
        m_frameBufferTexture.update(*m_window);
        m_frameBufferSprite.setTexture(m_frameBufferTexture);
        m_window->draw(m_frameBufferSprite, m_frameBufferStates);
}

I noticed that this cost me a substantial chunk of performance. (because of sf::Texture update function)
I still have more than enough FPS but I was wondering if there is a better way to do this?

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: How to do shaders on the frame buffer correctly?
« Reply #1 on: February 27, 2016, 10:24:03 pm »
sf::RenderTexture ?

Rob92

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
Re: How to do shaders on the frame buffer correctly?
« Reply #2 on: February 27, 2016, 11:29:01 pm »
Hi, I tried that but I couldnt figure out how to copy the screen buffer to the rendertarget. The draw function askes for a Drawable&.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to do shaders on the frame buffer correctly?
« Reply #3 on: February 28, 2016, 12:23:51 am »
sf::RenderTexture renderTexture;
// ...
{
    renderTexture.clear();
    renderTexture.draw(allGameObjects);
    renderTexture.display();
    window.clear()
    window.draw(sf::Sprite(renderTexture.getTexture()), states);
    window.display();
}
« Last Edit: February 28, 2016, 12:35:09 am by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: How to do shaders on the frame buffer correctly?
« Reply #4 on: February 28, 2016, 12:26:41 am »
To add some precision to Hapax message : obviously, you need to draw your whole game to the sf::RenderTexture instead of the window. And do not forget to call clear() and display() on the sf::RenderTexture itself too. :)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to do shaders on the frame buffer correctly?
« Reply #5 on: February 28, 2016, 12:36:22 am »
To add some precision to Hapax message : obviously, you need to draw your whole game to the sf::RenderTexture instead of the window. And do not forget to call clear() and display() on the sf::RenderTexture itself too. :)
I've modified my above post to be more clear and to include the advice you mentioned :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Rob92

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
Re: How to do shaders on the frame buffer correctly?
« Reply #6 on: March 01, 2016, 06:14:35 pm »
Thank you both, it works and gives me a bit better performance :)