Part of the Khronos Group

NOTE: This page contains old archived material and may not be relevant anymore.

Fast OpenGL-rendering of Lens Flares

Copyright 1998, 1999 by Mark J. Kilgard. Last Updated July 10, 2000 Commercial publication in written, electronic, or other forms without expressed written permission is prohibited. Electronic redistribution for educational or private use is permitted.

A lens flare is a cool effect that occurs when internal refractions and refractions of incident light within the internals of a physical camera create bright patterns within the image. Photographers generally try to avoid lens flares within their images by using lens hoods, though some photographers use the effect to artistic advantage.

Traditionally, computer graphics often tries to immitate the results of photography. This can even include immitating effects such as a lens flare that is in actuality an artifact of the photographic process. Adding a lens flare into a computer generated image or animation can help overcome the computer generated coldness of computer rendered scenes. A lens flare can make it look as if your computer rendered scene was captured by a camera.

A lens flare can be simulated with fast texture mapping techniques. The screen snapshots below show lens flare effects generated in real-time with OpenGL. The full source code and associated texture data files are freely available for download (see below).

The Origin of my Example

The glflare example presented here was derived from a Direct3D 5 demo called flare written by Stephen Coy of Microsoft. While I referenced Stephen's code during the writing of my code (freely available on the net), my code was completely written by me from scratch to take advantage of OpenGL. The algorithms are basically the same though. The image files used by this program are derived from images developed by Microsoft.

My OpenGL version is under 600 lines of code. Even if you include the source code for the OpenGL Utility Toolkit (GLUT) that my version uses, that would less than 3,500 lines of code (of course, this code is usually simply part of GLUT.DLL).

This GLUT-based example is portable to any Unix workstation, Linux PC or Windows capable machine.All the textures used can be stored (on both disk, host memory, and texture memory) as luminance textures instead of bulkier RGB textures (3 times larger).

Getting the Source Code and Textures

You can view the glflare source files online: glflare.c, vec3d.c, loadlum.c, and loadlum.h. You'll need the freely available GLUT source code distribution to compile this program. GLUT works for both Unix, Linux, and Win32 mchines.

You also simply download a ZIP file or GNUzipped Unix tar file containing all the source code and the flare and shine texture data files.

A Few of The Flare and Shine Texture Files

Here are a few of the 6 flare textures:

Here are a few of the 10 shine textures:

Note that the textures are only luminance data (instead of RGB color). Since OpenGL implementations support a luminance texture format, luminance textures generally take up less disk space, host memory, and texture memory than full color RGB textures (a third less).

How the Lens Flare Gets Rendered

This discussion briefly describes how OpenGL is used to render the lens flare effect.

The shine and flare textures are loaded as OpenGL luminance texture objects. The lens effect is really a series of screen-aligned textured quads projected in the direction of the flare. Some 3D vector calculations first calculates the direction and extent of the flare.

The colorful rainbow burst part of the flare is generated by texturing 3 quads, one red, one green, and one blue, each using a different one of the 10 shine texture objects. But how does that give a rainbow burst? The textures are simply luminance values (greyscale). The answer is that the GL_MODULATE texture environment is used to multiply the red, green, and blue of each of the three quads. This gives you shades of red, green, and blue.

But if you refer to the screen snapshots above, you'll see the center of the shine burst is white and there are colors other than red, green, and blue. You get a nice saturated white center because all the lens flare quads are being drawn with OpenGL blend enabled and the blend function being configured by calling:

  glBlendFunc(GL_ONE, GL_ONE);

This blend equation literally adds each red, green, or blue quad with what is already in the frame buffer. Since the center of the three textured quads for the burst is red, green, and blue respectively, you get a saturated white center. Also, because each quad is using a slightly different burst texture, the edges of the burst get different combinations of red, green, and blue, so the result is a rainbow pattern at the outward streaks of the burst.

In addition to the shiny burst, you also seen the red circles shooting out in the direction of the lens flare. These are just several more screen-aligned textured quads blended just like the burst except they are directed out in the direction of the lens flare. Instead of using the shine texture objects, they use flare texture objects.

Switching between various texture objects is easy in OpenGL. Just call glBindTexture. Between the rendering of each screen-aligned textured quad, glBindTexture is called.

A few last notes. Normally, you'll render your scene, then overlay the lens flare (hopefully based on where the sun or other bright light source is in your scene). The lens flare does not need depth testing to be enabled. You can also get away with disabling blending during the lens flare. This will definitely help improve the performance of this technique.

A POV-Ray Example of a Lens Flares

Here's an example of an images generated with a raytracing package (POV-Ray) also showing lens flaring effects. Note that you can capture considerably more complicated effects with real-time OpenGL rendering if you wanted. Real photographs or results from ray tracers like the one below can serve as your inspiration.

POV Lens Effects

Column Header
Column Footer