0

In OpenGL, I'm trying to render a depth map that doesn't loose precision with fa away objects. Do you know of any way I can accomplish this task?

My approach so far: I tried the "reverse depth" trick (here for example).

  • I modified the perspective projection matrix in such a way that the near and far values are mapped as [-n, -f] -> [1, 0] instead of the usual [-n, -f] -> [-1, 1]
  • I rendered the depth map to a framebuffer, using in particular the 32F depth component glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, conf::SCR_WIDTH, conf::SCR_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL)
  • plus I tackled the need for clipping to [1,0] using glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE) (documentation here)
  • lastly, after rendering the depth map to the framebuffer, I take the depth values and map them back to eye coordinates, divide by the far plane value, and use this value as a color

However I couldn't notice any gain in precision. Am I doing anything wrong? Is there any better approach?


Example on a close by cube: you can really see the discrete transitions, I would like a smoother output

enter image description here

Lilla
  • 209
  • 3
  • 9
  • 1
    "*However I couldn't notice any gain in precision.*" How are you determining this? – Nicol Bolas Mar 24 '22 at 18:23
  • The visual results look identical, I should better phrase it as "that didn't do the trick" @NicolBolas – Lilla Mar 24 '22 at 18:25
  • The visual results of what? It's not clear what you're rendering such that there would be a noticeable difference based on depth precision. – Nicol Bolas Mar 24 '22 at 18:45
  • There is an object, in the first pass I render it as a depth map using a framebuffer, then after the second pass I render this texture to the normal framebuffer @NicolBolas – Lilla Mar 24 '22 at 18:48
  • And you expect to be able to visually see a very small difference between two numbers when they are interpreted as colors? – Nicol Bolas Mar 24 '22 at 18:50
  • I don't know, I expect to see a visually good result, which is my goal. What I see is that it looks like my object is divided into "planes", instead of being a smooth thing (https://prnt.sc/m4CLI-1SwDhH) Do you have a suggestion? @NicolBolas – Lilla Mar 24 '22 at 18:54
  • 1
    Why do you think you have more color resolution in 8 bits than depth resolution in 24 bits? Just as a matter of mathematics, the 24 bits of depth just has more resolution than the 8 bits of monochrome color. It's not reasonable to expect to see more with less. – Nicol Bolas Mar 24 '22 at 19:54
  • @NicolBolas Good point. So, what is stored in my framebuffer 32F texture is probably fine (detailed enough), but I loose information when outputting the depth map to screen, correct? – Lilla Mar 24 '22 at 20:01
  • @NicolBolas Do you know of a way of getting out the high precision values from the depth framebuffer, into a variable on the cpu? – Lilla Mar 24 '22 at 20:14
  • In cases your scene covers too big range of depth or you need world coordinates system precision the logarithmic depth buffer usually fails. In such case usually greatly helps using [Linear depth buffer](https://stackoverflow.com/a/42515399/2521214) instead. Also you can use this [Spectral colors](https://stackoverflow.com/a/22681410/2521214) or this [Star BV index](https://stackoverflow.com/a/22630970/2521214) to create RGB color gradient that is not just 8bit ... so you can have much much more than just 256 colors... – Spektre Mar 25 '22 at 08:21

0 Answers0