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

Author Topic: sf::Triangles pixel accuracy  (Read 1954 times)

0 Members and 1 Guest are viewing this topic.

Greysuit

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
sf::Triangles pixel accuracy
« on: July 28, 2013, 05:55:48 am »
I have found that drawing triangles is not accurate to the coordinates I specify. I am having issues with vertex pixels being cut off, as well as edges. I am experiencing this with different orientations of triangles. I am only using integers, so it is not a decimal issue.

Below is the code, texture, and screenshot. In the screenshot below, the leftmost vertex is cut off, as is the rightmost edge.

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(512, 256), "Triangle");

        sf::VertexArray triangle(sf::Triangles, 3);
    sf::Texture tex;
    if (!tex.loadFromFile("texture.png"))
                return -1;
       
        triangle[0].position = sf::Vector2f  ( 0, 16);
        triangle[1].position = sf::Vector2f  ( 27, 0);
        triangle[2].position = sf::Vector2f  ( 27, 32);
        triangle[0].texCoords = sf::Vector2f ( 0, 16);
        triangle[1].texCoords = sf::Vector2f ( 27, 0);
        triangle[2].texCoords = sf::Vector2f ( 27, 32);
       
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(triangle, &tex);
        window.display();
    }

    return 0;
}

Sqasher

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: sf::Triangles pixel accuracy
« Reply #1 on: July 28, 2013, 01:47:36 pm »
You know that your texture is 28 x 33 pixels instead of 27 x 32?

Greysuit

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: sf::Triangles pixel accuracy
« Reply #2 on: July 28, 2013, 08:32:04 pm »
That is intentional, since, with the zero row and column, the triangle takes up that much space. In other words, if you open the texture up in a paint program, the bottom right pixel will be pixel 27, 32.

Sqasher

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: sf::Triangles pixel accuracy
« Reply #3 on: July 28, 2013, 10:28:06 pm »
I think SFML uses grid coordinates, not pixel coordinates. (Otherwise the tutorials would be wrong  ;))
Here is a good explanation.