Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (683)
Games in Android Showcase (196)
games submitted by our members
Games in WIP (750)
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  
  [Solved] Lighting problem  (Read 1544 times)
0 Members and 1 Guest are viewing this topic.
Offline iamtommo

Senior Newbie

« Posted 2011-12-06 20:33:10 »

I've been working on my per-tile lighting system, currently it is very simple and supports coloured lighting.

I started to notice that lighting wouldn't work for random tiles, here is an example:

At first i thought it was a problem in my tiling system, however if i disabled lighting the tiles were fine.

Here is my lighting code:
   public void updateLightValues() {
      for (Tile t : Engine.getMap().getVisibleTiles()) {
         Point p1 = new Point((int) x, (int) y);//Player coordinates
         double tileDistance = p1.distance((double) t.getX(), (double) t.getY());//the distance from the player to the tile
         tileDistance /= Tile.SIZE;//tiles are 16x16 therefore this is the amount of tiles between the player and the tile.
         if (tileDistance > lightDistance) continue;//if the distance is further than the light source's max distance, skip this tile.
         float light = lightDistance;//the initial light value (alpha value)
         light -= tileDistance;
         light /= 10;
         light += Engine.getMap().getLightModifier();//currently getlightmodifier() returns 0.
         if (light < 0) light = 0;
         if (light > 1) light = 1;

         float a = light + t.getLightValue() * (1 - light);
         float r = ((red * a) + (t.getLightRed() * t.getLightValue())) / a;
         float g = ((green * a) + (t.getLightGreen() * t.getLightValue())) / a;
         float b = ((blue * a) + (t.getLightBlue() * t.getLightValue())) / a;
         t.setLightValues(r, g, b, a);

That method is called every tick and will update light values around the light source.
Note that a light is represented by a LightSource, and the lightsource class holds all variables including this method.

My second problem is with the colour blending. It seems that a light source will blend into another light source, however the light source it's blending into won't be counter-blended, producing a weird blend like this:

Thankyou for your time.
Offline theagentd

« JGO Bitwise Duke »

Medals: 807
Projects: 4
Exp: 8 years

« Reply #1 - Posted 2011-12-07 03:59:04 »

Lighting always has additive blending. If you shine two lights on a surface, it will emit twice as many photons. Your blending problem is probably because you modify the alpha which would affect all lights touching the tile.

float a = light + t.getLightValue() * (1 - light);
float r = ((red * a) + (t.getLightRed() * t.getLightValue())) / a;
float g = ((green * a) + (t.getLightGreen() * t.getLightValue())) / a;
float b = ((blue * a) + (t.getLightBlue() * t.getLightValue())) / a;

How is this going to do additive blending? The light intensity of a tile is the sum of all lights affecting it.

You can solve this by keeping the RGB intensity in each tile. You add the RGB color intensity from all lights affecting the tile (modified by distance, etc), and draw the tile with that color (which might be far over 1.0). You should know that your graphics card can do all this for you. Just create an FBO, add level-sized texture to it and draw the lights circles or a textured quad where you want them. You can even use the scissor test to only light the tiles that are actually visible on the screen, or even a smarter algorithm that has a smaller texture if you have a large/infinitely large world.

Offline iamtommo

Senior Newbie

« Reply #2 - Posted 2011-12-09 21:30:15 »


Thanks alot for the info!
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

orrenravid (161 views)
2016-07-16 03:57:23

theagentd (230 views)
2016-07-11 14:28:54

Hydroque (313 views)
2016-07-06 05:56:57

Hydroque (465 views)
2016-07-03 08:52:54

GrandCastle (348 views)
2016-07-01 09:13:47

GrandCastle (340 views)
2016-07-01 09:09:45

CopyableCougar4 (394 views)
2016-06-25 16:56:52

Hydroque (374 views)
2016-06-22 02:17:53

SwampChicken (344 views)
2016-06-20 13:22:57

SwampChicken (290 views)
2016-06-20 13:22:49
Making a Dynamic Plugin System
by Hydroque
2016-06-25 00:13:25

Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42

FPS Camera Tutorial
by Hydroque
2016-05-22 05:40:58

Website offering 3D Models specifically for games for free
by vusman
2016-05-18 17:23:09

Website offering 3D Models specifically for games for free
by vusman
2016-05-09 08:50:56

Website offering 3D Models specifically for games for free
by vusman
2016-05-06 11:10:21

Website offering 3D Models specifically for games for free
by vusman
2016-04-29 12:56:17 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‑
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!