mllobera said:
So… the way I am thinking about it is that the StencilOp(GL_KEEP, GL_REPLACE, GL_KEEP)will only kick in after step 7?
No. Each time fragments that succeed the stencil test (you set it to always, so all fragments), GL will compare them to the depth buffer value at their coordinates. And each time these fragment depth value will fail the depth depth (basically being greater or equal), the stencil buffer will be updated, and a 1 will be set at their location.
Depending on the order you draw your cubes or send the vertices (and faces) of your cubes, results can be different from what you think. The sides of your first cube might be seen as front winded faces. And if you render them after the close face of your cube, they will fill the stencil buffer. This seems to be what's happening.
Since you render the closer cube after the far one, then nothing happens in the stencil operation for the seconde cube: all fragments succeed the depth buffer.
I think that if you move to a perspective instead of an orthographic projection, you'll have different values. My guess is that the stencil buffer will mostly remain empty (full of 0).