I'm implementing basic volumetric lighting using deferred shading (gbuffer + render to screen quad) and basic shadow mapping. So, I am sending the position texture and shadow map to a separate shader, applying ray-marching for every position and computing the scattering accumulation when is not in shadow. The question is , how do I know I am getting the right results? is it just by rendering the volumetric lighting texture to quad and look for colored patches in the scene? these are my results:
This is the code of my volumetric fragment shader:
uniform sampler2D positionMap;
uniform sampler2D shadowMap;
float NB_STEPS = 3.0f;
int NB_STEPS_INT = 3;
float G_SCATTERING = 0.7f;
out vec4 fragColor;
void main()
{
vec2 texCoord = vec2(gl_FragCoord.x/width,gl_FragCoord.y/height);
vec3 worldPosition = texture2D(positionMap,texCoord).xyz;
vec3 startPosition = ViewPos;
vec3 rayVector = worldPosition.xyz- startPosition;
float rayLength = length(rayVector);
vec3 rayDirection = normalize(rayVector);
float stepLength = rayLength / NB_STEPS;
vec3 step = rayDirection * stepLength;
vec3 currentPosition = startPosition;
vec3 accumFog = vec3(0.0f);
for(int i = 0; i < NB_STEPS_INT; i++)
{
bool shadow = ShadowCalculation(currentPosition);
if(!shadow)
{
accumFog += ComputeScattering(dot(rayDirection, light.position)) * light.diffuse;
//accumFog += light.diffuse;
}
currentPosition += step;
}
accumFog /= NB_STEPS;
fragColor = vec4(accumFog, 1);
}
I am computing the scattering factor like this:
float ComputeScattering(float lightDotView)
{
float result = 1.0f - G_SCATTERING * G_SCATTERING;
result /= (4.0f * PI * pow(1.0f + G_SCATTERING * G_SCATTERING - (2.0f * G_SCATTERING) * lightDotView, 1.5f));
return result;
}
At the end should I just blend the volumetric light buffer with the deferred shading final buffer?
I appreciate the help.