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 and Observations about Direct3D versus OpenGL
The glflare example presented here was derived from a Direct3D 5 demo called flare available on Microsoft's DirectX web site. Unfortunately, I was never able to get this Direct3D example to work on any PC that I tried it on. (Direct3D is just that bad! Others on the net confirmed this problem.) Since I was still interested in the technique, I quickly converted the source code to OpenGL (the code became much shorter and more understandable after the port!). Unlike Direct3D where rendering functionality depends on what drivers, Direct3D implementation, graphics hardware, etc. are on your system, OpenGL mandates all its OpenGL rendering functionality must be supported. My OpenGL version has worked on every PC and graphics workstation that I've tried it on!
So my source code is based on Direct3D-based code written by Stephen Coy of Microsoft. All credit for the original implementation of the idea should go to Stephen. 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. The algorithms are basically the same to ease critical comparison of the OpenGL and Direct3D versions.
The image files used by this program are derived from images developed by Microsoft.
Indeed, it is instructive to compare the source code required in my OpenGL-based version of the code to Microsoft's Direct3D version of the lens flare code. The complete C++ source code for the Direct3D version was over 12,000 lines (no joke!). 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).
Aside from the difference in the sheer amount of code required for the Direct3D version, I note that my GLUT-based example is portable to any Unix workstation, Linux PC, or Windows capable machine. Direct3D programs are completely unportable to any other operating systems other than Microsoft Windows. Note that Direct3D even working under Windows is a dubious proposition for the Direct3D version; I was unable to get the Direct3D binary to work properly on any PC that I tried it!
The OpenGL-based version of the lens flare code also had some other notable advantages. First, all the textures used can be stored (on both disk, host memory, and texture memory) as luminance textures instead of the bulkier RGB textures (3 times larger) used in the Direct3D version.
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 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 Raytraced Example of a Lens Flares (from the Net)
Here's an example of an image generated with a raytracing package 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.
http://www.geocities.com/SiliconValley/Lakes/1434/lenseffects.html#Samples Other Lens Flare info on the Web
Other Cool OpenGL-rendering Techniques
This tutorial and source code is just one in a series of tutorials on fast, cool OpenGL rendering techniques. Check out the full set of tutorials. I hope this help!
This is an automatically derived categorization of files of this directory. Only files which can be displayed or heard in some manner are included. Files may be immediately accessed by clicking the corresponding link.
Source
Documentation
Images
Reference
Complete Source