This project is read-only.

Depth value at pixel location is not correct

May 23, 2013 at 9:21 PM
Edited May 23, 2013 at 9:21 PM
Hi!

I try to get the depth value at a pixel location.
I use the following code:
 byte[] winZ = new byte[20];

 GL.ReadPixels(x, y, 1, 1, OpenGL.GL_DEPTH_COMPONENT, OpenGL.GL_BYTE, winZ);
The values of winZ are always the same: All values are zero, except the first one which has a value of 127.

What am I doing wrong?
May 25, 2013 at 2:37 PM
I also tried it in the sample applications now but I always get the result I showed above. Depth buffer is enabled depth buffer bit is cleared.

By the way, this code works:
            byte[] pixelData = new byte[20];
            r.GL.ReadPixels(x, y, 1, 1, OpenGL.GL_RGBA, OpenGL.GL_BYTE, pixelData);
Jun 5, 2013 at 12:30 PM
I get the same response.

Always 127 in the first position.
Jun 5, 2013 at 2:29 PM
Edited Jun 5, 2013 at 2:29 PM
Here's what I did to fix it:
//Where mouseX and mouseY are local mouse coordinates in the glcontext
var pixelData = new byte[4];
_gl.ReadPixels(mouseX, mouseY, 1, 1, OpenGL.GL_DEPTH_COMPONENT, OpenGL.GL_FLOAT, pixelData);
var depth = System.BitConverter.ToSingle(pixelData,0);
Depth will be a number between 0 and 1. I used it as the Z value in _gl.UnProject() sucessfully.
Jun 20, 2013 at 5:57 PM
Yes, very good. That works!!