Sign in to follow this  
jeffreyp23

Need help with shadow mapping

Recommended Posts

jeffreyp23    178
Hi guys,

I need help with my shadow mapping because I get incorrect results.
I think I calculate my light view and light projection matrix wrong.
Because I got a wrong shadow map too.

Here is the code :

[source]
DirectX::XMMATRIX CreateLightViewProjectionMatrix(DirectX::XMFLOAT3 slightPos)
{
DirectX::BoundingFrustum frustom(DirectX::XMMatrixIdentity());
DirectX::XMVECTOR up = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
DirectX::XMVECTOR lightDir = DirectX::XMLoadFloat3(&slightPos);
DirectX::XMMATRIX lightRotation = DirectX::XMMatrixLookAtLH(DirectX::XMVectorZero(),
DirectX::XMVectorSet(DirectX::XMVectorGetX(lightDir) * -1.0f,
DirectX::XMVectorGetY(lightDir) * -1.0f,
DirectX::XMVectorGetZ(lightDir) * -1.0f,
DirectX::XMVectorGetW(lightDir) * -1.0f), up);


DirectX::XMFLOAT3 *frustumCorners = new DirectX::XMFLOAT3[frustom.CORNER_COUNT];
frustom.GetCorners(frustumCorners);


for (int i = 0; i < frustom.CORNER_COUNT; i++)
{
DirectX::XMVECTOR trans = DirectX::XMLoadFloat3(&frustumCorners[i]);
trans = DirectX::XMVector3Transform(trans, lightRotation);
DirectX::XMStoreFloat3(&frustumCorners[i], trans);
}


DirectX::BoundingBox lightBox;
lightBox.CreateFromPoints(lightBox, frustom.CORNER_COUNT, frustumCorners, sizeof(DirectX::XMFLOAT3));
delete[] frustumCorners;
DirectX::XMVECTOR lightBoxExtents = DirectX::XMLoadFloat3(&lightBox.Extents);
DirectX::XMVECTOR lightBoxCenter = DirectX::XMLoadFloat3(&lightBox.Center);
DirectX::XMVECTOR lightBoxMax = DirectX::XMVectorAdd(lightBoxCenter, lightBoxExtents);
DirectX::XMVECTOR lightBoxMin = DirectX::XMVectorSubtract(lightBoxCenter, lightBoxExtents);

DirectX::XMVECTOR boxSize = DirectX::XMVectorSubtract(lightBoxMax, lightBoxMin);
DirectX::XMVECTOR halfBoxSize = DirectX::XMVectorScale(boxSize, 0.5f);

DirectX::XMVECTOR lightPosition = DirectX::XMVectorAdd(lightBoxMin, halfBoxSize);
DirectX::XMVectorSetZ(lightPosition, DirectX::XMVectorGetZ(lightBoxMin));

DirectX::XMVECTOR det = DirectX::XMVectorSet(-1.0f, -1.0f, -1.0f, -1.0f);
lightPosition = DirectX::XMVector3Transform(lightPosition,
DirectX::XMMatrixInverse(&det, lightRotation));

DirectX::XMMATRIX lightView = DirectX::XMMatrixLookAtLH(lightPosition,
DirectX::XMVectorSubtract(lightPosition, lightDir),
up);

lightProjection = DirectX::XMMatrixOrthographicLH(DirectX::XMVectorGetX(boxSize), DirectX::XMVectorGetY(boxSize),
-DirectX::XMVectorGetZ(boxSize), DirectX::XMVectorGetZ(boxSize));
return lightView * lightProjection;
}
[/source]

This is the same code is used in the msdn shadow mapping example(for XNA).
So I don't know what I did wrong.

Thanks in advance.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this