I am trying to restore the position from the depths. I separately created a position map in view space, which I use as a basis for tests. But I can not get the same result when I try to reconstruct the position from the depth. Here's how I'm trying to do this:
#define getPosition(texCoord) texture(positionMap, texCoord).xyz
vec3 posFromDepth(float depth) {
float z = depth * 2.0 - 1.0;
vec4 clipSpacePosition = vec4(texCoord * 2.0 - 1.0, z, 1.0);
vec4 viewSpacePosition = inverse(projection) * clipSpacePosition;
// Perspective division
viewSpacePosition /= viewSpacePosition.w;
return viewSpacePosition.xyz;
}
void main() {
float d = -(projection * vec4(getPosition(texCoord), 1.0)).z; // get depth
fragColor = posFromDepth(d);
//fragColor = getPosition(texCoord);
fragColor = clamp(fragColor, 0, 1);
}
Here is the correct position map:
But reconstructed looks like this:
What am I doing wrong?