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

Author Topic: ugly outline with overlapping sprites with smoothing on  (Read 4474 times)

0 Members and 1 Guest are viewing this topic.

tiby312

  • Newbie
  • *
  • Posts: 9
    • View Profile
ugly outline with overlapping sprites with smoothing on
« on: August 19, 2011, 08:12:43 pm »


See that green outline where the turret sprite overlaps with the base of the tank? That doesn't look too pretty. How can I get rid of that outline without disabling smoothing?

Why do I want to keep smoothing on? Since this tank moves around a lot pretty slowly, it is not pretty having the sprites snapping to the nearest pixel all the time when I want a nice smooth movement. Basically it's just prettier with it on.

Using sfml 1.6

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
ugly outline with overlapping sprites with smoothing on
« Reply #1 on: August 19, 2011, 08:30:56 pm »
It's simple, don't put green pixels around your tank in the original image ;)

The best solution should be to use the same color as the border (dark gray).
Laurent Gomila - SFML developer

tiby312

  • Newbie
  • *
  • Posts: 9
    • View Profile
ugly outline with overlapping sprites with smoothing on
« Reply #2 on: August 19, 2011, 08:39:53 pm »
But there are no green pixels in the image. It's a transparent png image. I mean, if there were green pixels in the image, they would appear even with smoothing off.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
ugly outline with overlapping sprites with smoothing on
« Reply #3 on: August 19, 2011, 09:11:12 pm »
Well, I think that your transparent pixels are green (it's usually what people do to see them easily when transparency is not applied). Don't forget that transparency is encoded in the alpha channel of a pixel, red green and blue are still there and can have any value.
Laurent Gomila - SFML developer

tiby312

  • Newbie
  • *
  • Posts: 9
    • View Profile
ugly outline with overlapping sprites with smoothing on
« Reply #4 on: August 19, 2011, 09:28:45 pm »
Ah, yes thank you. I changed the colors of those pixels, and it is not as ugly.

But still, I feel like this only masks the problem as those extra pixels are still there making the border of the sprite bigger and blurrier. I could try to compensate by making the original sprite border thinner, but I cannot give it an opaque border as the edge would always be semi-transparent. Plus the areas that do not overlap do not have this problem so the border of the sprite would thicken and thin depending on how it moves on the sprite under it. Is there any way to get the sprite to display as it is, nothing more, nothing less all the time?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
ugly outline with overlapping sprites with smoothing on
« Reply #5 on: August 19, 2011, 10:05:21 pm »
Quote
Plus the areas that do not overlap do not have this problem

They do, but the green border is blended with blue, which makes it much less noticeable than with black.

Quote
Is there any way to get the sprite to display as it is, nothing more, nothing less all the time?

Yes. Pixel perfect rendering involves turning smoothing off. When you enable it, pixels are modified and this problem cannot be avoided. A solution would be to smooth RGB components but not the alpha channel -- but OpenGL doesn't offer this feature.

But maybe there's a better solution. You can try to add this code after the creation of the window:
Code: [Select]
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_EQUAL, 1.f);

You must link your project with the OpenGL library (-lGL on Linux, opengl32.lib/a on Windows).
Laurent Gomila - SFML developer

tiby312

  • Newbie
  • *
  • Posts: 9
    • View Profile
ugly outline with overlapping sprites with smoothing on
« Reply #6 on: August 19, 2011, 10:54:25 pm »
Quote
A solution would be to smooth RGB components but not the alpha channel -- but OpenGL doesn't offer this feature.


This must be what I have been used to. The XNA game engine as well as the GameMaker engine and also HTML5 canvas must do it this way because I've used this same exact sprite with the invisible green pixels with those and had smooth sprites with no trouble. It would make sense because they don't use opengl.

Code: [Select]

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_EQUAL, 1.f);


This appears to work very well if your sprite doesn't have any semi-transparent pixels, but it got rid of all of my semi-transparent pixels in my sprite, so I changed it to this:

Code: [Select]

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER,0.39f);

I kept raising the limit until the outline was almost invisible. Looks pretty good now, but I feel dirty for using this messy solution and it's still not as perfect as possible, but I guess it's the best possible in opengl. Thanks for your patience and help!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
ugly outline with overlapping sprites with smoothing on
« Reply #7 on: August 19, 2011, 11:00:03 pm »
Quote
This must be what I have been used to. The XNA game engine as well as the GameMaker engine and almost HTML5 canvas must do it this way because I've used this same exact sprite with the invisible green pixels with those engines and had no trouble. It would make sense because neither use opengl.

OpenGL provides the same features as DirectX, they both give access to what graphics cards can do. So if the feature is available with XNA it must be available as well with OpenGL (I don't know what browsers use to implement HTML 5 rendering).

I'm curious about how XNA implements this feature.

Quote
This appears to work very well if your sprite doesn't have any semi-transparent pixels, but it got rid of all of my semi-transparent pixels in my sprite

Indeed, I forgot to tell you about that.

Quote
I kept raising the limit until the outline was almost invisible. Looks pretty good now, but I feel dirty for using this messy solution and it's still not as perfect as possible

I agree, it's not a solution but rather a quick and dirty workaround.
Laurent Gomila - SFML developer

 

anything