OpenGl clipping with potentially infinite hierarchy

68 Views Asked by At

I'm interested in rendering some object hierarchy (potentially infinite in depth) using OpenGl. I've already read many answers to this problem and almost all of them recommend stencil buffer.

This answer (https://stackoverflow.com/a/56637285/11754223) describes the method in detail and it looks like I'm not even forced to render objects layer by layer (I can do it depth-first), but as it states, the number of layers is limited to 256 in most cases.

int layer = 0;
glStencilFunc(GL_ALWAYS, 1, 0xff);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);

// draw initial layer
// ...

layer ++;
glStencilFunc(GL_EQUAL, layer, 0xff);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);

// draw layer 1
// ...

layer ++;
glStencilFunc(GL_EQUAL, layer, 0xff);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);

// draw layer 2

My question is how I can extend the method for an arbitrary number of layers.

The method I've thought about:

Reset all values of stencil buffer, that are not drawn.

This seems to be impossible because I can influence only drawn fragments and I cannot clear the whole buffer before drawing, because I need stencil buffer for clipping from the previous layer.

Are there any methods, that allow clipping bigger hierarchies except of using another buffers like depth buffer?

0

There are 0 best solutions below