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

This topic is 2974 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
Quote:
 Original post by codnobHi,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 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 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 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=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

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633690
• Total Posts
3013340
×