Search code examples
opengllwjglopengl-compat

glDepthTest troubles (alpha overlapping)


I'm trying to apply the solution described in this post (actually not the solution but the first answer): How to avoid transparency overlap using OpenGL? for the same problem.

I tried in a minimal project but i don't know why its not working, here is my render loop code:

private void render() {
    glClear(GL_DEPTH_BITS);
    glClear(GL_COLOR_BUFFER_BIT);

    glDepthFunc(GL_ALWAYS);
    glColorMask(false, false, false, false);
    renderBlocks(GL_QUADS);

    glDepthFunc(GL_LEQUAL);
    glColorMask(true, true, true, true);
    renderBlocks(GL_QUADS);
}

The render blocks function:

public void renderBlocks(int type) {
    glBegin(type);
    glColor4f(0.5f, 1f, 0f, 0.5f);
    glTexCoord2d(0, 0); glVertex2f(50, 50);
    glTexCoord2d(1, 0); glVertex2f(100, 50);
    glTexCoord2d(1, 1); glVertex2f(100, 100);
    glTexCoord2d(0, 1); glVertex2f(50, 100);
    glEnd();
    glBegin(type);
    glColor4f(0.5f, 1f, 0f, 0.5f);
    glTexCoord2d(0, 0); glVertex2f(75, 75);
    glTexCoord2d(1, 0); glVertex2f(150, 75);
    glTexCoord2d(1, 1); glVertex2f(150, 150);
    glTexCoord2d(0, 1); glVertex2f(75, 150);
    glEnd();
}

And my openGL initialisation:

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLU.gluOrtho2D(0, width, height, 0);
    glMatrixMode(GL_MODELVIEW);

    glClearColor(0, 0, 0, 0);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glClearColor (0.0f, 0.0f, 0.0f, 0.0f);

The result i'm getting: Squares are overlapping

The result i want: Squares (with alpha 0.5) not overlapping

Does someone have an idea of what i'm doing wrong ? Thanks !


Solution

  • Your solution won't work because all quads have the same depth (z=0.0) and the depth test function is GL_LEQUAL. It is clearly mentioned in the answer to the linked question that (How to avoid transparency overlap using OpenGL?):

    You are going to need to assign to each circle a different z value. [...].

    If the blend function is glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA), it works in the case of alpha = 0.5 because x * 0.5 + x * 0.5 = x.

    Render the blocks with different z coordinate to solve the issue:

    public void renderBlocks(int type) {
    
        float z = 0.0;
    
        glBegin(type);
        glColor4f(0.5f, 1f, 0f, 0.5f);
        glTexCoord2d(0, 0); glVertex3f(50, 50, z);
        glTexCoord2d(1, 0); glVertex3f(100, 50, z);
        glTexCoord2d(1, 1); glVertex3f(100, 100, z);
        glTexCoord2d(0, 1); glVertex3f(50, 100, z);
        glEnd();
    
        z -= 0.1f;
    
        glBegin(type);
        glColor4f(0.5f, 1f, 0f, 0.5f);
        glTexCoord2d(0, 0); glVertex3f(75, 75, z);
        glTexCoord2d(1, 0); glVertex3f(150, 75, z);
        glTexCoord2d(1, 1); glVertex3f(150, 150, z);
        glTexCoord2d(0, 1); glVertex3f(75, 150, z);
        glEnd();
    }