SFML community forums

Help => Graphics => Topic started by: cppxor2arr on August 15, 2017, 03:36:01 pm

Title: Boundaries of X, Y coordinates in window
Post by: cppxor2arr on August 15, 2017, 03:36:01 pm
In this code:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window{sf::VideoMode{300, 300}, "Window Title"};

    sf::VertexArray points{sf::Points, 4};

    // which two of these are the top left and bottom right corner

    points[0].position = sf::Vector2f{0, 0};
    points[1].position = sf::Vector2f{1, 1};
    points[2].position = sf::Vector2f{299,  299};
    points[3].position = sf::Vector2f{300, 300};

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed)
                window.close();
        window.clear(); window.draw(points); window.display();
    }
}

What are the boundaries? 0-299 or 1-300 (width or height)? Also, the question in the comment.
Title: Re: Boundaries of X, Y coordinates in window
Post by: eXpl0it3r on August 15, 2017, 03:50:26 pm
(0, 0) is top left
(width - 1, height -1) is bottom right

However, when you use sf::Points primitive, you have to consider that a pixel is still an area, while it can't be resolved any further by your screen, OpenGL still handles it as float, so the top left pixel starts at position (0, 0) and spawns up to the point (1, 1). So if you want to render a point on that pixel, you need to center the point, setting its position to (0.5f, 0.5f).

Illustration

(http://i.imgur.com/GIvqmob.png)

Red = Point at (0, 0)
Blue = Point at (0.5f, 0.5f)
Title: Re: Boundaries of X, Y coordinates in window
Post by: cppxor2arr on August 15, 2017, 04:15:09 pm
Thanks a lot!
Title: Re: Boundaries of X, Y coordinates in window
Post by: Laurent on August 25, 2017, 03:11:04 pm
It is a side effect of OpenGL rounding rules. The correct position is (0.5, 0.5).