Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Realistic Lighting in a 2D game  (Read 19774 times)
0 Members and 1 Guest are viewing this topic.
Offline Cero
« Posted 2009-03-29 16:34:22 »

We are talking about a platformer here, imagine Castlevania.

And what I would like to create is Lightning that is really realistic and reflective.
I mean a room, there is a lightsource, objects in the room are lightend the way the should.

If you create just a tile based game, there is not much lightning... but if you were to insert lightsources and stuff like that.

imagine even a broken lamp that flickers. that would really increase authentic and atmosphere

Was something like that ever done / is there even a game like that ?
how would you do it ?
are there "tricks" ?

Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2009-03-29 17:04:57 »

Depends on your technology, however I've found calculating vertex colours of the tiles when using OpenGL how some great effects.



Kev


Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #2 - Posted 2009-03-29 19:01:02 »

I do per-pixel lighting in Rescue Squad 2. Basically there's three passes, first a full bright pass consisting of the visible sprites:



Then there's a lightmap pass where sprites and other light geometry are drawn to:



Both of those are captured to a texture, and the final pass just multiplies the two together:



The nice thing about this is that lights can be any shape (like the search light's cone of light) and quite detailed, since really they're just another sprite. And it works on pretty much any pc hardware too.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #3 - Posted 2009-03-29 23:52:06 »

ya but how do you subtract darkness from the lightning map

I mean, most likely you would do it kinda professional =D
I would take a black color with an alpha value which is overall very high, and very low at lightsources

which means, since I'm using Java2D  Graphics.setColor and fill methods
only problem is that I cant render a square which a oval round which is empty =P
and I dont even wanna think about reflection in this regard

Offline mh114

Junior Member





« Reply #4 - Posted 2009-03-30 08:18:14 »

ya but how do you subtract darkness from the lightning map

I mean, most likely you would do it kinda professional =D
I would take a black color with an alpha value which is overall very high, and very low at lightsources

which means, since I'm using Java2D  Graphics.setColor and fill methods
only problem is that I cant render a square which a oval round which is empty =P
and I dont even wanna think about reflection in this regard
But you don't have to. Smiley If you do it like Orangy Tang explained, you can additively combine lights on top of an ambient light, and finally multiply the whole shebang with the unlit scene. You also get colored lights for free with this technique, unlike when you subtract alpha from black.

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #5 - Posted 2009-03-30 09:28:35 »

Although if you're stuck with Java2d then that's an entirely different kettle of fish - I doubt that method would run fast enough (although I'd be interested in seeing how fast it does run).

If you're stuck with j2d, then your best bet is to do the lighting manually per-pixel in a big working buffer - I believe L4kD does this so you might want to poke around the source for that. But you'll probably be limited to quite low resolutions with that method.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Cero
« Reply #6 - Posted 2009-03-30 10:50:05 »

I just looked into L4kD

seems awfully tedious to do in j2D
Overall I'm not that good of a programmer to do it like he did.

How difficult / how much is it to "convert" a j2d game in JOGL or slick or whatever you guys would suggest ?
I mean of course that depends on my game, but afterall its just the rendering processes which isnt as much

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 74
Projects: 15


★★★★★


« Reply #7 - Posted 2009-03-30 10:58:17 »

Its probably pretty quick to convert to slick since the api is very similar to java2d. However if you decide to use JOGL or LWJGL you'll have to use the opengl api which will take longer to convert too.
Offline zammbi

JGO Coder


Medals: 4



« Reply #8 - Posted 2009-03-30 11:12:09 »

If you still want software rendering you could try pulpcore: http://www.interactivepulp.com/pulpcore/
It might have what your looking for.

Current project - Rename and Sort
Online CommanderKeith
« Reply #9 - Posted 2009-03-30 13:25:28 »

Try this java2d library:

http://download.java.net/javadesktop/scenario/effects/releases/0.4.9/javadoc/com/sun/scenario/effect/Blend.Mode.html

It has additive blending, and it can be accelerated by the java2d d3d pipeline.

I've used it once just to test it out and it worked pretty good, but I didn't use additive blending, i just used the bloom effect.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zammbi

JGO Coder


Medals: 4



« Reply #10 - Posted 2009-03-30 14:02:47 »

Where's more info about this library?
I couldn't find much by searching for it...

Current project - Rename and Sort
Online CommanderKeith
« Reply #11 - Posted 2009-03-30 14:06:36 »

Yeah it's pretty hard to track it down. It's geared up for javafx so it's gone under the radar as a java2d API so far....

Check out this:
https://scenegraph.dev.java.net/faq.html

Offline skinny boy

Junior Member





« Reply #12 - Posted 2009-09-26 01:48:03 »

I do per-pixel lighting in Rescue Squad 2. Basically there's three passes, first a full bright pass consisting of the visible sprites:

Then there's a lightmap pass where sprites and other light geometry are drawn to:

Both of those are captured to a texture, and the final pass just multiplies the two together:

The nice thing about this is that lights can be any shape (like the search light's cone of light) and quite detailed, since really they're just another sprite. And it works on pretty much any pc hardware too.

why cant this multiply be done in j2d?? without considering the performance
can someone plz give me a hint or link to the math behind this (pixel by pixel) multiplication??

thank you in advance
Offline terloon

Junior Newbie





« Reply #13 - Posted 2009-09-26 08:54:45 »

I'm actually trying to create that affect. =) But, I'm not in that part of my development yet. I've always enjoyed the retro look of sprites, and I wanted to meld this with realistic lighting and shadow affects.

gamecodemonkey.blogspot.com
towerofinfinity.blogspot.com
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #14 - Posted 2009-09-26 11:51:40 »

why cant this multiply be done in j2d?? without considering the performance
can someone plz give me a hint or link to the math behind this (pixel by pixel) multiplication??

thank you in advance

The j2d api only* provides out-of-the-box functionality for compositing using the alpha channel, not for colour compositing. You can easily implement your own custom ColorComposite**, but it obviously won't be hw accelerated, so will be all-but useless in relation to performance.

I'm sure there is an RFE somewhere in the bug database on the subject, as its absence significantly restricts the usefulness of j2d for many interesting effects.

(* Technically there is one color composite operation supported; XOR mode. It's a legacy bit of functionality from the early days of Java; it doesn't use the more modern Composite interface. Also I remember reading somewhere that it doesn't work [very well/at all] in the hw pipeline.)

( ** Though you also need to be aware you will run into all sorts of problems with custom composites if you try and use them when drawing onto a VolatileImage or BufferStrategy.)

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline skinny boy

Junior Member





« Reply #15 - Posted 2009-09-26 14:32:10 »

ok, so i wont use it in  a game,,

but i asked "without considering the performance, can someone give me a hint or link or something about the math involved in this type of ColorComposition?"

i mean, does it just uses the src and dest pixel and " +" , or "*", or some binary operator, or.... ?

thank you
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #16 - Posted 2009-09-26 19:34:43 »

ok, so i wont use it in  a game,,

but i asked "without considering the performance, can someone give me a hint or link or something about the math involved in this type of ColorComposition?"

i mean, does it just uses the src and dest pixel and " +" , or "*", or some binary operator, or.... ?

thank you

Each channel of each pixel in the source is multiplied with the corresponding channel of each pixel in the destination, and the result is placed in the destination.
i.e.

Colorresultant = (Rsrc*Rdst,Gsrc*Gdst,Bsrc*Bdst)

A quick google found this, which in turn pointed to this

btw, I guess everybody has noticed but been too polite to mention the mistake in the original question?
Quote
Realistic Lightning in a 2D game

The two are very different problems =) (though not entirely unrelated!)

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline skinny boy

Junior Member





« Reply #17 - Posted 2009-09-27 17:43:40 »

thank you very much for your reply and links
Offline Cero
« Reply #18 - Posted 2009-09-27 23:06:11 »

btw, I guess everybody has noticed but been too polite to mention the mistake in the original question?
The two are very different problems =) (though not entirely unrelated!)

I guess nobody noticed. Fixed.

@ Topic, this is all above my head at this point.

Offline skinny boy

Junior Member





« Reply #19 - Posted 2009-09-28 15:58:57 »


Colorresultant = (Rsrc*Rdst,Gsrc*Gdst,Bsrc*Bdst)

if the values are float, then i suppose this works, (with a step of "1.0 (minus) "final result" " perhaps?)
if the values of the three channels are integers, i cant seem to work it out
for example, a light red, value70, and the light source with value 200. multiplying them, the final result is out of the permitted range [0,255],
so i guess there are two options:
the multiplication is a logical AND
or the programmer decides upon the ratio of the two values (destination, light), perhaps 3/7 meaning that the destination number will be multiplied by 0.3 and the light number by 0.7, bigger values for the destination mean that the difference between illuminated areas and non-illuminated will be smaller...

i believe i should make a code example to prove it (to myself mainly, and to all who are interested secondly)... damned university, taking all of my sober time away...
Offline rdcarvallo

Senior Member


Projects: 5
Exp: 15 years


2D Java games forever!


« Reply #20 - Posted 2009-09-28 17:49:09 »

I did some flashlight effect in J2D for my unfinished entry at LD48.

In pseudo code:

1  
2  
3  
4  
5  
define cone of light;
use cone for clipping;
draw scene;
define gradient (transparente to black) in the direction of light
paint the cone with the gradient;


The result is something like this image:


Then I added the same logic for a square around the player, and some options to control the ligh focus(distance v/s spread) getting this:


Don't know how is the performace in old machines or old JVM, but in my PC (athlon 64 3000Mhz, 1GB DDR2, nVidia7600GS 256 MB, jdk1.6_XX) it works fine.

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #21 - Posted 2009-09-28 20:46:25 »

hey rdcarvallo, you wouldnt happen to mind sending me/ posting the source code for that would you?
Offline skinny boy

Junior Member





« Reply #22 - Posted 2009-09-29 01:29:09 »

1  
2  
3  
4  
5  
define cone of light;
use cone for clipping;
draw scene;
define gradient (transparente to black) in the direction of light
paint the cone with the gradient;


thank you for the algorithm
Offline rdcarvallo

Senior Member


Projects: 5
Exp: 15 years


2D Java games forever!


« Reply #23 - Posted 2009-09-29 08:04:19 »

Here goes the code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
//g2 is a Graphics2D object
//p is a Player object
//map is a Map(TileMap) object
Arc2D.Double arc = new Arc2D.Double();
arc.setArcByCenter(p.getLightX(), p.getLightY(), radius, p.getAngle()-spread, 2*spread, Arc2D.PIE);
GradientPaint gp = new GradientPaint(p.getLightX(),p.getLightY(),new Color(0,0,0,0),
    p.getLightX()+radius*(float)Math.cos(Math.toRadians(p.getAngle())),p.getLightY()-radius*(float)Math.sin(Math.toRadians(p.getAngle())),new Color(0,0,0));
g2.clip(arc);
map.drawTo(g2);
g2.setPaint(gp);
g2.fill(arc);
g2.draw(arc);
g2.setClip(null);


And the Demo, could you post how it runs on your machines?

http://www.demegames.cl/demos/Journey08.jar
Controls:
LEFT, RIGHT: walk
UP, DOWN: flashlight angle
Z, X: flashlight focus
A: Run
S: Jump
Space: Pause/MiniMap
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #24 - Posted 2009-09-29 13:19:15 »

It freezes right away:P

windows XP
Offline skinny boy

Junior Member





« Reply #25 - Posted 2009-09-29 15:48:05 »

same here, windows XP also

the JVM doesnt even start (the small java icon at the bottom right of the screen..)
Offline rdcarvallo

Senior Member


Projects: 5
Exp: 15 years


2D Java games forever!


« Reply #26 - Posted 2009-09-29 16:42:58 »

Strange... I tested in my home (WinXP SP2, JDK1.6_XX).

I'll recompile for 1.4 and upload again later, now I'm at work.

 
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #27 - Posted 2009-09-29 16:54:40 »

that wont help, i have 1.6.

the problem is that the main screen appears, but then I get stuck there....
Offline Hansdampf

Senior Member


Projects: 3


too offending?


« Reply #28 - Posted 2009-09-29 16:56:45 »

the secret is to press s !!!

lots of sillystupid games: http://www.emaggame.com
Offline rdcarvallo

Senior Member


Projects: 5
Exp: 15 years


2D Java games forever!


« Reply #29 - Posted 2009-09-29 18:23:15 »

Ha... Press "S" to start Roll Eyes, thanks Hansdampf!!
Pages: [1] 2
  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.

E.R. Fleming (13 views)
2014-07-29 03:07:13

E.R. Fleming (4 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!