World to Screen coordinates

Started by
4 comments, last by haplesschap 21 years, 3 months ago
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.
Advertisement
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]
[Insert cool signature here]
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.
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]
[Insert cool signature here]
you have to test against t´he frustrum since you''ll get legal x y for the coordinates that are behind you.

[Insert cool signature here]
[Insert cool signature here]
Or you could use gluProject(), which may or may not be faster depending on your opengl implementation.

This topic is closed to new replies.

Advertisement