Martin Schmidt

i have read something about lens flares. the position of the lens flare are calculated by the vector which goes from the sun until (0, 0, 0). but in many games i see the following effect:

when i look into the sun and turn left or right the lens flares are turend left or right to. do they calculate the angle between the camera direction vector and the lens flare vector and turn the lens flare vector by this angle



Re: XNA Game Studio Express lens flares

Jon Watte

A lens flare is created in screen space.

Typically, you generate some number of "rings" for the different pieces of simulated optics that are the cause of the lens flare. Then you position these on the axis between screen space position of the sun, and center of screen. Typically, you represent the position as a value between 1 and -1, where 1 means "centered on the sun" and -1 means "diagonally opposite from the sun across screen center". 0, then, means "center of screen."

Then, you just draw these rings, using additive blending, at the screen positions calculated through that vector and position.

You also want to make sure that the sun is actually visible on the screen before you draw the lens flare.





Re: XNA Game Studio Express lens flares

Martin Schmidt

i implemented lens flares with billboards because i didn't wanted to use sprites in order to work with world space. the question is, how do i calculate the vector from the sun to the center of the screen



Re: XNA Game Studio Express lens flares

Jon Watte

You can still do billboards in screen space; just set the projection matrix to an orthographic matrix (or to identity).

If you use billboards, you have to settle on a distance from camera to billboard plane (say, 1.01 meters, if your near clipping plane is at 1.0 meters), and then project the ray to the sun from the camera location onto that plane for the sun location. Project the Forward ray of the camera orientation matrix (which is the inverse of the view matrix) onto that same plane to get "camera center."

Note that the plane normal of the projection plane will be the same as the camera Forward vector.

The math would look something like:

Vector3 cameraPos = ...;
Vector3 sunPos = ...;
float ProjectionPlaneDistance = ...;
Matrix cameraMat = viewMatrix.Inverse();
Vector3 sunPos = cameraMat.Forward * Vector3.Dot(sunPos - cameraPos, cameraMat.Forward) * ProjectionPlaneDistance + cameraPos;
Vector3 centerPos = cameraMat.Forward * ProjectionPlaneDistance + cameraPos;






Re: XNA Game Studio Express lens flares

Shawn Hargreaves - MSFT

Doing lensflares in 3D with billboards seems unneccessarily complicated. They are really a 2D effect, so you're going to end up having to do a lot of extra math to make them look right if you render in 3D.





Re: XNA Game Studio Express lens flares

LeeC22

Martin Schmidt wrote:

i have read something about lens flares. the position of the lens flare are calculated by the vector which goes from the sun until (0, 0, 0). but in many games i see the following effect:

when i look into the sun and turn left or right the lens flares are turend left or right to. do they calculate the angle between the camera direction vector and the lens flare vector and turn the lens flare vector by this angle

Please remember though that the human eye does not generate lens flares. If this is supposed to be the view through your eyes, don't add lens flares. If it's supposed to be you looking through a camera, then that's fine.

Lens flares are actually caused by light interracting with the glass elements within a lens. It may sound pedantic but there is nothing worse than an FPS or a driving game in the in-car view (i.e. drivers view) that has lens flare.






Re: XNA Game Studio Express lens flares

Shawn Hargreaves - MSFT

Unless you wear glasses like I do, in which case you get all kinds of crazy flare effects coming off your lenses when you drive at night...

Those look totally different to the camera lens flare that a lot of games simulate, though. I've never actaully seen anyone try to emulate the effect of light going through glasses, I guess because that would look really odd to people who don't wear glasses and wouldn't recognise what it was trying to do!





Re: XNA Game Studio Express lens flares

LeeC22

Shawn Hargreaves - MSFT wrote:
Unless you wear glasses like I do, in which case you get all kinds of crazy flare effects coming off your lenses when you drive at night...

Those look totally different to the camera lens flare that a lot of games simulate, though. I've never actaully seen anyone try to emulate the effect of light going through glasses, I guess because that would look really odd to people who don't wear glasses and wouldn't recognise what it was trying to do!

They look different because they're not caused by the sun.

You will probably find it is when the light cuts across your glasses and you get reflections between the front and rear internal surfaces of the glass. The angle of incidence wll be so shallow that it generates internal reflections. That combined with the refractions that manage to pass through do cause some funky effects... that I can vouch for first hand.






Re: XNA Game Studio Express lens flares

Martin Schmidt

so i have to transform the sun position into screenspace (view * projection matrix) and then i have to calculate a vector from the sun until the origin. right





Re: XNA Game Studio Express lens flares

Shawn Hargreaves - MSFT

Martin Schmidt wrote:

so i have to transform the sun position into screenspace (view * projection matrix) and then i have to calculate a vector from the sun until the origin. right



Yes. The easiest way to get the position in 2D is using the Viewport.Project method.

Once you have that, you calculate a vector from the 2D sun position to the middle of the screen, and then position your various 2D flare sprites at different positions along that line.





Re: XNA Game Studio Express lens flares

Martin Schmidt

it's a 3d program... so i think it would be the easiest way to do the following:

Vector3 vector = Vector3.Zero - (3d position of the sun * viewProjection);
int length = vector.Length;
vector.Normalize()

i think so because with this function i have not worry about the back buffer resolution... i can draw a rectangle using a vertex buffer in order to draw the texture. when i use coordinates with z=0 i do not have to do a rotation. but of course, i have to scale and i have to do a translation

for example:
Matrix.Scaling(0.5f, 0.5f, 1) * Matrix.Translation(vector * (length * 0.5f));

EDIT:

sorry, you said that i have to transform the 3d coordinate into 2d space, sorry :)