11

Please consider the following images for the illustration: enter image description here

Initially I fill the whole screen/stage with individual Images until the screen turns pink. Each blob of pink colour is an individual Image actor that I add to the stage.

Now I want to implement the touchDown method in such a way that each time the user touches the screen, it erases a part of that Image where the touch event took place. However, that touch event should not effect other Images/actors/TextureRegions that are behind or above the pink blob actors. How am I supposed to achieve this in libgdx using OpenGL ES? Please help me in this regard.

I found this link which explains how to modify a TextureRegion but I don't know how I am going to achieve solution for my problem using the technique explained in this blog. Here is the link

Rafay
  • 6,108
  • 11
  • 51
  • 71
  • On touch, do you want the complete image to be "erased"? Does erasing mean writing white values to the pixels, or making them transparent? You're using OpenGL ES 2? libgdx seems to support it. – Stefan Hanke May 04 '12 at 06:54
  • @StefanHanke No. I intend to erase only a part of the image where the touch event occurred. And I want to make them transparent rather than colouring them white so that the background becomes visible. – Rafay May 04 '12 at 09:43
  • It could be easier if you know which color is going to be erased now.. – Ron May 04 '12 at 14:32
  • @userSeven7s Okay if I do know what color it is to be erased, then how I am I supposed to do that? – Rafay May 05 '12 at 08:27

1 Answers1

5

Could you use FBO's and a stencil buffer?

Setup an FBO for your "pink" layer and a stencil buffer for it. On touch down, draw your touch as a mask to the pink FBO's stencil buffer. Now when you draw the pink FBO, the areas you touched wont be rendered so you'll be able to see the background FBO behind it.

This link http://www.opengl.org/archives/resources/faq/technical/rasterization.htm, section 14.050 tells you how to setup a stencil buffer:

You can set up OpenGL state as follows:

glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 0x1, 0x1);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);

Subsequent rendering will set a 1 bit in the stencil buffer for every pixel rendered.

You may have to fiddle with things so your masking comes out the right way (masks where you did touch, not where you didn't.)

Soup
  • 1,699
  • 15
  • 30
  • The idea of three stacked (XYZ) is exactly what I want. Now that you are discussing buffers here, would the operation be smooth enough to not to cause the rendering thread to create a noticeable jitter? Since I am relatively new to OpenGL ES, I have no idea of how buffers work so I would have to try things from a complete scratch. The thing is that I must be sure that this approach is the best for what I want to do here. As far as FBO's and stencil buffers are concerned, yes I can use them but I don't know how. Below are few references for FBO's and stencil buffer within the framework. – Rafay May 10 '12 at 15:39
  • http://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/graphics/glutils/FrameBuffer.html http://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/utils/BufferUtils.html http://www.badlogicgames.com/wordpress/?p=1923 I am already searching on how to do this using buffers as you have suggested. – Rafay May 10 '12 at 15:39
  • 1
    Now that I think about it a bit more, you don't really need three FBOs, just one with a stencil buffer to match. I have edited the question to be better. This answer has some stuff about FBO performance http://stackoverflow.com/a/2203931/959304 but I would just ignore performance until its working, and if performance gets bad, _then_ pay the extra code-complexity that will come when you want to optimize. – Soup May 10 '12 at 16:57
  • Okay so the whole idea is to set the buffers on the texture and modify individual pixels of those textures. Am I right?? – Rafay May 10 '12 at 17:02
  • 1
    Yeah I guess. A FBO can be considered a texture, its just easier and quicker to modify the contents of an FBO since you just use regular opengl drawing methods than to modify a bound texture. You would still be drawing your images (as textures) into the fbo, but when you draw out your FBO, you do that with the stencil buffer masking out certain areas of it. – Soup May 10 '12 at 17:13
  • I am thankful for your concern. Well I didn't try FBO yet...busy with my final exams. Will try soon and will let you know if I get it working. – Rafay May 19 '12 at 13:20
  • Your answer is completely to the point. Got into all these buffers and has so far managed to dynamically modify textures using FBO - and they are not effecting the fps at all!! :) However I am still not able to work out the Stencil Buffer part. I am not getting how to create a custom mask of a square shape.. I have rendered the required image in FBO and now want to apply the mask before rendering it to the screen. How would I do that?? I tried applying a for loop for the square with 0x0 so that things are not rendered where the stencil is 0x0 but that doesn't work. Any help? – Rafay Jun 14 '12 at 19:22
  • I'm not sure why you're using a for loop? These links might help you: http://www.opengl.org/wiki/Stencil_Mask http://en.wikibooks.org/wiki/OpenGL_Programming/Stencil_buffer (Note sample code here is backwards to what you want, draws to the screen where the mask is present, where as you want to NOT draw where the mask is present.) I think you might have missed a step where you're drawing your strokes to the FBO instead of the stencil mask. You want to draw to the stencil then apply that when you draw the FBO. I don't have time to make a proper example atm, sorry. Maybe one day... Iwouldliketo. – Soup Jun 26 '12 at 14:25