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
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?"
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.
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 is equal to the angle of incidence of light . 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 .
From the law of reflection 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 and 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 understand in detail the "magic" of the Phong lighting model .
Better organize tests and run them against multiple configuration with Xcode Test Plan
Recently I added a lot of Unit and UI tests to RangeUISlider, one of my open source projects. Let's see how I grouped them and run them against multiple configurations with Xcode Test Plan.Read More
Unit testing in Kotlin with JUnit 5 and MockK
I recently discovered MockK, a mocking library created for Kotlin. Let's see how it is possible to write modern unit tests with MockK + JUnit 5.Read More
Spring Boot + Kotlin Rest client cheatsheet: RestTemplate and Webclient
During the last months I worked a lot with Spring Boot backend applications. In this post I explain how you can consume a REST api from a Spring Boot application using RestTemplate and (the new) WebClient.Read More