0
$\begingroup$

I'm trying to draw onlines around the objects, the outlines for any object should be see though very other object, I've successfully been able to draw the outline but the pixels get discarded in the depth test. I've attached my code and issue, I do not know how to resolve it. There are 2 cubes in the scene and I want both of their outlines to be seen through everything else in the scene.

Here is the issue, looks like the pixels are being discarded during the depth test while drawing with outline shader

enter image description here

        glEnable(GL_DEPTH_TEST);
        glEnable(GL_STENCIL_TEST);
        glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE);
        
        glStencilMask(0xFF);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        glClearColor(window.backgroundColor.x, window.backgroundColor.y, window.backgroundColor.z, window.backgroundColor.w);


        // Draw the cubes while recording to the stencil buffer
        glStencilMask(0xFF); // Enable writing to the stencil buffer
        glStencilFunc(GL_ALWAYS, 1, 0xFF); // All fragments pass the stencil test with reference value 1
        glDepthMask(GL_TRUE);
        glDepthFunc(GL_LESS);
        //Enable or disable depth test visualisation
        meshShader.setUniform1i("enableDepthColors", enableDepthColors);
        cubePosition.scale = glm::vec3(1.0f);
        renderer.draw_raw_arrays_with_texture(cubePosition, meshShader, marble, cube.vao, 36);

        glStencilMask(0x00); // Disable writing to the stencil buffer
        glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
        glDepthMask(GL_FALSE);
        glDepthFunc(GL_ALWAYS);
        cubePosition.scale = glm::vec3(1.02f);
        cubePosition2.scale = glm::vec3(1.02f);
        renderer.draw_raw_arrays(cubePosition, outlineShader, cube.vao, 36);

        glClear(GL_STENCIL_BUFFER_BIT);

        glStencilFunc(GL_ALWAYS, 1, 0xFF);
        glStencilMask(0xFF);
        glDepthMask(GL_TRUE);
        glDepthFunc(GL_LESS);
        cubePosition2.scale = glm::vec3(1.0f);
        renderer.draw_raw_arrays_with_texture(cubePosition2, meshShader, marble, cube.vao, 36);

        glStencilMask(0x00);
        glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
        glDepthMask(GL_FALSE);
        glDepthFunc(GL_ALWAYS);
        cubePosition2.scale = glm::vec3(1.02f);
        renderer.draw_raw_arrays(cubePosition2, outlineShader, cube.vao, 36);

$\endgroup$
1
  • $\begingroup$ When drawing objects that " should be see though very other object". Draw them last, and disable the depth buffer. Another "trick" is just to leave depth buffering on and set the depth value to depth min. $\endgroup$ – pmw1234 Jun 7 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.