View Full Version : distance of a vertex from viewpoint

Roest

03-30-2007, 08:32 AM

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.

Overmind

03-30-2007, 08:47 AM

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

ector

04-01-2007, 03:39 PM

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]

V-man

04-03-2007, 03:23 AM

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));

Powered by vBulletin® Version 4.2.3 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.