OpenGL, offscreen hard shadows calculation with stencil

I am trying to figure it out if and how would be possible to calculate hard shadows from multiple light-sources off-screen.

preamble: scene is very complex, up to several million of triangles, up to hundred different light-sources, lights, material and depth enabled and maximum precision (at least 10kx10k pixel ground resolution). Shadows are nothing else than planar projected shadow, that is I am rendering the objects themselves by pre-multiplying by a shadow matrix (given a light-source position and a plane). Multiple objects, multiple light-sources but just a single plane.

For this reason real-time is impossible. Also impossible is to think about an incremental stencil value, since I have only one byte (255 useful values) and I may have more than 255 light-sources.

Therefore my idea was to render off-screen (to texture or render-buffer image).

For each light source, I calculate my shadow matrix and apply it. Then I render my first object with a specific stencil number (i.e. 1). Then I render my second object. I need to render it only where the stencil number is 1 and I need to zero all the other pixels, because I am interested in the hard shadow only. Then the third object and so on. At the end I repeat all the process for the second light source and so on.

Is it possible? Better texture or render-buffer image? Once hard shadow is calculated I need to retrieve it back on the cpu to analyze it and render it also on the ground in my scene.

Ideally I would need only the stencil buffer attached to my FBO but they say here https://www.opengl.org/wiki/Framebuffer_Object_Examples:

Stencil NEVER EVER MAKE A STENCIL buffer. All GPUs and all drivers do not support an independent stencil buffer. If you need a stencil buffer, then you need to make a Depth=24, Stencil=8 buffer, also called D24S8. Please search for the example about GL_EXT_packed_depth_stencil on this age.

Is this still valid today?

Looking around, I found here http://stackoverflow.com/questions/7570215/opengl-es-stencil-operations a way of zeroing all the other pixels by render a full screen polygon between each object, but I wonder if there is anything more efficient…?

Once hard shadow is calculated I need to retrieve

A texture buffer will be easier to work with

All GPUs and all drivers do not support an independent stencil buffer

In general this is true but if you only need to run the application on 1 machines you can code accordingly.

Unfortunately this is not the case… However for my purpose could I have a texture with just stencil and color attachment?

Wait… OpenGL Frame Buffer Object (FBO)

Renderbuffer ObjectIn addition, renderbuffer object is newly introduced for offscreen rendering. It allows to render a scene directly to a renderbuffer object, instead of rendering to a texture object. Renderbuffer is simply a data storage object containing a single image of a renderable internal format. It is used to store OpenGL logical buffers that do not have corresponding texture format, such as stencil or depth buffer.

This means I must use necessarily the renderbuffer image…right?

That’s the danger of using outdated information. Try the OpenGL Wiki; it’s at least trying to stay up to date.

Textures can have depth and stencil image formats in GL 3.x.

[QUOTE=Alfonse Reinheart;1248920]That’s the danger of using outdated information. Try the OpenGL Wiki; it’s at least trying to stay up to date.

Textures can have depth and stencil image formats in GL 3.x.[/QUOTE]

Thanks for the useful link, Alfonse, but since I am using GL2 I guess renderbuffer images are my only way…