2D lighting without shaders in libgdx

Hi all,

I’m experimenting with lighting in libgdx, and want to achieve an effect similar to the one found in this tutorial: http://www.alcove-games.com/opengl-es-2-tutorials/lightmap-shader-fire-effect-glsl/

However, at the moment, shaders are a bit over my head. I asked around and got a few answers that it was possible to just set OpenGL blending modes when rendering stuff with a SpriteBatch, but I can’t find the exact lines I need to use to get it to work.

Would anyone have a small example of how this could be done without shaders, just using mybatch.setBlendingFunction(…) etc. ?

Thanks!

You can take an image with a value that isn’t 1 in the alpha channel and draw it over other images to produce the same effect. By default blending is enabled with the spritebatches.

I’m not sure I follow, don’t I need some sort of line of code to tell it ‘use these blending modes’ to make it ‘subtract’ the light gradient image from the ‘darkness’ overlay?

As opop65 said, the sprite batch has alpha blend enabled by default. You just need to draw the 1 images on top of each other. If the result is not for Your taste, modify the images and try again.
You just need the base image and the lighting mask, as per in the tutorial.

In this case, would my lighting mask need to be transparent though? If I draw a similar lightmask image as from that tutorial, it just draws it solid and white, with a black square background, on top of my semi-transparent overlay. I don’t need to use batch.setBlendingFunction() to set it up correctly?

No, that function is already enabled. All you need to do is have an overlay with some alpha value that isn’t equal to 1, and draw it after you draw your other images. It’s likely that image doesn’t have an alpha value less than 1, however I don’t know since I haven’t checked.

Box2DLights is probably the easiest way to add lights to a game in LibGDX.

Great, I’ll take a look at that. Is it suitable to use even if my game doesn’t make use of any box2d physics and such?

I been triying this a lot and finaly did something that works.

What I do is:
1- Render to a FrameBufferObject (FBO) the entire scene of the game. With any blend you want (without any “light” or “shadow” here).

//******** PRE-RENDER OF THE SCENE ***********//
fbo.begin();
batch.begin();
		
//Render scene...use any blendfunc you want.
...
...
batch.end();
fbo.end();
//******** END PRE-RENDER ***********//

2- Render what I call an “environtment shadow”. Just a black rectangle with some transparency.

batch.begin();
		
//Normal alpha blending for the environtment shadow...
batch.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
batch.setColor(1, 1, 1, shadowIntensity);
batch.draw(shadow, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.setColor(1, 1, 1, 1);

3- Render to the alpha mask the lights.

Gdx.gl.glColorMask(false, false, false, true);
batch.setBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
for(int i= 0; i < lights.size(); i++) lights.get(i).render(batch);
batch.flush();

4- Render the scene again (its already on the FBO, so you only need to render the FBO). This time, you render considering the values of transparency of the alpha mask.

//Now we render the scene pre-rendered considering the alpha mask...
Gdx.gl.glColorMask(true, true, true, true);	
batch.setBlendFunction(GL10.GL_DST_ALPHA, GL10.GL_DST_ALPHA);
batch.draw(fboRegion, 0, 0);

5- And finally, if you want lights with color, you need to do one more render on top of all this mess to add colors.

batch.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
for(int i= 0; i < lights.size(); i++) lights.get(i).renderLightColor(batch);

Hope it helps.
If you need the Light class just ask for it, but its quite simple.

With this technique I been able to do something like this (the lantern light also change size over time):

http://s22.postimg.org/u8em2v7fl/light1.png

http://s22.postimg.org/5ugbv8ach/light2.png

PD: sorry for bad english xD

The blending enabled in SpriteBatch is alpha blending. If You do not know, how that works, please use google, blending functions are quite simple math functions.
You can however define other, different blendings also. Here’s a link that shows the blending forms http://lessie2d.tumblr.com/post/28673280483/opengl-blend-function-cheat-sheet-well-this-is

You could also try the other methods described in the replies.