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

Author Topic: sf::VertexArray strange lines  (Read 2149 times)

0 Members and 1 Guest are viewing this topic.

Donkamilo

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
sf::VertexArray strange lines
« on: May 05, 2017, 12:39:13 am »
Hello.
I'm currently working on a project. We are using vertexarray to display the titlemap. However they are bringing us quite an issue. We have managed to produce the minimal working example.
int main()
{
        sf::VertexArray vertexArray;
        int data[200][200];
        for (int i = 0; i < 200; i++) {
                for (int j = 0; j < 200; j++) {
                        data[i][j] = 0;
                }
        }

        vertexArray.resize(200 * 200 * 4);

        sf::Texture tex;
        tex.loadFromFile("MWE.png");

        for (int j = 0; j < 200; ++j) {
                for (int i = 0; i <200; ++i) {
                        int tileID = data[i][j];
                        sf::Vector2f size = sf::Vector2f((float)128, (float)128);
                        int rotation = 0;
                        int tu = tileID % (int)(tex.getSize().x / size.x);
                        int tv = tileID / (int)(tex.getSize().y / size.y);
                        sf::Vertex * quad = &vertexArray[size_t(i + j * 200)* 4];

                        quad[0].position = sf::Vector2f(std::floor((float)(i * 128)), std::floor((float)(j * 128)));
                        quad[1].position = sf::Vector2f(std::floor((float)(i * 128 + size.x)), std::floor((float)(j * 128)));
                        quad[2].position = sf::Vector2f(std::floor((float)(i * 128 + size.x)), std::floor((float)(j * 128 + size.y)));
                        quad[3].position = sf::Vector2f(std::floor((float)(i * 128)), std::floor((float)(j * 128 + size.y)));

                        quad[(0 + rotation / 90) % 4].texCoords = sf::Vector2f(std::floor((float)(tu)* 128), std::floor((float)(tv * 128)));
                        quad[(1 + rotation / 90) % 4].texCoords = sf::Vector2f((std::floor((float)(tu + 1)* 128)), std::floor((float)(tv * 128)));
                        quad[(2 + rotation / 90) % 4].texCoords = sf::Vector2f((std::floor((float)(tu + 1)* 128)), std::floor((float)((tv + 1) * 128)));
                        quad[(3 + rotation / 90) % 4].texCoords = sf::Vector2f(std::floor((float)(tu)* 128), std::floor((float)((tv + 1) * 128)));
                }
        }
        sf::RenderWindow window(sf::VideoMode(1600, 900), "My window");

        sf::View view;
        sf::RenderStates states;
        states.texture = &tex;
        view.setSize(1600, 900);
        view.setCenter(800, 450);
        window.setView(view);
        window.setFramerateLimit(60);

        // run the program as long as the window is open
        while (window.isOpen())
        {
                window.clear(sf::Color::Black);
                // check all the window's events that were triggered since the last iteration of the loop
                sf::Event event;
                while (window.pollEvent(event))
                {
                        // "close requested" event: we close the window
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Q)) {
                        view.zoom(1.1f);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::E)) {
                        view.zoom(0.9f);
                }
                //moving up
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && true) {
                        view.move(0, -32);
                }
                //movind down
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S) && true) {
                        view.move(0, 32);
                }
                //moving left
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A) && true) {
                        view.move(-32, 0);
                }
                //moving UP
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D) && true) {
                        view.move(32, 0);
                }
               
                window.setView(view);
                window.draw(&vertexArray[0], 200 * 200 *4, sf::Quads, states);
                window.display();
        }
        return 0;
}
 

The MWE.png file should be attached to the post as well the screenshot of the problem.

The description:
The cords in quads positions and texCords are rounded and created from integers. In the beginning there are no sings of the problem. It starts when we are zooming out (and it's especially visible when moving). It displays itself by overlapping the texture used with the pixels nearby that texture(you can see it as red lines - check the files.).

Is there a way to prevent it?
Thanks for help.
Peace.

« Last Edit: May 05, 2017, 12:42:58 am by Donkamilo »

Donkamilo

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: sf::VertexArray strange lines
« Reply #1 on: May 11, 2017, 09:57:20 pm »
Why no one replied?
Is something unclear or just I am being ignored?

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: sf::VertexArray strange lines
« Reply #2 on: May 11, 2017, 10:44:12 pm »
This is a common problem with vertex arrays (just search the forum for tilemap artifacts).  You can try zooming/moving the camera on rounded coordinates or rendering chunks of maps to a render texture then display that, although my favourite solution is this.

Donkamilo

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: sf::VertexArray strange lines
« Reply #3 on: May 26, 2017, 10:56:27 pm »
I like the idea you linked, however it will not work with some options i have planned to do.

I wonder if it's a common problem with the vertex array why one proper solution haven't been found. Is it impossible to have working vertex array with zooming?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: sf::VertexArray strange lines
« Reply #4 on: May 28, 2017, 02:31:24 am »
A couple of other, probably less common, solutions are:
  • render to a large render texture and scale that,
  • extend texture pieces. that is add an extra pixel around each tile with a copy of its outer pixels
note that "solution" 2 means altering the layout of the texture.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything