I'm facing very strange issue. My code for world pos reconstruction works correctly when the viewport size is equal to window size (or framebuffer size in other words). Below is the part for reconstruction to view space, later I multiply the result by inversed view matrix:
vec3 posFromDepth(in vec2 Tex, in float depth) {
vec3 pos = vec3(Tex, depth) * 2.0 - 1.0; // clip-space position
float z = -projection[3][2] / (pos.z + projection[2][2]);
float x = pos.x * halfTanFovX * -z;
float y = pos.y * halfTanFovY * -z;
return vec3(x, y, z);
}
When I change the viewport light and shadow calculations seems to be incorrect. The shadows for example instead of being "glued" to the object, floats a little bit along with camera movement.
When I switched my deferred renderer to use world pos stored in G-buffer then everything was calculated right. Is there some transformation of texture coordinates which I'm missing? I tried pass fullscreen quad coords in vertex attributes and compute using below formula:
gl_FragCoord.xy * texelSize
but the results were the same.
I'm searching for the issue more than week and slowly running out of ideas what could be wrong. Maybe you'll come up with something useful.