Depth Test is not working correctly in OpenGL

2

I'm having a problem with Depth Test . I have a scene where it contains three objects. A plan a cube and a cylinder. In my Render I'm doing for when rendering the cube it should disable the Depth Test. With this the cube (in orange) should overlap the plane (green) and the cylinder (blue). But that does not happen.

Normal render with Depth Test on and GL_LEQUAL mode.

RenderwithDepthTestturnedoffonlyforthecube.

Thecubeisoverlappingthegreenplane,butdoesnotoverlapthebluecylinder.

voidHOpenGLForwardRenderer::RenderOpaqueMesh(HGameObject*gameObject,HMaterial*material,HShader::HRenderPasspass){gEngine=HEngine::GetInstance();glEnable(GL_CULL_FACE);glCullFace(GL_BACK);glDepthMask(true);if(gameObject->mName=="Cube.1") {
            glDisable(GL_DEPTH_TEST);
        }
        else {
            glEnable(GL_DEPTH_TEST);
        }

        glDepthFunc(GL_LEQUAL);

        glUseProgram(pass.gpuProgram);....

What can I be doing wrong? I'm using OpenGL 2.1 and three shaders one for each object but with the same code (color only changes).

    #version 120

    uniform mat4 HEngine_MatrixMVP;

    attribute vec3 in_Position;

    void main()
    {
        gl_Position = HEngine_MatrixMVP * vec4(in_Position, 1.0);
    }

    #version 120

    void main()
    {
        gl_FragColor = vec4(0.8, 0.4, 0, 1); 
    }
    
asked by anonymous 04.02.2018 / 18:43

1 answer

2

If I'm reading your code correctly, it looks like it's not controlling the order in which you render the shapes. Without DEPTH_TEST, the render order matters. The newest form will be on top.

If the cylinder is rendered after the cube and the cube has been rendered with DEPTH_TEST off, the cylinder MUST stay on top. If you want the cube on top, you need to make sure you have rendered the last cube, after turning off DEPTH_TEST.

If it still does not work, you can also try to call glClear( GL_DEPTH_BUFFER_BIT ) to throw away the depth data in other ways. Even so, you need to render the last cube to stay on top.

    
08.02.2018 / 17:48