In this post, I’ll try to summary the method to realise the effect for direct illumination (diffuse shading, shadow, reflection, refraction, etc.). The codes can be downloaded in hudongyue1/myGL. And these methods are refer to Scratchapixel.

1 Type of Light

1-LightType

In general, there are two kinds of light source. The first kind of lights have their own shape and size (like sun, moon, bulb, etc.), they are somehow expensive to compute. Such lights are called area lights. The second kind of lights do not have their own size and shape, they just emit light (this kind of lights do not exist in nature). They are called delta lights. All of these lights have the attribute related to intensity and colour.

Delta lights can be further divided into distant\directional lights and point\spherical lights.

Distant lights are considered to be enough far away from us, so that the light arrived can be looked like parallel with each others. Just like the sunshine, the light beams come from sun can be looked parallel with each others. In this case, we do not care about the location of light source, we just need to know the direction instead.

Point lights can be looked like points which isotropically emit light beams around their location. As the increase of the distance to the point light, the intensity of light comes from that point lights will decrease. We need to notice the location of point lights.

2 Direct illumination

At first, we just consider the light source directly illuminate objects, and we ignore all the other light beams. It will make our compute much easier.

2.1 Diffuse Objects

Lambert’s Cosine Law and Albedo

A light beam illuminate on a diffuse object, the hit point on the surface of this object will receive some energy. This object will reserve a part of energy, then the other enrgy will be reflected.

The received energy at the hit point are not just related to the intensity of that incident light, but also related to the angle of the light beam. If the beam parallel with the normal of the hit point, it will receive the most energy. On the contrary, if the beam perpendicular to the normal, it will receive the least energy.

So, as the principle named Lambert’s Cosine Law, the amount of light that a surface receives is related to the angle between the hit point normal and light direction:

3-incidentLightAngle

The reserved energy and reflected energy are related to how much energy this diffuse object absorbed from the incident light. And this is related to the property of the diffuse object itself. It’s defined as albedo.

We can use the Greek alphabet to represent albedo, the to represent the incident light beam’s energy. So

Diffuse Shading

Diffuse objects have the properties of view independent. It means that you’ll see the same color from diffuse objects even though you are located in different places. This is because the diffuse objects’ surface will reflect the light impinging in equal quantities in every directions above the surface at the hit point. We can easily get that all these directions are distributed over a hemisphere.

2-diffuseObjectsSurface

To get the amount of light energy in each direction, we can just use the integral:

The exception is that the has the range , so we can not ensure the above formula. To normalize the result. we can just add a division for the above formula. . So we get:

Shadow

At a point of the diffuse object surface, if there are some things hinder the light beam from light source to it. Then this point are in shadow, it can not directly receive the light beam from light source.

So how to compute the shadow? It’s a somehow easy problem, we just need to reuse the previous method. Starting from a point, we could cast a ray with the direction opposite to the light beam from the light source. We can call this ray as shadow ray. Then, if the shadow ray can arrive the light source without obstacle, and it means this point isn’t in the shadow. Conversely, if the shadow ray meets something else, then it’s in the shadow.

Because of the different properties of distant light source and point light source, we’ll have different ways to compute the direction for shadow ray. For distant light source, the direction for shadow ray is just opposite to the direction of distant light source. For point light source, the direction is from the starting point to the position of point light source.

2.2 Multiple Lights

If a scenery has multiple lights, we should consider the light beams emited from all these light sources. But the situation isn’t very complex. Actually, we just need to compute all the light sources respectively, and we add up them. Then we can get the right result.

2.3 Reflection, Refraction and Fresnel

Reflection

Reflection is somehow easy to compute. It’s just like to mirror the incident light to a specific direction for reflected light (also the object will absorb some energy). The direction for reflected light is influenced by the direction of the incident light and the normal of the surface.

4-reflection

The angle between the incident light and surface normal is similar as the angle between the reflected light and surface normal . We can use the angle , normal and incident light to represent the reflected light :

Refraction and Snell’s Law

If a light beam transfer from one transparent medium to another, the direction of light beam will be changed. And this process could be described by the Snell’s Law.

5-refraction

In the above formula, the variable is the property of the medium that the light transfer in. It could be called as refractive index. And the variable related to the speed of light in vacuum , and the speed of light in this medium :

So, given two kinds of medium with refractive index and respetively, and a light beam with direction transfer from the former medium to the later medium. How to compute the refraction direction for this light beam?

Well, it could be computed as the following steps:

6-computeRefraction

Besides, . Now, we can use this method to compute the direction for refraction. But we still have several extra things to notice.

First of all, tolds us that . But we know, could happen. It means the previous inequality will limit the range of . And in reality, it’s indeed true. When the angle with the incident light and surface normal is somehow too large, the situation will be total internal reflection and without refraction. So we should check the value of to exclude this kind of special situation.

7-totalInternalReflection

Second, if the incident ray is inside the medium object. In this situation, the surface normal should be reversed. And we need to notice the order of and .

Fresnel

When a light beam illuminate on a transparent object, the reflection and refraction will happen at the same time. We just know how to deal with reflection and refraction respectively. We still need to find a way to combine them together for final result. And this method is Fresnel equations:

The above equations are for the parallel and perpendicular polarised light (the internal knowlege may be very complex, we do not care about it now). We just need to averge them to get the result for the portion of reflection .

And the portion for refraction is :

Now we have the integrate method to compute the color for transparent objects: