Hi, I’m checking I have everything down before I get to deep into screen space reflections but I cannot get UV coords from view space position.
I’m using the function nehon wrote in the SSAO filter to get position from depth and texCoord. (This is all a post filter frag shader)
vec3 getPosition(vec2 uv)
{
//Reconstruction from depth
float depthv = texture2D(m_DepthTexture,uv).r;
float depth = (2.0 * m_FrustumNearFar.x) / (m_FrustumNearFar.y + m_FrustumNearFar.x - depthv* (m_FrustumNearFar.y-m_FrustumNearFar.x));
//one frustum corner method
float x = mix(-m_FrustumCorner.x, m_FrustumCorner.x, uv.x);
float y = mix(-m_FrustumCorner.y, m_FrustumCorner.y, uv.y);
return depth* vec3(x, y, m_FrustumCorner.z);
}
My understanding is that this is in view space (camera or eye space or whatever you like )
I then multiply this by ‘g_ProjectionMatrix’ which I listed as a world parameter in the j3md file. So this takes it from
View space to Clip/Projection space.
Once it’s in projection space, since that’s [-1, 1] (is this wrong?) I multiply by 0.5, then add 0.5 to give the results between [0,1] to use as UV coords. Using this to get the output from the texture
texture2D(m_Texture, coords)
I know I’m misunderstanding something because my results are pretty messy.
Here is the full segment:
vec3 startPos = getPosition(texCoord);//get position using depth
vec4 screenSpaceStartPos = g_ProjectionMatrix * vec4(startPos,1.0);//view to projection space
screenSpaceStartPos.xy /= screenSpaceStartPos.w;
vec2 coords = (vec2(screenSpaceStartPos.x, screenSpaceStartPos.y) * 0.5) + 0.5;//0-1
//gl_FragColor = texture2D(m_Background,texCoord);//works as expected, normal way of doing it
gl_FragColor = texture2D(m_Background,coords);//broken
This is the bit I am confusing I think:
screenSpaceStartPos.xy /= screenSpaceStartPos.w;
I’m not sure if I need to factor in my screen width and height or if this is already done by the projection matrix, or if the [-1,1] is even correct. Could anyone tell me what I am getting wrong?