Depth Test is not working properly in Opengl

Asked

Viewed 95 times

2

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

Render normal with Depth Test on and in GL_LEQUAL mode.

inserir a descrição da imagem aqui

Render with Depth Test off only for cube.

inserir a descrição da imagem aqui

The cube overlaps the green plane, but does not overlap the blue cylinder.

void HOpenGLForwardRenderer::RenderOpaqueMesh(HGameObject* gameObject, HMaterial* material, HShader::HRenderPass pass)
    {
        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 could I be doing wrong? I’m using Opengl 2.1 and three shaders one for each object but with the same code (only changes color).

    #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); 
    }

1 answer

2

If I’m reading your code correctly, it doesn’t seem to be controlling the order in which it renders the shapes. Without DEPTH_TEST, 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 be on top. If you want the cube on top, you need to make sure you rendered the last cube, after turning off DEPTH_TEST.

If it still doesn’t work, you can also try calling glClear( GL_DEPTH_BUFFER_BIT ) to throw away the depth dice of the Deep shapes. Even so, need to render the last cube to stay on top.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.