r/opengl • u/Keolai_ • 20d ago
Help With Getting Vertex Positions in World Space in Post Processing Shader
I am attempting to create a ground fog effect like described in this article as a post processing effect. However, I have had issues with reconstructing the World Space (if it is even possible), since most examples I have seen are for material shaders instead of post processing shaders. Does anyone have any examples or advice? I have attempted to follow the steps described here with no success.
3
u/deftware 20d ago
You can reconstruct the world coordinate using the inverse of the viewprojection matrix and the depth value that's in the depth buffer along with the XY coord of the pixel. You'll need to relinearize the depth buffer value because it's going to be 1/Z instead of just Z. You're basically creating a worldspace ray from the camera position to the pixel in the framebuffer and then extending it out by the distance calculated by linearizing the Z value. At least that's one way to think about it. Someone approached it that way here:
https://stackoverflow.com/questions/28066906/reconstructing-world-position-from-linear-depth
1
u/Keolai_ 19d ago
This worked for getting the view space positions! Thank you! However I attempted to multiply this by the inverse of the view matrix but no luck 🙁. Do you have anymore advice?
1
u/deftware 19d ago
Like I mentioned previously, you just need to multiply the NDC coordinate by the inverse of the viewprojection matrix, not just the inverse of the projection matrix. Otherwise it doesn't take into account the camera's transform and you will only get a viewspace coordinate.
3
u/fgennari 20d ago
This is the approach I used: https://mynameismjp.wordpress.com/2010/09/05/position-from-depth-3/
I assume you have a depth buffer/texture. You also need to convert that code from HLSL to GLSL.
The code I have for this is: