Reconstructing world coordinates from depth buffer and arbitrary view-projection matrix

前端 未结 3 1890
有刺的猬
有刺的猬 2021-02-15 13:20

I\'m trying to reconstruct 3D world coordinates from depth values in my deferred renderer, but I\'m having a heck of a time. Most of the examples I find online assume a standard

3条回答
  •  抹茶落季
    2021-02-15 14:18

    I only needed to make a tiny fix. The line:

    clipSpaceLocation.z = texture(depthSampler, texcoord).r;
    

    should read:

    clipSpaceLocation.z = texture(depthSampler, texcoord).r * 2.0f - 1.0f;
    

    The way I understand it, projection matrices are designed so they map the near and far planes to [-1,1], not [0,1] like I had always assumed. OpenGL then normalizes them to the range [0,1] (a.k.a. "Window Space"), so I needed to perform the inverse of that normalization.

    This is all assuming glDepthRange(0, 1), which it is by default, and there's little reason to change it.

提交回复
热议问题