Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411512 Posts in 69376 Topics- by 58430 Members - Latest Member: Jesse Webb

April 27, 2024, 12:32:29 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Darkness in a 2D platformer
Pages: [1]
Print
Author Topic: Darkness in a 2D platformer  (Read 3068 times)
dspencer
Level 3
***


View Profile WWW
« on: February 08, 2009, 06:55:21 PM »

Hi,

I'm making a 2D platform adventure game. While most of the pieces of it are coming together nicely, I'm finding myself stumped on one aspect: how to do the lighting. I'm aiming for lighting in a manner similar to that in Spelunky (and a number of other games, but that was the first to come to mind) with a lit circle around the player + light sources, while the rest of the environment is darker.

I am using SDL+OpenGL for my graphics. The only way I could think of doing the lighting would be to have a black image, with a circle cut out of it, layered on top of the screen. This feels extremely inelegant, so, I'm guessing there is a better way. Can anyone either help me out, or point me in the direction of a tutorial that might be able to help? Would using straight SDL be better for this, or is there some other tool I should be using? Any ideas at all would help. Thank you very much, in advance.
Logged

Fletch
Guest
« Reply #1 on: February 08, 2009, 08:01:58 PM »

You might have an advantage since you're using OpenGL. Forgive me if this doesn't make sense, it has been a while since I played with raw OpenGL, so I hope everything I know hasn't been deprecated!

First of all, try doing it the sloppy way (draw the whole screen, then do your magic with the black-blocking objects). See how it hits performance. It probably won't. My games tend to fail to become completed because I burn out worrying about all the poor pixels being blitted only to be snuffed out by rgb(0,0,0). Wait until it takes a bite out of your performance, then evolve out of the naive approach.

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.” - Donald Knuth  Gentleman

Now, more helpfully Smiley, you could try a couple of things. You might try figuring out how many spotlights are visible to your viewport (a series of inexpensive AABB tests) then adjust your blitting code so you can tell it do only draw "(x,y)-(width,height)" of the screen. If you've ever done dirty-rectangles, that shouldn't be too awkward. You're really just replacing (0,0)-(SCREEN_W, SCREEN_H) with parameters (x, y)-(w,h). Then you can get away with only drawing the inscribed circle with black outside after plopping down potentially visible areas.

Other-otherwise, maybe there is some simple OpenGL magic where you can just let the system cull the quads you want hidden. I know scissor test can block out a rectangle at a time, is there some sort of scissor-buffer for arbitrary shapes?

Good luck, be sure to throw up a screenshot!
Rob
Logged
Glaiel-Gamer
Guest
« Reply #2 on: February 08, 2009, 08:47:40 PM »

you could try the stencil buffer
Logged
Fletch
Guest
« Reply #3 on: February 08, 2009, 09:16:56 PM »

you could try the stencil buffer

That's the one... I knew it started with an S.
Logged
Chris Whitman
Sepia Toned
Level 10
*****


A master of karate and friendship for everyone.


View Profile
« Reply #4 on: February 09, 2009, 12:05:43 AM »

My recommendation is to drop an extra multiply layer on top of everything.

If you use the stencil buffer, you're going to have hard edges on your lights, and you cut out the possibility of using light of various colours or intensities, since fragments can only pass or fail the stencil test.

Edit: if you're talking about using both, you'd have to check and see what the actual performance benefit is for using the stencil buffer. Keep in mind that it does not actually cull triangles, everything still has to be transformed before the stencil test occurs. It'd probably be easier (and faster) just to cull triangles manually if they are going to be completely in darkness, rather than using the stencil buffer.
« Last Edit: February 09, 2009, 12:09:39 AM by Chris Whitman » Logged

Formerly "I Like Cake."
Will Vale
Level 4
****



View Profile WWW
« Reply #5 on: February 09, 2009, 02:45:15 AM »

I agree with all the suggestions to just draw the black layer.

One tweak though - to avoid having to use a giant mask texture, just create your circular spotlight at roughly the size you want - maybe with a nice fade or whatever. Then set the texture address mode to CLAMP in both directions and draw a screen-size quad with the texture coordinates set to put the spot where you want it. That will handle the region outside the spot's circle nicely.

Multiple spotlights are harder since the blend is destructive. You could use multitexture or compose multiple masks into destination alpha (masking off colour) and then finally unmask colour and blend a black quad over the scene using the destination alpha as the key.

HTH,

Will
Logged
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #6 on: February 09, 2009, 03:26:41 AM »

Spelunky's lighting mechanic is great for gameplay, but I wanna see more games with real shadows, like Gish. Not sure how they did that; using a shadow volume seems a bit overkill for 2d.
Logged
Chris Whitman
Sepia Toned
Level 10
*****


A master of karate and friendship for everyone.


View Profile
« Reply #7 on: February 09, 2009, 10:32:49 AM »

I'd recommend the destination alpha method if all your lights are the same colour, actually.

Otherwise, just allocate a big frame buffer, draw your lights into it and multiply it over the scene. If your target machines cannot draw a few quads without a significant performance hit, you might be barking up the wrong tree if you're looking for fancy visual effects.

Alternately, you could support both and have a detail setting for slower machines.
Logged

Formerly "I Like Cake."
Ivan
Owl Country
Level 10
*


alright, let's see what we can see


View Profile
« Reply #8 on: February 09, 2009, 10:49:45 AM »

He'd have to render to texture for that to be fast enough, but yeah. That would be:

- clear screen
- draw lights as quads textured with light gradient flares
- render result to texture
- clear screen
- draw actual scene
- draw the rendered texture on top with multiply blending

This is actually a really good solution, because you can have any kind of images as light shapes then and you can easily flicker them by changing the alpha (or brightness, depending on how you're blending the final overlay) values when you render them out. Aaand, you can change the resolution of the render texture to adjust the quality of the lights (You would probably need a very low res texture for that anyway, unless you have fancy light shapes)

P.S. edit: And as Adam cleverly pointed out, you can have spotlights too that way by rendering out directional cones of light. You can make them smooth too by just texturing them with a blurry square texture!

 Hand Metal Left Droop  Hand Metal Right

« Last Edit: February 09, 2009, 10:57:09 AM by toastie » Logged

http://polycode.org/ - Free, cross-platform, open-source engine.
dspencer
Level 3
***


View Profile WWW
« Reply #9 on: February 11, 2009, 11:45:01 AM »

Hi,
Thank you all for your responses, they are really helpful! I've been thinking about how I want this to look, and rendering the light to a texture seems to be the best plan (just because I can do different intensities and multiple sources of light. Opengl Frame Buffers should be widely supported by different graphics cards, right?

Thank you all again. I'm teaching myself how to use opengl with this project, so even the solutions I didn't use (like the stencil buffer) were useful and taught me something new.
Logged

Queight
Guest
« Reply #10 on: February 12, 2009, 12:30:58 AM »

Classic article about this topic: http://www.gamedev.net/reference/programming/features/2dsoftshadow/
Work based on modified approach from previous article: http://www.incasoftware.de/~kamm/projects/index.php/2007/09/08/soft-shadows-2d/
Different shading - SSAO in 2d: http://triangularpixels.net/games/category/graphics/
Logged
Robotacon
Pixelhead
Level 3
******


Story mode


View Profile
« Reply #11 on: February 12, 2009, 09:54:59 AM »

What about this appoach?

Shaders with bumpmapping and shine based dithering.

Sorry for not having a newer screenshot but I'm working on a painting tool for this kind of graphics complete with spritesheet, tile and map editor.
Logged
William Broom
Level 10
*****


formerly chutup


View Profile
« Reply #12 on: February 13, 2009, 02:43:49 AM »

What about this appoach?
http://www.serius.se/images/vault/aa_darkness_shaders_dither.png
Shaders with bumpmapping and shine based dithering.

Sorry for not having a newer screenshot but I'm working on a painting tool for this kind of graphics complete with spritesheet, tile and map editor.
You made this? It looks awesome. Can we see it in motion?
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic