# Shaders, how to calculate the distance between vertex and camera?

Hello, the title explains my issue already well.

I would require the distance between a vertex and the camera position.

AFAIK i can get positions with

uniform vec3 g_CameraPosition;

and

uniform vec4 inPosition;

But with what matrixes do i need to multipy the positions if i want them in the same ‘space’?

I don’t see anywhere that you are actually taking a distance. What is it that you are trying to do?

Afaik the position is already in relation to the camera? Thats why you have to use the worldMatrix etc. to compute those positions.

The inPosition is in object/model space and the g_CameraPosition is in world space.

g_WorldMatrix * inPosition transforms inPosition from object space to world space.

You can also multiply with the g_WorldViewMatrix matrix to get the position in view space and then you do not need the g_CameraPosition since the camera position is always in (0,0,0) in viewspace ==> length(g_WorldViewMatrix * inPosition) will give you the distance… beware that calling length() is expensive since it does a sqrt() inside.

arrrrrrrr. Still not working:

WorldParamenters:

[java]

WorldParameters {

WorldViewProjectionMatrix

WorldViewMatrix

CameraPosition

NormalMatrix

}

[/java]

.vert

[java]

uniform mat4 g_WorldViewProjectionMatrix;

in vec3 inPosition;

in vec3 inNormal;

out vec3 vPosition;

void main()

{

gl_Position = g_WorldViewProjectionMatrix * vec4(inPosition,1);

vPosition = inPosition;

}

[/java]

.frag:

[java]

in vec3 vPosition;

uniform mat4 g_WorldViewMatrix;

void main()

{

gl_FragColor = vec4(vec3(1,0,0)*(vec4(vPosition,1)g_WorldViewMatrix).z, 1.0);

//gl_FragColor = vec4(vec3(1,0,0)
(vPosition).z, 1.0);

}

[/java]

Result, with both version i have no color at all. The code works, if i remove *(vPosition) all gets painted red as expected…

Nevermind. actually it looks like values are not passed to the FragmentShader when using Geometry/Tesslation Shaders. @pspeed said:
I don't see anywhere that you are actually taking a distance. What is it that you are trying to do?

Initially i wanted to implement a LOD for Tessellation.

Well, and I missed the “.z” in your original code.

Still, there are arguments that can be made that LOD should be done in projection space… which is what gl_Position.z already is. Though I guess maybe you were just using the .frag shader as a test or something.

Hmmm.

When using only inPosition.z for Tessellation factor i get this:

http://i.imgur.com/TRFbZ.png

(Looking from the side)

Multiplying inPosition with any of the matrixes (WorldViewProjectionMatrix,WorldViewMatrix/ or ProjectionMatrix) is resulting in factor 1 for the whole mesh.

inPosition.z will be in model space. From the picture it looks like your sphere might be a diameter of 1? (Edit, I see now that you divide by 2… so a radius of 1 then.)

When you multiply it by a matrix then it will be in world space or projection space. In world space, I’d imagine that z would be very large unless you were really close to the object. In projection space, I no longer remember what the range is but may be near/far relative.

It’s quite likely that your z isn’t a factor of 1 but just always greater than 1. Scale it down and see.

Following this, this code:

[java]

vec3 wvPosition=(vec4(inPosition,1)*g_WorldViewMatrix).xyz;

float dist=distance(g_CameraPosition,wvPosition);

[/java]

should give me the distance, however, it seems that dist stays constant…

Over my head @zzuegg said:
Following this, this code:
[java]
vec3 wvPosition=(vec4(inPosition,1)*g_WorldViewMatrix).xyz;
float dist=distance(g_CameraPosition,wvPosition);
[/java]

should give me the distance, however, it seems that dist stays constant....

How do you know that the distance is constant?

Also, I think in WorldView space, everything is relative to the camera already... thus the "view" part. 0,0,0 is the eye.
@pspeed said:
How do you know that the distance is constant?

Because the resulting tessellation factor does not change with the distance, but it changes when i rotate the camera :(
@zzuegg said:
Because the resulting tessellation factor does not change with the distance, but it changes when i rotate the camera :(

Well, sure because you are taking the distance between a view relative position and the camera. They are not in the same spaces but as you rotate around the distance will change.

…try length(wvPosition) or whatever.

@zzuegg said:
In the last post you said that the camera in WorldView is always at [0,0,0] so i changed the code to:
[java]
vec3 wvPosition=(vec4(inPosition,1)*g_WorldViewMatrix).xyz;
float dist=distance(vec3(0,0,0),wvPosition);
vDistance=51-clamp(dist/20,1,50);
[/java]

Following that the tessellation factor should change with distance every 20 Units

And... what does it do?
@pspeed said:
And... what does it do?

Changing the tessellation factor based on the 'view angle' :(