LWJGL Forum

Programming => OpenGL => Topic started by: noncompetitive_antagonist on November 16, 2020, 00:02:59

Title: I'm unable to properly blend z-ordered textures
Post by: noncompetitive_antagonist on November 16, 2020, 00:02:59
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:

Code: [Select]
        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!
Title: Re: I'm unable to properly blend z-ordered textures
Post by: KaiHH on November 16, 2020, 10:41:49
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.
Title: Re: I'm unable to properly blend z-ordered textures
Post by: noncompetitive_antagonist on November 19, 2020, 02:08:28
 :) Thank you. I suppose I'll have to render by z-index; hopefully, switching between 2d sprite textures won't be too performance intensive.
Title: Re: I'm unable to properly blend z-ordered textures
Post by: KaiHH on November 19, 2020, 08:04:00
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.