0
votes

I am trying to copy the depth buffer of a fbo to a vector.

I initially started with using renderbuffers and glReadPixels but this resulted in a vector full of zeros despite glGetErrors() not highlighting any issues.

I then switched to textures and drew these to the screen. The colour texture shows evidence of depth testing and the depth texture shows the depth buffer is populated with expected values.

I've trawled through the OpenGL pages on framebuffers, the common mistake page, and read the docs on all the functions as well as read the first 2 pages of google search results of queries related to the problem but these don't solve the problem and mostly use old OpenGL versions.

I've played with pixel pack buffers, packing, enabling/disabling random settings + binding/unbinding various buffers.

The code is roughly as follows:

std::vector<GLuint> someVector; //also tried unsigned int
someVector.resize( windowWidth * windowHeight ); //also tried .reserve

glBindFramebuffer( GL_FRAMEBUFFER, fbo );
glEnable( GL_DEPTH_TEST );

drawStuff(); //I know this works

//Ive also tried making the depth buffer a depth_stencil buffer and reading a GL_UNSIGNED_INT_24_8

//glReadPixels( 0, 0, windowWidth, windowHeight GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, &someVector[ 0 ] );
glBindTexture( GL_TEXTURE_2D, depthTexture );
glGetTexImage( GL_TEXTURE_2D, 0,
        GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, &someVector[ 0 ] );
glBindTexture( GL_TEXTURE_2D, 0 );


glBindeFrameBuffer( GL_FRAMEBUFFER, fbo );

wrapperForGlGetErrors(); //this indicates all is good

I'm on ubuntu 18.04, using opengl 3.3 and using the latest nvidia drivers.

Output of lspci | grep -i --color 'vga\|3d\|2d' is

00:02.0 VGA compatible controller: Intel Corporation HD Graphics 630 (rev 04)
01:00.0 3D controller: NVIDIA Corporation GP107M [GeForce GTX 1050 Mobile] (rev a1)

Update

Doing:

unsigned int x;

    glReadPixels( 100, 100, 1, 1, GL_DEPTH_COMPONENT,
                  GL_UNSIGNED_INT, &x );
    std::cerr << x;

outputs 4294967295, and not 0.

1
I think depth values are floating point ranging from 0.0 to 1.0, maybe the data format and vector needs to be of float types. - Samik
The common mistakes page lists using float values as a mistake. Also, I have tried it and it doesn't work See this: khronos.org/opengl/wiki/Common_Mistakes#Depth_Buffer_Precision - Edward
"outputs 4294967295, and not 0.". - 4294967295 is 0xffffffff. The depth is stored in a normalized floating point format, thus the value 4294967295 (maximum of unsigned 32-bit integer) represents 1.0. 1.0 is the default initialization value for the depth buffer (glClear (GL_DEPTH_BUFFER_BIT) writes 1.0 into the frambuffer) - Rabbid76
Please provide an actual reproducible example. Those snippets are not enough to reproduce the issue. - derhass

1 Answers

0
votes

[...] outputs 4294967295, and not 0. [...]

That is the expected behavior, 4294967295 is 0xffffffff.

The depth is stored in a normalized floating point format (see Normalized Integer), thus the value 4294967295 (maximum of unsigned 32-bit integer) represents 1.0. 1.0 is the default initialization value for the depth buffer (see glClearDepth). Note, glClear (GL_DEPTH_BUFFER_BIT) writes 1.0 into the depth buffer of the current framebuffer.