# OpenGL 2D Lighting in OpenGL w/ Texture Quads

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

## Recommended Posts

Hey guys. I'm working on a little 2D game using OpenGL, and for the most part things are going well. Initially I was using SDL but found it a little bit too clunky - the move to OpenGL was both quick and worthwhile. I would like to implement some simple lighting into my game but I'm unsure of the process in a couple of areas. I have worked out that I can re-colour each corner of my texture quads selectively illustrated in this image (lower-left corners are shaded blue): Click for image. What I'm intending to do is specify a list of lights with the following attributes: position, radius and intensity/brightness (non-directional). However, I'm unsure of the maths and the best method to work out which corners of the quads to recolour assuming they are within range of said lights. The other thing I'm unsure of is how to handle quads that are within range of multiple lights, so the calculation needs to take into account multiple re-colours. I also make the assumption that there is a calculation based on the light's intensity and the distance from the quad I'm going to re-colour, which will probably just effect the alpha value specified in a glColor call. Does anyone have any suggestions that may help? I have read various other posts from a search but these often involve image-based lightmaps which is not something I'm too concerned with. Also, please ignore that my BG image is not tiled, it soon will be :) Cheers for the help!

##### Share on other sites
There are, of course, many ways to approach it. But a simple method that easily extends your current method, at least the way I understand how you're doing it, is to just sum the contribution of all lights.

I assume you light each vertex based on some light at the moment. Either one global light, or you just assume that multiple lights don't overlap. So instead of just one light, sum the contribution of all lights. It means you have to loop over all lights, determine whether it is in range, and if so, accumulate its contribution.

##### Share on other sites
Thanks for your response. Is the accumulation calculation a case of the addition of the RGB values of the lights? Of course clamping, these values within the 0-255 range. I think I should do a little research into colour theory.

I would like to add a default uniformal light that ensures the scene is still visible without any additional lighting added (so I need to take this into account). I will use additional lighting mainly for special effects for things like explosions and projectiles which will be fairly short-lived. The aim here is not an accurate representation of real-world lighting.

I understand the need to loop through every light within range, this shouldn't be a problem as the number of lights per-frame should be fairly minimal. I don't currently apply any manual lighting to the scene, which I would assume is the same as calling glColor4f(1.0f,1.0f,1.0f,1.0f) before each vertex).

The main thing I'm struggling to get my head round is simply calculating which of the 4 vertices to effect based on their direction from the light-source, is there a simple vector operation that can be applied here?

##### Share on other sites
I just worked out that I can think of each quad as 4 rectangles and simply test whether each rectangle is within the radius of the effecting light to determine whether that vertex should be lit.

##### Share on other sites
Typically you would want to let the GPU handle your lighting, that is what it is there for. It sounds to me like you are trying to calculate the lights at each vertex on the CPU and then set the color via glColorf(), which is not the way that this is typically done. For any scene of even low-medium complexity this will kill the CPU.

If you are not ready to take the leap into shaders (the preferred method), OpenGL has a host of fixed-function lighting methods that will handle exactly what you are trying to do for you. Although these are now depreciated, they will still work.

Take a look at glLight*() function family. These will allow you to specify lights and all their parameters, and then will automatically apply appropriate lighting to any vertex you send to openGL.

##### Share on other sites
Also I should mention for this method that you need to specify normals for each vertex also for them to be lit. This also answers your question
Quote:
 The main thing I'm struggling to get my head round is simply calculating which of the 4 vertices to effect based on their direction from the light-source, is there a simple vector operation that can be applied here?

When you have the vertex normal, you then perform dot product of the normal with the light direction to determine how much to light the vertex. Normals that are pointing toward the light source get strongly lit, while those pointed away from the light are not.

##### Share on other sites
Quote:
Original post by karwosts
Also I should mention for this method that you need to specify normals for each vertex also for them to be lit. This also answers your question
Quote:
 The main thing I'm struggling to get my head round is simply calculating which of the 4 vertices to effect based on their direction from the light-source, is there a simple vector operation that can be applied here?

When you have the vertex normal, you then perform dot product of the normal with the light direction to determine how much to light the vertex. Normals that are pointing toward the light source get strongly lit, while those pointed away from the light are not.

Thanks for your response here. The built-in OpenGL lighting solution seems to do what I want it to do for the time being - I'll give it a shot. Shaders are a natural progression but as you've stated I don't think I'm ready to take the plunge into that arena just yet :)

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 22
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631732
• Total Posts
3001926
×