opengl-es - 使用模板缓冲区渲染时纹理中的 OpenGL 透明度

标签 opengl-es blending

由于评论,该问题已更新。

纹理如何重叠的屏幕截图
Screenshot of how textures overlap

要使用模板缓冲区用画笔纹理绘制 2 个点以避免纹理透明度重叠,请使用以下代码:

    glEnable(GL_STENCIL_TEST.gluint)

    glClear(GL_STENCIL_BUFFER_BIT.gluint | GL_DEPTH_BUFFER_BIT.gluint)

    glStencilOp(GL_KEEP.gluint, GL_KEEP.gluint, GL_REPLACE.gluint)

    glStencilFunc(GL_ALWAYS.gluint, 1, 1)
    glStencilMask(1)

    glDrawArrays(GL_POINTS.gluint, 0, 1)

    glStencilFunc(GL_NOTEQUAL.gluint, 1, 1)
    glStencilMask(1)

    glDrawArrays(GL_POINTS.gluint, 1, 1)

    glDisable(GL_STENCIL_TEST.gluint)

模板缓冲区可以工作,但是每个点都会填充模板缓冲区中的一个完整矩形,但纹理图像具有透明度。那么纹理的使用方式可能是错误的吗?

纹理是这样加载的

    glGenTextures(1, &gl_id)
    glBindTexture(GL_TEXTURE_2D.gluint, gl_id)
    glTexParameteri(GL_TEXTURE_2D.gluint, GL_TEXTURE_MIN_FILTER.gluint, GL_LINEAR)
    glTexImage2D(GL_TEXTURE_2D.gluint, 0, GL_RGBA, gl_width.int32, gl_height.int32, 0, GL_RGBA.gluint, GL_UNSIGNED_BYTE.gluint, gl_data)

混合设置为

    glEnable(GL_BLEND.gluint)
    glBlendFunc(GL_ONE.gluint, GL_ONE_MINUS_SRC_ALPHA.gluint)

您能否建议在哪里寻找以便通过笔刷图像的不透明区域在模板缓冲区中填充 1 秒?

最佳答案

我建议在片段着色器中丢弃纹理的透明部分。可以通过 discard 关键字在片段着色器中完全跳过片段。
请参阅Fragment Shader - Special operations .

如果纹理颜色的 Alpha channel 低于阈值,则使用较小的阈值并丢弃片段:

vec4  texture_color = .....;
float threshold = 0.01;
if ( texture_color.a < threshold )
    discard; 

另一种可能性是使用 Alpha test 。这仅在 OpenGL 兼容性配置文件中可用,但在核心配置文件或 OpenGL ES 中不可用。

参见Khronos OpenGL-Refpages glAlphaFunc :

The alpha test discards fragments depending on the outcome of a comparison between an incoming fragment's alpha value and a constant reference value.

通过以下 alpha 测试,alpha channel 低于阈值的片段将被丢弃:

float threshold = 0.01;
glAlphaFunc(GL_GEQUAL, threshold);
glEnable(GL_ALPHA_TEST)

关于opengl-es - 使用模板缓冲区渲染时纹理中的 OpenGL 透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51154213/

相关文章:

android - 为什么opengl shader mix函数让两张透明png图片变黑了?

c++ - OpenGL ES 是否适合执行骨骼动画?

opengl-es - OpenGL : Single vertex attribute for multiple vertices?

OpenGL:透明纹理问题

ios - 叠加混合模式公式?

filter - OpenAL同步

android - 我的顶点只显示一个三角形?

android - 华为8.0 Renderscript加载RS驱动失败

java - Libgdx - 仅动态着色 Sprite 的一部分