0

I'm writing a 2D UI library with OpenGL. Now, I want to make parent view clips it's children by "Stencil Test", I need test it's parent layer value, and write the layer value belong it self for next test.

For example test 0, and write 1. test 0, write 2.

Is there any way to do that? Or I can't use "Stencil Test" to solve my problem? If I can't use this, Any one have a good idea to solve my problem? Thanks a lot.

How can I write a different value to "Stencil Buffer"?

Addition: Like the picture below, I draw the three rect with three step. I first draw the red rect with stencil test 0 less or equal to the value in stencil buffer(After now,I will only show the value) and write 0 value. Second, draw the green rect with stencil test 0 and write 1. Third, draw blue rect with stencil test 0 and write 2.

pic

wudijimao
  • 53
  • 6
  • Do you want to increment the stencil value in a layer is drawn on a pixel? Then look at `glStencilOp` and the `GL_INCR` flag. Otherwise, please clarify for me what you mean by "I need test it's parent layer value, and write the layer value belong it self for next test.". – Peter Merkert Oct 31 '16 at 10:17
  • Thank you for your fast answer! I have tried to use GL_INCR and meet a issue. I have update my question for more detail. – wudijimao Oct 31 '16 at 10:58
  • Well, `GL_INCR` will start to write 1 for your first rect (I guess) and then 2, 3 and so forth. But besides that it should do exactly what you want. So, with `GL_INCR` you should get that the red color rectangle will be 1, green color than 2 and blue color than 3 in the stencil buffer. – Peter Merkert Oct 31 '16 at 14:39
  • I think by GL_INCR the blue color will be 3 only on green color part. it will be 2 on the red color part. Am I right? But I need all of the blue shadow part set to 3. – wudijimao Oct 31 '16 at 14:49
  • Hm, could happen, depending how you setup the stencil test. If you let the stencil test pass when >=1, after rendering the red quad, then you would render the whole blue quad on top of the red one. In these cases you might want to run [renderdoc](https://github.com/baldurk/renderdoc) to see the stencil result. – Peter Merkert Oct 31 '16 at 20:25
  • I don't think so. I already let the stencil test pass when >=1. red part is 1 so it increase to 2, by I want set it to 3. After more explore on the internet, I found stencil buffer can be change in fragment shader by ARB_shader_stencil_export feature. But AMD and N card's hardware only support – wudijimao Nov 01 '16 at 05:40
  • But AMD and N card's hardware only support 8 bit stencil, so it has less expansibility. So may be I have to find another way, for example: record stencil value in a texture. I don't no... – wudijimao Nov 01 '16 at 05:47

0 Answers0