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

Author Topic: [SOLVED] See through walls like effect  (Read 1775 times)

0 Members and 1 Guest are viewing this topic.

taifun93

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED] See through walls like effect
« on: May 09, 2023, 01:46:11 pm »
Hi!

I am working on a 2d isometric game with SFML, nothing fancy or professional and i am looking for a way to draw outlines to entities that are behind tall objects (trees, walls) and only partially.

The game first draws the ground which is a tilemap, then it draws the entities, then it draws the outlines.
Currently i am using a shader to draw the outline on top of everything at all times which is pretty close to what i am looking for but not too great. If the texture has opacity on the outline pixels then it looks really bad.

You can see in the attached image the result i am looking for.

Is there any way i can draw the outline only on the overlapping areas? I suppose i will have to determine the overlapping areas in a shader, but i don't know if that's possible of how to do it.
« Last Edit: May 12, 2023, 09:20:44 am by taifun93 »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: See through walls like effect
« Reply #1 on: May 09, 2023, 07:55:43 pm »
You can clip to another image's alpha mask using a render texture.

I gave a short example of how it could be done here:
https://en.sfml-dev.org/forums/index.php?topic=19505#msg140615

It gives this result (blood image clipped to the SFML logo shape):


It's an extra step but it is simple and doesn't require shaders.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

taifun93

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: See through walls like effect
« Reply #2 on: May 10, 2023, 11:44:21 am »
Having a second texture for the outline can be easily done.

All the entities will be already drawn (player, trees, enemies) on a texture (call it ent_texture) before i will apply the overlay. The entities need to be drawn in a specific order, the most distant will be drawn first. If i use that ent_texture's alpha mask then the entire outline will be rendered because the outline's entity is already there. And this way i end up with the same result as before.

Maybe i could have another texture (call it rev_ent_texture) to draw on it the entities in reverse order, and when i encounter an entity that might require an outline i will use the rev_ent_texture's alpha mask to draw the outline texture on the ent_texture that will finally be displayed to the user. I don't know what impact it will have on the performance but it's worth giving it a shot.

Thx

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: See through walls like effect
« Reply #3 on: May 10, 2023, 05:09:25 pm »
You can draw, to a render texture, only the objects that are obscuring the view of the player and then draw the player outline to that render texture, clipping it to the obscuring objects.
What you have on this render texture is just the outline where the player and the obscuring objects overlap.

Then, you can draw that render texture to the main render target (likely to be the window) and it will be just drawing the outline and only the outline that overlaps the obscuring objects.



If your issue is determining which objects overlap the player, you could simply draw all of the objects to that render texture that you would draw in front of the player. Then, the clipped outline would still give the same result: the outline at the only parts where the things in front of it would be overlap the player.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

taifun93

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: See through walls like effect
« Reply #4 on: May 12, 2023, 09:10:50 am »
Thank you for the clipping method!

I did it just like that and i got it working and it has no significant impact on the performance. The results are exactly what i was looking for.

This is the code if you want to see it: https://github.com/r-a-cristian-93/LastHero/blob/c8e5cd9b20e5c9ce83066f560a5b7781ab1e1e86/src/SystemDrawEntities.h

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [SOLVED] See through walls like effect
« Reply #5 on: May 15, 2023, 08:49:51 pm »
You are welcome! :)

Glad you got it doing what you wanted it to do.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*