Flashlight-effect in OpenGL 2D platform-game
In a 2D platform game, how can I create a flashlight-effect (like in this video at around 0:41 http://www.youtube.com/v/DHbjped9gM8&hl=en开发者_StackOverflow中文版_US&start=42) ?
I'm using OpenGL for my lighting.
PS: I've seen effects like this a few times, but I really don't know how to create them. I know that I can create new lightsources with glEnable, but they are always circular shining in a 90° angle onto my stage, so that's quite different from what I am looking for.
You have to tell OpenGL that you want a spot light, and what kind of cone you want. Let's guess that a typical flash-light covers around a 30 degree angle. For that you'd use:
glLightf(GL_LIGHTn, GL_SPOT_CUTOFF, 15.0f);
[where GL_LIGHTn
would be GL_LIGHT1
for light 1, GL_LIGHT2
for light 2, and so on]
You'll also need to use glLightfv
with GL_LIGHT_DIRECTION
to specify the direction the flashlight is pointing toward. You may also want to use GL_SPOT_EXPONENT
to specify how the light falls off at the edges of the cone. Oh, you may also want to use one of the GL_XXX_ATTENUATION
s as well (but a lot of times, that's unnecessary).
If you want to support shadows being cast, that's another, much more complex, subject of its own (probably too much to try to cover in an answer here).
What platform (as in hardware/operating system) are you developing for? Like previous post mentioned, it sounds like you're using fixed function OpenGL, something that is considered "deprecated" today. You might want to look into OpenGL 3.2, and do it with a full shader-based approach. This means handling all the light sources yourself. This will also allow you to create real-time shadows and other nice effects!
精彩评论