Archived

This topic is now archived and is closed to further replies.

how to read the depth buffer?

This topic is 5011 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I had just overcome the gluUnProject problem. But then another one come out! The problem is I don''t know the right way to get the depth value. Can anybody help me? Code: wglMakeCurrent(m_pDC->GetSafeHdc(),hrc); GLint viewport[4]; glGetIntegerv(GL_VIEWPORT,viewport); int x = point.x; int y = viewport[3] - point.y; glClearDepth(1.0); glClear(GL_DEPTH_BUFFER_BIT); double depth[4] = {0}; GLsizei w; GLsizei h; // if use w = h =1, I always get 5.2635442471209e-315.I don''t know why. // So I use w = h = 2 instead. Then it reads in two values. I''m puzzled. w = h = 2; glReadPixels(x,y,w,h,GL_DEPTH_COMPONENT,GL_FLOAT,depth); //I suppose depth[0] is 1.0. but it is 0.0078125018480932 indeed. why?

Share this post


Link to post
Share on other sites
glReadPixels is asking for the depth value for each pixel in your image. So that would only be valid for a 2x2 window (still, framrates would be up [8^)

did you want the limits (as in clip planes)?

lonesock

Piranha are people too.

Share this post


Link to post
Share on other sites
Do you mean I will get the right value (1.0) by using GLfloat?
I still didn''t understand why I use 2*2 but get 2 values, not 4 or just 1.That''s a big problem!

Share this post


Link to post
Share on other sites
Sorry I was unclear.

Thing 1:
a float is 4 bytes, a double is 8 bytes: so an array of 4 floats will fit very nicely into 2 doubles. Thus you are only getting two values. (Wow, who uses "thus"?)

Thing 2:
If you want to find out the near and far clip planes of your depth buffer, you would use glGetDoublev(GL_DEPTH_RANGE, NearAndFar), and that would put the values into your array of 2 doubles, ''NearAndFar'' (i.e. you would declare: double NearAndFar[2]

Thing 3:
The glReadPixels function will return the depth values of each pixel in your display. So, if you are in 640x480, and you are trying a glReadPixels into type GL_FLOAT, you would need an array of 307200 floats (not doubles [8^) in which to store the result. Each number would be scaled between 0.0 (near clip plane, see Thing 2) and 1.0 (far clip plane). Note that the scaling will probably not be linear, but a 1/x type of thing.

Hope that helps.

lonesock

Piranha are people too.

Share this post


Link to post
Share on other sites
quote:
Original post by lonesock
Thing 3:
The glReadPixels function will return the depth values of each pixel in your display.


This isn''t quite true. glReadPixels take 4 arguments that define what pixel(s) to get.

x, y, width and height. Thus (gah, I used it too!) glReadPixels (100, 100, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z); would get ONE pixel at the position {100,100} in your depth buffer.


Magnus Blikstad
www.witentertainment.com

Share this post


Link to post
Share on other sites