Lighting in OpenGL/2D

Started by
1 comment, last by NickGravelyn 18 years ago
Hello. I've been doing 2D rendering with OpenGL and so far it's working great. :-) I have (another) question though. I want to use lights in my engine. I know that sounds strange considering that I'm rendering 2D scenes, but I thought that I might be able to use OpenGL lighting to create some cool effects. For one thing, I'd like to make some sort of flashlight. How could I create a cone of light that would eluminate objects it's pointing towards? I've fiddled around with spotlights and the such, but I always get bad, unexpected results. :-( Usually, the entire scene just goes dark and rotating objects occasionally brighten up in one corner or two. I'm using glOrtho() with a far z-coordinate of 1.0 and a near z-coordinate of -1.0. Could someone show me a small example of how to create useful lights in a 2D OpenGL scene? Is using OpenGL lighting in this case just a waste? For basic effects, I could just use some nifty sprites with an alpha channel, but I'd like the engine to be more dynamic than that. Eventually, I want to create a Light class that's easy to use and setup. Any replies are greatly appreciated, as always. :-) Thanks in advance!
Advertisement
Opengl lighting is per vertex not per pixel. That is the lighting is calculated at the vertices of the geometry and then interpolated in between. So depending on how tesselated your shapes are you'll see very different results. You'll also need to provide normals for the opengl lighting equations to work. I would say that your probably best off writing your own code and doing the lighting in software. In particular opengl lighting is not occluded by geometry - so things behind a wall will still be illuminated by the flashlight
I think you'll have to fake a flashlight.

To do this you could simply render whatever shape your flashlight's light will be, texture it with a gradient texture (to give it a bit of "fuzz" at the edges), and then use glBlendFunc() to set the blending function to an additive blending so that it will look illuminated.

Sorry for the poor explanation. It sounds logical, but if you don't understand, I'd be more than willing to help you figure out what I mean. It's kinda late here :).

This topic is closed to new replies.

Advertisement