1

Related to this question.

I am having suffering due to lack of precision in the following function in glsl:

double LinearizeDepth(double depth)
{
  double z = depth * 2.0 - 1.0; // back to NDC
  return  (2.0 * near_plane * far_plane) / (far_plane + near_plane - z * (far_plane - near_plane));
}

I want to threshold on a depth value like so

abs(linearised_depth - threshold_value) < epsilon and my code works as expected when epsilon >= 0.5. I want to reduce epsilon to ~0.1. Are there any tricks to help with this?

Edward
  • 468
  • 4
  • 18
  • Are `near_plane` and `far_plane` `float`? if so, you could try to cast them to `double` – tuket Sep 09 '20 at 17:24
  • I think this answer is doing what you what. Yet the expression seems shorter. You might want to try it and see if it gives better precision. https://stackoverflow.com/a/51137756/1754322 – tuket Sep 09 '20 at 17:29
  • @tuket glsl has no equivalent to c/c++ type cast operators (see [GLSL - 5.1. Operators](https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.60.html#operators)). Anyway you can construct a double from a float, e.g. `float f; double d = double(f);` (see [GLSL - 5.4. Constructors](https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.60.html#constructors)) – Rabbid76 Sep 09 '20 at 17:37
  • 1
    I think your main issue is not the precision of the math here, but the precision of the underlying depth buffer where `depth` comes from. Using a reverse depth mapping + float depth buffer might help. – derhass Sep 09 '20 at 21:11
  • Reverse depth mapping? – Edward Sep 10 '20 at 09:09
  • 1
    my bet is your precision get lost between shader stages instead of the code you provided.... For more info and true linear depth see: [How to correctly linearize depth in OpenGL ES in iOS?](https://stackoverflow.com/a/42515399/2521214) – Spektre Oct 05 '20 at 08:16

0 Answers0