# Projective texturing in DirectX11

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;
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:

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;
}