Archived

This topic is now archived and is closed to further replies.

haplesschap

World to Screen coordinates

Recommended Posts

Fairly simple question here. I would like to add lens flare to my 3d engine and I need to project the 3d location of my light to 2d screen coordinates. Any help with the math/technique to accomplish this? thanks.

Share this post


Link to post
Share on other sites
transform it using your combined view and projection matrix.

so as: (pseudocode here since i dont know your api )

Matrix matView = GetWorldMatrix();
Matrix matProj = GetProjectionMatrix();

Vector vLight;
vLight *= ( matView * matProj ); // Should be projected back into screen space so w == 1

Then the x and y coordinates are your pixel values. This may however be erronous when the light is outside the clipping frustrum so you might need to do a frustrum test with it first.

[Insert cool signature here]

Share this post


Link to post
Share on other sites
Ah I see. I figured this was the way, but I wondered if there were a faster way. Anyhow, I use OpenGL currently. Must I test against the frustum or can I just test the resulting screen coordinates to see if they are within the screen''s boundaries (i.e. x >=0 && x < 640)? Thanks for the help.

Share this post


Link to post
Share on other sites
you could also dot against the near clipping plane, subtract this from the eye to light vector and you''ll get something similar:

Vector vLight, vEye;
Plane nearPlane;

Vector toLight = vLight - vEye;
Vector vNormalScaled = ( nearPlane dot toLight ) * nearplane.Normal

Vector posOnNearClip = toLight - vNormalScaled;

This will work though the range of this will not exacly be screen space so you''ll have to do some extra processing on the value. experiment with it a little.

You wont escape the matrix multiplication however and the extra processing is likely to be as expensive as the previous method. So i would stick to that one.

[Insert cool signature here]

Share this post


Link to post
Share on other sites