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

Author Topic: Tilemap rendering issues  (Read 4651 times)

0 Members and 1 Guest are viewing this topic.

MrShedman

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Tilemap rendering issues
« on: February 25, 2014, 11:04:53 pm »
Can anyone explain why I get these rendering quirks on different GPU's?

AMD HD5850: Looks perfect no artifacts :D


nVidia Gefore GT 740M: Some tiles shift upwards/downards by 1 pixel but leave no gaps


Intel 4400:  Only horizontal gaps and weird shifting inside the tiles

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Tilemap rendering issues
« Reply #1 on: February 25, 2014, 11:27:37 pm »
I guess you're using non-integral coordinates. Different graphics cards may rasterize the texture in different ways. If you need pixel-perfect rendering, round everything (see the note in the documentation of Drawable.hpp I recently added).

There are many threads about this and some related issues on the forum, e.g. this one, this one or this one.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

MrShedman

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Tilemap rendering issues
« Reply #2 on: February 25, 2014, 11:41:13 pm »
Each tile in my tileset texture is 48x48 and the same on screen, so the 1:1 mapping shouldn't be the problem.

The tilemap is never moved, only the view is moved and each tile on startup is given and integer position and size (although casted to a float to work with sf::Vertex)

The code below is what I'm using to draw the tilemap.

void draw()
{
        sf::View view = window->getView();
        float cx = view.getCenter().x;
        float cy = view.getCenter().y;
        float x = cx - window->getSize().x / 2;
        float y = cy - window->getSize().y / 2;
        cx = std::floor(cx);
        cy = std::floor(cy);

        view.setCenter(cx, cy);
        rTexture.setView(view);

        rTexture.clear(sf::Color::Transparent);
        rTexture.draw(*map);
        rTexture.display();

        const sf::Texture& texture = rTexture.getTexture();

        // draw it to the window
        sf::Sprite sprite(texture);
        sprite.setPosition(x ,y);

        window->draw(sprite);
}
 

ncsu121978

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Tilemap rendering issues
« Reply #3 on: March 05, 2014, 02:50:46 pm »
moving the view or moving the tilemap is essentially the same thing anyway ultimately to the video card doing the actual rendering.  It is ultimately trying to render your tiles at a non-integer location and with floating point precision not being perfect can lead exactly to the problem you are seeing.

I would suggest to keep a floating point location for your view, but before rendering, round/cast it to the closest integer.

MrShedman

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Tilemap rendering issues
« Reply #4 on: March 07, 2014, 02:51:04 am »
After hours of googling and several dodgy hacks later I came upon one method that works 100% on all of my different set-ups!

float x = view.getCenter().x;
float y = view.getCenter().y;

x = std::floor(x);
y = std::floor(y);
x += 0.375f;
y += 0.375f;

view.setCenter(x, y);

I was already rounding the views co-ords to integers, but adding 0.375f draws the tiles pixel-perfect every time! :D
« Last Edit: March 07, 2014, 02:53:41 am by MrShedman »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Tilemap rendering issues
« Reply #5 on: March 07, 2014, 11:27:15 am »
Strange... I remember that this 0.375 offset was once included in SFML because of exactly this problem. And for me personally, pixel-perfect rendering always worked with simple rounding of coordinates -- as long as I really did it for all the involved ones.

Are you using an outdated SFML version?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

MrShedman

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Tilemap rendering issues
« Reply #6 on: March 08, 2014, 05:34:23 am »
It does seem strange although I found this "hack" in several forums and the general reasoning behind it was AMD/nVidia round floating point numbers differently.

I'm using whatever was the latest revision on github about a month ago, so as far as I can see from the commits nothing has been changed that would affect this.


 

anything