Slick Forums

Discuss the Slick 2D Library
It is currently Wed May 22, 2013 4:51 pm

All times are UTC




Post new topic Reply to topic  [ 89 posts ]  Go to page 1, 2, 3, 4, 5, 6  Next
Author Message
PostPosted: Sat Oct 23, 2010 10:37 pm 
Offline
Oldbie
User avatar

Joined: Fri Jul 20, 2007 9:25 am
Posts: 410
Location: Croatia
Hello...

I'm interested to find a good method to use lighting in slick games.
There are few topics about lighting, but all of them pretty much don't provide solution beyond "use ability to alpha on image cornes".

Has anyone got reusable lighting working on non-tileset game?

This is concept I'm workin on right now. Basically it has been done but no one has provided examples. The concept is to render the game fully lit, as it's all under maximum light, then rendr shadow over it using black image with alpha values.
Steps:
1. render game world and objects fully lit
2. prepare translucent images that will be used as light source. Light = alpha in images, e.g. full light = transparent part
3. create a new, black image
4. for every light source in game, render light source image (step 2) so only alpha of image is rendered (Graphics.ALPHA_MAP mode is it?)
5. render the image over the game

So.. this might work but it will never be good enough as it does not actually produce light, areas with maximum light won't ever be blindingly bright or white...

I'm writing here to see if we can settle this once and for all, as there is no solution for lighting in slick so far. Any ideas how to do proper lighting?

EDIT: SOLVED
we've managed to build it, just keep reading


Last edited by Kova on Mon Nov 08, 2010 5:52 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 24, 2010 3:33 pm 
Offline
Game Developer

Joined: Sun Nov 12, 2006 11:18 pm
Posts: 890
Location: Germany
Hi Kova,

the example in SVN Slick/src/org/newdawn/slick/tests/AlphaMapTest.java might give you some good idea to get started.

At least it shows how an image with an alpha map is applied to some texture image which might result in a way to create some lighting effect.

Nevertheless I would also be interested in some simple example code where some (maybe different colored) lights are placed on screen.
A similar example using a TiledMap would also be cool :wink:

Maybe we can work something out? Or someone else already did it and just throws in his code example?

_________________
Right Angle Games | Marte Engine
Back to the past | Star Cleaner | SpiderTrap


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 24, 2010 5:35 pm 
Offline
Regular

Joined: Tue Mar 03, 2009 11:53 pm
Posts: 123
Hi!

In order to have "blindingly bright" aresa you need to change the blending mode when rendering the lightmap. Right now I don't remember how to do it on OpenGL, but I can try to put some code together:)

ciao!

_________________
Carotinho


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 24, 2010 6:17 pm 
Offline
Regular

Joined: Tue Mar 03, 2009 11:53 pm
Posts: 123
Ok, here I am. I think what you want it's already in Slick.

First, create an image, which will represent the "light", and make it black where you want no light, and dark gray when you want it to enlight.

then, use this code:

Code:
g.setDrawMode(Graphics.MODE_ADD);
light.drawCentered(posX, posY);
g.setDrawMode(Graphics.MODE_NORMAL);


MODE_ADD means that OpenGL takes the colour already present in the framebuffer, and numerically adds the colour you provide. 0 is black, and 1 is white, so 0.1 is a little bit of light to add.

ciao!

_________________
Carotinho


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 25, 2010 1:24 am 
Offline
Oldbie
User avatar

Joined: Fri Jul 20, 2007 9:25 am
Posts: 410
Location: Croatia
thank you for your replies

I am aware of alpha map test, but the principle used there is not good enough for lighting. In test first you draw the alpha then you blend the real images... I've found couple of problems that I won't mention now.

Also I know about additive blending (for a bright light) in OpenGL, but this would require to draw light images (e.g. translucent white) in addition to drawing shadow images... I think I wont touch this some time.

Anyway I'm working on something so I'll post my findings when I'm done. Alternative is to pick up a opengl book and learn lighting in low level, then use it directly in code... I hope this won't be needed for now.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 25, 2010 9:08 am 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
I've generally avoided using alpha maps for this sort of thing, it makes it harder to do line of sight lighting. Stick to vertex colours and doing ray casting calcs yourself, it should make it easier to put things in the way of the lights and cast shadows.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 25, 2010 9:55 am 
Offline
Oldbie
User avatar

Joined: Fri Jul 20, 2007 9:25 am
Posts: 410
Location: Croatia
Thx for the advice Kev, I'll look into that, I'm having too many troubles with alpha approach anyway.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 01, 2010 10:12 pm 
Offline
Game Developer

Joined: Sun Nov 12, 2006 11:18 pm
Posts: 890
Location: Germany
Hmm, still wondering how this could work...
I want some tile based level with several circular light spots in there. No shadows, just some gradient light circles (getting darker from center to border). Those light circles should be at any position on screen...

Can I do this using vertex colors? Can I do this using blend mode ADD? How would I assure that the rest of the screen stays black? Calculating and drawing black rectangles?

Is there some SUB mode? Then I could create a black image the size of the screen and subtract some gradient circles to have some "see through" light holes.
Drawing order would be: draw the level tiles, calculate position of spotlights and prepare black image with "see through" holes. Draw black image with holes on top of the level tiles...

Is there any simple source code to find?

You know, there is this competition over at http://www.experimentalgameplay.com titled "Night and Day" and there is this game idea in my head :wink:

_________________
Right Angle Games | Marte Engine
Back to the past | Star Cleaner | SpiderTrap


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 02, 2010 7:23 am 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Vertex colours could do that. You don't need any blending mode. You will need a custom renderer for TileD (or just modify the the renderer Slick has).

Your data model is:

1) Tiled Map
2) X,Y location for each light source.
3) Strength/Falloff for each light source.

When rendering:

1) Each time you render a tile for each corner:
a) Work out how far it is from the light (or used distance squared for efficientcy)
b) Decide if the point would be effected by any light (pick the one it would be effected by the most)
c) Set the vertex colour on the image you're about to draw based of the distance from the light. Linear fall off is easiest, i.e. multiply 1 over the distance by the light strength to work out the brightness.
2) Render every tile this way and the colours of the tiles will mesh together into a lighting map.

Extensions:

1) Don't just pick one light - combine the lights that effect a particular vertex.
2) Support for coloured lights by adding that to the data model and setting the vertex colour based on it.
3) Non-Linear Falloff, lights don't generally falloff linearally. Change 1c above.
4) Efficiency. Calculate the lighting of vertex colours sepeartely and only update when a light is changed.
5) Accuracy. Don't use the tile map to do the light, superimpose a tilemap over the top and render semi transparent tiles over the top of the scene to darken areas. Same method, just no tile images. The nice thing about this is you can change the resolution of the light based tilemap to make the effect look better.
6) Line of sight checks - even cooler, when you detemine which light effects a particular vertex take into account the blocking properties of your tiledmap - this is actually ultra efficient on a tilemap as wolfenstein showed. This means that light sources can be blocked from lighting particular areas - which net result gives you shadowy areas and is totally awesome :)
7) Dynamic lights - lights that flicker, lights that shudder (like torches in RPGs).

I'll try and knock up a demo soon unless Kova would rather I didn't - can see how hard you're working on this mate :)

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 02, 2010 10:06 am 
Offline
Game Developer

Joined: Sun Nov 12, 2006 11:18 pm
Posts: 890
Location: Germany
kevglass wrote:
Vertex colours could do that. You don't need any blending mode. You will need a custom renderer for TileD (or just modify the the renderer Slick has).

I think I will create my own renderer - similar to your platformer code as I want to keep the map editing simple (text file based).

Your explanation helps a lot, sounds pretty much like an exercise from school, especially in the Extensions section :lol:
Quote:
When rendering:

c) Set the vertex colour on the image you're about to draw based of the distance from the light. Linear fall off is easiest, i.e. multiply 1 over the distance by the light strength to work out the brightness.

So I multiply some distance and light strength based value to each R,G,B of the corner's color?

Quote:
Extensions:
...

Yes, in that order :lol:
No, you're right - all your suggestions make a lot of sense and I would love to have them...
Quote:
I'll try and knock up a demo soon unless Kova would rather I didn't - can see how hard you're working on this mate :)

Kev

Non native language error - not sure if I understand who's doing a demo and/or who is working hard and/or I'm missing some kind of joking/irony?!

Thanks a lot, Kev, I'll start fiddling around tonight...

Cheers,
Tommy

_________________
Right Angle Games | Marte Engine
Back to the past | Star Cleaner | SpiderTrap


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 02, 2010 11:13 am 
Offline
Oldbie
User avatar

Joined: Fri Jul 20, 2007 9:25 am
Posts: 410
Location: Croatia
I see my name mentioned so here I am to report things I've tried

I gave up on the lighting sample after I failed with
a) alpha map lighting
b) quick direct OpenGL lighting

with a) alpha map lighting the main problem was that I couldn't find proper way to blend the light images into one big light image. The idea was to start with all black, and for each light source take a light image where fully lit area is alpha = 0, full transparent, and render alpha channel to final light image, then the final light image would be rendered to the game as black image with translucent holes where the light is. Problem was I couldn't make a OpenGL blend mode where it would properly render light images, as I needed a blend mode that from source and destination alpha would render the lower one.. and that doesn't exist or I couldn't figure it out. Also as Kev said, it would be a pain to do shadows like this.

with b) OpenGL lighting directly, I've read couple of tutorials and enabled lighting but I couldn't make light that I added to work, not even change global light to fully illuminate everything although I did the needed code. This looks promising but I decided it would take too much time (a month?) to learn it all and provide a decent lighting system, so I postponed it for now and continue working on other parts of my game as they provide more "gameplay value" right now.

If I gave the idea that I'm working on a lighting system right now, sorry, I'm not, but if I ever have something new, I'll post here.

kevglass wrote:
Vertex colours could do that. You don't need any blending mode. You will need a custom renderer for TileD (or just modify the the renderer Slick has).
....


Excellent guide Kev!

Just on a side-note I want to add that this will work for tiled games, but what I'm trying to do is make lighting for non tiled games. So extension 5) got my attention...

kevglass wrote:
5) Accuracy. Don't use the tile map to do the light, superimpose a tilemap over the top and render semi transparent tiles over the top of the scene to darken areas. Same method, just no tile images. The nice thing about this is you can change the resolution of the light based tilemap to make the effect look better.


I don't quite understand this. Could you explain / rephrase this? It sounds like you first say don't use tilemap for lighting, then later to use it, just no images.

kevglass wrote:
I'll try and knock up a demo soon unless Kova would rather I didn't - can see how hard you're working on this mate :)


I must admit I don't get this either... must be irony as any code provided is good... so yeah, do the demo please, it will surely help a lot of people.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 02, 2010 1:33 pm 
Offline
Game Developer

Joined: Sun Nov 12, 2006 11:18 pm
Posts: 890
Location: Germany
Kova wrote:
kevglass wrote:
5) Accuracy. Don't use the tile map to do the light, superimpose a tilemap over the top and render semi transparent tiles over the top of the scene to darken areas. Same method, just no tile images. The nice thing about this is you can change the resolution of the light based tilemap to make the effect look better.


I don't quite understand this. Could you explain / rephrase this? It sounds like you first say don't use tilemap for lighting, then later to use it, just no images.


I think that's pretty simple (if I understood it correctly). Imagine your screen or game window is covered by some virtual grid of a given size (64x64 pixel for example). Then you could cover the whole screen/game window with a bunch of the same semi transparent image of size 64x64 pixel and just use the appropriate vertex colors based on your light sources that are placed anywhere on your underlying non tiled game area.
If you lower the size of this virtual grid (32x32 or 16x16) and the image you'll get an even smoother lighting effect (at the cost of performance of course).

But you use this grid just to cover your screen/game area with images to render the effect of your light sources.

BTW: I guess you could also implement fog of war using this mechanism...instead of light sources name it "discovered areas"...

_________________
Right Angle Games | Marte Engine
Back to the past | Star Cleaner | SpiderTrap


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 02, 2010 2:04 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Quote:
BTW: I guess you could also implement fog of war using this mechanism...instead of light sources name it "discovered areas"...


Yep, I used this in the old old old RPG, discovered areas got "lit". I'll try and find time for a demo later.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 02, 2010 11:48 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Roughed up a demo, shows how it works and what the limitations are. I've tried to explain how to extend it to get different types of lighting in the comments.

Image
Image
Image

The code is in Slick SVN under examples:

https://bob.newdawnsoftware.com/repos/s ... tTest.java
https://bob.newdawnsoftware.com/repos/s ... Light.java

It only uses Slick existing code as far as I know.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2010 8:25 am 
Offline
Game Developer

Joined: Sun Nov 12, 2006 11:18 pm
Posts: 890
Location: Germany
Wow! You're the man, Kev!

Thanks a lot for this great example! I think I got it now how to do it :wink:

Must have been a long night for you coding this 8)

Are the sample sprites from lostgarden.com?

Again, one more thank you!

Cheers,
Tommy

_________________
Right Angle Games | Marte Engine
Back to the past | Star Cleaner | SpiderTrap


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 89 posts ]  Go to page 1, 2, 3, 4, 5, 6  Next

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group