# Normalized sun direction to screen space position

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

## Recommended Posts

Can anyone please help me convert a normalized sun direction (xyz) vector to a screen space coordinate (xy) in pixels.
What would be the formula to do it?

##### Share on other sites
Your question is not well formulated; it makes no sense to convert a direction to a point like that. You have to better describe what you're really trying to achieve.

##### Share on other sites
Sorry if it did not make sense. I am using the trial sdk from sundog software silver lining sky library which is described like this:

[/url]void [url="http://www.sundog-soft.com/docs/html/class_silver_lining_1_1_atmosphere.html#a362efc03580e40bde60590db1ed39de1"]GetSunOrMoonPosition (float *x, float *y, float *z) const
Retrieve the normalized direction of the dominant directional light source.

And I want to add my godrays shader to to my engine which needs a screen space light position.
So I need to convert the output from sundog to the input that my shader needs.

##### Share on other sites
What you probably need then is the projected position of the sun/moon, not their vector. You can maybe approximate the position from the vector by treating the location as a point at infinity, and just treat the three-dimensional homogeneous position as [x,y,z,0], and then project that point onto the screen.

The exact details to do that depends on the API you use, but the major idea is to transform it by the view transform and the projection transform, do the perspective division, and then then perform the normalized device coordinate to screen space coordinate transform. Your API may have some helper function to do that already.

##### Share on other sites
On a second thought, is it really the screen position you need for this? The screen position is only relevant as long as the source is in front of you, but god rays may still be visible even if the source is behind you. I don't know, but isn't the direction vector more relevant than the screen position?

##### Share on other sites
http://sebastien.hillaire.free.fr/demos/godray/godray.htm

He this bit of code to deal with transformation, but I am just not sure if that would apply the same way to the vector returned from sundog library

//light in ve space to screen space
vector3 camToLightVector = lightPos - *cam.p.getPosition();
vector3 lightInCamSpace;
cam.p.transformeIntoLocalSpace(&camToLightVector,&lightInCamSpace);
float screenRatio = ((float)windowSizeY/(float)windowSizeX)/2.0f;
lightInCamSpace.x = 0.5f-screenRatio*(lightInCamSpace.x/lightInCamSpace.z);
lightInCamSpace.y = 0.5f*(lightInCamSpace.y/lightInCamSpace.z)+0.5f;
glUniform3fARB(gpupLS_screenLightPos, lightInCamSpace.x, lightInCamSpace.y,lightInCamSpace.z);

##### Share on other sites
You appear to have some sort of view and projection transform, but you're not really taking the whole projection matrix into account, nor do you perform the perspective division or the viewport trnasformation anywhere if you want to end up with the screen coordinate.

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633768
• Total Posts
3013740
×