Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
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  
  2d Lighting Question. Because there aren't enough of them.  (Read 4306 times)
0 Members and 1 Guest are viewing this topic.
Offline MunchGamer

Senior Newbie


Medals: 1



« Posted 2012-02-05 07:23:59 »

Hello!

I'm having trouble with a lighting problem.  I understand the concept of creating a lightmap, and even have a set of pixel blending functions graciously provided by Dx4 in this post: http://www.java-gaming.org/topics/some-new-blendmodes-add-multiply-overlay-etc/24529/view.html.

I'm currently running my prototype in FSEM with a BufferStrategy, Direct3d turned off by runtime options.  Now, I'm wondering, given the Graphics I've been drawing all my lovely (read: horrifyingly primitive) sprites to, how exactly should I access the pixel data for the blending methods?  Keeping a separate BufferedImage and calling out each pixel is EXTREMELY slow (especially in 1080p).  Even having that BufferedImage and not applying a lightmap to it, then drawing it with g.drawImage(im, blah) is slow.  Is there something I'm missing?

Best,
Munch

[EDIT] the end goal is something akin to this: http://www.youtube.com/watch?v=Xmn6zhDJGLE&feature=fvwp&NR=1 though without the bump mapping.
Offline theagentd
« Reply #1 - Posted 2012-02-05 07:46:02 »

A good approach would be to draw your game normally and then blend a light map onto it. For the light map just draw each light with additive blending to the light map and finally blend the light map to the screen buffer with multiplicative blending. You're right about the slowness though. A 1080p framebuffer has almost 2 million pixels, and with a lightmap you'd be filling every one of those at least twice assuming terrain and sprites cover the whole screen + the lightmap blending + any overdraw. As you've disabled hardware acceleration all this is done by the CPU, and you're CPU isn't a graphics card. Graphics cards exist for a reason. xd

To improve performance you can try to reduce the resolution of the light-map to 1/2th to 1/4th the resolution of the screen. With bilinear interpolation (which is pretty much free if you re-enable hardware acceleration) the difference is completely indistinguishable while light rendering performance shoots through the roof.

The video you posted also has another thing: shadows. If you want shadows, then you'll have to drop Java2D and use OpenGL. OpenGL can handle hundreds of shadow-casting lights per frame, while unaccelerated Java2D can hardly draw an image covering the whole screen with blending... >_>

Myomyomyo.
Offline MunchGamer

Senior Newbie


Medals: 1



« Reply #2 - Posted 2012-02-05 07:50:49 »

I am dissapoint.  I'm really not looking forward to learning OpenGl, it's intimidating...!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline MunchGamer

Senior Newbie


Medals: 1



« Reply #3 - Posted 2012-02-05 08:20:50 »

Oh also, for some reason FSEM in Java is extremely slow with D3D turned on.  With 0 game logic it peaks around 80 fps at 1080p.
Offline cylab

JGO Ninja


Medals: 38



« Reply #4 - Posted 2012-02-05 10:59:41 »

theagentd is right, you probably have to switch to an hardware accellerated api for 1080p with "effects". Take a look at slick2d or libgdx, which should offer enough of a high level api to make transition from java2d easier (especially slick2d, since it kind of mimicks the java2d concepts) and let you dig into the rendering pipeline, when you need to get advanced. libgdx will also offer android support.

Mathias - I Know What [you] Did Last Summer!
Offline pitbuller
« Reply #5 - Posted 2012-02-05 11:38:57 »

For Libgdx I have made light library that allow pretty much those effect that you see on that video out of the box.
http://code.google.com/p/box2dlights/

That work even on mobile devices and you dont have fiddle with opengl bits at all.


Offline MunchGamer

Senior Newbie


Medals: 1



« Reply #6 - Posted 2012-02-05 17:52:06 »

For Libgdx I have made light library that allow pretty much those effect that you see on that video out of the box.
http://code.google.com/p/box2dlights/

That work even on mobile devices and you dont have fiddle with opengl bits at all.




Cheesy neat.
Offline MunchGamer

Senior Newbie


Medals: 1



« Reply #7 - Posted 2012-02-05 18:19:43 »

For Libgdx I have made light library that allow pretty much those effect that you see on that video out of the box.
http://code.google.com/p/box2dlights/

That work even on mobile devices and you dont have fiddle with opengl bits at all.




Was trying to install it, but it seems to not mesh with the most recent libgdx download.  What version of libgdx are you using with it?
Offline pitbuller
« Reply #8 - Posted 2012-02-05 23:08:59 »

Nightlies. 0.92 is bit too old and there is ton of content and bugfixes happened after that point.

For fastest testing there is independed test project builded http://code.google.com/p/box2dlights/downloads/detail?name=box2dLightTestProject.zip
Offline MunchGamer

Senior Newbie


Medals: 1



« Reply #9 - Posted 2012-02-06 18:00:46 »

This may belong in the LWJGL board, but it's an extension of this thread so... yeah.

I'm working on using LWJGL to render a lightmap on top of the 2d scene (using orthographic projection).  Would it work to render the lightmap to an FBO, explored in this tutorial?: http://lwjgl.org/wiki/index.php?title=Render_to_Texture_with_Frame_Buffer_Objects_%28FBO%29

Sincerely,
Knee-deep-in-OpenGL-and-confused
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #10 - Posted 2012-02-06 18:59:44 »

This may belong in the LWJGL board, but it's an extension of this thread so... yeah.

I'm working on using LWJGL to render a lightmap on top of the 2d scene (using orthographic projection).  Would it work to render the lightmap to an FBO, explored in this tutorial?: http://lwjgl.org/wiki/index.php?title=Render_to_Texture_with_Frame_Buffer_Objects_%28FBO%29

Sincerely,
Knee-deep-in-OpenGL-and-confused


be advised that FBO are pretty advanced, and only available using OpenGL 3.0 and later
if you want a lot of people to be able to play your game, don't use it. Alternatives include VBO and such

Offline MunchGamer

Senior Newbie


Medals: 1



« Reply #11 - Posted 2012-02-06 19:28:01 »

This may belong in the LWJGL board, but it's an extension of this thread so... yeah.

I'm working on using LWJGL to render a lightmap on top of the 2d scene (using orthographic projection).  Would it work to render the lightmap to an FBO, explored in this tutorial?: http://lwjgl.org/wiki/index.php?title=Render_to_Texture_with_Frame_Buffer_Objects_%28FBO%29

Sincerely,
Knee-deep-in-OpenGL-and-confused


be advised that FBO are pretty advanced, and only available using OpenGL 3.0 and later
if you want a lot of people to be able to play your game, don't use it. Alternatives include VBO and such

So... create a VBO, render the lightmap to that, then render the VBO on top of the scene?
Offline pitbuller
« Reply #12 - Posted 2012-02-06 20:49:49 »

This may belong in the LWJGL board, but it's an extension of this thread so... yeah.

I'm working on using LWJGL to render a lightmap on top of the 2d scene (using orthographic projection).  Would it work to render the lightmap to an FBO, explored in this tutorial?: http://lwjgl.org/wiki/index.php?title=Render_to_Texture_with_Frame_Buffer_Objects_%28FBO%29

Sincerely,
Knee-deep-in-OpenGL-and-confused

I have two technique for lights.
Fixed pipeline goes like this:

1. Render scene.
2. Clear alpha channel(use color mask for other channels, clear alpha to ambient)
3. Render Lights with blending, alpha channel as intesity. gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
4. Render fullscreen black quad with blending. gl.glBlendFunc(GL10.GL_ONE, GL10.GL_DST_ALPHA);
5. Render UI.


Shaders + FBO's goes like this.

1. Render scene.
2. Render Lights with blending to small size FBO, use alpha channel as intesity. gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
3. Use gaussian blur to FBO.
4. Render FBO over to scene with custom shader that draw lights where intesity is high and shadow where intesity is low.
5. Render UI.

Both techniques are as fast. Shaders give soft lights/shadows but fixed pipeline work everywhere.
Offline MunchGamer

Senior Newbie


Medals: 1



« Reply #13 - Posted 2012-02-06 22:30:50 »

This may belong in the LWJGL board, but it's an extension of this thread so... yeah.

I'm working on using LWJGL to render a lightmap on top of the 2d scene (using orthographic projection).  Would it work to render the lightmap to an FBO, explored in this tutorial?: http://lwjgl.org/wiki/index.php?title=Render_to_Texture_with_Frame_Buffer_Objects_%28FBO%29

Sincerely,
Knee-deep-in-OpenGL-and-confused

I have two technique for lights.
Fixed pipeline goes like this:

1. Render scene.
2. Clear alpha channel(use color mask for other channels, clear alpha to ambient)
3. Render Lights with blending, alpha channel as intesity. gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
4. Render fullscreen black quad with blending. gl.glBlendFunc(GL10.GL_ONE, GL10.GL_DST_ALPHA);
5. Render UI.


Shaders + FBO's goes like this.

1. Render scene.
2. Render Lights with blending to small size FBO, use alpha channel as intesity. gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
3. Use gaussian blur to FBO.
4. Render FBO over to scene with custom shader that draw lights where intesity is high and shadow where intesity is low.
5. Render UI.

Both techniques are as fast. Shaders give soft lights/shadows but fixed pipeline work everywhere.


Super n00b question: How do I clear the alpha channel?
Offline pitbuller
« Reply #14 - Posted 2012-02-06 23:00:30 »

1  
2  
3  
4  
5  
6  
7  
   private void alphaChannelClear() {
      Gdx.gl10.glClearColor(0f, 0f, 0f, ambientLight);
      Gdx.gl10.glColorMask(false, false, false, true);
      Gdx.gl10.glClear(GL10.GL_COLOR_BUFFER_BIT);
      Gdx.gl10.glColorMask(true, true, true, true);
      Gdx.gl10.glClearColor(0f, 0f, 0f, 0f);
   }
Offline MunchGamer

Senior Newbie


Medals: 1



« Reply #15 - Posted 2012-02-06 23:14:10 »

Ok great.  But what about this problem:  With colored lights rendered over a white background (eg. snow) that blending function just takes it straight to white.  Render the black quad first? Huh
Offline MunchGamer

Senior Newbie


Medals: 1



« Reply #16 - Posted 2012-02-07 01:04:08 »

I seem to have found a workaround by, before clearing the alpha buffer, blending a colored 'light' onto the scene in the location (with reduced radius) of the future alpha buffered light.

I am next investigating into this approach: http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/dynamic-2d-soft-shadows-r2032?md=viewrev&rva=1&rvb=1#revisions which seems to take the approach of rendering the light into the alpha buffer FIRST, then for each light, rendering the scene.  The emissive pass at the end could be used to render the whole scene in an "ambient" sense under the darkness as well.
Offline pitbuller
« Reply #17 - Posted 2012-02-07 16:51:17 »

Ok great.  But what about this problem:  With colored lights rendered over a white background (eg. snow) that blending function just takes it straight to white.  Render the black quad first? Huh
Yeah you find the problem. But there is easy fix for that. Just use
1  
glBlendFunc( GL_DST_COLOR,GL_SRC_COLOR);

instead.

You really don't want to render your scene per light. Instead just draw light to accum buffer and blend that over the scene. For that you dont even need any shaders.
Offline MunchGamer

Senior Newbie


Medals: 1



« Reply #18 - Posted 2012-02-07 18:09:28 »

Ok great.  But what about this problem:  With colored lights rendered over a white background (eg. snow) that blending function just takes it straight to white.  Render the black quad first? Huh
Yeah you find the problem. But there is easy fix for that. Just use
1  
glBlendFunc( GL_DST_COLOR,GL_SRC_COLOR);

instead.

You really don't want to render your scene per light. Instead just draw light to accum buffer and blend that over the scene. For that you dont even need any shaders.

Using that blend function makes it look really bad, you can see the edges of the triangle fan of the lights instead of the smooth dropoff you get with a src_alpha blend.
Offline pitbuller
« Reply #19 - Posted 2012-02-07 18:15:32 »

That blend mode works for me but its not work with current shadow shader that I use but with withoutShadows shader(what is just texture color and nothing more) its work very nicely.

I will commit changes and screenshot later tonight.

Edit: in the trunk.
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 (18 views)
2014-07-24 01:59:36

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

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

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

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

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

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

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

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

Riven (50 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!