I've been trying to implement screen space reflections for the past couple of days, however it's been difficult finding specific implementation instructions or guides.
Most of the hits on the subject that I can find relate to UE4 or Unity, or a sample implementation in HLSL derived from GLSL work both using different coordinate systems for the y and z axes.
Following what little I could find, I've been using the following shader:
#version 430
layout (std430, binding = 2) buffer Camera_Frag
{
mat4 pMatrix;
mat4 InvPMatrix;
vec2 ScreenSize;
};
uniform sampler2D ViewNormalMap;
uniform sampler2D DepthMap;
uniform sampler2D LightMap;
in vec2 TexCoord;
layout (location = 0) out vec4 FragColor;
vec2 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth)
{
dir *= 0.25f;
for(int i = 0; i < 20; ++i) {
hitCoord += dir;
vec4 projectedCoord = pMatrix * vec4(hitCoord, 1.0);
projectedCoord.xy /= projectedCoord.w;
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
float depth = texture(DepthMap, projectedCoord.xy).r;
dDepth = hitCoord.z - depth;
if(dDepth < 0.0)
return projectedCoord.xy;
}
return vec2(0.0f);
}
void main(void)
{
vec3 View_Normal = texture(ViewNormalMap, TexCoord).xyz;
float View_Depth = texture(DepthMap, TexCoord).r;
vec3 ScreenPos = 2.0f * vec3(TexCoord, View_Depth) - 1.0f;
vec4 View_Pos = InvPMatrix * vec4(ScreenPos, 1.0f);
View_Pos /= View_Pos.w;
// Reflection vector
vec3 reflected = normalize(reflect(normalize(View_Pos.xyz), normalize(View_Normal)));
// Ray cast
vec3 hitPos = View_Pos.xyz;
float dDepth;
float minRayStep = 0.1f;
vec2 coords = RayCast(reflected * max(minRayStep, -View_Pos.z), hitPos, dDepth);
FragColor = textureLod(LightMap, coords, 0);
}
I get the scene projected further down, but it isn't upside down. Notice the vases and the plants on top of them. Its like the scene gets resampled and pushed downwards.
Can anyone help me understand why I'm not getting the expected results? I've never done any raytracing before, so perhaps I'm misunderstanding how this is supposed to work.