Jump to content
  • Advertisement
Sign in to follow this  

Shadow Mapping Perspective Projection

This topic is 2108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I have a problem with rendering shadows. If I created orthographics projection matrix or perspective projection matrix, it works , shadows are created, but it causes that in shadows are lighter places. They showing in random positions and it is really unpleasant(specially in moving, or with rotating lights). Here is a picture:




Here is  code for creating lights:

void Lights::Build()
	XMVECTOR Direction = XMLoadFloat3(&m_DirectionalLight.Direction);
	XMVECTOR Position = -1.f * m_BoundingScene.Radius * Direction;
	XMVECTOR Target = XMLoadFloat3(&m_BoundingScene.Center);
	XMVECTOR Up = XMVectorSet(0.f, 1.f, 0.f, 0.f);

	XMMATRIX V = XMMatrixLookAtLH(Position, Target, Up);

	XMFLOAT3 CenterOfTheSceneLS;
	XMStoreFloat3(&CenterOfTheSceneLS, XMVector3TransformCoord(Target, V));

	float l = CenterOfTheSceneLS.x - m_BoundingScene.Radius;
	float r = CenterOfTheSceneLS.x + m_BoundingScene.Radius;
	float b = CenterOfTheSceneLS.y - m_BoundingScene.Radius;
	float t = CenterOfTheSceneLS.y + m_BoundingScene.Radius;
	float n = CenterOfTheSceneLS.z - m_BoundingScene.Radius;
	float f = CenterOfTheSceneLS.z + m_BoundingScene.Radius;

	//XMMATRIX P = XMMatrixOrthographicOffCenterLH(l, r, b, t, n, f);          

	//XMMATRIX P = XMMatrixPerspectiveOffCenterLH(l, r, b, t, n, f);                 // Result is the same here

	XMMATRIX P = XMMatrixPerspectiveFovLH(0.5f * XM_PI, 1.f, 10.f, 400.f);             // and here

	XMMATRIX T = XMMatrixSet(
		0.5f, 0.f, 0.f, 0.f,
		0.f, -0.5f, 0.f, 0.f,
		0.f, 0.f, 1.f, 0.f,
		0.5f, 0.5f, 0.f, 1.f);
	XMMATRIX ShadowTransform = XMMatrixMultiply(XMMatrixMultiply(V, P), T);

	XMStoreFloat4x4(&m_View, V);
	XMStoreFloat4x4(&m_Proj, P);
	XMStoreFloat4x4(&m_ShadowTransform, ShadowTransform);

Here is shader code for sampling shadow map:

static const float SMAP_SIZE = 2048.f;
static const float SMAP_DX = 1.f / SMAP_SIZE;

float ComputePCF(SamplerComparisonState comparisonState, Texture2D ShadowMap, float4 ShadowPosH)
	ShadowPosH.xyz /= ShadowPosH.w;

	float2 Offsets[9] = {
		float2(-SMAP_DX, SMAP_DX), float2(0.f, SMAP_DX), float2(SMAP_DX, SMAP_DX),
		float2(-SMAP_DX, 0.f), float2(0.f, 0.f), float2(SMAP_DX, 0.f),
		float2(-SMAP_DX, -SMAP_DX), float2(0.f, -SMAP_DX), float2(SMAP_DX, -SMAP_DX)

	float PercList = 0.f;
	for (int i = 0; i < 9; i++)
		PercList += ShadowMap.SampleCmpLevelZero(comparisonState, ShadowPosH.xy + Offsets[i], ShadowPosH.z).r;
	return PercList / 9.f;

I tried different bias(or none), different samplers and different positions but nothing works. Thanks for advice







Problem solved, if someone has same problem, here is solution:


Problem with rendering shadows caused tessellation, more precisely tessellation factors.I've computed a tessellation factor inside Vertex Shader(based on distance to the camera). In the Hull shader I transfer tessellation factors from Vertices to the edges(through average = (A + B) * 0.5f), but I've had a bad order:

// Bad

pt.EdgeTessFactor[0] = 0.5f * (Patch[2].TessFactor + Patch[0].TessFactor);
pt.EdgeTessFactor[1] = 0.5f * (Patch[0].TessFactor + Patch[1].TessFactor);
pt.EdgeTessFactor[2] = 0.5f * (Patch[1].TessFactor + Patch[2].TessFactor);
pt.InsideTessFactor = pt.EdgeTessFactor[0];

// This one Works

pt.EdgeTessFactor[0] = 0.5f * (Patch[1].TessFactor + Patch[2].TessFactor);
pt.EdgeTessFactor[1] = 0.5f * (Patch[2].TessFactor + Patch[0].TessFactor);
pt.EdgeTessFactor[2] = 0.5f * (Patch[0].TessFactor + Patch[1].TessFactor);
pt.InsideTessFactor = pt.EdgeTessFactor[0];

I don't know exactly why first one didn't work and second one yes(if someone can explain why, I will be pleasured).

Edited by Xperience

Share this post

Link to post
Share on other sites
Funny this has an impact on the shadow. I rather expect cracks in the mesh for wrong factors (which I don't see). Anyway: Which edge is which when tesselating ?.

Additional recommendation: Use average (or min or max) of the edge factors for the inside tesselation factor, not just the value from one edge.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!