Understanding of a 'light scattering'

Hi, guys.

I try to understand ‘light scattering’ technique.

The several questions abount 1st frame (sun + black occluders):

  • What color is it using for empty fragments (positions that are out of sun and out of all occluders)?
  • Sun haves a white color(1,1,1,1) and occlures have a black(0,0,0,1). Am I right?

Thanks for any help.
Have a good day!

I solved problem with colors.
But Now I’m trying to calculate sun position in screen space.

To add vector ‘to sun direction’ with different lengths to camera position is not working.
When I do not move camera, but just rotate it, the sun is moving in the sky.

You can see that on screenshots: the ‘camera pos’ is the same, ‘camera dir’ is different:

It seems the math for sun rendering is not so simple as I thought before.

What I do wrong?


Choose the sun position (i.e. direction) in world space and give it a W component equal to zero. Then apply the usual transformations: model-view matrix, projection matrix, division by W, viewport transformation. Note that NDC Z will be greater than 1 (unless the far plane is at infinity), so you can’t just feed it to OpenGL as a normal object as it will get clipped out by the far plane.

If you want to render the sun using OpenGL, you can’t just treat it like a normal part of the scene. If you pick a point that’s closer than infinity, then it’s going to move if the camera position moves. If you pick a point at infinity it’s going to be clipped out by the far plane (unless your far distance is at infinity, and maybe even then). The usual solution is to have a separate model-view matrix for rendering the skybox (which needn’t be a literal box); this has the same rotation as the real model-view matrix but no translation, so the origin is always at the view position. You still need to pick an appropriate depth so that the sun is behind everything but the skybox.

Thank you!
I just forget divide it to w.
Now it is moving - well.

And I have another one problem with this technique: the ‘god rays’ is not smooth and have strange strips.

How can I fix it?

I’d assume that it’s the foliage that’s causing this.

One option is to apply some form of “despeckle” filter to the silhouette. Simply down-sampling it may work. Another is to use a filtered texture when rendering the trees into the silhouette (assuming that the silhouette is rendered as a separate pass, not generated from the main render pass).