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

Author Topic: Incorrect displaying VertexArray  (Read 2090 times)

0 Members and 1 Guest are viewing this topic.

Nanimo

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Incorrect displaying VertexArray
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Incorrect displaying VertexArray
« Reply #2 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?
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Incorrect displaying VertexArray
« Reply #3 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!
Laurent Gomila - SFML developer