Hi,
I have a terrain engine where the terrain and water are on different grids. So I'm trying to render planar reflections of the terrain into the water grid. After reading some web pages and docs and also trying to learn from the RasterTek reflections demo and the small water bodies demo as well. What I do is as follows:
1. Create a Reflection view matrix - Technically I ONLY flip the camera position in the Y direction (Positive Y is up) and add to it 2 * waterLevel. Then I update the View matrix and I save that matrix for later. The code:
void Camera::UpdateReflectionViewMatrix( float waterLevel )
{
mBackupPosition = mPosition;
mBackupLook = mLook;
mPosition.y = -mPosition.y + 2.0f * waterLevel;
//mLook.y = -mLook.y + 2.0f * waterLevel;
UpdateViewMatrix();
mReflectionView = View();
}
2. I render the Terrain geometry to a 512x512 sized Render target by using the Reflection view matrix and an opposite culling (My Terrain is using front culling by nature so I'm using back culling for the Reflction render pass). Let me say that I checked with the Graphics debugger and the Reflection Render target looks "OK" at this stage (Picture attached). I don't know if the fact that the terrain is shown only at the top are of the texture is expected or not, but it seems OK.
3. Render the Reflection texture into the water using projective texturing - I hope this step is OK code wise. Basically I'm sending to the shader the WorldReflectionViewProj matrix that was created at step 1 in order to use it for the projective texture coordinates, I then convert the position in the DS (Water and terrain are drawn with Tessellation) to the projective tex coords using that WorldReflectionViewProj matrix, then I sample the reflection texture after setting up the coordinates in the PS. Here is the code:
//Send the ReflectionWorldViewProj matrix to the shader:
XMStoreFloat4x4(&mPerFrameCB.Data.ReflectionWorldViewProj, XMMatrixTranspose( ( mWorld * pCam->GetReflectedView() ) * mProj ));
//Setting up the Projective tex coords in the DS:
Output.projTexPosition = mul(float4(worldPos.xyz, 1), g_ReflectionWorldViewProj);
//Setting up the coords in the PS and sampling the reflection texture:
float2 projTexCoords;
projTexCoords.x = input.projTexPosition.x / input.projTexPosition.w / 2.0 + 0.5;
projTexCoords.y = -input.projTexPosition.y / input.projTexPosition.w / 2.0 + 0.5;
projTexCoords += normal.xz * 0.025;
float4 reflectionColor = gReflectionMap.SampleLevel(SamplerClampLinear, projTexCoords, 0);
texColor += reflectionColor * 0.25;
I'll add that when compiling the PS I'm getting a warning on those dividing by input.projTexPosition.w for a possible float division by 0, I tried to add some offset or some minimum to the dividing term but that still not solved my issue.
Here is the problem itself. At relatively flat view angles I'm seeing correct reflections (Or at least so it seems), but as I pitch the camera down, I'm seeing those artifacts which I have no idea where are coming from. I'm culling the terrain in the reflection render pass when it's lower than water height (I have heightmaps for that).
Any help will be appreciated because I don't know what is wrong or where else to look.