ARB_fragment_program view vector

Does anyone know how to get/calculate the view vector in ARB_fragment_program? I am trying to implement phong with a fragment program. So I need to get/calculate the view vector in order to computer the specular component.

Or does any know how to implement phong in ARB_fragment_program?

Multiplying the inverse modelview matrix by {0.0,0.0,0.0,1.0} will give you the camera position.
You can pass your vertex position to the fragment processor through the vertex program and the rest is basic math :slight_smile:

Thx for the help.

You can pass your vertex position to the fragment processor through the vertex program
How do I do this?

Use one of the spare texcoord?
I usually use the 4th (texcoord[3]) since I reserve the first through the 3rd are for texture coordinates, binomals and tangent components…

This is what I use for my view direction:

SUB result.texcoord[1], mvit[3], vertex.position;

That’s in the vertex program, then I normalize it in the fragment program (mvit is the inverse transpose of the modelview matrix BTW).

Vertices are in EYE space after transformation through the modelview matrix it should be clear that the vertex coordinate for any vertex location expressed as a vector is the view vector (you may have to normalize or negate depending on your math).

object_space_vertex * modelview_matrix = eye_space_vertex = unnormalized_view_vector

Now if you have the object space view vector going in from the application you’d want to transform through inverse modelview (just because it’s a vector) but that would be an expensive contortion for no reason.