Dirty clean code. Creative Stuff. Stuff.

How to calculate the reflection vector

In this post I will talk about reflection vector used in some lighting models, for example the Phong reflection model. I will show you how this vector is calculated.

In a previous post I talked about the Phong lighting model. I described all the different components contained inside it and how they try to describe and simulate different kind of light components. In particular you will remember that there’s a specular component that try to simulated the light reflected in a specific direction: the reflection direction. In the shader implementation this direction has been calculated using a GLSL method reflect:

vec3 reflectionDirection = reflect(-lightDirection, normalInterp);

Easy, isn’t it? But the most curios of you may asking: “How the f*$k this method calculate this reflection direction?” :stuck_out_tongue_closed_eyes:
We will suppose as in the previous post about the phong model that all vectors are normalized. Let’s start from the beginning. The formula to calculate the reflection direction is:

How is this formula obtained? Let’s start from a picture that represents our reflection vector and the other vectors used in the calculation.

Model view presenter ios unit tests

Before we start with the demonstration we also need to know what is the law of reflection:

The incident light ray L, the reflected ray R, and the normal N to the surface of the mirror all lie in the same plane. The angle of reflection $\Theta_R$ is equal to the angle of incidence of light $\Theta_L$. Both angles are measured with respect to the normal to the mirror. The reflected ray and the incident ray are on the opposite sides of the normal.

Now we are ready for our demonstration :sunglasses:.
From the law of refraction reported above we know that:

This equation could be rewritten as the dot product of the reflection direction with the normal equals the dot product of the incident light direction and the normal (remember that the dot product of two vector is equal to the cosine of the angle between them). So we have:

From the image above it’s also evident for symmetry that:

As you can see again from the image above, this two vectors could be easily calculated. In fact the first one is the difference between the reflection vector and the projection of it on the normal. The second one is the difference between the light incident vector and the projection of it on the normal. So for the reflection side we could write:

For the light side we could write:

As a consequence we obtain the following equation:

Now we can see again from the image above that the vector projections ${\hat {N}^{\prime}}$ and ${\hat {N}^{\prime \prime}}$ are equals, that means we could change the previous equation by substituting the first one with the second one. So we obtain the following equation:

Now we have all we need to calculate our R vector:

That’s it!! We get our formula. Now you’re ready to explain in detail the entire “magic” of the Phong model :relaxed:.

Recent posts