# How do I keep stencil object outlines from affecting each other?

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

glClear(GL_STENCIL_BUFFER_BIT);

glBindVertexArray(cubeVAO);
glBindTexture(GL_TEXTURE_2D, cubeTexture);
model = glm::translate(model, glm::vec3(-1.0f, 0.1f, -1.0f));
glDrawArrays(GL_TRIANGLES, 0, 36);
model = glm::mat4();
model = glm::translate(model, glm::vec3(2.0f, 0.1f, 0.0f));
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);

// the cube outline
glStencilFunc( GL_EQUAL, 2, 0xFF);

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));
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));
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);

// the floor
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glBindVertexArray(planeVAO);
glBindTexture(GL_TEXTURE_2D, floorTexture);
model = glm::mat4();
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);


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


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


This is the result

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?

• 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. – MickLH Nov 30 '15 at 14:42
• @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 – Tifa Nov 30 '15 at 14:47