0
\$\begingroup\$

I'm trying to implement reflections on flat surfaces and render a simple scene with a reflection. So I'm rendering the scene from the point of view of a camera symmetrical to the reflection plane and then I pass the resulting rendered scene as a shader resource to the shader that renders the quad and I use projectiv texturing by sampling the texture using the quad screen space coordinates mapped to 0 - 1:

Texture2D quadTexture;
SamplerState textureSampler;

struct PS_Input
{
    float4 pos : SV_Position;
    float2 texCoords : TEX_COORDS;
    float4 clipPos : CLIP_POS;
};

float4 main(PS_Input input) : SV_Target
{
    float2 projectiveCoords = (float2)0;
    projectiveCoords.x = (input.clipPos.x / input.clipPos.w + 1.0) / 2.0;
    projectiveCoords.y = (-input.clipPos.y / input.clipPos.w + 1.0) / 2.0;
    float4 color = quadTexture.Sample(textureSampler, projectiveCoords);
    color.a = 0.2;
    return color + float4(0.0, 0.01, 0.01, 0.0);
}

I assume that since the y and the v axis are inverted with respect to each other that I need to negate the second texture coordinate (and all the code I saw on the net do so). But my code only works if I DON'T negate the second coordinate. If I negate the second coordinate the reflection is upside down:

enter image description here

The way I compute the reflected camera is I calculate the symmetric direction vector and use it in XMMatrixLookAtLH(). So when I calculate a new direction vector with a horizontal plane I should get a symmetric direction vector and the same right vector so my image is non mirrored on the horizontal axis. All works well when I don't invert the sign which is strange.

EDIT

I was not considering the fact that when rendering reflection on an horizontal plane, I have to take into account that the image being rendered is upside down with respect to the image I need to render on the surface. Not inverting the second projective texture coordinate has the effect of inverting the image (DirectX's v axis goes downwards, while projective texture coordinate y goes upwards). So when rendering a reflection on an horizontal surface I need not negate the coordinate. I need to negate both when rendering on a vertical surface (I always compute the symmetric camera direction vector and use the same world Up vector to compute the "reflected" view matrix):

float4 main(PS_Input input) : SV_Target
{
    float2 projectiveCoords = (float2)0;
    projectiveCoords.x = (input.clipPos.x / input.clipPos.w + 1.0) / 2.0;
    projectiveCoords.y = (input.clipPos.y / input.clipPos.w + 1.0) / 2.0;
    if (normal.y == 0)
    {
        projectiveCoords.x = -projectiveCoords.x;
        projectiveCoords.y = -projectiveCoords.y;
    }
    float4 color = quadTexture.Sample(textureSampler, projectiveCoords);
    color.a = 0.2;
    return color + float4(0.0, 0.01, 0.01, 0.0);
}

enter image description here

Now I have a new question: everything is fine if I want to render a reflection on an horizontal plane or a vertical one. How can I render a reflection on an (arbitrarily) incined surface? using the same calculations doesn't work anymore.

\$\endgroup\$
  • \$\begingroup\$ Maybe your reflection camera is not facing upside down? \$\endgroup\$ – János Turánszki May 10 '18 at 10:12
  • \$\begingroup\$ @JánosTuránszki I don't think so, I compute the symmetric camera direction vector with respect to the plane and the use the same world Up vector to compute the symmetric view camera so the facing direction should be symmetric as well \$\endgroup\$ – Luca May 10 '18 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.