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

Author Topic: OpenGL - clipping with not rectangular shape  (Read 4191 times)

0 Members and 1 Guest are viewing this topic.

janszy

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
OpenGL - clipping with not rectangular shape
« on: October 05, 2013, 09:39:57 am »
Hello Everyone!

I've got a problem, and haven't found a solution yet. Assume that I have a puzzle shape bounded by some bezier curves, and I want to clip that shape from a texture. Can I do this with OpenGL? I've never used it before and don't know if it is capable of such thing. Or do I have to define my own test function, and check every pixel if it's inside/outside the shape, and copy it from the original texture depending on that? If yes, what algorithm do I use? Can you suggest me a solution or some tutorial that can help me? Thanks!

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: OpenGL - clipping with not rectangular shape
« Reply #1 on: October 05, 2013, 10:20:43 am »
I'm not quite sure what you want, and I'm no OpenGL guru, but it sounds like you could achieve that by drawing to a RenderTexture using a shader that sets pixels outside the curves to be transparent.  And that can be done entirely within SFML (except for writing the shader of course).

janszy

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: OpenGL - clipping with not rectangular shape
« Reply #2 on: October 05, 2013, 11:41:15 am »
Yes, you understood what I want, and I know thats the method for doing this (although I didn't know I can do this inside SFML). I just wondered OpenGL could do this for me automatically with a closed shape defined by its bounding curves, or do I have to find and implement an algorithm for that myself.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: OpenGL - clipping with not rectangular shape
« Reply #3 on: October 06, 2013, 10:47:03 am »
I think I might've stumbled across something in OpenGL that could be useful for you: Stencil Buffers

Note I only just learned about these myself (been reading about OpenGL a lot lately), but since no one else seems to be posting in this topic I figure a shot in the dark from me is better than nothing.

Kojay

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Re: OpenGL - clipping with not rectangular shape
« Reply #4 on: October 06, 2013, 11:15:39 am »
If there's a single color outside the curve and you do want to make that transparent, sf::Image::createMaskFromColor is sufficient.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: OpenGL - clipping with not rectangular shape
« Reply #5 on: October 06, 2013, 11:18:08 am »
Nobody probably posted anything because the initial idea janszy had is probably the most efficient one. Assuming that this entity keeps its shape for a decent amount of time, there is no reason to tax the graphics hardware more by using it to perform per-pixel tests let alone bind a shader and involve an additional rendering pass.

Shaders are executed inside compute units and that takes away from the time available for standard rendering. Buffer operations (stencil, accumulation, depth, color...) are even worse as they are less easily optimized by the hardware (imagine having to lock an entire buffer for a single pass while you iterate over every element) incurring pipeline stalls etc.

Simply generate the visible portion of the entity once (or every time the puzzle shape changes) by e.g. setting the alpha values of all invisible pixels to 0 and save that for drawing over and over.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

janszy

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: OpenGL - clipping with not rectangular shape
« Reply #6 on: October 06, 2013, 06:11:38 pm »
Thank you for the answers. Sorry for not being clear in the first place. So my original idea was, that I create a png image with the puzzle shape (I draw it with a graphical program), set the inside part to transparent and the outside part lets say to green. Then I mask this image with the original picture and set the green pixels to transparent.

But I'd like to add an extra functionality: The user can define a deformation rate, and so every piece would have a unique deformed shape. I don't know how to do this with the above method. So after some search I found that the common way to draw puzzle pieces is with Bezier curves. So I would give only the control points of the curve (and I slightly modify the control points to get a deformed shape), but this way I need to check every point if it's inside or outside the shape that the control points define. And therefore I need an algorithm to test for a point if it's outside or inside. (It would be enough, if I knew which side of a given curve is the point and test it for all of the four sides.) And I didn't know that I need to create such a test function myself (and if I need, how to do this) or OpenGL could handle this automatically for me.

By the way I need to create the puzzle pieces only once, then I'd store them (with their transparent bounding rectangle) on a bigger texture, and use that texture for drawing sprites.