Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How To Create Lighting (Java2D)  (Read 2598 times)
0 Members and 1 Guest are viewing this topic.
Offline ghostsoldier23

Junior Member


Medals: 1



« Posted 2011-12-28 16: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
« Reply #1 - Posted 2011-12-28 17: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.

Myomyomyo.
Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #2 - Posted 2011-12-28 19:12:23 »

Is a lightmap just a map of alpha values?
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline theagentd
« Reply #3 - Posted 2011-12-28 20: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?).

Myomyomyo.
Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #4 - Posted 2011-12-29 06: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
« Reply #5 - Posted 2011-12-29 13: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

Praxis LIVE - Open-source graphical environment for developing intermedia performance tools, projections and interactive spaces.
Praxis LIVE on Twitter
Offline theagentd
« Reply #6 - Posted 2011-12-29 14: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

Myomyomyo.
Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #7 - Posted 2012-01-01 21: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
« Reply #8 - Posted 2012-01-01 21: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.

Myomyomyo.
Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #9 - Posted 2012-01-01 22:00:28 »

But how do you change the alpha values on the channel to alter the lighting?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline The Voice

Junior Newbie





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

Check out java.awt.RadialGradientPaint
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
hobbles (7 views)
2013-05-22 00:54:55

cubemaster21 (74 views)
2013-05-17 21:29:12

alaslipknot (86 views)
2013-05-16 21:24:48

gouessej (115 views)
2013-05-16 00:53:38

gouessej (110 views)
2013-05-16 00:17:58

theagentd (121 views)
2013-05-15 15:01:13

theagentd (110 views)
2013-05-15 15:00:54

StreetDoggy (155 views)
2013-05-14 15:56:26

kutucuk (177 views)
2013-05-12 17:10:36

kutucuk (175 views)
2013-05-12 15:36:09
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.114 seconds with 21 queries.