sancelot
sancelot

Reputation: 2053

how to retrieve z depth and color of a rendered pixel

I would like to retrieve the z height of each pixels of a rendered object in a scene. I will need to retrieve the color rendered too.

What are the opengl technics to implement ?

Upvotes: 2

Views: 1513

Answers (1)

Spektre
Spektre

Reputation: 51845

  1. glReadPixels and CPU side code

    use glReadPixels to obtain both RGB and Depth buffers. Here examples for both:

    That will read the buffers into CPU accessible memory. This way is slow (due to sync) but should work on any platform.

  2. FBO render to texture and GPU shader

    Faster method is to use FBO and render to texture and use that output in next rendering pass as input texture for computing your stuff inside shaders. This however will not run properly on Intel and might need additional tweaking of code between nVidia and AMD.

    If you have per pixel output use single QUAD covering your screen as the second rendering pass.

    If you got single output for the whole screen instead use single POINT render and compute all in the fragment shader (scann the whole texture inside) something like this:

    The difference is that by usnig shaders and FBO you are not transferring data between GPU/CPU so its way faster.

    The content of the targeted textures can be still readed by CPU using texture related GL functions

  3. compute GPU shaders

    There are also compute shaders out there but I did not use them yet so I am just guessing however with them it might be possible to do your stuff in single pass and also the form of the result and computation should not be as limiting.

My bet is that you are doing some post processing similar to Deferred Shading so googling such topic/tutorials might help.

Upvotes: 2

Related Questions