Sign in to follow this  
codnob

Projecting a 3D vector in screen space (or homogeneous clip space)

Recommended Posts

Hi,
I'm sorry, this is probably a very noob question but i wonder, if i have a vector direction and a start position, is there a simple mathematical way to compute the size of this vector on the final screen or must I project both the 3D start & end position (using worldviewproj) and then recompute a 2D vector ?

(I guess just transform my 3D vector by the worldviewproj is not enough ?)

Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by codnob
Hi,
I'm sorry, this is probably a very noob question but i wonder, if i have a vector direction and a start position, is there a simple mathematical way to compute the size of this vector on the final screen or must I project both the 3D start & end position (using worldviewproj) and then recompute a 2D vector ?

(I guess just transform my 3D vector by the worldviewproj is not enough ?)

Thanks.

When not using orthogonal projection, I would project both positions to the screen.

assumption: center of projection(=camear position) is (0,0,0)
given:
normal = projection plane normal (=camera look_at)
near = distance from center to projection to screen plane(=near plane)

v_start_dist = dot(normal,v_start)
v_start' = v_start * (near / v_start_dist)

v_end_dist = dot(normal,v_end)
v_end' = v_end * (near / v_end_dist)

v_len' = dot(v_end'-v_start',v_end'-v_start') / len(v_end'-v_start')



Share this post


Link to post
Share on other sites
I'm wondering too what you get by simply transforming the vector using the ModelViewProj matrix in the vertex shader?? Would that work or not?

Actually in my case I'm not interesting in getting the length of the vector but just its direction in screen space.

Share this post


Link to post
Share on other sites
Well you can transform a vector with a matrix but you keep sure that it's a 3-component vector. This will keep it from all translations (position modifications). Also don't project a vector. I don't believe that would work.

Share this post


Link to post
Share on other sites
If you have your ModelViewProjection matrix ready it is probably best to do what Ashaman suggested and project the start and end points. Not tested pseudocode:


// MVP: ModelViewProj, start/end_obj: 4d object space pos with w=1
start_proj = MVP*start_obj; // to "projection" space (or clip space if you are clipping there)
start_NDC /= start_proj.w; // to normalized device coords

end_proj = MVP*end_obj;
end_NDC /= end_proj.w;

dir_NDC = end_NDC.xy-start_NDC.xy; // difference ignoring z

dir_screen = dir_NDC*.5 + .5; // to a system where x,y is in [0,1], +x right, +y up

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this