2

I have to calculate the Specular Highlights (phong) of an Image. the normal Vector and the "light vector" are given. Now I have to calculate the light reflection - is there an efficient matlab function to flip the light Vector over the normal vector to get the reflected-light-vector?

Ispec = ks * I * (r * v)p

Where: l is the light vector
n is the normal vector of surface
r is the reflection vector
v is the vector from reflection point to viewer
p is the shininess

Eitan T
  • 32,660
  • 14
  • 72
  • 109
NaN
  • 3,501
  • 8
  • 44
  • 77
  • Is it a different reflection than using `a(end:-1:1)`? – petrichor Jun 13 '12 at 11:48
  • Just to be clear: is your problem the same as computing the anelastic bouncing of an object on a planar surface with no gravity? How is the reflecting surface expressed with? – Luca Geretti Jun 13 '12 at 12:02

1 Answers1

6

I would solve this mathematically:

Let N be the normal vector. Let V be the light vector. Let O be the reflected vector.

  1. O is in the same plane as N,V
  2. The cosine of the angle between V and N is the same as the cosine of the angle between V and O (With a minus sign).
  3. O has the same same length as V

This yields 3 equations:

  1. dot(O, cross(N,V)) = 0
  2. dot(N,V)/ norm(N) / norm(V) = - dot(N,O) / norm(N) / norm(O)
  3. norm(O) = norm(V)

After manipulating these equations, you will reach a 3x3 equations system. All that is left is to solve it.


Edit My colleague has just told me of an easier way:

V can be separated into 2 parts, V = Vp + Vn

  1. Vp - parallel to N
  2. Vn - has straight angle with N

O has the same parallel part Vp, but exactly the opposite Vn

Thus, O = Vp - Vn, but V = Vp + Vn and then O = V - 2 * Vn Where Vn = dot(V,N) * N (Assuming that N has norm of 1)

So the final answer is:

 function O = FindReflected(V,N)
     N = N / norm(N);
     O = V - 2 * dot(V,N) * N;
 end

Edit 2 I've just found a much better explanation on Math.stackexchange: https://math.stackexchange.com/questions/13261/how-to-get-a-reflection-vector

Community
  • 1
  • 1
Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
  • I have theta (the cos angle between n and l and n and O (r) - I have the angle, the normal Vector and the light vector, isn't there an easier way? – NaN Jun 13 '12 at 12:15