PDA

View Full Version : How to read back stencil buffer



yalmar
11-20-2006, 10:34 AM
Hi all,

I'm need read back the stencil buffer, see the below code


...
// Disable depth buffer updates. Pass pixels if nearer than stored depth value
glDepthMask(GL_FALSE);
glDepthFunc(GL_LESS);
// Increment stencil buffer for object A frontfaces
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
glCullFace(GL_BACK);
DrawObject(A);
// Decrement stencil buffer for object A backfaces
glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
glCullFace(GL_FRONT);
DrawObject(A);
// Read back stencil buffer.
...I need it for determining if two objects are colliding or not.

Thanks,

Yalmar.

Humus
11-20-2006, 04:50 PM
Read back as in copy to system memory? You can use glReadPixels() for that. It'll most likely be awfully slow though.

yalmar
11-20-2006, 07:17 PM
exactly, using glReadPixels. But what params should i use? I'm not sure of using GL_STENCIL_INDEX, anybody have an illustrative example?

Humus
11-21-2006, 09:14 AM
This should do:

glDrawPixels(width, height, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, data);

jide
11-21-2006, 10:02 AM
@ Humus: why would it be awfully slow ? I haven't done it yet. But, from my point of view, it should be faster than doing for example real-time shadow mapping or anything involving read/write/copy from textures and FBOs : the stencil buffer is only 8 bits sized. Has it something to do with non-hardware support ?

yalmar
11-21-2006, 12:12 PM
Thanks Humus,
I would like to know if nowadays exist someway of render to Stencil Buffer and attach it as a texture, for instance, using EXT_packed_depth_stencil.

Humus
11-21-2006, 07:28 PM
Originally posted by jide:
@ Humus: why would it be awfully slow ?glReadPixels() is always slow because it forces a glFinish() and thus kills all parallelism, and it copies the data to system memory, which is slow. So even when it's accelerated, things are quite slow. With stencil readback I don't think it'll be accelerated either.

jide
11-22-2006, 03:08 AM
Thanks Humus.

yalmar, you can use FBO to deal with render to stencil, using stencil render buffer objects. But AFAIK, you can't directly attach it to a texture. I guess you'll have to deal with read/write pixels on a 8 bits monochromatic texture.

yalmar
11-22-2006, 12:04 PM
:-(
what a pity,

Thanks.

Humus
11-23-2006, 02:28 PM
Are you sure you really need to read back stencil though? Can't you use an occlusion query to solve the problem?

JeffJ
11-27-2006, 06:46 AM
Originally posted by jide:
I would like to know if nowadays exist someway of render to Stencil Buffer and attach it as a texture, for instance, using EXT_packed_depth_stencil.Yes. EXT_packed_depth_stencil enables you to render your stencil content to an FBO that has a DEPTH_STENCIL attachment. The depth_stencil format is supported for both texture and renderbuffer objects.

Also, if you must do ReadPixels of stencil data, you are best off using ReadPixels of DEPTH_STENCIL data format and UNSIGNED_INT_24_8 packed data type. This ReadPixels usage was first supported long ago by the NV_packed_depth_stencil upon which EXT_packed_depth_stencil is based.

jide
11-27-2006, 09:16 AM
JeffJ, I haven't wrote that quote ! :)

JeffJ
11-27-2006, 12:43 PM
Oops, sorry, yalmar wrote the text I quoted. I had a copy/paste typo that I failed to notice when previewing.

yalmar
11-29-2006, 03:38 AM
Thanks for your replies. I will attempt to use that extension (EXT_packed_depth_stencil).