Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


CSM (based on nvidia's paper) swimming


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
10 replies to this topic

#1 csisy   Members   -  Reputation: 256

Like
0Likes
Like

Posted 28 June 2012 - 11:50 PM

Hi there

I know, there are some similar topics, but I don't understand the solution. I made the CSM based on this paper: http://developer.download.nvidia.com/SDK/10.5/opengl/src/cascaded_shadow_maps/doc/cascaded_shadow_maps.pdf
with some "improvements". Here is the code:

I have a matrix which is used when I create the shadows
   float texOffset = 0.5f + (0.5f / (float)shadowSize);
   float bias = 0.003f;
   textureMat = Matrix(0.5f, 0.0f, 0.0f, 0.0f,
	    0.0f, -0.5f, 0.0f, 0.0f,
	    0.0f, 0.0f, 1.0f, 0.0f,
	    texOffset, texOffset, -bias, 1.0f);
Now, here is the frustum corner's computing:
  void Shadow::ComputeFrustumCorners()
  {
   float& camAspect = scene->camera->aspect;
   Vector3& camEye = scene->camera->eyePosition;
   Vector3 dir;
   dir.x = scene->camera->GetView().m[0][2];
   dir.y = scene->camera->GetView().m[1][2];
   dir.z = scene->camera->GetView().m[2][2];
   Vector3 x;
   x.x = scene->camera->GetView().m[0][0];
   x.y = scene->camera->GetView().m[1][0];
   x.z = scene->camera->GetView().m[2][0];
   Vector3 y;
   y.x = scene->camera->GetView().m[0][1];
   y.y = scene->camera->GetView().m[1][1];
   y.z = scene->camera->GetView().m[2][1];
   tanHalfFov = tanf(scene->camera->fov * 0.5f);
   float nPlaneH = tanHalfFov * nearPlane;
   float nPlaneW = nPlaneH * camAspect;
   float fPlaneH = tanHalfFov * farPlane;
   float fPlaneW = fPlaneH * camAspect;
   Vector3 nPlaneX = x * nPlaneW;
   Vector3 nPlaneY = y * nPlaneH;
   Vector3 fPlaneX = x * fPlaneW;
   Vector3 fPlaneY = y * fPlaneH;
   nPlaneCenter = camEye + dir * nearPlane;
   fPlaneCenter = camEye + dir * farPlane;
   frustumCorners[0] = nPlaneCenter - nPlaneX - nPlaneY;
   frustumCorners[1] = nPlaneCenter - nPlaneX + nPlaneY;
   frustumCorners[2] = nPlaneCenter + nPlaneX + nPlaneY;
   frustumCorners[3] = nPlaneCenter + nPlaneX - nPlaneY;
   frustumCorners[4] = fPlaneCenter - fPlaneX - fPlaneY;
   frustumCorners[5] = fPlaneCenter - fPlaneX + fPlaneY;
   frustumCorners[6] = fPlaneCenter + fPlaneX + fPlaneY;
   frustumCorners[7] = fPlaneCenter + fPlaneX - fPlaneY;
  }
I get the x,y,z axes from the view matrix, so I don't have to compute these values.

And of course the "main part" of CSM; compute the viewproj matrix for the shadow map generation, and the "final matrix" for the "final" shadow rendering. I think I should modify this part to kill the swimming edges.
  void Shadow::ComputeMatrices()
  {
   ComputeFrustumCorners();
   view = Matrix::LookAt(Vector3::Zero, light->direction, Vector3::Up);
   Vector3 min = Vector3::Max;
   Vector3 max = Vector3::Min;
   Vector4 transformed;
   for (int i = 0; i < NUM_CORNERS; i++)
   {
    transformed = Vector4::Transform(frustumCorners[i], view);
    transformed /= transformed.w;
    if (transformed.x < min.x)
	 min.x = transformed.x;
    if (transformed.y < min.y)
	 min.y = transformed.y;
    if (transformed.z < min.z)
	 min.z = transformed.z;
    if (transformed.x > max.x)
	 max.x = transformed.x;
    if (transformed.y > max.y)
	 max.y = transformed.y;
    if (transformed.z > max.z)
	 max.z = transformed.z;
   }
   float scaleX = 2.0f / (max.x - min.x);
   float scaleY = 2.0f / (max.y - min.y);
   float offsetX = -0.5f * (min.x + max.x) * scaleX;
   float offsetY = -0.5f * (min.y + max.y) * scaleY;
   cropMat.m[0][0] = scaleX;
   cropMat.m[1][1] = scaleY;
   cropMat.m[2][2] = 1.0f;
   cropMat.m[3][0] = offsetX;
   cropMat.m[3][1] = offsetY;
   cropMat.m[3][3] = 1.0f;
   // INFO: bias
   float bias = 10.0f;
   min.z -= bias;
   max.z += bias;
   proj = Matrix::OrthographicOffCenter(-1, 1, -1, 1, min.z, max.z);
   viewProj = view * proj * cropMat;
   finalMat = viewProj * textureMat;
  }

So the problem is that, when I move or rotate the camera (not the light), the shadow edges are swimming / flickering. What is the solution for my code?

Thanks for all replies!

Attached Thumbnails

  • shadow.JPG

sorry for my bad english :)

Sponsor:

#2 MJP   Moderators   -  Reputation: 11736

Like
2Likes
Like

Posted 29 June 2012 - 12:23 PM

There's an article in ShaderX6 about stabilizing your cascades so that edges don't crawl as the camera moves, so if you have access to that you should give it a read. Otherwise I explained it in more detail in this thread.

The other option is to get your shadow filtering and resolution good enough so that you don't notice crawling edges, but doing this requires a combination of good shadow filtering techniques as well dynamic optimization of your cascade partitions based on what's visible to the camera.

#3 csisy   Members   -  Reputation: 256

Like
0Likes
Like

Posted 29 June 2012 - 01:01 PM

Thanks for the reply!

I put your code between the viewProj and the finalMat compute:

   proj = Matrix::OrthographicOffCenter(-1, 1, -1, 1, min.z, max.z);
   viewProj = view * proj * cropMat;
   Vector3 shadowOrigin = Vector3::Transform(Vector3::Zero, viewProj);
   shadowOrigin *= shadowHalfSize;
   Vector2 roundedOrigin = Vector2(Helpers::MathHelper::Round(shadowOrigin.x), Helpers::MathHelper::Round(shadowOrigin.y));
   Vector2 rounding = roundedOrigin - Vector2(shadowOrigin.x, shadowOrigin.y);
   rounding /= ((float)shadowSize * 0.5f);
   Matrix roundMatrix = Matrix::Translate(rounding.x, rounding.y, 0.0f);
   viewProj *= roundMatrix;
   finalMat = viewProj * textureMat;

I think it works, because the static meshes' shadow is not flickering (I think Posted Image), but the character (the player) moves together with the camera, and its shadow is still flickering.

This can be solved by the bounding sphere instead of light AABB?

EDIT:
I read your post, and I understand that we need a fix size for the projection (this kills the flickering when rotating the camera) and a little round to texels (which kills the flickering when the camera moves). So I rewrite my code based on your code and the idea:
   ComputeFrustumCorners();

   // spheres for each split

   Vector3 sphereCenter;
   for (int i = 0; i < NUM_CORNERS; i++)
   {
    sphereCenter += frustumCorners[i];
   }
   sphereCenter /= NUM_CORNERS;

   float sphereRadius = Vector3::Distance(sphereCenter, frustumCorners[0]);

   float nearClip = 1.0f;
   float backupDist = 10.0f + sphereRadius + nearClip;

   Vector3 camPos = sphereCenter - light->direction * backupDist;
   view = Matrix::LookAt(camPos, sphereCenter, Vector3::Up);

   float bounds = sphereRadius * 2.0f;
   float farClip = backupDist + sphereRadius;
   proj = Matrix::Orthographic(bounds, bounds, nearClip, farClip);
   viewProj = view * proj;

   // round to texel

   origin = Vector3::Transform(Vector3::Zero, viewProj);
   origin *= shadowHalfSize;

   originRounded.x = Helpers::MathHelper::Round(origin.x);
   originRounded.y = Helpers::MathHelper::Round(origin.y);

   rounding.x = originRounded.x - origin.x;
   rounding.y = originRounded.y - origin.y;
   rounding /= shadowHalfSize;

   roundingMatrix = Matrix::Translate(rounding.x, rounding.y, 0.0f);
   viewProj *= roundingMatrix;

   // compute final components

   finalMat = viewProj * textureMat;
   viewFrustum->Update(viewProj);

Is this code correct? If it is, there are some problem: my character (which moves with the camera) still flickering and the "backupDist" is really game and eyeposition relevant, so I can't hardcode it.

Or am I doing something wrong?
sorry for my bad english :)

#4 MJP   Moderators   -  Reputation: 11736

Like
2Likes
Like

Posted 29 June 2012 - 03:30 PM

If geometry moves in world space then it is still going to flicker, you can't fix that by stabilizing cascades.

As for the backup distance, it's true that it's very annoying but you'd have the same problem in your original implementation, which is that objects behind the projection's near clip plane will get clipped out and so you have to have some means of "pulling back" the minz value. In D3D10/D3D11 there is actually a really nice solution to this problem, which is that you can turn of Z clipping and then objects behind the near clip plane won't get clipped. However as far as I know there is no equivalent for D3D9, so you're kind of stuck. The best you can do is try to automatically determine the required min z distance based on the extents of your level geometry.

#5 csisy   Members   -  Reputation: 256

Like
0Likes
Like

Posted 30 June 2012 - 01:06 AM

Thanks for your reply!

So isn't there any solution for the moving objects? And I have to compute dinamically the min and max values depended on the scene. I'll use a space-partitioning, maybe I will be able to use it to determine the min and max values.

Now, my static objects aren't flickering, and your solution is faster than my (nvidia's) old one, so it is awesome! :) Thanks for your help!
sorry for my bad english :)

#6 csisy   Members   -  Reputation: 256

Like
0Likes
Like

Posted 30 June 2012 - 02:53 AM

Hm, it's strange. Yesterday, I didn't noticed this bug (see the pictures). I use this code now:

   sphereCenter = Vector3::Zero;
   for (int i = 0; i < NUM_CORNERS; i++)
   {
	sphereCenter += frustumCorners[i];
   }
   sphereCenter /= NUM_CORNERS;

   sphereRadius = Vector3::Distance(sphereCenter, frustumCorners[0]);
   sphereBounds = sphereRadius * 2.0f;

   float nearClip = 1.0f;
   float backupDist = 20.0f + nearClip + sphereRadius;
   float farClip = backupDist + sphereRadius;

   camPosition = sphereCenter - light->direction * backupDist;
   view = Matrix::LookAt(camPosition, sphereCenter, Vector3::Up);

   proj = Matrix::Orthographic(sphereBounds, sphereBounds, nearClip, farClip);
   viewProj = view * proj;

s1.JPG
s2.JPG

EDIT:
I compiled a Release from the code, here is the binaries, if someone would like to try it.
Attached File  ShadowTest.zip   432.02KB   55 downloads
sorry for my bad english :)

#7 MJP   Moderators   -  Reputation: 11736

Like
0Likes
Like

Posted 30 June 2012 - 11:35 AM

It occurred to me that if you can't turn of z clipping, then you might be able to just clamp your vertex position to the near plane in the vertex shader by setting the projected z coordinate to max(z, 0.0f). I'm not sure if it would cause problems since I haven't tried it or heavily thought it through, but I think it might work. Then you could just size the orthographic projection to the min and max of the frustum.

#8 csisy   Members   -  Reputation: 256

Like
0Likes
Like

Posted 30 June 2012 - 12:12 PM

I render the shadowmap to the screen, and it looks like the sphereBounds was too "small". And I find the problem, I was stupid Posted Image
When I computed the sphere's center, I get the radius from the d(center, corners[0]), which is the near-left-bottom corner of the frustum. The correct is the d(corners[0], corners[6]), where the 6th corner is the far-right-top corner.

sphereBounds = Vector3::Distance(frustumCorners[0], frustumCorners[6]);
sphereRadius = sphereBounds * 0.5f;

Now, it works really good. Thanks for your help, the topic is solved (for me).

Ps.:
Can I set the topic to solved state, or set your post as a solution?

EDIT:
I have to use another little hardcoded thing:
sphereBounds = Vector3::Distance(frustumCorners[0], frustumCorners[6]) + 5.0f;
because the camera sees the scene from top, and the sphereBounds wasn't big enough.

Edited by csisy, 30 June 2012 - 12:23 PM.

sorry for my bad english :)

#9 csisy   Members   -  Reputation: 256

Like
0Likes
Like

Posted 09 July 2012 - 01:38 AM

Hi again!

I've tested the shadows with a different camera view and I noticed that the shadowmap is maybe wrong. I uploaded a video yesterday which shows the problem:


When I turn the camera right the Sun should "looks" right too, but now the Sun "looks" left. And visa versa.

Here is the important code. Can anyone check it?

  Shadow::Shadow(Base::Game* _game, Game::Scene* _scene)
   : Renderer(_game, _scene)
  {
   vsShadowMap = 0;
   vsShadowMapAnim = 0;
   vsCombine = 0;

   psShadowMap = 0;
   psCombine = 0;

   rtShadowMap = 0;
   depthStencil = 0;
   depthStencilOld = 0;

   frustumCorners = 0;
   viewFrustum = new ViewFrustum();

   shadowSize = game->GetConfig()->GetShadowSize();
   bias = 0.001f;

   sphereBonus = 5.0f;
   backupBonus = 20.0f;
  }
  void Shadow::ComputeFrustumCorners()
  {
   float& camAspect = scene->camera->aspect;
   Vector3& camEye = scene->camera->eyePosition;

   Vector3 dir;
   dir.x = scene->camera->GetView().m[0][2];
   dir.y = scene->camera->GetView().m[1][2];
   dir.z = scene->camera->GetView().m[2][2];

   Vector3 x;
   x.x = scene->camera->GetView().m[0][0];
   x.y = scene->camera->GetView().m[1][0];
   x.z = scene->camera->GetView().m[2][0];

   Vector3 y;
   y.x = scene->camera->GetView().m[0][1];
   y.y = scene->camera->GetView().m[1][1];
   y.z = scene->camera->GetView().m[2][1];

   tanHalfFov = tanf(scene->camera->fov * 0.5f);

   float nPlaneH = tanHalfFov * nearPlane;
   float nPlaneW = nPlaneH * camAspect;
   float fPlaneH = tanHalfFov * farPlane;
   float fPlaneW = fPlaneH * camAspect;

   Vector3 nPlaneX = x * nPlaneW;
   Vector3 nPlaneY = y * nPlaneH;
   Vector3 fPlaneX = x * fPlaneW;
   Vector3 fPlaneY = y * fPlaneH;

   nPlaneCenter = camEye + dir * nearPlane;
   fPlaneCenter = camEye + dir * farPlane;

   frustumCorners[0] = nPlaneCenter - nPlaneX - nPlaneY;
   frustumCorners[1] = nPlaneCenter - nPlaneX + nPlaneY;
   frustumCorners[2] = nPlaneCenter + nPlaneX + nPlaneY;
   frustumCorners[3] = nPlaneCenter + nPlaneX - nPlaneY;

   frustumCorners[4] = fPlaneCenter - fPlaneX - fPlaneY;
   frustumCorners[5] = fPlaneCenter - fPlaneX + fPlaneY;
   frustumCorners[6] = fPlaneCenter + fPlaneX + fPlaneY;
   frustumCorners[7] = fPlaneCenter + fPlaneX - fPlaneY;
  }
  void Shadow::ComputeMatrices()
  {
   ComputeFrustumCorners();

   // search corners' center
   sphereCenter = Vector3::Zero;
   for (int i = 0; i < NUM_CORNERS; i++)
   {
	sphereCenter += frustumCorners[i];
   }
   sphereCenter /= NUM_CORNERS;

   // get sphere radius
   sphereBounds = Vector3::Distance(frustumCorners[0], frustumCorners[6]) + sphereBonus;
   sphereRadius = sphereBounds * 0.5f;

   // near, far and cameraPosition
   float nearClip = 1.0f;
   float backupDist = nearClip + sphereRadius + backupBonus;
   float farClip = backupDist + sphereRadius + sphereBonus;

   camPosition = sphereCenter - light->direction * backupDist;
   view = Matrix::CreateLookAt(camPosition, sphereCenter, Vector3::Up);

   proj = Matrix::CreateOrthographic(sphereBounds, sphereBounds, nearClip, farClip);
   viewProj = view * proj;

   // round to texel

   origin = Vector3::Transform(Vector3::Zero, viewProj);
   origin *= shadowHalfSize;

   originRounded.x = Helpers::MathHelper::Round(origin.x);
   originRounded.y = Helpers::MathHelper::Round(origin.y);

   rounding.x = originRounded.x - origin.x;
   rounding.y = originRounded.y - origin.y;
   rounding /= shadowHalfSize;

   roundingMatrix = Matrix::CreateTranslate(rounding.x, rounding.y, 0.0f);
   viewProj = viewProj * roundingMatrix;

   // compute final components

   finalMat = viewProj * textureMat;
   viewFrustum->Update(viewProj);
  }

I would be grateful if anyone could help me.

Edited by csisy, 09 July 2012 - 01:40 AM.

sorry for my bad english :)

#10 Zoner   Members   -  Reputation: 232

Like
1Likes
Like

Posted 12 July 2012 - 05:25 PM

Stabilizing the shadowmap requires a few steps:
  • Padding up the shadowmap by 1 additional texel than required, then translating the shadowmap projection by an offset so that the center texel is centered at all times. This will stop the crawling when you translate the camera.
  • Mapping the visible part of the view frustum to a sphere before projected that into a texture, will protect it from crawls caused by rotating the camera.
  • If your camera's field of view animates, the shadows will also crawl as the view frustum will dynamically make the fit sphere larger or smaller. This can be hidden by making taking the FOV and rounding it up into buckets of increments that affect the sphere fitting, or you can just live with it if you don't change the FOV more or at all. The bucket strategy will avoid the crawl but you will get pops when changing buckets instead. If you constrain the min and max FOV well enough you could probably use a single value and never see a pop.


#11 csisy   Members   -  Reputation: 256

Like
0Likes
Like

Posted 12 July 2012 - 11:43 PM

I don't change the FOV. :) The only shadow flickering comes on when I move an object (the character with the camera). When I disabled the rounding the result was better for moving objects, but for static objects (of course) the flickering comes back. So for moving objects the rounding is the "bad step".
sorry for my bad english :)




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.



PARTNERS