Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visualize the depth buffer

Tags:

c++

opengl

I'm attempting to visualize the depth buffer for debugging purposes, by drawing it on top of the actual rendering when a key is pressed. It's mostly working, but the resulting image appears to be zoomed in. (It's not just the original image, in an odd grayscale) Why is it not the same size as the color buffer?

This is what I'm using the view the depth buffer:

void get_gl_size(int &width, int &height)
{
    int iv[4];
    glGetIntegerv(GL_VIEWPORT, iv);
    width = iv[2];
    height = iv[3];
}

void visualize_depth_buffer()
{
    int width, height;

    get_gl_size(width, height);

    float *data = new float[width * height];

    glReadPixels(0, 0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, data);
    glDrawPixels(width, height, GL_LUMINANCE, GL_FLOAT, data);

    delete [] data;
}
like image 271
Thanatos Avatar asked Apr 30 '10 16:04

Thanatos


People also ask

What is depth buffer in opengl?

A depth buffer, also known as a z-buffer, is a type of data buffer used in computer graphics to represent depth information of objects in 3D space from a particular perspective. Depth buffers are an aid to rendering a scene to ensure that the correct polygons properly occlude other polygons.

How do you Linearize depth?

To linearize the sampled depth-buffer value, we can multiply the native device coordinates (ndc) vector by the inverse projection matrix and divide the result by the w coordinate (as the result is a homogenous vector).

What is depth precision?

Depth value precisionThe depth buffer contains depth values between 0.0 and 1.0 and it compares its content with the z-values of all the objects in the scene as seen from the viewer. These z-values in view space can be any value between the projection-frustum's near and far plane.

What is gl_FragDepth?

Available only in the fragment language, gl_FragDepth is an output variable that is used to establish the depth value for the current fragment.


1 Answers

I'm not sure whether this is your actual error, but there's a few things that I can recommend you check. It would be nice if you actually provided a screenshot of how exactly it's zoomed in.

First off, make sure the projection/modelview matrices are exactly the same as your rendering. I can't see how it would affect it, but it would be something to look into.

Second, the spec specifies that glPixelStore, glPixelTransfer, and glPixelMap can affect the results, you would want to make sure that these are being set properly.

If you do end up getting this working, please share what went wrong - it seems an interesting question. :)

like image 128
Andrei Krotkov Avatar answered Oct 02 '22 09:10

Andrei Krotkov