Question

I've been trying to render some textures as part of a HUD in front of a 3D world. I've set up methods so that I can easily change between 2D and 3D (setPerspective() and setOrtho()). Most textures work fine and show up just how I want them, however I've been having some difficulty with textures containing transparency. The transparent parts of the texture don't turn out transparent, and instead are the same colour as the background.

I read somewhere that I have to render the background first, however when I do this, for some reason the HUD doesn't seem to show up at all.

public static void render() {

    glClear(GL_COLOR_BUFFER_BIT | GL_BIT_BUFFER_BIT);
    glLoadIdentity();

    glClearColor(0.75f, 1, 1, 0);

    setOrtho();
    //2D Here

    setPerspective();
    //3D Here

}

And yes, I do have this in my code;

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
Was it helpful?

Solution

That blend mode is order-dependent.

You are blending based on values already in the color buffer and probably writing to the depth buffer at the same time. The depth buffer is not used to re-order your polygons, just to test whether the fragments they create should be visible or not. Since depth testing results in a binary decision (accept/reject on the basis of what has already been drawn), this really makes life difficult when it comes time to blend translucent polygons.

If you do not sort your translucent polygons from back-to-front and draw all opaque polygons first, then your translucent polygons can wind up occluding things instead of properly blending.

This article on Transparency Sorting might explain what I just said a little bit more clearly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top