How to create Stencil buffer with texture (Image) in OpenGL-ES 2.0

后端 未结 2 2020
轮回少年
轮回少年 2021-01-05 02:59

Can I have Stencil prepared with a texture (Image) in OpenGL 2.0 So that some part of the Image will be transparent and as a result it will be transfered as is
to Sten

相关标签:
2条回答
  • 2021-01-05 03:43

    Finally I have managed to do it using conditional passing of colors using discard keyword in shader. I also used 2 textures in same shader and combined them accordingly.

    Thanks all.

    0 讨论(0)
  • 2021-01-05 03:47

    Yes, you can use alpha testing (glEnable(GL_ALPHA_TEST); glAlphaFunc(COMPARISION, VALUE);) to discard fragments; discarded fragments will not be stencil tested, so you can use the passing fragments to build the stencil mask.

    EDIT code example

    /* the order in which orthogonal OpenGL state is set doesn't matter */
    glEnable(GL_STENCIL_TEST);
    glEnable(GL_ALPHA_TEST);
    
    /* don't write to color or depth buffer */
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    glDepshMask(GL_FALSE);
    
    glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
    
    /* first set alpha test so that fragments greater than the threshold
       will pass thus will set nonzero bits masked by 1 in stencil */
    glStencilFunc(GL_ALWAYS, 1, 1);
    glAlphaFunc(GL_GREATER, threshold);
    draw_textured_primitive();
    
    
    /* second pass of the fragments less or equal than the threshold
       will pass thus will set zero bits masked by 1 in stencil */
    glStencilFunc(GL_ALWAYS, 0, 1);
    glAlphaFunc(GL_LEQUAL, threshold);
    draw_textured_primitive();
    

    Don't forget to revert OpenGL state for drawing afterwards.

    EDIT to account for updated question:

    Since you're actually using iOS, thus OpenGL-ES 2.0 things get a bit different. There's no alpha test in OpenGL-ES2. Instead you're expected to discard the fragment in the fragment shader, if it's below/above a chosen threshold, by using the discard keyword/statement.

    0 讨论(0)
提交回复
热议问题