# How to calculate the view vector in Phong Lighting Model?

All the books and reference I have read say that the view vector is calculated by subtracting the point where eye is at, from the point where we want to calculate light. But since, eye is at (0, 0, 0) the view vector would be just the negative of the point where eye is at? Is my understanding correct?

• If the eye is at the origin then the view vector is the negative position (usually also normalized to have length 1) – Sebastián Mestre Feb 23 '18 at 6:47

EDIT

While the basic principle of vector calculation below is correct, as Sebastián Mestre pointed out, in lighting you would use the vector from the lighted fragment to the eye position. Therefore, you would in fact use $v = -(point - eye) = eye - point = -point$, so you were actually correct in your "negative" assumption. Still (what might have been more a typo than a misunderstanding), it is the negative position of the lighted fragment, rather than the negative eye (which would still be the origin).

As Sebastián also said in a comment to you, you would usually normalize the view vector after you have calculated it. Sebastián is the MVP.

Calculating a vector $v$ from point $A$ to point $B$ is subtracting the values of $B$ from $A$:

$v = \left(\begin{array}{c}x\\y\\z\end{array}\right) = B - A = \left(\begin{array}{c}b_x - a_x\\b_y-a_y\\b_z-a_z\end{array}\right)$

So in your case, you need to subtract the $eye$ point from the point you're calculating light ($pos$) for:

$v = point -eye$

Your $eye$ is the origin, so remaining is only the $point$. Therefore: no, it is not the negative position of the lighted point, but the positive position.

• It is very common (specially in more advanced brdfs) to use the vector that points from the shaded fragment to the eye as the view vector instead. It would be good if you mentioned this in your answer. – Sebastián Mestre Feb 23 '18 at 7:01
• you are right, i'll add it – Tare Feb 23 '18 at 7:11

It's correct as far as it goes, but there's more. The view vector goes from the shading point to the eye point, but it's normalized (divided by its length so that it has length 1). So if you're shading $p$ and the eye is at the origin, the view vector is $\tfrac{-p}{|-p|}$.

It's not always this simple, because the "eye" isn't always at the origin. In a scanline rasteriser, you use the view transform to move things to the origin, because that's the only way to make the projection work - but not in a ray-tracer. The camera can be anywhere in a ray-tracer, and when you trace secondary rays (for reflections, refractions, or some other effects), it's definitely not in the same place as the camera.

In the case you're working on right now, it sounds like you can just normalize $-p$, but you need to remember that it's really the direction from the shading point to the eye point, so that you can do the right thing when the eye point isn't the origin.