In the previous two posts I discussed why one might want direct lighting as a separate computation. But what goes in that function?
If we are just sampling a single light, then we need to bite the bullet and do a Monte Carlo integration with actual probability density functions. These functions can either be defined over the area of the light, or the angles the light is seen through. You can dive into all of that later-- it looks intimidating but is mainly like learning anything new with bad terminology and symbols-- just eat the pain :)
But for now, we can do it the easiest way possible and "just take my word for it". After you have implemented this once, the written descriptions will make more sense.
First, pick a random point uniformly on the surface of the light. Chapter 16 of this book will show you how to do this for various shapes.
The magic integral you need to solve, often referred to as the "rendering equation" is this:
The key formula you evaluate is in red. Area is the total area of the light. The "rho" is the BRDF and for a diffuse (matte) surface that is "rho = reflectance / pi". So pick a random point y uniformly on the light, see if x can "see" y by tracing a "shadow" ray from x to y (or vice-versa). That's it! It will be noisy for spherical lights (half the time, y is on the back and so will be shadowed, and the cosb will vary a lot), but it will work well for polygonal lights. Worry about that later-- just get it to work.
What about multiple lights. Three choices:
- do one of these computations for each light and add them
- pick one of the N lights at random, do the computation for that one light, and multiply by N
- implement reservoir sampling like all the cool kids do (it is a new technique and works really well)
I endorse option 2 as your first step.