A Gentle Introduction to the Rendering Equation
The equation that makes realistic rendering possible
I’ve been playing around with Blender lately, and it’s amazing how software can produce incredibly realistic images from 3d scenes.
Notice the effects in the image below. The ripples in the water, smoke coming out of the chimney, the glow from the fireplace in the cabin.
Lake House by kless, made with Blender.Blender has a powerful built-in renderer called Cycles that produced this image. The artist created the 3d models, specified their materials, added some details and lighting, and Cycles generated a 2d image.
What if you wanted to write your own renderer?
You'd need to first model the way that light moves in 3d space. You'd need a rendering equation.
The equation above is the famous Rendering Equation, which calculates the light leaving a point on an object in a certain direction.
We'll first get a feel for how this works, and then go over it in detail.
Why is the wall orange?
Let's take a deeper look at one part of the image above- the glow from the fireplace on the interior wall.
A recreation of the interior wall zoomed-in.We can use the Rendering Equation to calculate the light coming from the interior wall of the cabin to our eye.
Here we have a 3d scene that includes just the two walls of the cabin and the fireplace.
A simplified recreation of the walls of the cabin and the fireplace.Let's choose a spot on that wall. Imagine a beam shooting out from our eye to that spot, and bouncing to hit the bright fire in the fireplace.
The green line shows the path the light ray takes from the fireplace to our eye.The equation tells us how the properties of the wall, the fire, and the way the light bounces gives us the orange colour.
Building some intuition
Let’s go over some concepts that can help you intuitively grasp the Rendering Equation.
How light can hit your eye
There are three ways that light can hit your eye:
- Light directly hits your eye, ex. a lightbulb
- Light refracts, ex. a lightbulb through a glass window
- Light reflects, ex. a lightbulb reflecting off of a mirror or the sun reflecting off of a car
Everything reflects
When we think of reflecting, we often think mirrors, but every object reflects light to some degree. If you’re reading a book on a sunny day, you’re reflecting a little bit of light onto your face.
Material matters
Imagine you're holding a mirror in your hands angled towards your face on a sunny day. This is going to shine a lot more light onto your face than if you were reading a book.
Materials (like metal and paper) have different properties that affect how light is reflected and refracted. They have levels of roughness, transmission (high for glass), and specularity (low for matte surfaces).
The equation
Put more simply, this equation is: light hitting your eye = the light emitted from the object + the sum over light coming from every 3d direction weighted by some calculations having to do with the material and the angle.
Light emitted from the object
An object itself can emit light, like a fire, or a piece of metal that gets really hot.
The integral over the unit hemisphere
When we think of light bouncing, we might think it comes in at the same direction that it leaves, like a mirror. But for any realistic surface, there are many light beams that will exit in a different angle.
What I mean here is that the light reflected from a surface into our eye could have hit the surface at any angle. So, to account for all this light, we have to consider every incoming direction.
The material function
The material function considers many properties of the material and how they affect how the incoming light reflects into outgoing light.
is also called the BRDF, or bi-directional reflectance distribution function. The value of this function ranges from 0 to 1 and it calculates the amount of light from the incoming ray that is reflected in the direction of the outgoing ray.
The dot product
The difference between the surface normal and the angle of the incoming light affects how much light is reflected.
Challenges in building a renderer using the Rendering Equation
The Rendering Equation is very computationally expensive, so renderers approximate it by simulating rays bouncing around a 3d scene. This is also called ray tracing.
The goal is to produce the best-looking image, as efficiently as possible.
Challenge #1: Light can bounce many, many times
Light rarely bounces only one time before hitting our eye. It usually bounces off multiple objects or refracts many times. It only stops when the light loses energy.
This means the function is recursive, which becomes expensive quickly.
Renders usually implement a maximum depth, or maximum number of bounces.
Challenge #2: On each bounce, you calculate a sum over infinite vectors
For a point on a wine glass, you might get light coming through the other side of the glass, from an overhead light on the restaurant, and from the red shirt of the person drinking from it.
All of this light is coming from different directions, and affects the outgoing light going into your eye.
Renderers may choose which directions to calculate on a bounce, preferring directions leading toward significant sources of light.
Questions
How do we get the colour from this equation?
We've been talking about how the rendering equation measures "light." More specifically, what it measures is something called spectral radiance. Put simply, spectral radiance is a representation of light that encodes data that gives us the colour and intensity.
What about refraction?
The equation above just focuses on reflection, but can be expanded to include refraction. Instead of integrating over the unit hemisphere, we'd integrate over the unit sphere, and the material function would account for transmission of light.
What other techniques do renderers implement to solve the rendering equation?
Renderers may sample a pixel many times to produce the most accurate colour, ignore objects that are far away, and use integrated hardware like GPUs to speed things up.
Further reading
Build your own C++ ray tracer with "Ray Tracing in one Weekend"