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

Author Topic: Texture problem  (Read 1718 times)

0 Members and 1 Guest are viewing this topic.

NOVa

  • Newbie
  • *
  • Posts: 4
    • View Profile
Texture problem
« on: July 03, 2016, 11:24:54 pm »
Hello!
I've found one strange bug.
When I try to set the position of any sprite to (x.5,x.5) texture becomes weird. It looks like it looses it's size.
Like this: sprite.setPosition(0.5, 0.5)
X and Y can be = 0.5 separately from each other to create this bug.
I included the screenshot with broken sprite at the left side and normal one at the right side.
Thank you for attention and sorry for my English. =)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
AW: Texture problem
« Reply #1 on: July 04, 2016, 03:06:09 am »
What do you expect to happen when you tell SFML/OpenGL to draw a half pixel? How should your display pixel show?

When something should be drawn between pixels, OpenGL has to do an approximation which can have multiple side effects. One of which is that the next pixels of the underlying texture are being used despite the texture rect not going as far.

The general solution is to use interger positions.

Other/additional solutions may include adding a one pixel border around all sprites to prevent that texture "bleeding". Or to render things first to a render texture and only then to the screen.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

NOVa

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Texture problem
« Reply #2 on: July 04, 2016, 08:19:59 am »
eXpl0it3r, thank you a lot for the answer! My guess was right, but I was suprised, that for all other fractional parts of the position everything works great.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Texture problem
« Reply #3 on: July 04, 2016, 08:47:15 am »
0.5 is exactly the middle position between two pixels. So, with the help of floating point inaccuracy, the OpenGL rasterizer may not round the beginning and end positions of your sprite in the same direction, resulting in +- 1 extra pixel and the stretching issue you're experiencing.
Laurent Gomila - SFML developer

NOVa

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Texture problem
« Reply #4 on: July 04, 2016, 09:06:46 am »
Laurent, thank you for help! But I guess, that this problem appears with scaling and rotating too. What kind of solution can be this time (excluding use of integer)?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Texture problem
« Reply #5 on: July 04, 2016, 09:26:00 am »
Yes, it appears with scaling and rotation too. And there's no general solution.

But this is actually a desired effect: move, rotate or scale slowly your sprite, and you'll see a nice smooth movement. If the interpolation that you now see as an issue was not performed, the movement would be jerky. So this is great for movement but indeed a problem with static entites. The only workaround is to ensure pixel-perfect rasterization by controlling all the transformations involved in the rasterization process.
Laurent Gomila - SFML developer

NOVa

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Texture problem
« Reply #6 on: July 04, 2016, 09:33:36 am »
Laurent, thank you! I think, that using small borders (as eXpl0it3r offered) with smoothing will reduce this effect. Now I understand fully. Problem is solved. Thanks to everybody!

 

anything