Accessing the stencil or similiar buffer?

I’m trying to write a 2D (sorta) game that will use 3D. Okay, let me clarify, I’m using 3D but the game is played out on a 2D plane. In other words, nothing that is relevant to the game moves along the Z axis. The Z axis will only be used for graphics. Okay, with that explained, I’m performing collision detection by keeping an array of values that represent each “pixel” if you will. I store the ID# of whatever object occupies that “cell” and this allows very simple collision detection for 2D. As I understand, you can render things to the stencil buffer and this is basically what it does. As I understand it writes an ID# that you specify to the stencil buffer on all the pixels drawn on. So, the question is, how do I then access individual pixels in the stencil buffer to do my own collision detection? Thanks!

To do 2D collision detection, I would not use the stencil buffer. Instead I would likely use either axis aligned bounding boxes or object oriented bounding boxes.

Trust me, the type of thing I’m doing in this game require the method I’ve described. Actually, I have the non-OpenGL stuff written already and it essentially uses an array to store the ID#s I need. If there’s a way to render an object to some other buffer the same way the stencil buffer does, then I’m all ears. The reason I need to do it this way is because I’m dealing with THOUSANDS upon THOUSANDS of particles (points/pixels) and lots of collision detection with LOTS and LOTS of other objects. It’s really complicated to explain but suffice it to say traditional collision detection methods would bring this game to it’s knees. And I’m not going to be rendering more than 5-10 very low-detail models so doing multiple rendering passes for the stencil buffer won’t be much of an issue. And even at that, I’ll probably only be having to actually re-render a sinlge model at a time. (hard to explain why without going very deep into it) So my question still is, how do I access individual pixels in the stencil or similiar buffer? When I say similiar, I mean if I can use another buffer for this same type of thing then how to do that? Thanks for the input though!

Ah well now that I have a better idea of what you are doing, bounding boxes are not good obviously. Unfortunately, many video cards do not support hardware stencil buffers and they are very slow since they must fall back to software rendering when stenciling is enabled. Also the stencil buffer generally only has 8 bits of precision, so you are limited to 255 id values (assuming 1 value is used for background stencil) so you’ll have to deal with that if you need more. Well anyway you can read back the 8 bit stencil values with glReadPixels. For example to read the stencil value at (x,y) you could use:

GLbyte stencil_pixel;
glReadPixels(x,y,1,1,GL_STENCIL_INDEX, GL_BYTE, &stencil_pixel);

Note: (x,y) is relative to lower left corner of window.

The only other buffer I’d consider using is an alpha buffer, but many video cards lack alpha buffer support altogether.

[This message has been edited by DFrey (edited 11-01-2000).]

Thanks, man! This is exactly what I need. It just so happens that the buffer I was already using is 8-bit also. This is to save memory since the nature of my game is such that I won’t realistically need more than 256 possible ID#s. And lack of hardware stencil buffering shouldn’t pose that much of a problem for this game because the 3D models are merely spaceships the players rotate around and since the players have to take turns, I’ll only have to write to the stencil buffer one ship at a time and even then only once (not in real-time) when the player takes his shot. So it won’t be a time-critial operation. Thanks again!

“The only other buffer I’d consider using is an alpha buffer, but many video cards lack alpha buffer support altogether”

what?? u mean that there are cards around that don’t have an alpha channel in the colour buffer

Yep, most older video cards have no destination alpha buffer, or have only a 1 bit alpha buffer. Some even have difficulty with source alpha blending. NVIDIA began supporting full featured alpha blending with the TNT line. Others like Voodoo 1 and 2 may use an alpha buffer, but it is exclusive of the depth buffer, i.e. either you use a depth buffer or alpha buffer but not both.