🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Stencil buffer strange result

Started by
15 comments, last by mllobera 3 weeks, 2 days ago

mllobera said:
I also have to check to see how this would extend if there were several objects that I was interested in.

Just give different values to each in StencilFunc.

mllobera said:
I am still uncertain what was wrong with my previous thinking

From your first post, you were setting a value to the stencil buffer each time the fragment fails the depth test. I'm not sure if that's what you was wanting to do. Plus, since you was doing this in a single pass, stencil buffer was filled with 1 for both failed depth-test pixels from both objects.

Advertisement

Thanks @_Silence_ !

From your first post, you were setting a value to the stencil buffer each time the fragment fails the depth test. I'm not sure if that's what you was wanting to do. Plus, since you was doing this in a single pass, stencil buffer was filled with 1 for both failed depth-test pixels from both objects.

Hmm… I need to think about this. From your suggestion it is clear that it works because whatever I draw will fail due to the depth test not passing. In my case, I was relying on the part that had to do with the stencil test passing and the depth test not passing. I guess I do not understand why this was not happening. I get a sneaky feeling that what I am not understanding is at what point the stencil test kicks in. I also cannot understand the original result I get given my original stencil test set up. What I managed to understand is what is the result I get from that original test, What gets recorded in the stencil buffer, are the sides of the cube that are not facing the camera (i.e. that is what is creating that square). Again, but I still do not understand why?

In any case thanks for the help!!!

You just got the pixels that did not succeed the depth test, that is, the ones that are not visible, the ones that are hidden by previous treated pixels (ie pixels lying on the visible sides of the cube). This depends a lot on the order you sent the vertices.

Face culling happens before stencil, so you should not have such pixels at all.

@_Silence_ Continuing our conversation (just trying to figure this out). This is what I had originally,

  1. Clear color+depth+stencil
  2. Set stencil mask to 255
  3. Set the stencil test to ALWAYS pass, use as reference value 1 and function mask 255
  4. Set the stencil operation to replace the stencil buffer value with the reference value whenever it passes the stencil test but fails the depth test
  5. Draw a large cube (or following my example a house)
  6. Set the stencil mask to 0 (so nothing passes the stencil test)
  7. Draw a small cube in front of the large cube (or draw the tree), that is closer to the viewer.

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?

Also,… if I wanted to record in the stencil buffer the visible parts of more than one object. How would one solve for the possible occlusion amongst these? (Hope this make sense).

Thanks again for everything.

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

Hi @_Silence_,

Okay, this is more complicated than I first imagined. I think that the main mistake I was making was thinking in terms of entire 3D objects when in reality I should be thinking in terms of primitives.

Might be worth restating what I would like to achieve and see whether this is possible at all. I am essentially looking at the building a terrain viewer on which the user can put 3D objects (there is no way of knowing what number of objects or in which order these are added to a scene). Imagine being able to wonder around this terrain. I would like the viewer to be able to select any object/s (better still parts of object/s if this was possible) and determine how much these parts are occluded by the terrain and/or other objects. My hope is to come up with a sequence of commands will achieve this.

Thanks again for your explanation, at least, now I understand better the results I was getting.

Advertisement