SFML community forums
Help => Graphics => Topic started by: janszy 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!
-
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).
-
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.
-
I think I might've stumbled across something in OpenGL that could be useful for you: Stencil Buffers (http://en.wikibooks.org/wiki/OpenGL_Programming/Stencil_buffer)
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.
-
If there's a single color outside the curve and you do want to make that transparent, sf::Image::createMaskFromColor is sufficient.
-
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.
-
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.