Reflections shader question

I was just wondering whether someone could explain how the pixel shader in WaterRenderPass generates the correct reflection. In particular, how does the math work for this modification, which, if there is no normal or dudv mapping, is about all that is needed to produce nice reflections from a texture rendered by a camera reflected around a plane:



varying vec4 viewCoords;

uniform sampler2D reflection;

uniform int infrontof;

void main()
{
   vec2 projCoord = viewCoords.xy/viewCoords.q;
   projCoord = (projCoord + 1.0) * 0.5;
   if ( infrontof == 1 ) {
      projCoord.x = 1.0 - projCoord.x;
   }
   
   projCoord = clamp(projCoord, 0.001, 0.999);
   vec4 reflectionColor = texture2D(reflection, projCoord.xy);
   if ( infrontof == 0 ) {
      reflectionColor *= vec4(0.5,0.6,0.7,1.0);
   }
   
   gl_FragColor = reflectionColor;
}



Any help would be appreciated. I've been going through the math for the past 5 hours and can't understand it. (This code is actually from an old forum topic ("Simply reflections ?"), that I might try replying to if this doesn't get a response.