I'm unable to properly blend z-ordered textures

Started by noncompetitive_antagonist, November 16, 2020, 00:02:59

Previous topic - Next topic

noncompetitive_antagonist

Hi there,

I am not able to implement transparency blending properly when I have one object being drawn *after* another object, and *beneath* that other object. See here:
https://imgur.com/ccmg0IT
In this picture, the upper-left image is being rendered first, and the lower-right image is being rendered second. The upper-left image has a lower z index, so it is higher than the lower-right image; this is correct. However, its transparency "overwrites" the non-transparency of the lower-right image beneath it. I am currently using the following code for my blending function and equation:

        glEnable(GL_BLEND);
        glEnable(GL_DEPTH_TEST);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glBlendEquation(GL_FUNC_ADD);


Am I missing something obvious? (I've double-checked that I'm not setting a different blend function elsewhere, for instance.)

Thanks!

KaiHH

Depth testing happens before alpha blending, so all fragments of the later drawn object behind the front object are discarded.
When you want to draw transparent objects, you must sort/draw them from back/furthest to front/nearest.

noncompetitive_antagonist

 :) Thank you. I suppose I'll have to render by z-index; hopefully, switching between 2d sprite textures won't be too performance intensive.

KaiHH

You could always use texture atlases (pack multiple images into the same texture and assign texture coordinates accordingly) or use array textures, where each layer is a different image and index into the corresponding layer in the fragment shader.