옆히

How to calculate the reflection vector 본문

기타/scrape

How to calculate the reflection vector

옆집히드라 2024. 1. 23. 23:00

출처

https://chicio.medium.com/how-to-calculate-the-reflection-vector-7f8cab12dc42

 

 

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?” 😝

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 ΘR the angle of incidence of light Θ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 😎. 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 N’ and N’’ 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 😌.

 

+ 내적 교환 법칙 성립

 

 

'기타 > scrape' 카테고리의 다른 글

Image sampling, quantization  (0) 2024.01.27
fprintf(stderr, "error!");  (0) 2024.01.26
interlaced vs scanprogressive scan  (0) 2024.01.24