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

Author Topic: Drawing points in vertex array is offset?  (Read 3446 times)

0 Members and 1 Guest are viewing this topic.

Robbis_1

  • Newbie
  • *
  • Posts: 9
    • View Profile
Drawing points in vertex array is offset?
« on: January 09, 2014, 06:49:19 pm »
Hello, I'm running SFML 2.1 with revision (https://github.com/SFML/SFML/tree/56c2eb8cea5253e079bbf10aad80503512b87dd9) which is only 3 months old.

Take a look at this code:
int main()
{
        sf::RenderWindow window(sf::VideoMode(80, 60), "SFML window");
        window.setFramerateLimit(30);

        sf::VertexArray points;
        points.setPrimitiveType(sf::Points);
        points.append(sf::Vertex(sf::Vector2f(0, 0), sf::Color(255, 0, 0, 255)));
        points.append(sf::Vertex(sf::Vector2f(1, 1), sf::Color(255, 0, 0, 255)));
        points.append(sf::Vertex(sf::Vector2f(2, 2), sf::Color(255, 0, 0, 255)));

        sf::VertexArray lines;
        lines.setPrimitiveType(sf::Lines);
        lines.append(sf::Vertex(sf::Vector2f(0, 0), sf::Color(0, 255, 0, 127)));
        lines.append(sf::Vertex(sf::Vector2f(5, 5), sf::Color(0, 255, 0, 127)));

        while (window.isOpen())
        {
                window.clear();
                window.draw(points);
                window.draw(lines);
                window.display();
        }

        return 0;
}

This is currently producing this as a result (zoomed in):


Shouldn't it be something more like this?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Drawing points in vertex array is offset?
« Reply #1 on: January 09, 2014, 06:53:15 pm »
Seems to be another issue of how OpenGL tries to handle pixels. Make sure your view position is a whole number and it matches the window 1:1 (same size and not zoomed).   ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Robbis_1

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Drawing points in vertex array is offset?
« Reply #2 on: January 09, 2014, 06:59:24 pm »
Considering that is the only code it should be whole numbers and no zoom.

EDIT: To clarify, the zoom is by taking a picture of the application that is running the code above and then scaling it in a separate program.
« Last Edit: January 09, 2014, 07:03:52 pm by Robbis_1 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Drawing points in vertex array is offset?
« Reply #3 on: January 10, 2014, 09:46:15 am »
Pixel perfect drawing is not the strongest feature of OpenGL unfortunately. Since you didn't put it in perspective, is the green pixel at 0,0 or is the red pixel at 0,0? Because sometimes OpenGL will select the middle path and thus the drawing can be one pixel off.
Maybe a screenshot of the window would be good enough. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Robbis_1

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Drawing points in vertex array is offset?
« Reply #4 on: January 10, 2014, 10:58:35 am »
I'm not sure what you mean that I didn't put in perspective. The two images are taken directly from the window running the code I posted.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Drawing points in vertex array is offset?
« Reply #5 on: January 10, 2014, 11:30:06 am »
Yes of course, but we don't know whether the top most pixel of your posted image is actually at position 0,0.
If the line gets drawn at the pixel position (0,1), (1,2), (2,3), (3,4), (4,5) then it can kind of make sense and the red pixels would be at the right position.

What happens if you draw the line as following:
    sf::VertexArray lines;
    lines.setPrimitiveType(sf::Lines);
    lines.append(sf::Vertex(sf::Vector2f(0.5f, 0.5f), sf::Color(0, 255, 0, 127)));
    lines.append(sf::Vertex(sf::Vector2f(4.5f, 4.5f), sf::Color(0, 255, 0, 127)));
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Robbis_1

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Drawing points in vertex array is offset?
« Reply #6 on: January 10, 2014, 12:07:54 pm »
But isn't the top left pixel of the screen always 0, 0?

Your code produces the same result as the first image.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Drawing points in vertex array is offset?
« Reply #7 on: January 10, 2014, 12:24:26 pm »
But isn't the top left pixel of the screen always 0, 0?
Yes (unless you change the view), but that is exactly the point. You did not say whether the image you posted are from 0,0 or not. It might have been drawn on the bottom right of the window and we'd never know, since you didn't say. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Robbis_1

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Drawing points in vertex array is offset?
« Reply #8 on: January 10, 2014, 12:31:58 pm »
Right, sorry. I assumed it was obvious that my pictures are taken from running the example code.  :)
So the top left in my images is at 0, 0.

 

anything