glPoint Alpha混合问题 [英] glPoint Alpha Blending issue

查看:90
本文介绍了glPoint Alpha混合问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Alpha混合glPoints的问题.当glPoint位于背景上方时,alpha混合正常工作,但是当glPoint与另一个glPoint重叠时,背景颜色可见,而不是基础glPoint.

I am having issues alpha blending glPoints. The alpha blending works correctly when the glPoint is over the background but when a glPoint overlaps another glPoint the background color is visible rather then the underlying glPoint.

// create texture

    GLuint tex;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);

    // Draw

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glBindTexture(GL_TEXTURE_2D, tex);
    ....
    glDrawArrays(GL_POINTS, 0, numberOfPoints);

    // Frag Shader

    uniform sampler2D Texture; 

    void main(void) 
    {
        gl_FragColor = texture2D(Texture, gl_PointCoord); 
    }

我在做什么错了?

推荐答案

似乎深度测试正在引起您的问题.正如您所描述的,首先绘制了一个前面的点,然后替换了深度值.然后将后面的点栅格化,但是所有片段都无法通过深度测试,因此不再渲染/混合.

It looks like the depth test is causing your issues. What happens is, as you describe, a point in front is drawn first and the depth value is replaced. Then the point behind is rasterized but all the fragments fail the depth test so nothing more is rendered/blended.

  1. 正如Andon M. Coleman所指出的那样,您确实需要按深度顺序对片段进行排序,以进行正确的alpha混合(对于粒子而言,精确顺序无关的透明性目前尚不可行,尽管您可以尝试一些近似技术.所有使用alpha值作为权重的颜色也可以得到不错的效果.

  1. As Andon M. Coleman pointed out, you really need to sort the fragments in order of depth for correct alpha blending (exact order independent transparency is currently impractical for particles although you could try some of the approximate techniques. averaging all colour using alpha values as a weight can give decent results too).

特别是对于您拥有的粒子密度以及粒子之间缺乏变异的情况,排序与不排序之间可能不会有太大差异.在这种情况下,请确保先启用所有深度测试,然后再绘制所有不透明的东西,然后再绘制粒子.您想保持深度测试处于启用状态,以便当它们位于不透明物体后面时,不会绘制它们的粒子,但是您不希望它们彼此遮挡-您想使用深度缓冲区进行测试,但不要写入深度值.为此,请使用 glDepthMask

Especially for the particle density you have and the lack of variation among particles there probably won't be much difference between sorting and not sorting. In this case, make sure you draw all the opaque stuff first, with depth testing enabled and then draw the particles. You want to keep depth testing enabled so your particles aren't drawn when they're behind opaque things however you don't want them to obscure each other - you want to use the depth buffer for testing but not write depth values. For this, use glDepthMask

这篇关于glPoint Alpha混合问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆