# Eye position values in shader?

I’ve started trying to implement shaders in my main project recently, and I’ve been coming across a problem. I’ve read plenty of examples, and I’m trying to go about implementing simple lighting before moving on to more difficult tasks.

All the example code I’ve seen uses a value for the eye position in the calculation of the specular lighting component, computing V as the eye position - the vertex/fragment position, normalized.

My problem is this: the sample code I’ve looked at used a constant viewport, and thus could send a simple value, like (0.0, 0.0, 1.0, 1.0), as the eye position as a constant. However, I already have a movable camera system, and no matter how I try to send the current camera position to the program, my specular highlights manifest themselves in incorrect positions on the model.

What’s the right way to calculate the eye position value so that my shader will produce the correct results?

And similarly, what version of the eye position should I use in calculating the incident vector to be reflected for looking up a fragment from an environment cube map?

(0.0, 0.0, 1.0, 1.0) is only the eye position if everything else is already in eye-space (normal transformed by the inverse-transpose modelview matrix). What I do in my programs is use the last row of the inverse-modelview matrix (the transpose portion of the matrix, inverse, IIRC. In a fragment program, you might have to use the inverse-transpose in a vertex program.)

Edit:
This is what I use for my eye/view vector in my fragment programs (ARB_fp):

SUB View, mvinv[3], Vertex;
DP3 View.w, View, View;
RSQ View.w, View.w;
MUL View, View, View.w;

[This message has been edited by NitroGL (edited 09-22-2003).]

Sorry but isn`t the eye position be (0, 0, 0, 1) instead of (0, 0, 1, 1)?

I second you

Depends on how you want to do it.

If you transform your normals into eye-space, then the eye position is {0,0,1,1}, but if you transform the eye position into eye-space then it’s {0,0,0,1}.

He? Eye position is always (0, 0, 0) in the view space as eye is it’s center.