HI All,
I'm currently walking through a tutorial on Richardsoftware.net about shadow mapping ( http://richardssoftware.net/Home/Post/37).
I've already have my depthbuffer/Shaderresourceview code working, so I know that I am writing correctly to a shader buffer.
When I apply my normal Projection Matrix everything renders fine, when I try to do an orthographic projection I'm coming up 0. Now to tell the truth, I've adapted the code and I know the theory to a degree. But I think i'm executing incorrectly. Below, i've attached the code for my matrix generation which I believe is incorrectly generated (fairly obvious that this is where the problem should be). I've moved from Richards Left hand coordinate tutorial to Right hand (which is what my code is based off).
I'm not as familiar with the bounding sphere usage, but I do believe I should be setting the centre to the centre of the scene.
My updatecamera function is called only at the point of time I am pulling the matrices from the containing object. So its called Just in time. This works for projection fine, but as mentioned, ortho is failing. Matrix math has always been a nightmare to me, im hoping someone can suggest some directions so I can learn more.
Very grateful if anyone can throw some ideas on table.
void updateCamera()
{
m_changed = false;
m_worldMatrix = Matrix.RotationYawPitchRoll(0.0f, 0.0f, 0.0f);
m_viewMatrix = Matrix.LookAtRH(m_position, m_targetPosition, m_cameraUp);
if (m_displayMode == ProjectionMode.PROJECTION)
{
m_projectionMatrix = Matrix.PerspectiveFovRH(fov, m_viewport.AspectRatio, 0.1f, c_maxDepth);
m_wvp = m_worldMatrix * m_viewMatrix * m_projectionMatrix;
}
else if (m_displayMode == ProjectionMode.SHADOW)
{
BuildShadowTransform();
}
else
{
m_projectionMatrix = Matrix.OrthoRH(m_viewport.Width, m_viewport.Height, 0.1f, c_maxDepth);
m_wvp = m_projectionMatrix;
}
generateFrustumPlanes();
}
private void BuildShadowTransform()
{
BoundingSphere _sceneBounds = new BoundingSphere(new Vector3(), (float) Math.Sqrt(10 * 10 + 15 * 15));
_sceneBounds.Center = Lookat;
var lightPos = -2.0f * _sceneBounds.Radius * Direction;
var targetPos = _sceneBounds.Center;
Vector3 sphereCenterLS = Vector3.TransformCoordinate(targetPos, m_viewMatrix);
var l = sphereCenterLS.X - _sceneBounds.Radius;
var b = sphereCenterLS.Y - _sceneBounds.Radius;
var n = sphereCenterLS.Z - _sceneBounds.Radius;
var r = sphereCenterLS.X + _sceneBounds.Radius;
var t = sphereCenterLS.Y + _sceneBounds.Radius;
var f = sphereCenterLS.Z + _sceneBounds.Radius;
m_projectionMatrix = Matrix.OrthoOffCenterRH(l, r, b, t, n, f);
var T = new Matrix
{
M11 = 0.5f,
M22 = -0.5f,
M33 = 1.0f,
M41 = 0.5f,
M42 = 0.5f,
M44 = 1.0f
};
m_inverseMatrix = m_viewMatrix * m_projectionMatrix * T;
m_wvp = m_worldMatrix * m_viewMatrix * m_projectionMatrix;
}