• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Need help with shadow mapping

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

No replies to this topic

### #1Jeffreyp  Members

Posted 18 November 2012 - 05:54 AM

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 :

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::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++)
{
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 lightBoxMin = DirectX::XMVectorSubtract(lightBoxCenter, lightBoxExtents);

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

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


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