SFML community forums

Help => Graphics => Topic started by: bclever on February 10, 2018, 06:14:59 pm

Title: Vertical line artifacts in simple image
Post by: bclever on February 10, 2018, 06:14:59 pm
Hello all, I apologize for the noob level question... In this basic SFML code I can't figure out where the
black vertical lines are coming from. Any help would be appreciated.

(BTW using Visual Studio 2017 on Windows 10 running on a partitioned iMac.)

int main()
{
        sf::RenderWindow window(sf::VideoMode(520, 480), "Vertical lines issue??");

        while (window.isOpen())
        {
                sf::Event event;

                window.clear();
                drawBasicFlat(window);
                window.display();

                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                        {
                                window.close();
                        }
                }
        }

    return 0;
}

//-------------------------------------------------------------
void drawBasicFlat(sf::RenderWindow& theWindow)
{
        unsigned max_x = theWindow.getSize().x;
        unsigned max_y = theWindow.getSize().y;

        unsigned vertexCount;

        for (unsigned x = 0; x < max_x; ++x)
        {
                sf::VertexArray line(sf::Points, max_y);

                for (unsigned y = 0; y < max_y; ++y)
                {
                        line[y].position = sf::Vector2f(x + 1, y + 1);
                        line[y].color = sf::Color(150, 100, 75);
                }

                theWindow.draw(line);
        }
}

 
Title: Re: Vertical line artifacts in simple image
Post by: dmitry_t on February 16, 2018, 08:13:15 am
A screenshot, please?
Title: Re: Vertical line artifacts in simple image
Post by: Hapax on February 17, 2018, 03:37:47 pm
Without seeing a screenshot, I could guess that it's because of the positioning of the points.
Points are the 'centre' of the point. The co-ordinate of (0, 0) for example) in a default view) is the very corner of the window, not the top-left pixel. To draw that pixel, you need to fill from (0, 0) to (1, 1). Drawing a point fills the entire pixel that it is in inside. If the co-ordinate is (1, 1) though, it may choose any of the four pixels that share that corner.

The solution then, when drawing points, is to add (0.5, 0.5) to each (rounded) pixel co-ordinate. This places the point in the centre of the pixel.