OpenGL and top down 2d lighting?

Hey JGO,

I was wondering if anyone here knows how to do a “fake lighting” effect in OpenGL?

At the moment I’m raycasting rays in all directions from the centre of the player, making a list of the hit vertexes and drawing a TriangleFan with those vertexes.

I was wondering if anyone knew a better approach to this?

Thanks in advance!

Well, off the top of my head (but never tried), there is a HUGE page on just this here on JGO. It is great! It looks pretty complex though, but if you are up for the challenge: here.

You should most likely use box2D.

This is quite a nice technique, and can be done without complex raycasting.

Funnily, I’ve just implemented an algorithm to do this in Java2d :slight_smile:

Though I’m rewriting it atm to change the way the level is represented, so it’ll be simpler to procedurally generate level data.

I doubt it’ll be of much use if you’re using ogl, but if you like I can post the code once I’ve refactored & neatened everything up a bit.

A quick overview of the algo:

  • map defined as a maze of interconnected convex polygons
  • lights are arcs (mathematical ‘sector’), with an origin, radius, start & end angles.
  • lights are rendered as gradient fill circles (pre-rendered to an image if you want extra speed), and are drawn through a polygon clip.
  • the polygon clip is constructed by:
    • cast 1 ray out from light origin along start angle, looking for edge intersection with the polygon currently containing the light.
    • If the intersected edge is a wall (rather than a doorway into another polygon), add the intersection point as a vertex to your clip polygon.
    • walk around polygon’s vertices, adding each as a vertex to your clip polygon.
    • once you reach a vertex that that lies beyond the light’s end angle, cast a 2nd ray to find the intersection point
    • for each edge you walk around that is a doorway into another convex polygon (and is inside the radius of the light), process it recursively using the same algorithm.

Computationally it’s very cheap; my initial implementation could render 1000+ clipped light sources @ 60fps.

There are of course limitations; no spot effect (focal point), no smooth transitions at the start/end of the light arc (sector), no surface specific lighting.