opengl z-sorting transparency

前端 未结 3 1233
说谎
说谎 2020-12-11 13:00

im rendering png\'s on simple squares in opengl es 2.0, but when i try and draw something behind an square i have already drawn the transparent area in my top square are ren

相关标签:
3条回答
  • 2020-12-11 13:03

    Your title is essentially the answer to your question!

    Generally transparency is done by first rendering all opaque objects in the scene (letting the z-buffer figure out what's visible), then rendering all transparent objects from back to front.

    0 讨论(0)
  • 2020-12-11 13:14

    Drew Hall gave you a good answer but another option is to set glEnable(GL_ALPHA_TEST) with glAlphaFunc(GL_GREATER, 0.1f). This will prevent transparent pixels (in this case, ones with alpha < 0.1f) from being rendered at all. That way they do not write into the Z buffer and other things can "show through". However, this only works on fully transparent objects. It also has rough edges wherever the 0.1 alpha edge is and this can look bad for distant features where the pixels are large compared to the object.

    0 讨论(0)
  • 2020-12-11 13:18

    Figured it out. You can discard in the fragment shader

    mediump vec4 basecolor = texture2D(sTexture, TexCoord);
    
    if (basecolor.a == 0.0){
        discard;
    }
    
    gl_FragColor = basecolor;
    
    0 讨论(0)
提交回复
热议问题