Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411712 Posts in 69402 Topics- by 58456 Members - Latest Member: FezzikTheGiant

May 21, 2024, 06:41:53 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)2D Lighting in OpenGL... a few questions
Pages: [1]
Print
Author Topic: 2D Lighting in OpenGL... a few questions  (Read 5145 times)
Ethan_Hall
Level 0
**


View Profile
« on: June 28, 2013, 11:54:30 AM »

Hello again TIGSource! I'm starting my epic journey into the world of lighting in OpenGL 3.3, for a 2D game.

Background: Right now, I have quads that are rendered using a single VBO. The VBO has vertices (0,0) (0,1) (1,0) (1,1), and is scaled to whatever size I need. I render a texture on this quad whenever I need to display a 2D sprite.

I have been fooling around with my fragment shader, just implementing simple ambient lighting, and a few different effects based on glFragCoord. I feel comfortable moving on to more complicated things.

Questions:

  • Could the lights in OpenGL (like GL_LIGHT0) be used for a 2D game? I guess I don't really understand what they are used for. I know there is some kind of misconception that there is a maximum of 8 lights, but this isn't actually the case... what are these lights and how do they pertain to me?
.

  • I understand that a texture of a white circle with a gradient can be used to create some simple lighting effects in 2D. Is this used at all in more complicated systems? I understand the concept here, and if it isn't that useful outside of very simple lighting effects, I'd like to avoid it altogether.
.

  • I understand that there are basically 3 different types of light: ambient, diffuse, and specular. Will I still be using these concepts in a 2D environment?
.

  • One thing that is bothering me is point lights. My understanding of a point light is literally a point in space that is a source of light; the effect of the light on the surrounding objects/environment is weaker as the distance from the light increases. Do I need to use an actual OpenGL point primitive paired with a home-rolled shader to achieve this kind of light? I'd rather not use the "texture of a white circle" thing because I would like the light to be hindered by walls, which can't be done using the texture approach.


I apologize for the long winded questions. I also apologize if I missed something obvious in my research. Thanks for all of your help TIGSource!

EDIT: I also really like the idea of creating normal maps for characters and objects in the game and using these with shaders for some interesting effects... I ~think~ I understand how to do this, but I would probably need to know how to implement point lights first...
« Last Edit: June 28, 2013, 12:18:25 PM by Ethan_Hall » Logged
powly
Level 4
****



View Profile WWW
« Reply #1 on: June 28, 2013, 12:38:20 PM »

Could the lights in OpenGL (like GL_LIGHT0) be used for a 2D game?

GL_LIGHT0 and its buddies are a part of the fixed function pipeline - the way things were done before shaders. Any state that has to do with these doesn't actually do anything since you calculate your pixel colours in the shader yourself instead of GL figuring it out based on lights and materials and such.

I understand that a texture of a white circle with a gradient can be used to create some simple lighting effects in 2D. Is this used at all in more complicated systems? I understand the concept here, and if it isn't that useful outside of very simple lighting effects, I'd like to avoid it altogether.

Yes, you could use it to define different attenuations. Though a circle is redundant, you can just have a 1D texture and check it from the coordinate (length(lightPosition-pixelPosition),0).

I understand that there are basically 3 different types of light: ambient, diffuse, and specular. Will I still be using these concepts in a 2D environment?

Yes. You might want your whole level to be rendered with ambient light to look a little less harsh - absolutely black shadow for over half of the screen area usually looks somehow depressing, scary or anxious. Of course you might be aiming for those. The diffuse component will show up on most lit areas like usual, differing them from shadow. Specular is usually ignored (I think?), but if you really want the extra coolness you can combine it with the normal map thingy and a specular map and do some nice shines.


One thing that is bothering me is point lights. My understanding of a point light is literally a point in space that is a source of light; the effect of the light on the surrounding objects/environment is weaker as the distance from the light increases. Do I need to use an actual OpenGL point primitive paired with a home-rolled shader to achieve this kind of light? I'd rather not use the "texture of a white circle" thing because I would like the light to be hindered by walls, which can't be done using the texture approach.

It is indeed a literal point, but you only need the position of it to be able to calculate its effects - it can be rendered too, but it's not necessary to be able to compute its effects on the scene. That said, you can easily render it with the circle thing so the light itself won't be rendered, but it's effect will be directly visible. The hardest thing will indeed be the walls and other occluders and there are multiple solutions like raytracing through some acceleration structure or rendering the occluders extruded away from the light as shadows.
Logged
Daid
Level 3
***



View Profile
« Reply #2 on: June 28, 2013, 01:53:09 PM »

One thing that is bothering me is point lights. My understanding of a point light is literally a point in space that is a source of light; the effect of the light on the surrounding objects/environment is weaker as the distance from the light increases. Do I need to use an actual OpenGL point primitive paired with a home-rolled shader to achieve this kind of light? I'd rather not use the "texture of a white circle" thing because I would like the light to be hindered by walls, which can't be done using the texture approach.
What you actually want there is shadows. The default light system of OpenGL (legacy) and most examples do not do shadows when you are talking about light.

You might want to look up "shadow casting" which is usually one of the simpler ways to get this effect in 2D.
Logged

Software engineer by trade. Game development by hobby.
The Tribute Of Legends Devlog Co-op zelda.
EmptyEpsilon Free Co-op multiplayer spaceship simulator
notnowlewis
Level 0
*

physics and ennui


View Profile
« Reply #3 on: July 02, 2013, 12:32:11 PM »

I have some links collected that might be helpful. I'm looking at adding the same type of effects to WRLD if I get the motivation Smiley




http://www.catalinzima.com/samples/shader-based-dynamic-2d-smooth-shadows/
http://rabidlion.com/?p=10
http://ahamnett.blogspot.co.uk/2013/05/2d-shadows-shader.html

That should be enough to get you on the path to having something working.
Logged
Ethan_Hall
Level 0
**


View Profile
« Reply #4 on: July 05, 2013, 01:12:41 PM »

@notnowlewis I had already seen the first two, but I haven't seen the second two links. Thanks for the help!

I actually inserted some code that allowed me to move the position of my "light" around, and it definitely looks like a point light. But as has been said already, I probably need some shadows to make everything look good.

One thing that I haven't accomplished very well is making my quads respond to light in a more believable way. Whenever I move the point light closer/further from a quad, that quad just has its own colors augmented between 100% and 0%, based on distance. I think I need to edit the equation a bit so that the quads get a kind of highlight and appear white when the light passes over them.
Logged
notnowlewis
Level 0
*

physics and ennui


View Profile
« Reply #5 on: July 05, 2013, 02:13:00 PM »

One thing that I haven't accomplished very well is making my quads respond to light in a more believable way. Whenever I move the point light closer/further from a quad, that quad just has its own colors augmented between 100% and 0%, based on distance. I think I need to edit the equation a bit so that the quads get a kind of highlight and appear white when the light passes over them.

You should be able to use a vertex shader on each point of your quads for that. http://www.lighthouse3d.com/opengl/glsl/ is a great resource that might have some helpful tutorials for you.
Logged
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #6 on: July 06, 2013, 01:31:48 AM »

Also relevant (if optional):

http://www.alkemi-games.com/a-game-of-tricks/
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic