1
\$\begingroup\$

I'm currently drawing my directional light shadow using this view and projection:

XMFLOAT3 dir((float)pitch, (float)yaw, (float)roll);
XMFLOAT3 center(0.0f, 0.0f, 0.0f);

XMVECTOR lightDir = XMLoadFloat3(&dir);
XMVECTOR lightPos = radius * lightDir;
XMVECTOR targetPos = XMLoadFloat3(&center);
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

XMMATRIX V = XMMatrixLookAtLH(lightPos, targetPos, up); // This is the view

// Transform bounding sphere to light space.
XMFLOAT3 sphereCenterLS;
XMStoreFloat3(&sphereCenterLS, XMVector3TransformCoord(targetPos, V));

// Ortho frustum in light space encloses scene.
float l = sphereCenterLS.x - radius;
float b = sphereCenterLS.y - radius;
float n = sphereCenterLS.z - radius;
float r = sphereCenterLS.x + radius;
float t = sphereCenterLS.y + radius;
float f = sphereCenterLS.z + radius;
XMMATRIX P = XMMatrixOrthographicOffCenterLH(l, r, b, t, n, f); // This is the projection

Which works prefect if the center of my scene is at 0.0, 0.0, 0.0. What I would like to do is move the center of the scene relative to the cameras position. How can I do that?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$
XMFLOAT3 dir((float)pitch, (float)yaw, (float)roll);
XMVECTOR center = XMVectorSet(eye(0), eye(1), eye(2), 0.0); // Se the center to the eye

XMVECTOR lightDir = XMLoadFloat3(&dir);
XMVECTOR lightPos = (radius * lightDir) + center; // Add the center
XMVECTOR targetPos = center;
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

XMMATRIX V = XMMatrixLookAtLH(lightPos, targetPos, up);

// Transform bounding sphere to light space.
XMFLOAT3 sphereCenterLS;
XMStoreFloat3(&sphereCenterLS, XMVector3TransformCoord(targetPos, V));

// Ortho frustum in light space encloses scene.
float l = sphereCenterLS.x - radius;
float b = sphereCenterLS.y - radius;
float n = sphereCenterLS.z - radius;
float r = sphereCenterLS.x + radius;
float t = sphereCenterLS.y + radius;
float f = sphereCenterLS.z + radius;
XMMATRIX P = XMMatrixOrthographicOffCenterLH(l, r, b, t, n, f);
\$\endgroup\$
1
  • \$\begingroup\$ Some explanations would help \$\endgroup\$
    – Kromster
    Mar 2, 2014 at 8:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .