• Advertisement
Sign in to follow this  

Is there a way to specifically set stencil on specific object?

This topic is 872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wanted to do some object outlining on a specific object. however all objects of the scene got affected as well.

 

stencil.jpg

 

This is the code

glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

the game loop

glStencilFunc(GL_ALWAYS, 1, 0xFF);         
        glStencilMask(0xFF);
        // Floor
        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);


        GLfloat scale = 1.1;
         
        // draw the outline but discard the original size
        glStencilFunc(GL_NOTEQUAL, 1, 0xFF);    
        glStencilMask(0x00);            // stop writing to the buffer            
        glDisable(GL_DEPTH_TEST);                
        myTestShader.Use();                

        glBindVertexArray(planeVAO);
        glBindTexture(GL_TEXTURE_2D, floorTexture);
        model = glm::mat4();
        model = glm::scale(model, glm::vec3(scale, scale, scale));
        glUniformMatrix4fv(glGetUniformLocation(myTestShader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
        glDrawArrays(GL_TRIANGLES, 0, 6);
        glBindVertexArray(0);
        
        
        glStencilMask(0xFF);
        glEnable(GL_DEPTH_TEST);

        glClear(GL_STENCIL_BUFFER_BIT); // -- didnt work

        glStencilFunc(GL_ALWAYS, 0, 0xFF);         // -- didnt work
        glStencilMask(0x00);



        // Cubes
        glBindVertexArray(cubeVAO);
        glBindTexture(GL_TEXTURE_2D, cubeTexture);
        model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -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.0f, 0.0f));
        glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
        glDrawArrays(GL_TRIANGLES, 0, 36);
        glBindVertexArray(0);

The two cubes seems to also be affected by the stencil buffer. Which is weird because I tried clearing the buffer and/or setting the buffer to 0's before rendering the cubes. If I were to visualize whats happening. First I set 1s to the stencil buffer wherever the floors fragment is rendered. Then draw the scaled up version of floor but with different shader in this case its orange. Then clear the stencill buffer or set the buffer to 0 to disable it. Then render the cube. But for some reason the cube is being affected by stencill buffer. What is it that I am missing?

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement