Jump to content
  • Advertisement
Sign in to follow this  
codnob

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

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

If you intended to correct an error in the post then please contact us.

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
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')



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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!