1
\$\begingroup\$

I was trying to make two object outlines using the stencil buffer. One would color the object with orange and other with green. I just wanna know if is it possible to have two object outlines at the same time, because every time I tried to draw the second object it always got affected by the stencil buffer used by the first object:

// the cubes
        glStencilFunc(GL_ALWAYS, 2, 0xFF);
        glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
        glStencilMask(0xFF);

        glClear(GL_STENCIL_BUFFER_BIT);

        glBindVertexArray(cubeVAO);
        glBindTexture(GL_TEXTURE_2D, cubeTexture);
        model = glm::translate(model, glm::vec3(-1.0f, 0.1f, -1.0f));
        glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
        glDrawArrays(GL_TRIANGLES, 0, 36);
        model = glm::mat4();
        model = glm::translate(model, glm::vec3(2.0f, 0.1f, 0.0f));
        glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
        glDrawArrays(GL_TRIANGLES, 0, 36);
        glBindVertexArray(0);



        // the cube outline
        glStencilFunc( GL_EQUAL, 2, 0xFF);
        glStencilMask(0x00);                    
        glDepthMask(GL_FALSE);
        shaderSingleColor.Use();

        GLfloat scale = 1.1;                    

        glBindVertexArray(cubeVAO);
        glBindTexture(GL_TEXTURE_2D, cubeTexture);
        model = glm::mat4();
        model = glm::translate(model, glm::vec3(-1.0f, 0.1f, -1.0f));
        model = glm::scale(model, glm::vec3(scale, scale, scale));
        glUniformMatrix4fv(glGetUniformLocation(shaderSingleColor.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
        glDrawArrays(GL_TRIANGLES, 0, 36);
        model = glm::mat4();
        model = glm::translate(model, glm::vec3(2.0f, 0.1f, 0.0f));
        model = glm::scale(model, glm::vec3(scale, scale, scale));
        glUniformMatrix4fv(glGetUniformLocation(shaderSingleColor.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
        glDrawArrays(GL_TRIANGLES, 0, 36);
        glDepthMask(GL_TRUE);
        glBindVertexArray(0);
        glStencilMask(0x00);


// the floor
        glStencilFunc(GL_ALWAYS, 1, 0xFF);
        glStencilMask(0xFF);
        glBindVertexArray(planeVAO);
        glBindTexture(GL_TEXTURE_2D, floorTexture);
        model = glm::mat4();
        glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
        glDrawArrays(GL_TRIANGLES, 0, 6);
        glBindVertexArray(0);
        glStencilMask(0x00);

I tried setting the stencil for the cube object to 2.

glStencilFunc(GL_ALWAYS, 2, 0xFF);

and then draw the should be cube outline

glStencilFunc( GL_EQUAL, 2, 0xFF);
            glStencilMask(0x00);    

of course what would happen here is that it will fill the whole cube with green color. I was expecting that. The problem is that even the floor is also filled with green color even though I disabled the stencil mask

glStencilMask(0x00);

and then set the stencilFunc to

glStencilFunc(GL_ALWAYS, 1, 0xFF);
        glStencilMask(0xFF);

This is the result

enter image description here

If I remove that stencil function and stencil mask before rendering code of the floor. It wont show up on the screen. This is what confuses me a lot. How is that happening? I disabled the stencil mask before rendering the should to be cube outline?

  1. The obvious solution here is to draw the floor first on the top code before doing some stencil. But what if I want two object outline with different shaders to use? Like in my case I want the cube to be green and the floor to be orange? In that case it doesnt matter now if I who object should be draw first and whos not as the first object stencil will affect the 2nd object.

  2. I am really confuse why is it affecting the drawing of the floor if I didnt put glStencilFunc on the code of floor?

  3. Why then is it affecting the floor even if I put GL_EQUAL = 2 on the cube outline? why does the floor has to be affected by it? I disable the stencil long before the code for the floor is executed?

\$\endgroup\$
  • 1
    \$\begingroup\$ Have you tried reading the manual? You appear to be completely guessing at what the stencil operations do... Ping me in the chatroom if you want me to answer, because we will need to have a discussion to set some things straight. \$\endgroup\$ – MickLH Nov 30 '15 at 14:42
  • \$\begingroup\$ @MickLH I havent visit the discussion room but and I have no clue how to ping you. I however created a new discussion room titled Stencil buffer \$\endgroup\$ – Tifa Nov 30 '15 at 14: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.