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

Author Topic: Vertex strange stripes  (Read 2618 times)

0 Members and 1 Guest are viewing this topic.

Merulo

  • Newbie
  • *
  • Posts: 7
    • View Profile
Vertex strange stripes
« on: October 30, 2016, 08:25:37 pm »
Welcome!

I'm working on some project.

I wrote this function:

void Display::Draw(unsigned int layer)
{
        for (unsigned int i = 0; i < m_width; ++i)
                for (unsigned int j = 0; j < m_height; ++j)
                {
                        int tileNumber;

                        tileNumber = Map->MapData[i][j].TextureID;

                        int tu = tileNumber % (tilemap.getSize().x / 64);
                        int tv = tileNumber / (tilemap.getSize().x / 64);

                        sf::Vertex* quad = &vertices[(i + j * m_width) * 4];

                        quad[0].position = sf::Vector2f((float)i * 64 - MOVED, (float)j * 64 - MOVED);
                        quad[1].position = sf::Vector2f((float)(i + 1) * 64 - MOVED, (float)j * 64 - MOVED);
                        quad[2].position = sf::Vector2f((float)(i + 1) * 64 - MOVED, (float)(j + 1) * 64 - MOVED);
                        quad[3].position = sf::Vector2f((float)i * 64 - MOVED, (float)(j + 1) * 64 - MOVED);

                        quad[0].texCoords = sf::Vector2f((float) tu * 64, (float)tv * 64);
                        quad[1].texCoords = sf::Vector2f((float)(tu + 1) * 64, (float)tv * 64);
                        quad[2].texCoords = sf::Vector2f((float)(tu + 1) * 64, (float)(tv + 1) * 64);
                        quad[3].texCoords = sf::Vector2f((float) tu * 64, (float)(tv + 1) * 64);
                }

        window->draw(vertices, &tilemap);

}

 

But on some computers a strange thing happens. The file which is held in the tilemap is attached to the post.
(The textures aren't beautiful, so don't judge). The problem are the stripes which are showing (check the attached screenshot). They shouldn't since TextureID contains numbers 0-5.

Why are they showing,
and how to prevent them? 

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Vertex strange stripes
« Reply #1 on: October 31, 2016, 12:02:04 am »
i had same problem with this,
so, the only solution that work for me, is by adding "half-pixel" to texture coordinate. as shown here

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Vertex strange stripes
« Reply #2 on: October 31, 2016, 04:04:19 pm »
It's a precision problem. Round your coordinates. Sometimes you're rounding up, sometimes down (which then causes those gaps).

Merulo

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Vertex strange stripes
« Reply #3 on: October 31, 2016, 05:43:58 pm »
Adding "half-pixel" is helping when its not zoomed-out. I guess i can tie it up to zooming, but still, this is not perfect solution.

If it's a precision problem then rounding correctly won't help. Even though i tried rounding all up, rounding all down, and rounding some down and some up it did not help.
The problem is probably in the non-perfect representation of numbers by the float type.

For the record, this is the way i round down:
                        quad[0].texCoords = sf::Vector2f(floor((float)tu *       64 + Fraction), floor((float)tv *       64 + Fraction));
                        quad[1].texCoords = sf::Vector2f(floor((float)(tu + 1) * 64 - Fraction), floor((float)tv *       64 + Fraction));
                        quad[2].texCoords = sf::Vector2f(floor((float)(tu + 1) * 64 - Fraction), floor((float)(tv + 1) * 64 - Fraction));
                        quad[3].texCoords = sf::Vector2f(floor((float)tu *       64 + Fraction), floor((float)(tv + 1) * 64 - Fraction));
 

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Vertex strange stripes
« Reply #4 on: November 01, 2016, 12:02:28 am »
It's a precision problem. Round your coordinates. Sometimes you're rounding up, sometimes down (which then causes those gaps).
there is a question regard this issue in the forums , to add correction factor to texture coordinates is a valid solution to this problem. based on that question


Adding "half-pixel" is helping when its not zoomed-out. I guess i can tie it up to zooming, but still, this is not perfect solution.
thats true. the example that i showed has fix zooming. but incase varying zooming i added "0.1f" to the vertex position instead. as shown here.
note: i used isomeric grid.
i hope that will help



BlueCobold

  • Full Member
  • ***
  • Posts: 105
    • View Profile
Re: Vertex strange stripes
« Reply #5 on: November 02, 2016, 07:10:11 am »
When zooming, even perfectly aligned vertices/polygons might show weirdly colored pixels at the edges between the polygons. That's due to the fact that the color from the texture needs to be interpolated and that's where filters will be used by OpenGL. Those can cause texture-bleeds (where pixels next to your source-area must be taken or at least incorporated into the final color). You can't solve this problem by overlapping your vertices and you shouldn't overlap them anyway. You need to fix your texture to not have areas which during sampling might take a totally different color (as it will happen if you just put different types of textures right next to one another in the same texture file).
tl;dr: Change your texture to prevent-texture bleeding, and don't do weird offset-stuff in your render-code.
« Last Edit: November 02, 2016, 07:12:14 am by BlueCobold »