My ultimate aim is to determine a sequence of (stencil) operations so that regardless of the order in which objects are rendered I can end up with the visible (non-occluded) part of any object. To this end, I have written some code where I am displaying two cubes one in front of the other. The smaller one is blocking partially the larger one. What I want to retrieve is the part of the larger cube that is visible.
I have cleared the stencil buffer (I am assuming that the default value is 1)
The section where I am using the stencil is shown here,
glStencilMask(0xFF) # write to the entire stencil buffer
glStencilFunc(GL_ALWAYS, 1, 0xFF) # All fragments pass stencil test
glStencilOp(GL_KEEP, GL_REPLACE, GL_KEEP) # only keep those fragments that pass stencil but fail depth
# cube 1. (large cube to be render to the stencil)
glBindVertexArray(cubeVAO)
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, cubeTexture)
model = glm.mat4(1.0)
model = glm.translate(model, glm.vec3(0.0, 0.0, -0.75))
shader.setMat4("model", model)
glDrawArrays(GL_TRIANGLES, 0, 36)
# Stop rendering to the stencil
glStencilMask(0x00)
# cube 2 (small cube)
scale = 0.35
model = glm.mat4(1.0)
model = glm.scale(model, glm.vec3(scale, scale, scale))
model = glm.translate(model, glm.vec3(0.0, 0, 2.0))
shader.setMat4("model", model)
glDrawArrays(GL_TRIANGLES, 0, 36)
glBindVertexArray(0)
Here are two renderings. The one on the left shows distance to the two cubes (and a floor). The one to the right shows what is the result of the stencil operations above.
The small cube that we can see on the left is clearly in front of the large cube yet the output I get on the right does not seem to correspond at all with what I would expect based on the left image.
I am using pyOpengl and I am reading the results from the stencil buffer into a numpy array (for those that are familiar with these). I use the following code to do this (I am including it here in case I am inadvertendly changing something)
stencil = glReadPixels(0,0, SCR_WIDTH, SCR_HEIGHT, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE)
stencil = np.frombuffer(stencil, dtype=np.uint8).reshape((800,-1))
There is definitely a disconnect between what I am rendering and the result I am getting from the stencil buffer. I changed the size of the cube (much smaller) that is blocking the larger cube and I get exactly the exact same result from the stencil buffer.
I am trying to figure out what I am doing wrong?