# God rays/light scattering problems.

## Recommended Posts

Hello!

I implemented the god rays algorith presented in GPU gems but I have a few questions, here the link of the article (http://http.developer.nvidia.com/GPUGems3/gpugems3_ch13.html).

How could I remove the rays when I'm not facing the sun? As the effect is being aplied if the sun is behind the view it gives weird results.

Right now, I'm checking the projected z value and if it is less than 0, I just dont perform the effect, this works OK but the change is really abrupt.

As I'm aplying the god rays to the sun, to get the sun in NDC space I'm doing the following math:

glm::vec3 sunEyePos = mCamera.GetPosition() + (mSunDirection);
glm::vec4 sunProj =  mCamera.Projection * mCamera.View * glm::vec4(sunEyePos, 1.0f);
sunProj /= sunProj.w;
sunProj = (sunProj + 1.0f) * 0.5f;


I'm a bit worried on how do I get the sun pos, the idea is to get a global light source , is that ok?

Thanks!

Edited by piluve

##### Share on other sites

You can just dot the vector towards the sun with the camera forward facing vector, and if that is negative it means the sun is behind you.

To avoid the harsh transition you can use the dot value of the normalized vectors as a fade value, eg.

float fade = saturate(dot3(normalize3(vectorToSun), normalize3(cameraForward)) * 16)

##### Share on other sites

You can just dot the vector towards the sun with the camera forward facing vector, and if that is negative it means the sun is behind you.

To avoid the harsh transition you can use the dot value of the normalized vectors as a fade value, eg.

float fade = saturate(dot3(normalize3(vectorToSun), normalize3(cameraForward)) * 16)

Hello!

Thanks for the tip, I'll give it a try ;)