Wednesday, March 6, 2013

Global Illumination...






Shedding some light

Nowadays games in the current market are strewn with both direct and indirect light in an impressive show of graphics capabilities without much negative consequence to the framerate or graphics driver. Global Illumination, especially Radiosity is one of the most prominent techniques in accomplishing this task using special algorithms to calculate more realistic lighting. This blog is intended to explain the concept of Global Illumination and Radiosity, how they work, where they're used and how they're used. The information in this blog  can be traced back to a Siggraph talk on CryENGINE 3 in 2010 which can be found here as well as a presentation on Global Illumination presented at Gamefest 2008 found here



Let's talk definitions

Global Illumination is an integral part of most next-generation games and game engines, but what is it? Global Illumination is a technique that encompasses several algorithms meant to add realistic lighting to 3D scenes. The algorithms used for this kind of illumination take into account both direct and indirect sources of light. This means they focus on the light coming directly from light sources as well as the light bouncing off surfaces in the scene. Global Illumination includes reflections, refractions and shadows as every object affects the rendering of other objects. This graphical technique causes images to appear more photorealistic with the downside of being more computationally expensive and consequently slower to generate. 

Radiosity is the method of computing the global illumination of a scene and storing the information within the geometry. This will then generate images from different viewpoints without having to go through expensive lighting calculations. Typical radiosity methods only account for paths which leave a light source and are reflected diffusely before hitting the eye. Radiosity calculations are viewpoint independent which increases the computations involved, but makes them useful for all viewpoints. 

The reason Global Illumination has come around as a prominent graphical benchmark is due to the commonly held belief that inherently, it is very important to provide a plausible picture to a gamer in order to deliver a better gaming experience. 





Before and after

Ever since the first 3D games came out, game developers have recycled the formula for lighting up their levels. They begin by making their level's geometry and textures, adding their static lights (sun, lamp lights, flickering bulbs, etc.) and baking which includes all the rays of light projecting out and bouncing around all unchanging objects and world geometry. The specularity of the materials is used to judge the amount of bounces each ray completes before disappearing. This means that the amount of rays populating a given area generally determine how bright it will be. Once these calculations are finally computed, your level is fully lit and ready to go. 

Unfortunately, this method slows down productivity due to the time it takes to adjust a light and then re-bake all your lights onto complex maps. This can take anywhere from a few minutes to a few hours. Keep in mind, however, that the game will still look great though the concept behind it all is clunky and inefficient. 

As a result, games have modernized this technique by performing their calculations in real-time. This allows game developers to quickly change lighting conditions and see the results of their adjustments instantly. The light transfers effects like reflection, refraction, caustics, occlusion and scattering without a drop in the frame rate. Later we will take a look into how CryENGINE 3 manages to do this successfully. 




At the end of the day, Global Illumination adds extra detail and demonstrates a better understanding of 3D shapes and of the distance between objects in the scene being rendered. Various techniques used for creating Global Illumination include ray tracing, path tracing, photon tracing, radiosity (discussed earlier), photon mapping and instant radiosity. This translates to static light maps and shadows, irradiance environment maps, radiosity maps, ambient occlusion (AO), screen-space ambient occlusion (SSAO), real-time radiosity, etc. It is important to keep in mind that Global Illumination is a complex problem. It not only has long iteration times to build content but is also expensive in terms of money, CPU and GPU processing, memory, disk space and streaming. However, the trade-off is that it is non-trivial to implement and could be a differentiating factor for games in the eyes of gamers. Due to this, Global Illumination is already a standard for graphics in next-gen video games. 


CryENGINE 3 

So let's talk about the core idea behind CryENGINE 3's lighting. Imagine the primary light emanating light rays and assume the whole scene consists of only diffuse surfaces. Each ray then excites a secondary emission of bounced radiance along a visible hemisphere of surface element. A regular grid is now introduced to approximate the bounced radiance. All the bounced results inside each cell are accumulated into that cell and we have an initial accumulated indirect radiance distribution. After this the radiance is iteratively propagated around the 3D grid until the light passes the entire grid. This approach uses many lights for approximating reflected lights and use sampled lit surfaces to initialize the 3D grid with the initial lighting distribution. A few bands of SH basis are used to approximate lighting in angular space. This iterative propagation approach lowers down the rendering complexity of many secondary lights. 

This may all seem overwhelming and complex but is only a small portion of the idea behind the larger technique in which point-based global illumination is used within CryENGINE 3. 

The 3.0 version Crytek's engine is a big improvement over version 2.0 in everything from visuals and characters to physics and performance. One of the welcome features is of course Global Illumination as demonstrated below. 






A few words on SSAO

Ambient occlusion is an approximation of the amount by which a point on a surface is occluded by the surrounding geometry. This, therefore, allows the simulation of proximity shadows seen in the corners of rooms and narrow spaces between objects within games. The technique itself is subtle but manages to dramatically improve the visual realism of a computer-generated scene. Let's discuss the basic idea behind this method of making games appear more realistic. 

An occlusion factor is computed for each point on a surface and incorporated into the lighting model by modulating the ambient term to make it so that more occlusion yields less light and vice versa for less occlusion. This computation can be expensive. Offline renderers compute the occlusion factor by casting a large number of rays in a normal-oriented hemisphere to sample occluding geometry around a point. But this is not practical for realtime rendering. So to optimize computation of the occlusion factor, we might want to pre-calculate it. The downfall of this approach is that it limits how dynamic a scene can be since lights may move around but geometry can not. 





Crytek managed to implement a realtime solution sampling the depth buffer at points derived from samples of a sphere. They begin by projecting each sample point into screen space to get the coordinates into the depth buffer. They then sample the depth buffer and if the sample position is behind the sampled depth (inside the geometry), it contributes to the occlusion factor. This makes the quality of the results directly proportional to the number of samples taken. Here's is where we see the trade-off between performance and graphical prowess. 

Reducing the number of samples produces 'banding' artifacts while increasing this number produces a much better looking result with expense to the performance. To settle, a random rotation of the sample kernel at each pixel seems to trade the banding for a high frequency noise which can be blurred to eliminate the artifact. Over the years, of course, this method has been enhanced time and time again. To find out more about implementing SSAO, including real code visit John Chapman's Graphics Blog.






In Conclusion

Ever since good ol' 3D games like Wolfenstein and Duke Nukem, realistic lighting has been a primary goal for game developers. As next generation graphic cards give developers the power to pursue their wildest dreams in realizing real-time lighting computations, there continue to be widely used industry benchmark techniques such as those demonstrated with CryENGINE 3. There is always room for improvement and enhancements are promised with every next-gen consoles. Either graphic designers get more efficient with their algorithms or graphical performance boundaries are pushed by the hardware. The fact of the matter remains that it isn't too long till we see a solid and viable implementation of raycasting and more advanced techniques used for Global Illumination. The new frontier is simply over the horizon. 









1 comment: