Jump to content
  • Advertisement
Sign in to follow this  
barney1

OpenGL "Lighting" with OpenGL 2D graphics

This topic is 2500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello OpenGL/ graphics experts. I'm looking to add some artificial "lighting" textures to my 2d game. Does anyone have a simple explanation for how to do this? Here's what I mean:

I'm currently doing a 2d game by making simple calls to glDrawTexfOES to draw my sprites. Now, the sprites are pretty simple and mostly solid colors. What I want to do is make spots of the screen look like they are lit more than others. I have done some searching and it seems this could be done with a "flashlight" texture, which as I understand is just a circle in the middle of a texture with necessary transparency.

I'm not sure I understand how to do this? What part of the texture should be transparent? And how should I blend the texture after applying it?

Thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure exactly how you handle your textures, but if you're not already using PNG, I'd recommend using it as this will probably be easier if you do. Anyways...

To do flashlight lighting, you'd want to create a blank image (as in transparent background) in your paint program. Then just draw a spherical gradient from white to transparent in the center of the image. Save that as the light. In your code you then load the image and enable blending (glEnable(GL_BLEND)) and set the blending function to glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); and draw your light quad with the light texture on it. Probably want to set the transparency down on it too, maybe set glColor4f(1, 1, 1, 0.7f); or something so the things underneath it still show through.

I could be mistaken on the exact parameters for the blend func, I haven't played with it in a while and I haven't started implementing the lighting in my current project so I'm not totally up on it, but a quick Google on glBlendFunc can list the different choices and if you just plugin a few different combinations you'll find the one you're looking for.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!