Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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 1313 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.

PROBLEM 1:
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
   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.

PROBLEM 2:
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
« 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.

1  
2  
3  
4  
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.

Myomyomyo.
Offline iamtommo

Senior Newbie





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

<3!

Thanks alot for the info!
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.

Pippogeek (36 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (41 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (60 views)
2014-09-21 02:42:18

BurntPizza (30 views)
2014-09-21 01:30:30

moogie (35 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (52 views)
2014-09-19 03:14:18
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!