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

Author Topic: Black line is not black  (Read 1022 times)

0 Members and 1 Guest are viewing this topic.

Garwin

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Black line is not black
« on: May 27, 2023, 05:46:22 pm »
I am doing axis of graph by simple using sf::Vertex array.
const sf::Color         AXIS_COLOR          (sf::Color::Black);
   
sf::Vertex              axis[] =
{
    sf::Vertex          (sf::Vector2f       (0.f, GRAPH_SIZE/2), AXIS_COLOR),
    sf::Vertex          (sf::Vector2f       (GRAPH_SIZE, GRAPH_SIZE/2), AXIS_COLOR),
    sf::Vertex          (sf::Vector2f       (GRAPH_SIZE/2, 0.f), AXIS_COLOR),
    sf::Vertex          (sf::Vector2f       (GRAPH_SIZE/2, GRAPH_SIZE), AXIS_COLOR),
};

// and draw it

window.draw (axis, 4, sf::Lines);
 

I am using antialiasing:

sf::ContextSettings settings;
settings.antialiasingLevel = 8;
sf::RenderWindow            window           ({SCREEN_WIDTH,SCREEN_HEIGHT},APPLICATION_NAME, WINDOW_STYLE, settings);
 

However the axis is practically gray and if I set antialiasing to 0, I have a black line.
Is this because of basic coordination use floats?

Is there a way to have both, really black and antialiasing?

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Black line is not black
« Reply #1 on: May 27, 2023, 08:46:00 pm »
the problem is exactly antialiasing, i believe. a line is exactly 1 pixel wide. anything that tries to smooth it will end up mixing a perfect black with anything close to it. thats the point of antialiasing, mixing and blending colors to make things smooth.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Hapax

  • Hero Member
  • *****
  • Posts: 3372
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Black line is not black
« Reply #2 on: May 27, 2023, 11:22:11 pm »
When it comes to 'infinitely thin' lines and points, you should always aim to use the "add a half" system to their co-ordinates. An integer co-ordinate is the top-corner of the pixel you want to use so adding a half to the y (and x) puts the co-ordinates right in the middle of the pixel.
Try that now: add a half to your co-ordinates and voila - black line!

When a co-ordinate is on the edge (or corner) of a pixel, it's not sure which pixel it should be. Add anti-alias into this and you're smoothing it across two pixels (not necessarily evenly either!).
When that co-ordinate is obviously inside the pixel, it knows it should be there.

Note that this becomes slightly more complicated when a co-ordinate system doesn't match the pixel 1-to-1. i.e. when the view isn't the size of the window.



y = 25:


y = 25.5:





EDIT: Added example images.
« Last Edit: May 27, 2023, 11:33:38 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Garwin

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Black line is not black
« Reply #3 on: May 29, 2023, 09:03:49 pm »
When it comes to 'infinitely thin' lines and points, you should always aim to use the "add a half" system to their co-ordinates. An integer co-ordinate is the top-corner of the pixel you want to use so adding a half to the y (and x) puts the co-ordinates right in the middle of the pixel.
Try that now: add a half to your co-ordinates and voila - black line!


Thanks, I did not know that, you can solve it this way.