When to draw lens flares

My simulation has the camera orbiting around a planet. The sun is drawn in the distance as a decaled quad, with flares overlaid after the planet is drawn. The sun gets clipped first by a frustum cull, and then by the depth buffer test, so it’s not drawn when not visible.

How do I tell when the sun is “behind” the planet so I know not to overlay the flares? Is this a job for ray-casting?

That would be one way of doing it Since you only have a single sphere to test it should be simple. You should probably fire a few rays and fade the glare based on the number of hits.

Or you use the ARB_occlusion_query extension to let the gfx-card tell you how much of the sun is visible.


Thanks. I’ll try the raycasting first. However, I’m currently having problems with my view frustum culling. I’m using Mark Morley’s frustum code verbatim (http://cslab.wheaton.edu/~dthulson/projects/Pirates/frustum.html) and testing the location of the sun (0, 0, 0) in my world. My tests report that the sun is visible (and hence I draw the flares) when the sun is still not visible on the screen. Pseudocode is as follows:

  • call gluLookAt()
  • extract view frustum with MM’s code
  • draw scene
  • test (0,0,0) against frustum using MM’s “point in frustum” code. If it’s inside, draw the flares, otherwise don’t

However, the flares appear before the sun is on screen and after it’s left the screen. Any general hints here or should I whittle code and post a snippet?

I’ve used MM’s frustum code with much success!

Are you using the point-in-frustum code or the
sphere-in-frustum code? Try using the other
and also bear in mind if you’ve adapted either method to clip just outside of the view frustum (so as not to get visibly-clipped triangles appearing at the edges of the screen) then this is why you’re seeing these problems.

You’ll just need to adjust the calculation for when you are testing the sun. The best way I can think of would be to actually only draw the flares when the center point of the sun is on screen.


Found my problem! I was extracting my frustum after the camera transformation, but also after the 90 deg FOV projection transform for my skybox! This gets reset to a more normal 40 deg FOV for my geometry after the sky box is drawn.

Long story short, I didn’t like the effect of my skybox so I decided to remove it, and lo and behold the flares started working properly!

Also, per dorbie’s advice, I do a raycast to test for visibility as the sun goes behind a planet. I added a nice fade in/fade out effect to the flares that occurs when the sun rises/sets. It looks pretty cool IMO! I’ll try to get a working version online soon as I’d like to get all of your opinions on it.

Thanks for all the help, guys.