Normalized sun direction to screen space position

Started by
5 comments, last by Brother Bob 12 years, 8 months ago
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?
Advertisement
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.
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.
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.
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?
Thanks for reply, im using this code as the godrays shader:
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);
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.

This topic is closed to new replies.

Advertisement