Hey guys I was looking at mjp's deferred shadow mapping sample (http://mynameismjp.wordpress.com/samples-tutorials-tools/deferred-shadow-maps-sample/) and have been trying to convert this to my c++ dx11 based engine.

The issues I'm having are with the creation of the view and the orthogonal projection matrix for the directional light.

See bottom post...

### Show differencesHistory of post edits

### #11lipsryme

Posted 24 November 2012 - 08:33 AM

Hey guys I was looking at mjp's deferred shadow mapping sample (http://mynameismjp.wordpress.com/samples-tutorials-tools/deferred-shadow-maps-sample/) and have been trying to convert this to my c++ dx11 based engine.

The issues I'm having are with the creation of the view and the orthogonal projection matrix for the directional light.

-> see last post...

The issues I'm having are with the creation of the view and the orthogonal projection matrix for the directional light.

-> see last post...

### #10lipsryme

Posted 24 November 2012 - 08:33 AM

Hey guys I was looking at mjp's deferred shadow mapping sample (http://mynameismjp.wordpress.com/samples-tutorials-tools/deferred-shadow-maps-sample/) and have been trying to convert this to my c++ dx11 based engine.

The issues I'm having are with the creation of the view and the orthogonal projection matrix for the directional light.

-> see last post...

The issues I'm having are with the creation of the view and the orthogonal projection matrix for the directional light.

-> see last post...

### #9lipsryme

Posted 24 November 2012 - 08:33 AM

Hey guys I was looking at mjp's deferred shadow mapping sample (http://mynameismjp.wordpress.com/samples-tutorials-tools/deferred-shadow-maps-sample/) and have been trying to convert this to my c++ dx11 based engine.

The issues I'm having are with the creation of the view and the orthogonal projection matrix for the directional light.

Basically what I'm getting as the depth output is nothing.

I've tried some custom defined view and projection matrices and can confirm they work so the only problem is the "CalculateFrustum" part.

This is what I have so far:

...refer to last post...

The issues I'm having are with the creation of the view and the orthogonal projection matrix for the directional light.

Basically what I'm getting as the depth output is nothing.

I've tried some custom defined view and projection matrices and can confirm they work so the only problem is the "CalculateFrustum" part.

This is what I have so far:

...refer to last post...

### #8lipsryme

Posted 24 November 2012 - 08:32 AM

Hey guys I was looking at mjp's deferred shadow mapping sample (http://mynameismjp.wordpress.com/samples-tutorials-tools/deferred-shadow-maps-sample/) and have been trying to convert this to my c++ dx11 based engine.

The issues I'm having are with the creation of the view and the orthogonal projection matrix for the directional light.

Basically what I'm getting as the depth output is nothing.

I've tried some custom defined view and projection matrices and can confirm they work so the only problem is the "CalculateFrustum" part.

This is what I have so far:

...See last post...

Here's how I calculate my frustum corner:

Having read how the xna function BoundingFrustum.GetCorner works, this "should" be correct. Meaning the near plane is from [0-3] and is defined clockwise starting from the top left from the positive Z axis looking at the origin. So basically I'm defining them counter-clockwise.

Same with the far plane [4-7].

And this is how I've translated his "CalculateFrustum" function:

Any help would be appreciated :/

edit: Not sure if it tells you anything but here's some results (or rather inputs) I'm getting for the matrices:

EyePosition = {1, -992,522, 821,255, 1334,95}

EyeTarget = {1, 34,3589, -0,25, 0}

EyeUp = {1, 0, 1, 0}

mins = {x=-269.78665 y=-407.21408 z=1424.8429 }

maxes = {x=325.82809 y=426.11288 z=2361.6660 }

And my camera setup is:

nearClip = 0.01f

farClip = 100.0f

FoV = 55 (degree)

AspectRatio = 1280/720

The issues I'm having are with the creation of the view and the orthogonal projection matrix for the directional light.

Basically what I'm getting as the depth output is nothing.

I've tried some custom defined view and projection matrices and can confirm they work so the only problem is the "CalculateFrustum" part.

This is what I have so far:

...See last post...

Here's how I calculate my frustum corner:

void ExtractFrustumCorner(XMFLOAT3* frustum) { XMVECTOR corner[8]; // Calculate Width &amp;amp;amp;amp;amp;amp; Height of near and far plane float Hnear = 2 * tan(this->fov / 2) * this->nearClip; float Wnear = Hnear * this->aspectRatio; float Hfar = 2 * tan(this->fov * 2) * this->farClip; float Wfar = Hfar * this->aspectRatio; // Calculate Frustum corner XMVECTOR camPos = XMLoadFloat4(&amp;amp;amp;amp;amp;amp;this->camPosition); XMVECTOR camTa = XMLoadFloat4(&amp;amp;amp;amp;amp;amp;this->camForward); camTa = XMVector4Normalize(camTa); XMVECTOR up = XMLoadFloat4(&amp;amp;amp;amp;amp;amp;this->camUp); XMVECTOR right = XMLoadFloat4(&amp;amp;amp;amp;amp;amp;this->camRight); XMVECTOR nc = camPos + camTa * this->nearClip; corner[0] = nc + (up * Hnear/2) + (right * Wnear/2); corner[1] = nc + (up * Hnear/2) - (right * Wnear/2); corner[2] = nc - (up * Hnear/2) - (right * Wnear/2); corner[3] = nc - (up * Hnear/2) + (right * Wnear/2); XMVECTOR fc = camPos + camTa * this->farClip; corner[4] = fc + (up * Hfar/2) + (right * Wfar/2); corner[5] = fc + (up * Hfar/2) - (right * Wfar/2); corner[6] = fc - (up * Hfar/2) - (right * Wfar/2); corner[7] = fc - (up * Hfar/2) + (right * Wfar/2); for(int i = 0; i < 8; i++) { XMStoreFloat3(&amp;amp;amp;amp;amp;amp;frustum[i], corner[i]); } }

Having read how the xna function BoundingFrustum.GetCorner works, this "should" be correct. Meaning the near plane is from [0-3] and is defined clockwise starting from the top left from the positive Z axis looking at the origin. So basically I'm defining them counter-clockwise.

Same with the far plane [4-7].

And this is how I've translated his "CalculateFrustum" function:

void DirectionalLight::CalculateLightProjection(BaseCamera* camera) { XMMATRIX CamView, CamProj; XMMATRIX LightView, LightProj; CamView = camera->GetViewMatrixNonTransposed(); CamProj = camera->GetProjectionMatrixNonTransposed(); XMFLOAT3 _cornerWS[8]; camera->ExtractFrustumCorner(_cornerWS); XMVECTOR cornerWS[8]; XMVECTOR cornerVS[8]; XMVECTOR cornerLS[8]; // Find the centroid XMVECTOR frustumCentroid = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f); for(short i = 0; i < 8; i++) { // Initialize XMVECTOR because of the w component cornerWS[i] = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f); cornerVS[i] = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f); cornerLS[i] = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f); cornerWS[i] = XMLoadFloat3(&amp;amp;amp;amp;amp;amp;_cornerWS[i]); cornerWS[i] = XMVectorSetW(cornerWS[i], 1.0f); frustumCentroid += cornerWS[i]; XMStoreFloat3(&amp;amp;amp;amp;amp;amp;this->FrustumCornerWS[i], cornerWS[i]); } frustumCentroid /= 8; frustumCentroid = XMVectorSetW(frustumCentroid, 1.0f); // Calculate view space corner for(int i = 0; i < 8; i++) { cornerVS[i] = XMVector4Transform(cornerWS[i], CamView); XMStoreFloat3(&amp;amp;amp;amp;amp;amp;this->FrustumCornerVS[i], cornerVS[i]); } // Save far frustum corner for(int i = 0; i < 4; i++) { this->farFrustumCornerVS[i] = this->FrustumCornerVS[i + 4]; } // Position the shadow caster camera so that it's looking at the centroid // and backed up in the direction of the sunlight XMVECTOR temp = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f); temp = XMVectorSubtract(cornerVS[5], cornerVS[4]); temp = XMVector3Length(temp); float frustumDistance = 0.0f; XMStoreFloat(&amp;amp;amp;amp;amp;amp;frustumDistance, temp); float distFromCentroid = max(camera->GetFarClip() - camera->GetNearClip(), frustumDistance + 50.0f); // Create View Matrix XMVECTOR LightDir = XMLoadFloat3(&amp;amp;amp;amp;amp;amp;this->direction); LightDir = XMVectorSetW(LightDir, 1.0f); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f); XMVECTOR EyePosition = frustumCentroid - (LightDir * distFromCentroid); EyePosition = XMVectorSetW(EyePosition, 1.0f); XMVECTOR EyeTarget = frustumCentroid; LightView = XMMatrixLookAtLH(EyePosition, EyeTarget, up); XMStoreFloat4x4(&amp;amp;amp;amp;amp;amp;this->View, LightView); // Determine the position of the frustum corner in light space for(int i = 0; i < 8; i++) { cornerLS[i] = XMVector4Transform(cornerWS[i], LightView); XMStoreFloat3(&amp;amp;amp;amp;amp;amp;this->FrustumCornerLS[i], cornerLS[i]); } // Calculate an orthographics projection by sizing a bounding box // to the frustum coordinates in light space XMFLOAT3 mins = this->FrustumCornerLS[0]; XMFLOAT3 maxes = this->FrustumCornerLS[0]; for(int i = 0; i < 8; i++) { if(this->FrustumCornerLS[i].x > maxes.x) { maxes.x = this->FrustumCornerLS[i].x; } else if(this->FrustumCornerLS[i].x < mins.x) { mins.x = this->FrustumCornerLS[i].x; } if(this->FrustumCornerLS[i].y > maxes.y) { maxes.y = this->FrustumCornerLS[i].y; } else if(this->FrustumCornerLS[i].y < mins.y) { mins.y = this->FrustumCornerLS[i].y; } if(this->FrustumCornerLS[i].z > maxes.z) { maxes.z = this->FrustumCornerLS[i].z; } else if(this->FrustumCornerLS[i].z < mins.z) { mins.z = this->FrustumCornerLS[i].z; } } // Create an orthographic camera for use as a shader caster const float nearClipOffset = 100.0f; LightProj = XMMatrixOrthographicOffCenterLH(mins.x, maxes.x, mins.y, maxes.y, -maxes.z - nearClipOffset, -mins.z); XMStoreFloat4x4(&amp;amp;amp;amp;amp;amp;this->Projection, LightProj); }

Any help would be appreciated :/

edit: Not sure if it tells you anything but here's some results (or rather inputs) I'm getting for the matrices:

EyePosition = {1, -992,522, 821,255, 1334,95}

EyeTarget = {1, 34,3589, -0,25, 0}

EyeUp = {1, 0, 1, 0}

mins = {x=-269.78665 y=-407.21408 z=1424.8429 }

maxes = {x=325.82809 y=426.11288 z=2361.6660 }

And my camera setup is:

nearClip = 0.01f

farClip = 100.0f

FoV = 55 (degree)

AspectRatio = 1280/720

### #7lipsryme

Posted 21 November 2012 - 09:47 AM

Hey guys I was looking at mjp's deferred shadow mapping sample (http://mynameismjp.wordpress.com/samples-tutorials-tools/deferred-shadow-maps-sample/) and have been trying to convert this to my c++ dx11 based engine.

The issues I'm having are with the creation of the view and the orthogonal projection matrix for the directional light.

Basically what I'm getting as the depth output is nothing.

I've tried some custom defined view and projection matrices and can confirm they work so the only problem is the "CalculateFrustum" part.

This is what I have so far:

Here's how I calculate my frustum corner:

Having read how the xna function BoundingFrustum.GetCorner works, this "should" be correct. Meaning the near plane is from [0-3] and is defined clockwise starting from the top left from the positive Z axis looking at the origin. So basically I'm defining them counter-clockwise.

Same with the far plane [4-7].

And this is how I've translated his "CalculateFrustum" function:

Any help would be appreciated :/

edit: Not sure if it tells you anything but here's some results (or rather inputs) I'm getting for the matrices:

EyePosition = {1, -992,522, 821,255, 1334,95}

EyeTarget = {1, 34,3589, -0,25, 0}

EyeUp = {1, 0, 1, 0}

mins = {x=-269.78665 y=-407.21408 z=1424.8429 }

maxes = {x=325.82809 y=426.11288 z=2361.6660 }

And my camera setup is:

nearClip = 0.01f

farClip = 100.0f

FoV = 55 (degree)

AspectRatio = 1280/720

The issues I'm having are with the creation of the view and the orthogonal projection matrix for the directional light.

Basically what I'm getting as the depth output is nothing.

I've tried some custom defined view and projection matrices and can confirm they work so the only problem is the "CalculateFrustum" part.

This is what I have so far:

Here's how I calculate my frustum corner:

void ExtractFrustumCorner(XMFLOAT3* frustum) { XMVECTOR corner[8]; // Calculate Width &amp;amp;amp;amp;amp; Height of near and far plane float Hnear = 2 * tan(this->fov / 2) * this->nearClip; float Wnear = Hnear * this->aspectRatio; float Hfar = 2 * tan(this->fov * 2) * this->farClip; float Wfar = Hfar * this->aspectRatio; // Calculate Frustum corner XMVECTOR camPos = XMLoadFloat4(&amp;amp;amp;amp;amp;this->camPosition); XMVECTOR camTa = XMLoadFloat4(&amp;amp;amp;amp;amp;this->camForward); camTa = XMVector4Normalize(camTa); XMVECTOR up = XMLoadFloat4(&amp;amp;amp;amp;amp;this->camUp); XMVECTOR right = XMLoadFloat4(&amp;amp;amp;amp;amp;this->camRight); XMVECTOR nc = camPos + camTa * this->nearClip; corner[0] = nc + (up * Hnear/2) + (right * Wnear/2); corner[1] = nc + (up * Hnear/2) - (right * Wnear/2); corner[2] = nc - (up * Hnear/2) - (right * Wnear/2); corner[3] = nc - (up * Hnear/2) + (right * Wnear/2); XMVECTOR fc = camPos + camTa * this->farClip; corner[4] = fc + (up * Hfar/2) + (right * Wfar/2); corner[5] = fc + (up * Hfar/2) - (right * Wfar/2); corner[6] = fc - (up * Hfar/2) - (right * Wfar/2); corner[7] = fc - (up * Hfar/2) + (right * Wfar/2); for(int i = 0; i < 8; i++) { XMStoreFloat3(&amp;amp;amp;amp;amp;frustum[i], corner[i]); } }

Having read how the xna function BoundingFrustum.GetCorner works, this "should" be correct. Meaning the near plane is from [0-3] and is defined clockwise starting from the top left from the positive Z axis looking at the origin. So basically I'm defining them counter-clockwise.

Same with the far plane [4-7].

And this is how I've translated his "CalculateFrustum" function:

void DirectionalLight::CalculateLightProjection(BaseCamera* camera) { XMMATRIX CamView, CamProj; XMMATRIX LightView, LightProj; CamView = camera->GetViewMatrixNonTransposed(); CamProj = camera->GetProjectionMatrixNonTransposed(); XMFLOAT3 _cornerWS[8]; camera->ExtractFrustumCorner(_cornerWS); XMVECTOR cornerWS[8]; XMVECTOR cornerVS[8]; XMVECTOR cornerLS[8]; // Find the centroid XMVECTOR frustumCentroid = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f); for(short i = 0; i < 8; i++) { // Initialize XMVECTOR because of the w component cornerWS[i] = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f); cornerVS[i] = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f); cornerLS[i] = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f); cornerWS[i] = XMLoadFloat3(&amp;amp;amp;amp;amp;_cornerWS[i]); cornerWS[i] = XMVectorSetW(cornerWS[i], 1.0f); frustumCentroid += cornerWS[i]; XMStoreFloat3(&amp;amp;amp;amp;amp;this->FrustumCornerWS[i], cornerWS[i]); } frustumCentroid /= 8; frustumCentroid = XMVectorSetW(frustumCentroid, 1.0f); // Calculate view space corner for(int i = 0; i < 8; i++) { cornerVS[i] = XMVector4Transform(cornerWS[i], CamView); XMStoreFloat3(&amp;amp;amp;amp;amp;this->FrustumCornerVS[i], cornerVS[i]); } // Save far frustum corner for(int i = 0; i < 4; i++) { this->farFrustumCornerVS[i] = this->FrustumCornerVS[i + 4]; } // Position the shadow caster camera so that it's looking at the centroid // and backed up in the direction of the sunlight XMVECTOR temp = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f); temp = XMVectorSubtract(cornerVS[5], cornerVS[4]); temp = XMVector3Length(temp); float frustumDistance = 0.0f; XMStoreFloat(&amp;amp;amp;amp;amp;frustumDistance, temp); float distFromCentroid = max(camera->GetFarClip() - camera->GetNearClip(), frustumDistance + 50.0f); // Create View Matrix XMVECTOR LightDir = XMLoadFloat3(&amp;amp;amp;amp;amp;this->direction); LightDir = XMVectorSetW(LightDir, 1.0f); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f); XMVECTOR EyePosition = frustumCentroid - (LightDir * distFromCentroid); EyePosition = XMVectorSetW(EyePosition, 1.0f); XMVECTOR EyeTarget = frustumCentroid; LightView = XMMatrixLookAtLH(EyePosition, EyeTarget, up); XMStoreFloat4x4(&amp;amp;amp;amp;amp;this->View, LightView); // Determine the position of the frustum corner in light space for(int i = 0; i < 8; i++) { cornerLS[i] = XMVector4Transform(cornerWS[i], LightView); XMStoreFloat3(&amp;amp;amp;amp;amp;this->FrustumCornerLS[i], cornerLS[i]); } // Calculate an orthographics projection by sizing a bounding box // to the frustum coordinates in light space XMFLOAT3 mins = this->FrustumCornerLS[0]; XMFLOAT3 maxes = this->FrustumCornerLS[0]; for(int i = 0; i < 8; i++) { if(this->FrustumCornerLS[i].x > maxes.x) { maxes.x = this->FrustumCornerLS[i].x; } else if(this->FrustumCornerLS[i].x < mins.x) { mins.x = this->FrustumCornerLS[i].x; } if(this->FrustumCornerLS[i].y > maxes.y) { maxes.y = this->FrustumCornerLS[i].y; } else if(this->FrustumCornerLS[i].y < mins.y) { mins.y = this->FrustumCornerLS[i].y; } if(this->FrustumCornerLS[i].z > maxes.z) { maxes.z = this->FrustumCornerLS[i].z; } else if(this->FrustumCornerLS[i].z < mins.z) { mins.z = this->FrustumCornerLS[i].z; } } // Create an orthographic camera for use as a shader caster const float nearClipOffset = 100.0f; LightProj = XMMatrixOrthographicOffCenterLH(mins.x, maxes.x, mins.y, maxes.y, -maxes.z - nearClipOffset, -mins.z); XMStoreFloat4x4(&amp;amp;amp;amp;amp;this->Projection, LightProj); }

Any help would be appreciated :/

edit: Not sure if it tells you anything but here's some results (or rather inputs) I'm getting for the matrices:

EyePosition = {1, -992,522, 821,255, 1334,95}

EyeTarget = {1, 34,3589, -0,25, 0}

EyeUp = {1, 0, 1, 0}

mins = {x=-269.78665 y=-407.21408 z=1424.8429 }

maxes = {x=325.82809 y=426.11288 z=2361.6660 }

And my camera setup is:

nearClip = 0.01f

farClip = 100.0f

FoV = 55 (degree)

AspectRatio = 1280/720