Sign in to follow this  
wildboar

Normalized sun direction to screen space position

Recommended Posts

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="http://www.sundog-soft.com/docs/html/class_silver_lining_1_1_atmosphere.html#a362efc03580e40bde60590db1ed39de1"][/url]void [url="http://www.sundog-soft.com/docs/html/class_silver_lining_1_1_atmosphere.html#a362efc03580e40bde60590db1ed39de1"]GetSunOrMoonPosition[/url] (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 this post


Link to post
Share on other sites
What you probably need then is the projected [i]position[/i] 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 this post


Link to post
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 this post


Link to post
Share on other sites
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);

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this