Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  How To Create Lighting (Java2D)  (Read 1779 times)
0 Members and 2 Guests are viewing this topic.
Offline ghostsoldier23

Jr. Member
**

Posts: 67
Medals: 1



« on: 2011-12-28 10:40:29 »

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?
Offline theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #1 on: 2011-12-28 11:06:27 »

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.

There is no god.
Offline ghostsoldier23

Jr. Member
**

Posts: 67
Medals: 1



« Reply #2 on: 2011-12-28 13:12:23 »

Is a lightmap just a map of alpha values?
Games published by our own members! Go get 'em!
Offline theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #3 on: 2011-12-28 14:29:19 »

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?).

There is no god.
Offline ghostsoldier23

Jr. Member
**

Posts: 67
Medals: 1



« Reply #4 on: 2011-12-29 00:21:07 »

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)
Offline nsigma

Sr. Member
**

Posts: 342
Medals: 18



« Reply #5 on: 2011-12-29 07:16:41 »

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

Offline theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #6 on: 2011-12-29 08:20:09 »

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)
Well, I can give you some psuedocode. =S

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
//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

There is no god.
Offline ghostsoldier23

Jr. Member
**

Posts: 67
Medals: 1



« Reply #7 on: 2012-01-01 15:26:55 »

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.
Offline theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #8 on: 2012-01-01 15:54:25 »

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.

There is no god.
Offline ghostsoldier23

Jr. Member
**

Posts: 67
Medals: 1



« Reply #9 on: 2012-01-01 16:00:28 »

But how do you change the alpha values on the channel to alter the lighting?
Games published by our own members! Go get 'em!
Offline The Voice

JGO n00b
*

Posts: 3



« Reply #10 on: 2012-02-06 13:00:19 »

Check out java.awt.RadialGradientPaint
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.066 seconds with 19 queries.