## Saturday, June 12, 2010

### Calculating the reflected ray/vector

In computer graphics applications its often needed to calculate the reflection ray for example if you are writing a ray tracer, a shader for some advanced lighting , or environment mapping etc.

If you are writing shaders , there are standard library function to do that . In cg shading language there is a function reflect(also its more efficient than writing our own).

In this post rather than just giving the vector formula for reflection ray , i am trying to explain the simple mathematics behind that.

See the following image, I is the original ray, and R is the reflected ray which we need to found.N is the normal of the incident plane. P is the line perpendicular from normal to both rays. it is obvious that at both ends the length of P will be same.

Dot product between two unit vectors gives the cosine of the angle between them. So using this idea we can find

R = DotProduct[ I, N ] * N + P . ---> Eq(1)

We don't know P now. But I + P = N * DotProduct[ I,N].

So by rearranging P = N * DotProduct[ I,N] - I. Substituting the value of P now in equation(1) gives the final equation.

Here it is the final equation R = 2 * N * ( DotProduct[ I,N] ) - I