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

Started by
3 comments, last by macnihilist 13 years, 5 months ago
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.
Advertisement
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')
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.
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.
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=1start_proj = MVP*start_obj;  // to "projection" space (or clip space if you are clipping there)start_NDC /= start_proj.w;    // to normalized device coordsend_proj = MVP*end_obj;end_NDC /= end_proj.w;dir_NDC = end_NDC.xy-start_NDC.xy;  // difference ignoring zdir_screen = dir_NDC*.5 + .5;       // to a system where x,y is in [0,1], +x right, +y up

This topic is closed to new replies.

Advertisement