Ambient occlusion for terrain

I just committed some simple but effective ambient occlusion support to GeoMipTerrain. It’s simple to enable, isn’t very heavy on the GPU (it bakes the AO into the vertex colours) and requires a simple function call (terrain.calcAmbientOcclusion()) before you generate the terrain. It doesn’t use shaders.

A screenshot:

Looks nice :slight_smile:
May I ask why only GeoMipterrains though? If it works on vertex colors, I would guess it could be used on any NodePath, like egg terrains.

It’s a cheap technique that uses the heightmap image to calculate the ambient occlusion. It wouldn’t work for non-heightfield geometry. For that, you’d have to use SSAO, which is a bit similar in principle (but much more complex).

Oh OK.
I’ve had problems with SSAO before. It seems it’s only useful for indoor scenes as the effects “bleeds” to the skydome. Or maybe I just set it up wrong.

You can probably fix that by making sure the skybox doesn’t write to the depth buffer.

Not really, the sky consists of few layers, like the cloud layer is separate from the gradient.

It’s lovely! I have been putting off working on this for my terrain and i get to enjoy it anyways. :smiley: Is there a way to grab the ambient occlusion as a texture or PNMImage with python so I can combine it easily with my shader?

After calculating the ambient occlusion, use terrain.colorMap() to get a reference to the computed lightmap, as a PNMImage. Note that if you want to use the ambient occlusion in your shader, you can also just read out vtx_color in the shader, as the AO map is baked into the vertex colours.

Thanks! I’m not sure I can use this feature with my current shader though. My shader actually includes lighting calculations in order to add normal mapping. Actually if the engine is calculating lighting for the terrain even if it has a shader, that means my program is doing the same work twice?

I’m not sure what you mean. Ambient occlusion is only computed once, not every frame.

You can combine the vertex colour (which will just be a grayscale colour between black and white) with your existing lighting calculations to combine the ambient occlusion with your lighting model.

Ohhh. Ok. Good. That’s what I originally assumed when you said it was baked in. Sorry, I just second guessed myself out of paranoia about where and how it was getting these vertex colors outside of my shader. I guess my understanding of the renderer is still rather flimsy after all this time. :unamused: