OpenGL 2D Lighting in OpenGL w/ Texture Quads

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 :)

Create an account

Register a new account

• Forum Statistics

• Total Topics
627700
• Total Posts
2978695
• Similar Content

• A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

-What I'm using:
C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.
-Questions
Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• 21
• 14
• 12
• 10
• 12