Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 3260 times)
0 Members and 1 Guest are viewing this topic.
Offline ghostsoldier23

Junior Member


Medals: 1



« Posted 2011-12-28 15: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 16: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 18:12:23 »

Is a lightmap just a map of alpha values?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2011-12-28 19: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 05: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 12: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 intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline theagentd
« Reply #6 - Posted 2011-12-29 13: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 20: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 20: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 21: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 18:00:19 »

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

 

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

The first screenshot will be displayed as a thumbnail.

Dwinin (21 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (66 views)
2014-09-10 06:39:09

Tekkerue (33 views)
2014-09-09 02:24:56

mitcheeb (54 views)
2014-09-08 06:06:29

BurntPizza (38 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (30 views)
2014-09-07 01:11:22

Longarmx (28 views)
2014-09-07 01:10:19

mitcheeb (37 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!