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

Author Topic: Line of sight / Fog drawing with SFML  (Read 1388 times)

0 Members and 1 Guest are viewing this topic.

Ettur

  • Newbie
  • *
  • Posts: 6
    • View Profile
Line of sight / Fog drawing with SFML
« on: March 21, 2020, 06:18:36 pm »
Hi,

I would like to implement a feature so that player would have a certain line of sight visible in front of him. For example a triangular area where one vertex is at player and other two would go to distance of screen length at an angle between them.

As an idea, I thought to make a rectangle with alpha 0.5 to cover entire screen and then a triangle shaped area that would show through the rectangle but I cant think of how would I make it or is that idea even possible?

I guess one option would be to drawn 0.5 alpha area pixel by pixel and I could calculate the area of that visible triangle shape and make those pixels transparent? That means drawing every pixel on screen for every frame, sounds a bit too much hardware usage ?

Or is there some way I am not thinking it would be possible to do?

If you have played DOTA2 , thats the sort of mechanism Im aiming for. Later on I would like to implement structures also that player cant see through but for starters id be happy to understand simple fog reveal.

I tryed to google line of sight and fog of war on google but I didnt find quite the thing I was aiming for. If anyone has experience with this could you share ideas? Or if you know resource / tutorials where I could get help / hints id appreciate it very much :)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Line of sight / Fog drawing with SFML
« Reply #1 on: March 22, 2020, 09:54:26 pm »
If you want to cover parts outside the triangle (either completely or slightly, for fog), you could draw a single shape that covers the entire window/scene except the triangle. It could be black for darkness, white with alpha for fogs or anything else really!

Another way is to use a lighting texture system, which can be a little more involved.
Basically, setup a render texture the same size as the window, clear it a solid colour (black or grey) and draw the triangle as a solid full white.
Then draw that render texture onto the fully drawn window using blend multiply. White parts are fully shown, black parts are erased and become fully black, greys are somewhere inbetween; they will look darkened.

A third way is the same as the second except it's the opposite approach.
Same render texture but clear it as white (or grey) and draw the triangle as a solid full black.
When drawing the render texture onto the fully drawn window, use blend add. Black parts are left as normal and white parts become fully white. Greys will bring up the colours towards white.

The first method is simplest and easiest and also really flexible.
The second can only darken, not add white fog.
The third can only add white fog, not darken.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything