CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Lens Flare effect

1. Member
Join Date
Apr 2003
Location
Russia
Posts
47

## Lens Flare effect

Hello!

I've got one more question for you. Please tell me how can I generate Lens flare effect. May be there is a project written in C++ in the Web.

Thank you in advance.

2. You can find a tutorial for this at http://nehe.gamedev.net/data/lessons....asp?lesson=44

3. Member
Join Date
Apr 2003
Location
Russia
Posts
47
I'm going to generate the Lens Flare effect using GDI so the only thing I want is understanding how it works. Could you explain the main algorithm.

4. Originally posted by Reunion
Could you explain the main algorithm.
The algorithm to use lens flares has two main steps.

1) detect if light is in view
2) draw a lens flare if 1)

Step 1 depends on what your internal 3d representation is and what projection type you are using (since you are using GDI, its hard to make assumptions). But basically, the idea is to calculate whether or not the light point is in the frustrum of your projection and is in the line of sight (ie. not blocked by other objects).

The lens flare can be done in several different ways. One approach is to just use a preconceived notion of what to draw. That is purely artistic license. However, if you want your lens flares to be physics simulated, there are several important things to note about the calculation. First, it is a raytrace calculation. It requires you implement reflection and refraction algorithms for all of the optical elements of the lens you want to simulate (so one of the variables is the lens mechanics). For realistic effects, you will also want to model the chromatic aberration, the coating thin-film optics applied to lens surfaces, and such. Also the streaking seen in lens flares is due to the aperature blades, where they touch each other being the point of greatest reflection.

5. Member
Join Date
Apr 2003
Location
Russia
Posts
47
Really, I just want to generate Lens Flare effect like in Photoshop - it's easier. I have only one problem: generating main flare in the given point (it's always inside the screen rectangle). What can you advise in this situation?

6. If you mean something like pictured in this site and you are free to use artistic license, then I would just draw a circle with an alpha channel. Make the central alpha values very close to one and the outer values near the circumference of the circle have alpha value near 0. Set the color channels to what ever color you'd like. If you aren't using an API with alpha support, you can calculate the result of alpha blending in your own program and just output the resulting pixel color at each point. In fact, you could even start with using

a(r) := 1 - r / circleRadius

and tweak it for the visual effect you are looking for.

7. Member
Join Date
Apr 2003
Location
Russia
Posts
47
Yes, I mean this.
Well, now it seems to me that there is only one problem: I know how to generate a solid circle of a given radius. You gave me the formula for filling the circle with alpha-gradient. But how it's better to apply it? Scan all the lines of the circle, calculate the radius and apply the formula? Or a faster method exist?

8. Probably the fastest way is to make yourself a bitmap with an alpha channel and AlphaBlend (GDI msimg) it onto your scene. This code demonstrates. You could make the circle in a simple paint program and save as 32 bit bitmap.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)