Jump to content

  • Log In with Google      Sign In   
  • Create Account

Calling all IT Pros from Canada and Australia.. we need your help! Support our site by taking a quick sponsored surveyand win a chance at a $50 Amazon gift card. Click here to get started!

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.

  • You cannot reply to this topic
No replies to this topic

#1 Jeffreyp   Members   -  Reputation: 178


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::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];

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),

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.

Thanks in advance.


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.