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

Author Topic: Drawing line from vertices not visible when x = 0  (Read 112 times)

0 Members and 1 Guest are viewing this topic.

Neil

  • Newbie
  • *
  • Posts: 27
    • View Profile
Drawing line from vertices not visible when x = 0
« on: June 28, 2020, 03:47:33 am »
I am trying to draw a vertical line where x=0 but I can't see it. I have to use x=1 to see a line. However, for horizontal lines, I can see lines where y=0. I don't understand the difference in behavior.

sf::RenderWindow window(sf::VideoMode(640, 480), "fghjnufhg");

{

                        const std::array<sf::Vertex, 2> line =
                        {
                                sf::Vertex(sf::Vector2f(0, 0), sf::Color::Red),
                                sf::Vertex(sf::Vector2f(0, 100), sf::Color::Red)
                        };

                        window.draw(line.data(), line.size(), sf::Lines, sf::RenderStates::Default);
                }

                {

                        const std::array<sf::Vertex, 2> line =
                        {
                                sf::Vertex(sf::Vector2f(1, 0), sf::Color::Green),
                                sf::Vertex(sf::Vector2f(1, 100), sf::Color::Green)
                        };

                        window.draw(line.data(), line.size(), sf::Lines, sf::RenderStates::Default);
                }

                {

                        const std::array<sf::Vertex, 2> line =
                        {
                                sf::Vertex(sf::Vector2f(0, 0), sf::Color::Yellow),
                                sf::Vertex(sf::Vector2f(100, 0), sf::Color::Yellow)
                        };

                        window.draw(line.data(), line.size(), sf::Lines, sf::RenderStates::Default);
                }

                {

                        const std::array<sf::Vertex, 2> line =
                        {
                                sf::Vertex(sf::Vector2f(0, 1), sf::Color::Blue),
                                sf::Vertex(sf::Vector2f(100, 1), sf::Color::Blue)
                        };

                        window.draw(line.data(), line.size(), sf::Lines, sf::RenderStates::Default);
                }

                window.display();
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32415
    • View Profile
    • SFML's website
    • Email
Re: Drawing line from vertices not visible when x = 0
« Reply #1 on: June 28, 2020, 01:08:21 pm »
This question is asked quite frequently. Have you first tried to use the search engine?
Laurent Gomila - SFML developer

Neil

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Drawing line from vertices not visible when x = 0
« Reply #2 on: July 01, 2020, 05:06:47 am »
I found this: https://en.sfml-dev.org/forums/index.php?topic=20500.msg147365#msg147365

Adding 0.5 to my vertex coordinates seems to work, though it seems strange to me the different directions would behave differently.

Do I need to add 0.5 to other drawable objects? That post says "You should add 0.5 to all your coordinates", but when I draw a sf::RectangleShape at (0.5, 0.5), it is at the left edge but overcorrected down 1 pixel. (0, 0) works as expected. How can I know when I need this correction or not?
« Last Edit: July 01, 2020, 05:10:58 am by Neil »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32415
    • View Profile
    • SFML's website
    • Email
Re: Drawing line from vertices not visible when x = 0
« Reply #3 on: July 01, 2020, 07:52:53 am »
Quote
though it seems strange to me the different directions would behave differently
It's because SFML reverts the Y axis (OpenGL initially has it from bottom to top), so I guess that rasterization rules still round towards the bottom for Y values.

Quote
Do I need to add 0.5 to other drawable objects?
Only for lines. They are a special case: to rasterize them, OpenGL fills pixels crossed by the line. So you easily understand that if the line is exactly between two pixel rows or columns, it's an edge case, and OpenGL may choose one or the other depending on internal rounding rules that may not be clear to the end user.

To understand this problem, the best is to take square paper and try to rasterize (fill whole squares -- "pixels") these different cases yourself, as well as other primitives such as rectangles.
Laurent Gomila - SFML developer