VERC: Realistic Reflections in Real-Time Last edited 3 years ago2019-04-21 12:38:04 UTC

We're all suckers for eye-candy, myself included. A robot with a plain-coloured silver overcoat looks much less appealing than the same robot with a shiny, reflective overcoat - and if two games were identical bar some eye candy, we'd go for the one with better visuals. It's the way we're made - we want the best visual experience we can get. Of course, they can get in the way of gameplay, but that's an entirely different essay.

Reflections are one of the most eye-catching visual effects a game can have. They bring a whole new dimension to the environment - you can actually see other things reflecting off each other, and the reflections altering. It brings the sense of reality to a whole new level, because reflections do not feature heavily in the real world - most surfaces are dull and matte, and so are very bad reflectors.

Before a few years ago, there just wasn't the computational power available to perform reflections in real-time. It was too complex and wasn't critical enough to warrant the setting aside of a large chunk of processing time to compute it. The graphics card has changed all that, but it was only with the GeForce that the power for realistic real-time reflections was available - before then, it was all pre-defined environments that would reflect.

The best reflections are generated using ray tracing. This is a very computationally-intensive method of rendering highly realistic graphics - but at a very slow speed. A complex scene may take hours to render, and so it is for this reason that ray tracing is restricted to 3D modeling applications (for rendering of a model). In a nutshell, ray tracing shoots a ray out through each pixel of the screen and sees what the ray hits - what the ray hits determines the pixel's colour.

Reflections with ray tracing are done by reflecting the ray off the object that it hits. The colour of the object that this secondary ray hits is modulated with the first object's colour, depending on how reflective it is, and the colour is returned to the pixel. You don't have to stop at just a secondary ray though - you can have as many rays reflecting off each other as you want, but normally a limit is set as to how many generations of ray will be created (between 5 and 20 is a standard value).
Raytracing one pixel. The final colour will be between grey and red.Raytracing one pixel. The final colour will be between grey and red.
Real-time ray tracing does exist - however, it is only possible at very low resolutions (320x240) and with a limited number of objects in the scene if you want it to run at any real speed. There must be a way of getting fast reflections in real-time, mustn't there?

A method pioneered by Duke Nukem 3D, and used by other games such as Deus Ex and Unreal, is by exploiting the way the level is stored and rendered. These games use a system of portals, which define which parts of the level are connected to others. To create a mirror, all that is done is a portal is placed where the mirror should be which points back into the room. This simply causes the room to be rendered again as if it was another room past the mirror, giving a very nice mirror effect. Unfortunately, this method is limited to flat polygonal surfaces in the map - it is useless for moving objects, monsters, and so forth. So what else can we use? Enter environment mapping.

Environment mapping does exactly what it says on the tin - maps the environment onto objects, and very speedily, making it suitable for real-time applications. There are three types of environment mapping - spherical, dual paraboloid, and cubic. Dual paraboloid never really took off, and has the same limitations as spherical, so we won't be discussing it in this article, but spherical and cubic are the two big daddies of environment mapping.

Spherical environment mapping was the first to emerge, and is faster than cubic environment mapping. It works on the principle of having the object entirely enclosed by a sphere, which has an environment map applied to it. An environment map is a texture that shows what the object will reflect - it shows the environment. For every vertex on the sphere calculations are done, based on the current viewpoint and viewing direction, to find out what part of the spherical environment map should be mapped onto the object at that point. It's a very fast method, but has a lot of caveats (which will be discussed later).

Cubic environment mapping works in exactly the same way as spherical environment mapping, but uses a cube surrounding the object instead of a sphere.
Cubic environment mappingCubic environment mapping
There's one thing I haven't mentioned so far; how to get truly real-time reflections. It's all well and good having pre-defined environment maps for your objects, but that isn't TRULY real-time, is it? It looks good, certainly, and will suffice for many applications, but it doesn't fit our specification. This is where cubic mapping triumphs over spherical - cubic mapping allows the real-time generation of environment maps much more easily than spherical mapping does. Allow me to explain.

In spherical environment mapping, only one texture is used to define the environment map. Due to it having to be applied to an (invisible) sphere, it can't just be a straight texture - it has to be warped so that it can be applied properly. Unfortunately, this leads to lots of visual artifacts and a low-quality environment map, especially for the back surface of the sphere. The required distortion makes it very hard to generate the environment map with any real speed.

Cubic environment mapping, on the other hand, is much easier. Instead of using one texture, it uses six - one for each face of the cube - and due to this, they do not need to have any distortions. This retains texture quality and facilitates the real-time generation of new environment maps. All that needs to be done is the six views rendered from the centre of the reflective object, and these six views uploaded into the texture memory of the cube environment map.
A surface with cubic environment mapping applied.A surface with cubic environment mapping applied.
In this article, I've skimmed over a lot of the technical details to reveal the bare bones of the principles, and to enable us to achieve our goal - to show you how realistic reflections can be generated in real-time. That goal has been achieved, and generated cubic environment mapping is really taking off. Look out for it in the next game you buy - it's almost certainly sure to be there. I know I'll be using it.
This article was originally published on the Valve Editing Resource Collective (VERC).
TWHL only archives articles from defunct websites. For more information on TWHL's archiving efforts, please visit the TWHL Archiving Project page.


You must log in to post a comment. You can login or register a new account.