Using a Stencil Buffer to render the interaction between two objects


I am trying to implement a Stencil Buffer with jMonkeyEngine and can’t find much in the way of documentation or any examples where someone has actually used one. I’ve searched through the javadoc but can’t figure out how to actually implement it. Any suggestions or guidance would be greatly appreciated.

If it helps, this is what I’m trying to do:

I am interested in rendering the intersection between two objects. I have defined two objects, which I have called blobs.

  1. A data blob (scene graph)
  2. A filter blob (stencil)

    I only want to render the pixels within the overlap between these two blobs. That is, I only want render the pixels of the data blob where it intersects with the filter blob. To make things a little simpler I am starting off by limiting the filter blob to a plane that is defined by a value on the z axis. In my simplified model the data blob is sphere, so that the intersection of the two blobs would result in the outline of a circle.

    If I can get this working I hope to use a Mesh for the stencil (filter blob). One concern is that the Mesh that defines the stencil will be dynamic and needs to update in real time (i.e. in every frame). Could this be a problem? .




jME isn’t really made for just drawing with opengl, maybe you should use Slick2D or OpenGL directly. In jme I’d go and compute the areas totally off-screen and then just display the overlaps etc. as separately created quads etc.

Can you describe in more detail what you are trying to accomplish?

I do not think stencil buffers are necessarily the best approach in this case but I can’t be sure.

Thanks for your responses,

I have a 3d model of a an object, say a house and I want to move a plane through it and display the 2d cross section of the house. In some cases it would not be a simple plane, but a curve. Does this help?




Sounds like a case for shaders for me somehow. At least for a plane this should be fairly simple

→ calculate world position ov fragment, discard or render after a plane intersection calculation.

For abitrary meshes this would be a bit more difficult.

Cool, thanks. I’ll have a crack at implementing it with shaders.