Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (498)
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  
  Java 2D Fake Lighting  (Read 4453 times)
0 Members and 1 Guest are viewing this topic.
Offline StumpyStrust
« Posted 2012-07-14 10:52:16 »

So here is meh hacky lighting. It is very hacky and you don't get "real" shadows say but it is super fast and you can choose the shadow map quality which makes it very scalable for different resolutions.

Here are some screens





Here is a prog to test it

http://www.mediafire.com/download.php?8ccknsm60bjycz5

Controls
Q lowers the lighting map quality
W increases it
s lowers the amount of color on lights
a increases color
x lowers ambient light
z increases ambient light

v spawns some lights
click to spawn static larger lights
K kills all lights

I want to know if anyone would be interested in some fake 2d lighting in java 2D. I know that lighting is not that hard with opengl using slick or libgdx but it is still fun trying to do something in plain old java2D.




Offline theagentd
« Reply #1 - Posted 2012-07-14 13:13:57 »

Tried it, but I'm getting terrible flickering here. Might be some driver setting if it's just me.

Works fine otherwise, never drops below 60 FPS. Any chance of more info of how you did it? Does it support any shadows?

Myomyomyo.
Offline Mads

JGO Ninja


Medals: 24
Projects: 3


One for all!


« Reply #2 - Posted 2012-07-14 13:49:53 »

Very nice! It worked for me. Anything open?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StumpyStrust
« Reply #3 - Posted 2012-07-14 16:29:29 »

Quote
Tried it, but I'm getting terrible flickering here. Might be some driver setting if it's just me.

Works fine otherwise, never drops below 60 FPS. Any chance of more info of how you did it? Does it support any shadows?

It is not a driver thing i get is sometimes but I think is because I threw this together really fast and may have done the buffering wrong or something.

Anyways when I say no shadows I mean I am basically drawing images with a specific alpha interpolation that takes the sources alpha and uses that for the destination. So you are not ever casting any rays or gradients or checking to see if you hit some wall.

The pipeline goes like this: Draw everything effected by light, draw lights to an image or lightmap (this is very slow on high resolutions), draw lightmap, draw anything else not effect by light. You can do this any number of times layering lighting effects.

Here is the core of the trick

1  
2  
3  
4  
5  
6  
7  
8  
Image light = drawLights();
            g2d.setClip(light.getGraphics().getClip());
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_ATOP, all));
            g2d.drawImage(light,0,0,frame.getWidth(),frame.getHeight(),null);
           
            g2d.setComposite(AlphaComposite.getInstance(
                      AlphaComposite.SRC_OVER, color));
            g2d.drawImage(light,0,0,frame.getWidth(),frame.getHeight(),null);


The first draw call sets the light and should always be 1. You could lower it to lower all light values but that should not be done here. The second call is how much of the images color you are going to put on top of the light. You could use none but it would look a little strange.

The light map or image I use when I call drawLights() is a smaller resolution then the window/frame/canvas I am drawing on. This reduces the quality of the lights but greatly improves performance to the point where rendering lights cost almost nothing. ( I also like the pixelated effect at low resolutions)  After the lights are drawn onto the image I render the image but stretch it to fit the window.

Here is the drawLights() method

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public Image drawLights()
       {
          BufferedImage buff = BufferUtils.getCompatibleImage(pane.getWidth()/lightmapScale, pane.getHeight()/lightmapScale);
          Graphics2D g = (Graphics2D) buff.getGraphics();
          g.setClip(0,0,buff.getWidth(),buff.getHeight());
          for(int i = 0; i <= lights.size()-1;i++)
          {
             lights.get(i).render(g, lightmapScale);
          }
          g.setColor(Color.black);
          g.setComposite(AlphaComposite.getInstance(
                      AlphaComposite.SRC_OVER, ambient));
          g.fillRect(0, 0, pane.getWidth(), pane.getHeight());

          g.dispose();
          return buff;
       }


There are a lot of optimizations you could do through all of the example but I just wanted to see if it would even work at some form of fps.

The problem is you really cant get cool looking shadows because if you say have a square block the light would go right through it or below it. I think I could come up with something so when the light hits those objects that block light it simply stops. Wont be really cool sharp shadows but it would be much more useable.

Even with this down side, the performance is great and it still can work in many games i think.


Offline The-Next

Senior Member


Medals: 5
Projects: 1



« Reply #4 - Posted 2012-08-20 00:04:45 »

Hi!

Thank you very much for your idea! I was looking for a very fast way of performing lightning an shadows. I managed to do dynamic shadows by clipping the light sprite:

Online jonjava
« Reply #5 - Posted 2012-08-20 00:17:55 »

For those wondering how to achieve the above check out http://www.java-gaming.org/index.php?topic=27012.0

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.

xsi3rr4x (16 views)
2014-04-15 18:08:23

BurntPizza (14 views)
2014-04-15 03:46:01

UprightPath (27 views)
2014-04-14 17:39:50

UprightPath (12 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (51 views)
2014-04-10 04:04:31

BurntPizza (110 views)
2014-04-08 23:06:04

tom_mai78101 (211 views)
2014-04-05 13:34:39

trollwarrior1 (179 views)
2014-04-04 12:06:45

CJLetsGame (185 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!