SFML community forums

Help => Graphics => Topic started by: Nanimo on November 30, 2018, 04:05:29 am

Title: Incorrect displaying VertexArray
Post by: Nanimo on November 30, 2018, 04:05:29 am
I wanted to display points 16x16 on window with changed view, but my settings not work as I expected.
First line of points are not draw on window, until i changed {0, 0, 16, 16} to {0, -1, 16, 16} why is that work that way?

sf::RenderWindow window(sf::VideoMode(800, 600), "Window");

    sf::VertexArray points(sf::Points, 256);

    for(size_t i = 0u; i < 16; ++i)
    {
        for(size_t j = 0u; j < 16; ++j)
        {
            if(i == 0 || j == 0 || i == 15 || j == 15)
                points[i * 16 + j] = {{static_cast<float>(j), static_cast<float>(i)}, sf::Color(0u, 255u, 0u)};
            else
                points[i * 16 + j] = {{static_cast<float>(j), static_cast<float>(i)}, sf::Color(255u, 0u, 0u)};
        }
    }

    sf::View view;
    view.reset({0, 0, 16, 16});
    // view.reset({0, -1, 16, 16});
    view.setViewport({0.25f, 0.25f, 0.5f, 0.5f});

    window.setView(view);
 

The whole code in attachment.
Title: Re: Incorrect displaying VertexArray
Post by: Laurent on November 30, 2018, 07:51:02 am
https://en.sfml-dev.org/forums/index.php?topic=24740
Title: Re: Incorrect displaying VertexArray
Post by: FRex on November 30, 2018, 08:04:11 am
Laruent. I was putting together a bigger example. Is this due to that Y axis flipping or what?

Because effects of this small quirk are really unintuitive.

Like this code draws red and blue in bottom right corner and blue and yellow in top left one. No green at all. With off = 0.5f it's of course correct (red in bottom right, all four in top left).

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow win(sf::VideoMode(200u, 200u), "win");
    win.setFramerateLimit(30u);

    sf::VertexArray pts(sf::Points);

    const float off = 0.0f;

    pts.append(sf::Vertex(sf::Vector2f(0.f + off, 0.f + off), sf::Color::Red));
    pts.append(sf::Vertex(sf::Vector2f(1.f + off, 0.f + off), sf::Color::Green));
    pts.append(sf::Vertex(sf::Vector2f(0.f + off, 1.f + off), sf::Color::Blue));
    pts.append(sf::Vertex(sf::Vector2f(1.f + off, 1.f + off), sf::Color::Yellow));

    for(int i = 0; i < 4; ++i)
    {
        sf::Vertex v = pts[i];
        v.position += sf::Vector2f(199.f, 199.f);
        pts.append(v);
    }//for

    while(win.isOpen())
    {
        sf::Event eve;
        while(win.pollEvent(eve))
            if(eve.type == sf::Event::Closed)
                win.close();

        win.clear();
        win.draw(pts);
        win.display();
    }
}

Is this in FAQ for Vertices?
Title: Re: Incorrect displaying VertexArray
Post by: Laurent on November 30, 2018, 09:43:37 am
Quote
Is this due to that Y axis flipping or what?
I think the rounding direction which is different on X and Y axes, is caused by that, yes.

Quote
Because effects of this small quirk are really unintuitive.

Like this code draws red and blue in bottom right corner and blue and yellow in top left one. No green at all. With off = 0.5f it's of course correct (red in bottom right, all four in top left).
It is important to understand what causes it, and the correct way to handle single points coordinates. Understanding how it fails when using wrong coordinates, is not necessary (it's like trying to understand the effects of a C++ UB).

Quote
Is this in FAQ for Vertices?
I don't know, but it really should!