Finding rendering extents

This question is actually something I’ve already figured out, but I think I probably did it the hard way. So maybe there is an easier way.

I have a program that allows the user to rotate, pan, and zoom a model in GL. I need to be able to find the extents of the Open GL window. For example, for a given view I need to know that min/max x ranges from -5 to +10 and min/max y goes from -4.4 to +10.5.

What I did was to start at each edge (top, bottom, left, right) and use glReadPixels to read the RGB values a column or row at a time. Since the background is black, I just keep reading until I found a value that isn’t black. Then I read the depth buffer and do a gluUnProject to get the 3D value at each edge. The left side gives me minx, the right side gives me maxx, etc.

This works but did I do it the hard way? Is there some simple command that does the same thing?

Thank you.

Is the model static? Does it have to be a tight fit? If not, you can precompute a bounding box and transform it into the view frustum and compute the extents of that. That would get rid of the slow readback.

if u want a pixel perfect result then i suppose u will have to do a readpixels (use Humus’s suggestion with the Bounding shape to narrow the amount to read)
also a single big glReadPixels is perhaps better than multiple small ones

another idea (perhaps quicker unless the model has > million vertices is to transform the verts yourself one at a time + then check there window positions)

I’m not sure what you mean by the model being static. There are no moving objects in the scene if that is what you mean. But the user can rotate and translate the entire scene. I’ll have to think about the bounding box, that may do it.

Actually, doing the readback using glReadPixels isn’t slow at all. Originally I was concerned that it might be, but it is almost instant. It just seems like a kludge which is why I was looking for a better way.