I am trying to create my view matrix in the shader. I cribbed some of the code from Matrix.CreateLookAt and tried modifying it to work in the shader, but I am not getting the correct results. The model isn’t being shown.
I am pretty crappy with matrix math, so it’s probably something simple which is eluding me. I tried all variations of setting the matrix using column and row formats, but nothing seems to work.
Here’s the code I have. Any pointers will be greatly appreciated! Note, position is the vertex position passed from the vertex shader to the CalcView function.
float4x4 CalcView(float3 position)
{
float4x4 result;
float3 cameraPosition = EyePosition;
float3 cameraTarget = position;
float3 zAxis = normalize(cameraPosition - cameraTarget);
float3 xAxis = normalize(cross(float3(0, 1, 0), zAxis));
float3 yAxis = cross(zAxis, xAxis);
result[0][0] = xAxis.x;
result[0][1] = yAxis.x;
result[0][2] = zAxis.x;
result[0][3] = 0.0;
result[1][0] = xAxis.y;
result[1][1] = yAxis.y;
result[1][2] = zAxis.y;
result[1][3] = 0.0;
result[2][0] = xAxis.z;
result[2][1] = yAxis.z;
result[2][2] = zAxis.z;
result[2][3] = 0.0;
result[3][0] = 0.0 - dot(xAxis, cameraPosition);
result[3][1] = 0.0 - dot(yAxis, cameraPosition);
result[3][2] = 0.0 - dot(zAxis, cameraPosition);
result[3][3] = 1.0;
result = transpose(result);
return result;
}