Sign in to follow this  
Juliean

Lens flare: sun position?

Recommended Posts

Hi,

I want to use an oldschool lens-flar like in this article:
[url="http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/lens-flare-tutorial-r813"]http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/lens-flare-tutorial-r813[/url]

First of all I want this to happen when looking into my games sun. However, I'm rendering the sun just using atmospheric scattering, so all I've got is the suns direction. Does anyone know how to calculate the suns screen position based on knowing sun direction? It's rendered in this shader:

[code]VS_OUTPUT_BUILD vsBuild(VS_INPUT_BUILD i)
{
VS_OUTPUT_BUILD o;

o.vWorldPos = float4(mul(i.vPos, World), 1.0f);
float4 vWVPPos = mul(i.vPos, WorldViewProj);
float4 vPosLast = mul(i.vPos, LastWorldViewProj);
o.vPos = vWVPPos;
o.vPos.w = o.vPos.z * (1.00001f);
o.vTex0 = i.vTex0;

vWVPPos.xy /= vWVPPos.z;
vPosLast.xy /= vPosLast.z;

o.vVelocity = vWVPPos.xy - vPosLast.xy;
o.vVelocity /= 2.0f * 6.0f;

float3 vertex = mul(i.vPos, WorldView).xyz;

float3 viewDirection = normalize( vertex );
float distance = length( vertex )*100.0f;

//
// Angle between sun direction and view direction
//
float3 sunDirection = normalize(mul(float4(SunDir, 1.0),WorldView).xyz );

float theta = dot( sunDirection, viewDirection );

//
// Phase1 and Phase2
//

float phase1 = 1.0 + theta * theta;
float phase2 = pow( rsqrt( HGg.y - HGg.z * theta ), 3 ) * HGg.x;

//
// Extinction term
//

float3 extinction = exp( -BetaRPlusBetaM * distance );
float3 totalExtinction = extinction * Multipliers.yzw;

//
// Inscattering term
//

float3 betaRay = BetaDashR * phase1;
float3 betaMie = BetaDashM * phase2;

float3 inscatter = (betaRay + betaMie) * OneOverBetaRPlusBetaM * (1.0 - extinction);

//
// Apply inscattering contribution factors
//

inscatter *= Multipliers.x;

//
// Scale with sun color & intensity
//

inscatter *= SunColorIntensity.xyz * SunColorIntensity.w;

o.vDiff.rgb = inscatter;
o.vDiff.rgb = (1.5f - sqrt(i.vNrm.y))*inscatter;
o.vDiff.a = 1.0f;
return o;
};[/code]

My first thought was to just use the suns direction (0, 1, 1), take a point at a certain lenght like (0, 10, 10) and multiply it with the WorldViewProjection-matrix. then I should get the sun position in screen space, at least in theory. However I really need the point of the sun on my sky-sphere, which is modulated in the shader to always stay behind everything.. Any ideas? Thanks in advance!

Share this post


Link to post
Share on other sites
To avoid advanced ray tracing when the sun is halfway hidden behind something and seen thru reflection or refraction, just use a post effect.
Sample the image in multiple scales from the center and fade it away on the edges to use it for anything that is very bright.

This example in my SDK have the post effect for sun flare combined with the bloom effect to reuse the downsampling.
http://www.youtube.com/watch?v=v_IYPFLC5D0

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