• ### What is your GameDev Story?

#### Archived

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

# World to Screen coordinates

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

## 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 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 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 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 on other sites
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]

##### Share on other sites
Or you could use gluProject(), which may or may not be faster depending on your opengl implementation.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 11
• 9
• 38
• 16
• ### Forum Statistics

• Total Topics
634128
• Total Posts
3015693
×