Wooni
Wooni

Reputation: 501

Is there any way to access depth buffer for OpenGL?

As an example, there are two objects and i see them from (0,0,100) to (0,0,0).

Object1 : Cube at center (length/width/height 10) Object2 : Sphere at Center (radius 5)

then, I can see only cube (at this time i do not consider field of view and view volume)

Because openGL uses z-buffer algorithm, at the center of the window, a pixel has four depth values at first. depth 90 (front face of cube), depth 110 (back face of cube) depth 95 and 105 (for sphere)

added : then z-buffer displays only the color of cube (at depth 90, front)

then now, I want to get the value 4 which is saved on pixel at center of window. IS there any way to get that value?

Upvotes: 0

Views: 727

Answers (2)

Jadh4v
Jadh4v

Reputation: 130

Use the following openGL API:

void glReadPixels( GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid * data);

along with GL_DEPTH_COMPONENT as the format parameter. The returned value is between 0 and 1 which you will have to transform based on your viewing frustum. You won't get four values simultaneously. You can render individual objects separately and read the different points that you need.

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385385

This is kind of backwards; pixels don't have depth values. The colour of a pixel is calculated based on the depth of the objects in your scene, their relative location and orientation, and the camera setup. The pixel is the result of all that, not the input data.

You can perform the inverse of the camera transformation on the point you're interested in, to get a line in your scene's "space". Then you can intersect that line through the objects in your scene; where those intersections occur is the Z values of the objects behind that pixel.

But again this all seems rather backwards to normal techniques. Perhaps if we knew what you were trying to accomplish, we could suggest a concrete resolution.

Upvotes: 1

Related Questions