Calculating distance from vert to specified point

Let’s say I have a point, called MyPoint, that I pass into the vertex shader:

uniform vec3 MyShader;

In my vertex shader I want to calculate the distance from MyPoint to the curent vertex. How do I do this?

Thanks,
CB

First you must ensure that your vertex MyPoint is in the same coordinate space as gl_Vertex. Then use the built in function distance(myPoint,gl_Vertex).

Edit: I must write that your vertex MyPoint and gl_Vertex are both in the same coordinate space.

What coordinate system are gl_Vertex in?

Any space under the sun. It’s just a vec4, after all. Heck you could pass in quaternions if you had a mind to…

You can use something like this:


uniform vec4 MyPoint
...
//Transform vertices in eye coordinates
vec4 eyeVertexPos = gl_ModelViewMatrix * gl_Vertex;
vec4 eyeMyPointPos = gl_ModelViewMatrix * MyPoint; 
float dist = distance(eyeMyPointPos,eyeVertexPos);

I assume here that MyPoint is defined in object (modeling) coordinates like gl_Vertex is.

Wow, you are doing two additional matrix multiplications, just to get the distance? Guys, you are really complicated…

uniform vec4 MyPoint;

float dist = distance(MyPoint,gl_Vertex);

This is enough. If you do not know what coordinate-spaces are, then you are most certainly always working in world-space, so that no transformations are necessary.

Jan.

Wow, you are doing two additional matrix multiplications, just to get the distance? Guys, you are really complicated

I submit this sample code because if there is a scale factor in the model view matrix, this should affect the distance between the two points.

A valid concern, but the point uniform should be given in the space it will be used.

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