2D - Smooth lighting with shadows for a tile-based game

Hi, so I’ve been stalking these forums for the last couple of months now, and it seems like a real great community, I’ve tried asking the question on gamedev.stackexchange (here : http://gamedev.stackexchange.com/questions/104785/2d-smooth-lighting-with-shadows-for-a-tile-based-game) but to no avail.
So, here we go -
I’ve been trying to implement efficient lighting in my starbound/terraria-like game. I’m creating it in LibGDX, and I am aware of Box2D, but I am more doing this for the learning experience, so reinventing the wheel here is useful. I’ve been doing tons of research and experimenting, I’ve seen many posts on here and other places, and I just cannot figure it out. What I’m going for isn’t Terraria’s tiled-based lighting, but like Starbound (http://playstarbound.com/wp-content/uploads/2012/04/Dungeon.jpg).

I do have something that works for the most part :

This is done by shooting a ray to each tile in the area around the light, and if it doesn’t hit any other block, it puts a vertex there. Afterwards, I sort all the vertices and assign them color values of black and create a middle pixel that has a color value of black but with 0 alpha. I learned that shaders automatically create the gradient between the vertices, so this works pretty well. The problem I’ve now encountered is actually darkening everything else. As you can see in the picture, it doesn’t black out the tiles near the edges of the light since they are squares.

My idea right now is to render a black mesh over the screen with an alpha value of one, and I’m wondering if it’s possible, using blendFuncs and blendEquations, to subtract the alpha of the black mesh from the mesh of the light (if that makes any sense) - So the darkness is rendered, then the light afterwards, and I don’t know if there’s a way to just remove some of the alpha from some of the pixels that are overlapping using blend funcs.

P.S. Please excuse my complete lack of knowledge about OpenGL and LibGDX in general, I’m trying to learn it and most of the stuff I’m saying could make absolutely no sense to experts and I may come back in a couple of years and laugh at how naive I was.

You might want to take a look at this tutorial. It isn’t quite what you want but I think could easily be adapted. https://github.com/mattdesl/lwjgl-basics/wiki/2D-Pixel-Perfect-Shadows, it isn’t the easiest but would probably look the best in the end. (disclaimer: I’ve never actually implemented anything in this tutorial, just read it a while back out of interest).

If nothing else, give it a read and it’ll give you an idea of how shadows are more commonly implemented.

I saw this implementation when I first started creating my lighting, so I decided to give it another go, but I can’t figure out how to make it bleed, and the biggest problem of darkening the rest of the screen. But the implementation makes a lot more sense than when I first saw it. And this would be a great way to implement realistic-ish shadows if I ever have to.

Omg, I think it’s working, I was just randomly browsing some of the most viewed posts on the Newbie and Debugging questions area and I happened to see an article called, “2d Lighting Question. Because there aren’t enough of them.”, and then I saw a post by pitbuller for rendering a black quad with some sort of blend function and alpha clearing that he specified, and I think it’s working! It’s a bit buggy, but when I polish it I’ll try and explain as best I can how it’s done so that others hopefully won’t have to go through the suffering I did to get here.

here’s the post : http://www.java-gaming.org/topics/2d-lighting-question-because-there-aren-t-enough-of-them/25681/view.html

So, I’ve run into a problem, once I originally got it working, it didn’t look great because once it hit the block, it went a few more pixels in that direction then just set an alpha value to blend to, so it made the light darker before it had even hit the block which looked weird. So the fix to that was to make a mesh that stops once it hits the block and assign an alpha value there of what it would be at that spot if it were to continue until it reached the end of it’s range. Then, I created another mesh with GL_TRIANGLE_STRIP that goes around the entire previous mesh and goes from the point of contact to a couple extra pixels to quickly interpolate to darkness. This worked well, but now I have a couple of artifacts that I can’t quite figure out. Here’s a video of the (hopefully obvious) artifacts, some more dramatic then others.
And if code is required then I’ll post it.

_C6JH53LyIY

So… I think I know why one of the artifacts is happening, I think the light is being too quickly interpolated when going from shadow to non-shadow and so it looks funky, that’s about all I could figure out and I really have no idea how to fix it. And I still have absolutely no idea why it goes crazy when right next to an edge or corner. Anyone have any ideas?