# Distance of a vertex from viewpoint

Is there an easy way to determine the distance of a vertex from the viewpoint in a shader program? I’m trying to modify the width of a quadstrip according to its location in space by moving the vertices. With fixed values this works already, now i only need a multiplier for the distance.

Just take the length of the position vector after ModelView transformation, but before projection. In this space, the camera is at (0,0,0).

Or just grab the Z value in camera space as an approximation.

you can calculate like this way.
EyePosition - VertexPosition;
you will get eyevector, and length of the eyevector is the distance of a vertex from the viewpoint.
If i am wrong let me know.

ill add my voice to the disonant choorus
camera pos is ( i think) gl_MatrixInverseModelView[3]

gl_ModelViewMatrixInverse[3] is camera position in object space so you can do

float mydistance = distance(gl_ModelViewMatrixInverse[3].xyz, gl_Vertex.xyz);

or you can do

vec3 myvertex = vec3(gl_ModelViewMatrix * gl_Vertex);
float mydistance = distance(vec3(0.0), myvertex);

or

vec3 myvertex = vec3(gl_ModelViewMatrix * gl_Vertex);
float mydistance = sqrt(dot(myvertex, myvertex));

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.