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

Author Topic: How to draw masked / stenciled graphics?  (Read 2810 times)

0 Members and 1 Guest are viewing this topic.

raycrasher

  • Newbie
  • *
  • Posts: 24
    • View Profile
How to draw masked / stenciled graphics?
« on: June 20, 2013, 05:31:28 am »
In my project I have to draw damage sprites (hull breaches, smoldering twisted metal, etc.) over ships depending on where they are hit. The problem is, I want those damage sprites to be drawn only over non-transparent parts of the ship, because drawing a burning piece of bulkhead over empty space looks plain weird.  So I'm asking for help on how should I do this.

Stencil masks? Render to texture? Shaders?

Currently, I'm thinking along the lines of the following:

1. Draw hull(s) normally, but also draw to stencil all pixels which has alpha==255.
2. Draw damage sprites, using stencil mask.
3. Reset stencil so that subsequent draws will not be affected.

But how, OpenGL and code wise? Many thanks in advance!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to draw masked / stenciled graphics?
« Reply #1 on: June 20, 2013, 07:53:36 am »
If you draw with OpenGL, the stencil buffer will be the best solution.
If you draw with SFML, you won't be able to mix OpenGL stencil with SFML sprites (it may work, but it's unreliable).
Laurent Gomila - SFML developer

raycrasher

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: How to draw masked / stenciled graphics?
« Reply #2 on: July 08, 2013, 10:21:05 am »
You're right, getting stencil to work properly with SFML is hard. I ended up using alternate sprites, which is way easier and better looking.

I used this as reference:
http://en.sfml-dev.org/forums/index.php?topic=3108.0

It didn't work for me, since I was trying to implement it in C# :D But it would be a nice add-on to SFML if it did work.

Anyway, thanks!