Main Site Links Resources Tutorials
News VB Gaming Code Downloads DirectX 7
Contact Webmaster VB Programming Product Reviews DirectX 8
  General Multimedia Articles DirectX 9

DirectXGraphics: Lens Flares
Author: Jack Hoxley
Written: 14th June 2001
Contact: [EMail]
Download: GR_LensFlare.Zip (82kb)

Contents of this lesson
1. Introduction
2. About Lens Flares

1. Introduction

Welcome to the first of a few (I have no idea how many) short articles. Now that the DirectXGraphics series is completed I'm not going to be doing any more full length tutorials about Direct3D8; but, I decided that there were a few little things I wanted to include - hence these articles appeared.

These articles are just going to be short descriptions of what it's all about, and then the code to download; you should be able to decipher the code, any particularly complex bits will be explained, and 90% of the Direct3D/DirectX code you should know by now...

2. About Lens Flares

This is a funny one really, you see these a lot in computer games, racing games in particular (NFS4). But the funny part is - computer games are slowing rivalling real television style graphics, added realism, better special effects etc.. etc.. But for a long while the film and tv industries have tried very hard to get rid of lens flares - special lens' designed for the cameras to remove them, special programs to remove them; yet games still stick loads of them in... odd.

Anyway, a lens flare is an artifact, an imperfection caused when a light shines directly at a lens, it's all down to reflection, refraction and so on, which isn't really important. To model a lens flare in 3D we need to render a set of flares over the top of our scene (in 2D). The downloadable code chooses a point on a rotating cube to attach the lens flare, and when it becomes visible (ie, it's not obscured by the cube) we brighten the screen and render a line of 9-10 flares; these flares follow a line going from the projected position of the light source through the center of the screen and to another point on the opposite side the same distance from the center as the light source. We then use this line to scale the size, color and alpha transparency... and render. simple as that really..

The only complicated part that you'll need to bare in mind when using this technique - checking if the light source is visible. Depending on how your engine is structured this may be difficult or it may be easy. I attempted two ways, the first was to lock the backbuffer and check the pixel colour - if the colour was the same as the flares center (white) the flare was visible; this worked okay - but it was unbelievably slow; so I scrapped it. instead I sorted the depths of the 8 cube vertices; and if the flare was 6th, 7th or 8th in depth it was invisible, which works excellently for this sample, but it won't in every case. you choose...

You can download the source code from the top of the page, or from the downloads page. enjoy...

any feedback or improvements can be emailed to me - I'm always interested...

DirectX 4 VB 2000 Jack Hoxley. All rights reserved.
Reproduction of this site and it's contents, in whole or in part, is prohibited,
except where explicitly stated otherwise.
Design by Mateo
Contact Webmaster