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

Author Topic: [SOLVED] Vertical lines shifted ?  (Read 4083 times)

0 Members and 1 Guest are viewing this topic.

Yohdu

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
[SOLVED] Vertical lines shifted ?
« on: July 01, 2016, 01:44:13 pm »
Hello everyone,

I'm currently tryring to implement a tile map editor using sfml and Qt, and I am meeting some kind of a problem/bug when drawing vertical and horizontal lines. I can easily work around it, but it just does not feel right to me :).

Here is a code snippet that reproduces my problem :

    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");

    sf::Vertex hLine[] =
    {
        sf::Vertex(sf::Vector2f(0, 0)),
        sf::Vertex(sf::Vector2f(150, 0))
    };

    sf::Vertex vLine[] =
    {
        sf::Vertex(sf::Vector2f(0, 0)),
        sf::Vertex(sf::Vector2f(0, 150))
    };

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

        window.clear();
        window.draw(hLine, 2, sf::Lines);
        window.draw(vLine, 2, sf::Lines);
        window.display();
    }
 

My question is : how is it that I can see the horizontal line but not the vertical one ?

If I add an offset of 0.01 for the x dimension of the vertical line, I can see it. But why ?
Now, if I try to move the lines at the extreme opposite of the window (hLine at y = 600 and vLine at x = 800), I can now see the vertical line but not the horizontal line. It makes sense regarding the initial issue, but then again it does not feel right, the two dimensions should work the same way...

Here are some related topics, they talk about a "0.375 offset issue", but it was supposed to be solved a long long time ago.

http://en.sfml-dev.org/forums/index.php?topic=15747.0
http://en.sfml-dev.org/forums/index.php?topic=14504
https://github.com/SFML/SFML/issues/252

Thank you in advance for your answers :)
« Last Edit: July 21, 2016, 10:19:09 am by Yohdu »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Vertical lines shifted ?
« Reply #1 on: July 01, 2016, 01:52:14 pm »
The line will cover 0.5 pixels above and 0.5 pixels below the given coordinate. So your point Y coordinates have to be the pixels centers (0.5 and 150.5, for example). With integral coordinates, like you do, the OpenGL rasterizer may choose to fill either one or the other side, since the center is located exactly between two pixels.

Same reasoning for vertical lines and X coordinate.
Laurent Gomila - SFML developer

Yohdu

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: Vertical lines shifted ?
« Reply #2 on: July 01, 2016, 01:55:12 pm »
Thank you Laurent for your quick answer. So should I just add 0.5 to the x dimension of all of my vertical lines ? What if on another machine it works the other way ?

EDIT : sorry, bad reading from me. So if I add 0.5 to all of my x and y coordinates, there should be no problem anymore ?

EDIT 2 : should I also add 0.5 when drawing bitmaps or shapes (rectangles, circles, ...) ?

I'm not sure to fully understand where the problem comes from. When exactly should I add 0.5 ? When drawing any kind of shapes or primitives ?
« Last Edit: July 01, 2016, 02:09:39 pm by Yohdu »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Vertical lines shifted ?
« Reply #3 on: July 01, 2016, 02:24:49 pm »
You should add 0.5 to all your coordinates.

When you draw points or lines, you specify a single center position and the point/line expands by a half-pixel on each side of the given coordinate. That's why it should be a .5 coordinate if you want it to fill a full pixel.

When you draw triangles or quads, you specify a range of positions, and every pixel in that range is filled by the primitive. So with triangles and quads you must have integral coordinates.
« Last Edit: July 01, 2016, 02:53:37 pm by Laurent »
Laurent Gomila - SFML developer

Yohdu

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: Vertical lines shifted ?
« Reply #4 on: July 01, 2016, 02:47:11 pm »
Mmmh OK. I think I'll just use sf::RectangleShape to draw my vertical/horizontal lines then, with a width/height of 1 pixel. It seems more practical to me.

Thank you again.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Vertical lines shifted ?
« Reply #5 on: July 01, 2016, 02:55:41 pm »
I wouldn't do that. The impact on performances will be huge.
Laurent Gomila - SFML developer

Yohdu

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: Vertical lines shifted ?
« Reply #6 on: July 01, 2016, 03:04:17 pm »
Oh really ? I did not look for the implementation of sf::RectangleShape, but I was supposing that it used the vertex primitives to draw.

I'll use the 0.5 offset on line primitives then ! :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Vertical lines shifted ?
« Reply #7 on: July 01, 2016, 03:27:41 pm »
sf::RectangleShape uses 1 triangle fan composed by 6 vertices, resulting in 4 triangles being rasterized. Compared to 1 line composed by 2 vertices, you already see the impact.

But the worst is that you'll need one sf::RectangleShape per line, which means as many draw calls as you have lines. Compared to the single vertex array (and thus single draw call) that you could have with sf::Lines for the entire grid.
Laurent Gomila - SFML developer

Yohdu

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: Vertical lines shifted ?
« Reply #8 on: July 01, 2016, 03:34:08 pm »
Wow, I actually did not think of drawing every line in a single draw call.
I was also wondering how to draw dotted line efficiently, I now have my answer!

Well, thanks a lot, that was very constructive for me :)
« Last Edit: July 01, 2016, 03:36:15 pm by Yohdu »