Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  [libgdx] Shaders, Light system.  (Read 9030 times)
0 Members and 2 Guests are viewing this topic.
Offline CyberJoe

Senior Newbie





« Posted 2013-02-08 12:13:53 »

Hi, new here!

So me and my friend are working hard on our 2D game engine based on LibGDX,
and it can do a lot but there is no support for Lights, So we need to implement a good lighting system,

I am relatively new to shaders though, while I've read lot's of cool tutorials on how it's done, the question that is left for me is an architecture of that all.
There are tons of tutorials on how to cast a light on one particular texture, but we have an entire scene, build of many many textures.

So what is the best way to do that? Render all scene to FBO, then apply light to it? if so, is it optimal to render entire scene to FBO or maybe just parts that potentially will be light up? What are the common practices/mistakes? Basically I am not seeing entire picture of how is this usually done Smiley Anyone did this before? would really appreciate any help or advice, and I can provide a screenshot of a level for example to make it really clear what I need to be done.

Thank!
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #1 - Posted 2013-02-08 12:53:03 »

I dont think sendng all lightsources to the shader is an good idea.
If its just 2d, you could just calculate an light level with each block?
Offline CyberJoe

Senior Newbie





« Reply #2 - Posted 2013-02-08 12:58:27 »

I am not sure I understand what do you mean by "Block"

Let me show you a picture with example.



And here is an awesome tutorial I read before:
https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson6
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cheatsguy

Junior Member


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Reply #3 - Posted 2013-02-08 13:06:56 »

There's another tutorial you should take a look at that isn't exclusively java, is this the effect you're looking for? http://www.gamedev.net/page/reference/index.html/_/technical/graphics-programming-and-theory/dynamic-2d-soft-shadows-r2032

Also, this wiki post has loads of goodies for this effect. http://www.java-gaming.org/topics/2d-dynamic-lighting/27012/view.html

Busy between school, work, life, games, programming and general screwing around.
If you'd like some pixel art for your game, send me a PM, i'll see what I can do.
Current project: http://elementalwarblog.wordpress.com/
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #4 - Posted 2013-02-08 13:08:34 »

Haha, i hoped for an much more simple design (square tiles) Smiley.
I guess the simplest solution is using an texture with all the light sources (in small scale).
Hopefully someone with more knowledge could answer this, im curious about it to Smiley
Offline CyberJoe

Senior Newbie





« Reply #5 - Posted 2013-02-08 13:15:51 »

Thanks a lot guys, I'll read through some links! Smiley

But it's worth mentioning that I do not need any complex type of lights like directional ones, just point light that ill illuminate it's surroundings in a round shape.
I can basically do this with a small fragment shader code for one texture, but if used on entire screen I guess a Frame Buffer Object needs to be used, and here I am not sure about architecture and best practices.
Offline CyberJoe

Senior Newbie





« Reply #6 - Posted 2013-02-08 14:21:46 »

I think I will use a better wording if I say I need to implement "Ambient light" or "Spot Light", not sure but this seems like the right terminology do describe it Smiley
Offline cheatsguy

Junior Member


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Reply #7 - Posted 2013-02-08 14:23:35 »

A spot(or point) light is usually light that starts from one specific point and continues outward. Is that sort of what you're looking for?

Busy between school, work, life, games, programming and general screwing around.
If you'd like some pixel art for your game, send me a PM, i'll see what I can do.
Current project: http://elementalwarblog.wordpress.com/
Offline CyberJoe

Senior Newbie





« Reply #8 - Posted 2013-02-08 14:35:31 »

I am not sure about the naming, I am looking for effect that looks like a round circle. (you can see it on a screenshot)
Basically it's a simple effect of a gradient fading circle that overlays the scene. It gives the near surroundings a color. It does not have direction or anything.

And most importantly it does not need any fancy things like stop on walls or whatever.

What I need, looks like this Smiley
Offline cheatsguy

Junior Member


Medals: 3


Gamer turned Pixel Artist turned Programmer


« Reply #9 - Posted 2013-02-08 15:36:38 »

That is a point light. The first link I posted should help you understand them.

Busy between school, work, life, games, programming and general screwing around.
If you'd like some pixel art for your game, send me a PM, i'll see what I can do.
Current project: http://elementalwarblog.wordpress.com/
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #10 - Posted 2013-02-08 17:07:19 »

Do the lights need to be dynamic? If not, just bake them into your textures.

If the lights need to be dynamic, you can "fake" it by overlaying them as a second draw pass. Basically this involves rendering a transparent feathered circle atop your scene, using different vertex colors and blend modes to achieve something like this (Slick code here). The problem, of course, is that it is not truly dynamic -- it won't cast any shadows if you place the light next to an occluder (wall or player).

For something more dynamic you'll want to use a solution like in box2dlights. If you want to learn how it works, read here.

You can also use a normal mapping technique like I discussed in the tutorials, but generally that's not going to be very efficient if you have many lights (unless you apply some sort of deferred lighting technique). Further, these shader-based techniques may perform poorly on Android/iOS.

Offline CyberJoe

Senior Newbie





« Reply #11 - Posted 2013-02-08 21:30:03 »

Wow, thanks! I think I am more interested in the one where it is pseudo dynamic, I am afraid bo2xlight might be using too much resources, so I'll take a look at your example code, looks pretty good so far! This is nor a primarily Android game, it is PC, but with a possibility of future Android port.
I will look at the example code more detailed tomorrow, but just to clarify, is this the right method?
1) Draw entire screen to buffer
2) Overlay Light to buffer using shaders
3) Render buffer to actual screen
4) Render elements that are not affected to light on screen.

If so, is there a room for optimization like:
1) render only parts that are expected to be affected by light to buffer so buffer will take less memory? (Does this make sense?)
2) Any caching techniques maybe?

Thanks a lot again!
Offline davedes
« Reply #12 - Posted 2013-02-08 21:59:43 »

box2dlights will generally be less intensive since it is not dependent on fill-rate and multiple draw passes.

The steps you outlined will work but involve more fill rate. Really the main performance hit here is the extra fill and state changes involved with rendering to an FBO with different shaders before rendering to screen.

Offline CyberJoe

Senior Newbie





« Reply #13 - Posted 2013-02-09 09:24:07 »

So, that means that even if I do not need "more dynamic" system, box2dlight is still better in performance then the fake dynamic overlaying to buffer approach?
Offline pitbuller
« Reply #14 - Posted 2013-02-09 12:25:00 »

So, that means that even if I do not need "more dynamic" system, box2dlight is still better in performance then the fake dynamic overlaying to buffer approach?
As author of box2dLights I can say that performance with box2dLights is way better than any other multiple light solution that I have tested. If you don't need shadows then algorithm is cheap as hell. Its designed to work with mobile devices so performance with pc is top notch.

Offline CyberJoe

Senior Newbie





« Reply #15 - Posted 2013-02-09 13:48:54 »

Oh well, that's great to hear then! we just go ahead and try to use it then! Thanks a lot!

First i read through the code of box2light I was a bit worried that it uses a physics engine to do a calculations, and I thought it might be slow, but yeah if you say it's not then it's great news.

I hope it is possible to use it with Stage class right?
Offline pitbuller
« Reply #16 - Posted 2013-02-09 13:54:00 »

Oh well, that's great to hear then! we just go ahead and try to use it then! Thanks a lot!

First i read through the code of box2light I was a bit worried that it uses a physics engine to do a calculations, and I thought it might be slow, but yeah if you say it's not then it's great news.

I hope it is possible to use it with Stage class right?


Should be usable with Stage.
Box2d is only used for raycasting what is needed for shadows. Box2d have really good and efficient acceleration structure for collisions so that plus native code make generic raycasting with it really fast.
Offline CyberJoe

Senior Newbie





« Reply #17 - Posted 2013-02-09 14:28:07 »

Thanks!

I'll show you some screenshots when integrated, the project we are working on is hopefully very beautiful, and I hope you will like to see how it will all look like with box2dlight integrated Smiley

Here is the website if curious.
http://underwaterapps.com/project_moon/

Thanks again!
Offline CyberJoe

Senior Newbie





« Reply #18 - Posted 2013-02-11 15:32:57 »

Hi, a quick question:

The diffuse mode seems a lot more eye appealing, but it has some awkward effect when lights overlap. (green+green = red)
Is that a normal behavior? Or did I do something wrong?
Offline Danny02
« Reply #19 - Posted 2013-02-11 17:35:33 »

sounds like a byte overflow bug with the blending
Offline CyberJoe

Senior Newbie





« Reply #20 - Posted 2013-02-12 14:33:20 »

Yeah, but is it like a known bug of box2dlight or just me?
Offline CyberJoe

Senior Newbie





« Reply #21 - Posted 2013-02-16 16:05:52 »

Let me show the code, hope you guys can pinpoint what am I doing wrong here. (please help!)

Box2dLight used.

The Application Listener:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public void create() {   
   testStage = new BaseStage(800, 480, true);      
   FileHandle backgroundPicture = Gdx.files.internal("assets/sampleBG.png");
   texture = new Texture(backgroundPicture);
   Image img = new Image("bg", texture);
   img.x = 0;
   img.y = 0;      
   testStage.addActor(img);      
}
@Override
public void render() {      
    GLCommon gl = Gdx.gl;
    gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
   
    testStage.act(Gdx.graphics.getDeltaTime());
    testStage.draw();
}


The Base Stage:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
public class BaseStage extends Stage implements InputProcessor {
   
   RayHandler rayHandler;  
   World world;
   OrthographicCamera camera;
   
   public BaseStage(float width, float height, boolean stretch) {
      super(width, height, stretch);
      Gdx.input.setInputProcessor(this);
     
      camera = new OrthographicCamera(80, 48);
      world = new World(new Vector2(0, 0), true);
      rayHandler = new RayHandler(world);
      rayHandler.setCulling(true);
      rayHandler.setCombinedMatrix(camera.combined);      
      rayHandler.useDiffuseLight(true);
      rayHandler.setAmbientLight(0.2f, 0.2f, 0.2f,1.0f);
   }

   @Override
   public void draw () {
      super.draw();        
     
      rayHandler.updateAndRender();
   }
   
   @Override
   public void dispose() {
      rayHandler.dispose();
   }
   
   public Vector2 gdxCoordinateToBox2D(float x, float y) {
      float K = 10;
      return new Vector2(x/K-width/(2*K), height/(2*K)-y/K);
     
   }

   @Override
   public boolean touchUp(int x, int y, int pointer, int button) {
      Vector2 coordinate = gdxCoordinateToBox2D(x, y);
      Color clr3 = new Color(0.5f,0.8f,0.2f, 0.9f);
      Light pt = new PointLight(rayHandler, 5, clr3, 30, coordinate.x, coordinate.y);
      pt.setStaticLight(true);
     
      return false;
   }
}




The Problem:



So when lights overlap, instead of getting more greener, it kinda cancels itsef and get's to original picture.
I guess I am using it wrong, any help will be so much appreciated!
Offline pitbuller
« Reply #22 - Posted 2013-03-02 23:03:22 »

Sorry for late answer.
Thats not a bug. Problem is related to low dynamic range rendering. You just overburn the color buffer and everything is solid white at those points where many light overlap.
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.

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

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

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

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

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

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

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

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (56 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
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!