1

I am trying to read the depth buffer and store it into an array using openTK in C#.

This is my code so far:

    //Get depth buffer
    int[] depthBufferID = new int[1];
    GL.GenBuffers(1, depthBufferID);
    GL.BindBuffer(BufferTarget.PixelPackBuffer, depthBufferID[0]);
    GL.BufferData(BufferTarget.PixelPackBuffer, glControlGLRender.Width * 
        glControlGLRender.Height * sizeof(Single), IntPtr.Zero, BufferUsageHint.StreamRead);
    IntPtr depthBufferPTR = GL.MapBuffer(BufferTarget.PixelPackBuffer, 
        BufferAccess.ReadOnly);
    GL.ReadPixels(0, 0, glControlGLRender.Width, glControlGLRender.Height, 
        PixelFormat.DepthComponent, PixelType.Float, depthBufferPTR);


    float[] myPixels = new float[glControlGLRender.Width * glControlGLRender.Height];
    Marshal.Copy(depthBufferPTR, myPixels, 0, glControlGLRender.Width * 
        glControlGLRender.Height);

    this.glControlGLRender.SwapBuffers();

Well it does not produce any errors, but it is not working either. The output is an array (myPixels) where all values are 0.

So how do you read the depth buffer in openTK using the PixelPackBuffer?

Thank you

1 Answer 1

1

I have solved it meanwhile. I was pretty close with my code. The correct code goes like this:

//Get depth buffer
int[] depthBufferID = new int[1];
GL.GenBuffers(1, depthBufferID);
GL.BindBuffer(BufferTarget.PixelPackBuffer, depthBufferID[0]);
GL.BufferData(BufferTarget.PixelPackBuffer, glControlGLRender.Width * glControlGLRender.Height * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamRead);
GL.ReadPixels(0, 0, glControlGLRender.Width, glControlGLRender.Height, PixelFormat.DepthComponent, PixelType.Float, IntPtr.Zero);
IntPtr depthBufferPTR = GL.MapBuffer(BufferTarget.PixelPackBuffer, BufferAccess.ReadOnly);

float[] myPixels = new float[glControlGLRender.Width * glControlGLRender.Height];
Marshal.Copy(depthBufferPTR, myPixels, 0, glControlGLRender.Width * glControlGLRender.Height); 
Sign up to request clarification or add additional context in comments.

3 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
So the only difference was the sizeof(Single) to sizeof(float)? Single struct in C# is by default 4 bytes in size so I can't see how just that fixed it. Also as another recommendation is that if you need to fetch depth (or other attachments) on a framebuffer frequently, its much more convenient to add textures to your framebuffer attachments. That way you can just query the texture information directly
Hello Greg. Thank you for your feedback. No it was not only fhe single vs float. I guess the main thing was to call the intptr after the Gl.ReadPixels call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.