2

I'm making a 2D game with OpenGL, using textured quads to display 2D sprites. I'd like to create an effect whereby any character sprite that's partially hidden by a terrain sprite will have the occluded region visible as a solid-colored silhouette, as demonstrated by the pastoral scene in this image.

I'm really not sure how to achieve this. I'm guessing the solution will involve some trick with the fragment shader, but as for specifics I'm stumped. Can anyone point me in the right direction?

whig
  • 23
  • 2

2 Answers2

1

You can do things like this using stenciling or depth buffering. When rendering the wall make sure that it writes a different value to the stencil buffer than the background. Then render the cow twice, once passing the stencil test when not at the wall, and once otherwise. Use a different shader each time.

starmole
  • 4,974
  • 1
  • 28
  • 48
1

Here's what I've done in the past

  • Draw the world/terrain (everything you want the silhouette to show through)
  • Disable depth test
  • disable draw to depth buffer
  • Draw sprites in silhouette mode (a different shader or texture)
  • enable depth test
  • enable draw to depth buffer
  • draw sprites in normal mode
  • draw anything else that should go on top (like the HUD)

Explanation: When you draw the first time (in silhouette mode) it will draw over everything, but not affect the depth buffer, so that when you draw the 2nd time you won't get z-fighting. When you draw the 2nd time, some of it will be behind the terrain, but where the silhouette has already been drawn.

Tom
  • 1,221
  • 10
  • 21