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

Author Topic: Smoothing Shapes on a rendertexture  (Read 3205 times)

0 Members and 1 Guest are viewing this topic.

Jonny

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • Email
Smoothing Shapes on a rendertexture
« on: June 26, 2014, 01:01:48 pm »
Hello all,

I work with/for the user slotdev and I've only been using SFML and C++ for about a year now, so I'll apologize in advance for any silly mistakes, but here's my problem...

As we make games which need to be run at multiple different resolutions, depending on the hardware, we've previously had plenty of issues with artifacts appearing around sprites due to scaling. I came up for a fix for this by using an sf::RenderTexture as a sort of buffer which would essentially 'flatten' all the sprites at native resolution before scaling the RenderTexture and drawing it to the window. This worked perfectly in terms of removing the artifacts, but has causes some issues with sf::shapes...

We use sf::TriangleStrip to draw lines between winning symbol combinations, which could be at several positions in the window. Initially any section of the line which wasn't horizontal/vertical would look jagged, so we set antialiasing on in the window's contextsettings which solved that problem, however, now the shapes are getting drawn to the RenderTexture (which is set smooth, i might add), they are not smoothed by the window antialiasing so look jagged again.

does anyone know of a possible solution for this?

Here is some sample code which reproduces the problem:

#include <SFML\Graphics.hpp>

int main()
{
        //create the window
        sf::RenderWindow window(sf::VideoMode(500, 500), "Shape",sf::Style::Default,sf::ContextSettings(0,0,4));

        //create the shape
    sf::RectangleShape shape(sf::Vector2f(200,200));
    shape.setFillColor(sf::Color::Green);
        shape.setRotation(0);
        shape.setOrigin(100,100);
        shape.setPosition(250,250);

        //rendertexture stuff
        sf::RenderTexture windowTexture;
        windowTexture.create(500,500);
        windowTexture.setSmooth(true);
        sf::Sprite windowSprite;
        windowSprite.setTexture(windowTexture.getTexture());
        bool useRenderTexture=true;

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
                        if (event.type == sf::Event::KeyPressed)
                        {
                                switch (event.key.code)
                                {
                                        //rotate left
                                case sf::Keyboard::Left:
                                        shape.rotate(-5);
                                        break;
                                        //rotate right
                                case sf::Keyboard::Right:
                                        shape.rotate(5);
                                        break;
                                        //set window antialiasing OFF
                                case sf::Keyboard::Num1:
                                        window.create(sf::VideoMode(500, 500), "Shape",sf::Style::Default,sf::ContextSettings(0,0,0));
                                        break;
                                        //set window antialiasing ON
                                case sf::Keyboard::Num2:
                                        window.create(sf::VideoMode(500, 500), "Shape",sf::Style::Default,sf::ContextSettings(0,0,4));
                                        break;
                                        //toggle the use of the rendertexture
                                case sf::Keyboard::R:
                                        useRenderTexture=!useRenderTexture;
                                        break;
                                }
                        }
        }
                if(useRenderTexture)
                {
                        windowTexture.clear(sf::Color::Black);
                        windowTexture.draw(shape);
                        windowTexture.display();
                }

                window.clear(sf::Color::Black);
                if(useRenderTexture)
                        window.draw(windowSprite);
                else
                        window.draw(shape);
        window.display();
    }

    return 0;
}

to reproduce the problem, just push left/right to rotate the square slightly, then use 'R' to toggle the rendertexture on/off, and '1' or '2' to set the window antialiasing off/on.

Thanks in advance

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Smoothing Shapes on a rendertexture
« Reply #1 on: June 26, 2014, 01:35:31 pm »
It's not a "problem", it's known that sf::RenderTexture doesn't support antialiasing yet.
Laurent Gomila - SFML developer

Jonny

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • Email
Re: Smoothing Shapes on a rendertexture
« Reply #2 on: June 26, 2014, 02:30:39 pm »
I guess by your wording it is something which is planned on being implemented? In the mean time if anyone has suggestions on how to solve this it would be much appreciated.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Smoothing Shapes on a rendertexture
« Reply #3 on: June 26, 2014, 02:55:39 pm »
Yes, it would have to be implemented some day.

Possible workarounds are:
- use an antialiasing pixel shader
- implement it yourself :)
Laurent Gomila - SFML developer

Jonny

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • Email
Re: Smoothing Shapes on a rendertexture
« Reply #4 on: June 26, 2014, 03:39:26 pm »
Given that I've barely dipped my toes into SFML's source code, I'll do it with a shader for now.

Thanks for the help