gluUnProject

I’m having a problem with gluUnProject.

I first render the scene to the frame buffer. Then I read back into software the color buffer and the depth buffer. I then iterate over all of the pixels in my software color buffer. Here’s how I read the depth buffer:

glReadPixels(0, 0, _imageResolution.x, _imageResolution.y, GL_DEPTH_COMPONENT, GL_DOUBLE, _depthBuffer);

If the color in the buffer is the color of an object I rendered (and not the background color) I make a call to gluUnProject:

gluUnProject((double)x, (double)y, _depthBuffer[x][y], modelView, projection, viewport, &position.x, &position.y, &position.z);

y is corrected by: double(viewport[3]) - y
The clear depth is 1.0. The depth when I find a pixel of an object is very close to zero.

Say I have rendered a sphere that is projected exactly to a circle. When I write to a file the positions gluUnProject gives me, the positions simply grow by 5 in x and by 10 in z (both are always integers) and y is always zero. Clearly, this is not what is desired when rasterizing a sphere.

I don’t see anything wrong with the matrices or the viewport.

I need the correct world position to do shading. This is my first time using gluUnProject. Does anyone have an idea what’s happening?

Thanks,
TT

I fixed it.
I guess my implementation/program settings doesn’t like it when I read the depth buffer as doubles.
They should be floats.