Thursday, 3 July 2014

Custom GLSL shaders in BGE: View independent reflections

Reflections are an important part of creating realistic materials, unfortunately they are difficult to render in realtime. The specular component is a vague approximation of a light reflection but for metals or glossy surfaces it isn't really good enough.
Using raytracing is an elegant solution, however it is beyond the abilities of modern graphics cards to render accurate realtime reflections with this method and maintain playable framerates.

Thus we get image based techniques for rendering realtime reflections. There are 3 main techniques for displaying reflections, sphere mapping, dual paraboloid mapping and cube mapping.

Sphere mapping is the simplest method of generating reflections, it is also the easiest to use in the blender game engine as it is natively supported by the material editor.


A sphere mapped Suzanne
The main drawback of sphere mapping is the fact that it's view dependent, meaning, whatever angle you view the object from, the reflection will always be the same.

This is where view independent reflections come in!

Two techniques exist to render view independent reflections, the current industry standard method is cube mapping, but there is also a technique named dual paraboloid mapping.

Dual paraboloid mapped Suzanne's
The benefit of using dual paraboloid mapping is that you only need two textures to achieve the effect, rather than the six required for cube mapping.


2 Textures representing the reflection on an upper and lower paraboloid
Dual paraboloids do have disadvantages compared to cube maps, they are more prone to distortion and are less well used so resources on them are limited, if you are interested in learning more, these are the resources I used when implementing them:
Dual paraboloid reflections - Kyle Hayward
The art of texturing using the opengl shading language - Environment Mapping - Jerome Guinot

Finally we have cube mapping, as previously mentioned cube mapping is the most common technique you will see for displaying image based reflections. Compared to dual paraboloid maps, cube maps have less distortion, a more consistent pixel density and are also easier to generate.

Cube mapping

Cube maps do however require 6 separate images making them the most expensive to render in realtime. Blender has support for cubemaps (via custom GLSL shaders), it does however have a few issues with it's implementation the most notable being the inability to change the cube map with bge.texture (as far as I can tell).

A blender cube map
Information on cube maps can be found everywhere, here is a link to the site I used to create the shader. There is also many advanced techniques that can be used to enhance cube maps, notably these include box projected cube environment mapping. Remember me and Ryse son of rome also used a technique called Parallax corrected cube mapping, the details of that went a little over my head however!

===================== File Download ========================

This was quite a broad overview of these techniques, there is plenty more to discover if this interested you.

Again, there will be a post up in the coming weeks continuing on from this.