How To Create Lighting (Java2D)

How can you manipulate BufferedImages loaded from ImageIO (usually of TYPE_BYTE_INDEXED I think?) to create light/dark and maybe even shadow effects? Is this even possible?

Draw everything normally and then overlay a generated lightmap over it. It’s pretty slow though, you might want to switch to OpenGL if you need shadows. Otherwise, you just draw everything normally and keep a separate BufferedImage (typically with only alpha unless you want colored lights). On the second BufferedImage (the lightmap), you draw the light sources. Finally you overlay the lightmap over the normally rendered one with alpha blending.

Is a lightmap just a map of alpha values?

Pretty much. You can use an RGB-image to store the light intensity of each color. You would then “overlay” it over the standard image by multiplying the red, green and blue values separately of the lightmap with the back buffer image. You’d have to treat the values as floats going between 0 and 1 instead of bytes going between 0 and 255 when multiplying of course, like OpenGL does it. There might be a built in function for this in Java2D (AlphaComposite was it?).

I get the concept but I’m a bit confused on how to code it.

Might you be able to provide a code example? (snippets)

You could also try the blend modes in this thread with code from Dx4 and myself - http://www.java-gaming.org/topics/some-new-blendmodes-add-multiply-overlay-etc/24529/view.html - in particular look at multiply.

You’ll need to convert your loaded images to TYPE_INT_RGB or TYPE_INT_ARGB_PRE depending on whether you need an alpha channel. Just draw the loaded image into a BufferedImage of the right type to convert.

If the images are the same size, you can just get the int[] array of the pixels from each (off the top of my head ((DataBufferInt)image.getData().getDataBuffer()).getData(); ) and use the RGBComposite code linked to towards the end of that thread that takes arrays of pixels.

Best wishes, Neil

Well, I can give you some psuedocode. =S


//Game loop

//Logic....
updateEverything();

//Render everything
clearBackbuffer();
drawBackgroundMapLevelEtcToBackbuffer();
for(int i = 0; i < objects.size(); i++){
    objects.get(i).drawToBackbuffer();
}

//Render lightmap
clearLightmap();
for(int i = 0; i < lights.size(); i++){
    lights.get(i).drawLightImageToLightmap();
}

//Overlay the lightmap
setBlendMode(); // to the Graphics object drawing to the backbuffer
drawImage(lightmap, 0, 0, null); //Draw the lightmap to the backbuffer blending it using the above blend mode
restoreBlendModeToStandardMode();


Just follow Nsigma’s advice on BufferedImages and you’ll be fine! =D

But how do you actually modify Alpha values? I tried modifying the values of the Alpha raster and it seemed to have no effect…

(Btw nsigma I will take a look at your blending code, thanks! Is it able to create a lighting effect?)

P.S. I’m probably not going to need to use light sources. Its a nice thought to keep in mind for future development, but this is a fairly simple game and I’m really just looking into a full screen decrease in light.

Just load an image with only an alpha channel (or to keep it simple, an RGBA BufferedImage or something) and draw it over the screen.

But how do you change the alpha values on the channel to alter the lighting?

Check out java.awt.RadialGradientPaint