Hello there. I am sorry that I have not written since long time ago, but I had to pass my exams ^^.

So, what is ambient occlusion?

There, we can see respictively : No ambient occlusion, occlusion map, and rendering with ambient occlusion.

So, Ambient occlusion allow to improve shadow in the scene.

Generally, Ambient Occlusion is defined by

Ohhhh, it’s a very difficult formula, with a strange integrale.

We are going to see how we can get one formula in this kind.

Firstly, ambient occlusion is a « ray tracing » technique. The idea behind ambient occlusion is, you launch many rays throughout the hemisphere oriented by normal

Obviously, we can say that one orthogonal ray to normal is not influent compared to one parallel ray, so, we can introduce a dot product between ray and normal into the integral. We do not perform this integral in all hemisphere, but only in the hemisphere’s surface.

I remind you that the infinitesimal surface of sphere is .

So, we can lay down that :

Where is the hemisphere oriented by and is the ray « launched » in the hemisphere, is the view function defined by

K is the constant as we have to compute, because ka have to be ranged between 0 and 1. So, now we can try to compute K, with the view function always to 1, because we compute the case with no occlusion is performed.

So, , so, we have the same expression of the first integral in the beginning of this article :

For people who like so much rigourous mathematics, I know it’s not very rigourous, but it’s with this « method » that we will compute our occlusion factor :).

If you prefer a more accurate technique, you can integrate in all Hemisphere (with the variable radius and take a view function who return one value between 0 and 1 according to the distance of occluder from the origin of ray) and, you get exactly the same formula cause you do one thing like this : with is from the View function to limit the return value from 0 to 1.

So, now, we can try to approximate this integral. We have two problems, we can’t perform « launching » infinite rays, so, we launch only few rays, and we have to use a « inverse » of view function

After that, we can blur the occlusion map to improve its rendering.

So we just use a simple blur.

#version 440 core /* Uniform */ #define CONTEXT 0 #define MATRIX 1 #define MATERIAL 2 #define POINT_LIGHT 3 #define MATRIX_SHADOW 4 layout(local_size_x = 256)in; layout(shared, binding = CONTEXT) uniform Context { uvec4 sizeScreenFrameBuffer; vec4 posCamera; mat4 invProjectionViewMatrix; }; layout(binding = 4) uniform sampler2D AO; layout(binding = 4, r32f) uniform image2D imageAO; void main(void) { float blur = texture(AO, vec2(gl_GlobalInvocationID.xy) / sizeScreenFrameBuffer.zw).x; for(int i = 4; i > 0; --i) blur += texture(AO, vec2((ivec2(gl_GlobalInvocationID.xy) + ivec2(-i, 0))) / sizeScreenFrameBuffer.zw).x; for(int i = 4; i > 0; --i) blur += texture(AO, vec2((ivec2(gl_GlobalInvocationID.xy) + ivec2(i, 0))) / sizeScreenFrameBuffer.zw).x; imageStore(imageAO, ivec2(gl_GlobalInvocationID.xy), vec4(blur / 9, 0, 0, 0)); }

Now, we have to code our ambient occlusion.

This picture is very good to understand how we can use our approximation.

Indeed, there, we can see that if the point is red, we have , so You just have to test the depth buffer to know if the point is occluder or no.

#version 440 core /* Uniform */ #define CONTEXT 0 #define MATRIX 1 #define MATERIAL 2 #define POINT_LIGHT 3 #define MATRIX_SHADOW 4 layout(shared, binding = CONTEXT) uniform Context { uvec4 sizeScreenFrameBuffer; vec4 posCamera; mat4 invProjectionViewMatrix; }; layout(local_size_x = 16, local_size_y = 16) in; layout(binding = 1) uniform sampler2D position; layout(binding = 2) uniform sampler2D normal; layout(binding = 3) uniform sampler2D distSquare; writeonly layout(binding = 4, r16f) uniform image2D AO; void main(void) { float ao = 0.0; const ivec2 texCoord = ivec2(gl_GlobalInvocationID.xy); const vec2 texCoordAO = vec2(texCoord) / sizeScreenFrameBuffer.zw; vec3 positionAO = texture(position, texCoordAO).xyz; vec3 normalAO = texture(normal, texCoordAO).xyz; float distSquareAO = texture(distSquare, texCoordAO).x; for(int j = -2; j < 3; ++j) { for(int i = -2; i < 3; ++i) { vec2 texCoordRay = vec2(texCoord + ivec2(i, j)) / sizeScreenFrameBuffer.zw; vec3 positionRay = texture(position, texCoordRay).xyz; float distSquareRay = texture(distSquare, texCoordRay).x; float c = dot(normalAO, normalize(positionRay - positionAO)); if(c < 0.0) c = -c; if(distSquareRay < distSquareAO) ao += c; } } imageStore(AO, texCoord, vec4((1 - ao / 25), 0.0, 0.0, 0.0)); }

Strangely, in this case, if I use shared memory, I have badder result than just texture. Maybe the allocation of shared memory is longer and it’s not very efficient here :-).

I advise you to use texture instead of imageLoad, indeed, I get 8 times performance better with texture ^^.

Bye :). The next time, we will talk about shadows !