2D Lighting Tribulations with Shaders

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

2D Lighting Tribulations with Shaders

Rejechted
(Repost from java-gaming.org)

We've figured out our text renderer issues and have moved onto dynamic lighting.  Currently we are having some trouble.  A bit of background, explained again later: We are new to OpenGL and have just now learned how to utilize shaders, but haven't yet implemented FBOs or VBOs.  Just a bit of context moving forward.

We started out with the example given here:
http://www.youtube.com/watch?v=s60AljUbpKY
which goes into dynamic soft shadows via creating an alpha texture, rendering a black "ambient darkness" sheet with alpha 1.0f over the scene, and then using the alpha map of your lights to "reveal" through the darkness.  Downsides to this system were the lack of our ability to do any kind of bloom effect without shader language, which led us to...

http://www.youtube.com/watch?v=fsbECSpwtig
Finally implementing GLSL and GL2.0 compatibility, we explored the possibility of using shader programs for our lights.  Currently we have the following simple fragment shader that draws a basic circular light:

fragment shader

varying vec2 pos;
uniform vec4 color;

void main()
{
    float t = 1.0 - sqrt(pos.x*pos.x + pos.y*pos.y);
    //Enable this line if you want sigmoid function on the light interpolation
    //t = 1.0 / (1.0 + exp(-(t*12.0 - 6.0)));
    gl_FragColor = vec4(color.r, color.g, color.b, color.a) * t;
}


(The vertices are +/-1, +/-1, so this math works out)

Which is an example from the youtube video.  Using this, we draw the lights to the screen as before, create the alpha map, and then blend this alpha map into our finished scene at the very end.

Again we run into the problem of being unable to add any sort of bloom effect (we assume that this is another type of fragment shader that we would have to pass our lights through), and the idea of how to achieve even "hard" shadows is somewhat of a mystery (save for finding the dot product of the light vector and the normal of the shadow casting surface).  

The context of our project: We are developing a 2D indie title that we hope will be as backwards-compatible as possible (I think GL2 is pretty safe).  However, having good looking shadows gives a game a great "pop" factor.  We are complete strangers to VBOs and FBOs, having used the fixed function pipeline until now.  However, we understand the basic purposes of these objects; we're just lost on implementation and how they might help us arrive at a lighting solution without breaking the rest of our working code.

A picture of our current light... uses a shader program, but uses GL_QUADS to actually draw it... kind of a bastardization of GL2, but we're working on it:



Hopefully you guys see that we have put a decent amount of effort into figuring this out, and aren't just begging for help.  That said, you're the experts, so any advice would be most appreciated.  In return, we can offer a gift... these two droids.  Uh, I mean, the two shaders, once we get them working, so that hopefully others can benefit from our combined efforts.  If you need any more code from our project in order to help, just ask and I'll try to dig up something resembling a program.

Thanks in advance!
Reply | Threaded
Open this post in threaded view
|

Re: 2D Lighting Tribulations with Shaders

Sven Gothel
Administrator
On Wednesday, August 24, 2011 02:29:15 AM Rejechted [via jogamp] wrote:

>
> (Repost from java-gaming.org)
>
> We've figured out our text renderer issues and have moved onto dynamic
> lighting.  Currently we are having some trouble.  A bit of background,
> explained again later: We are new to OpenGL and have just now learned how to
> utilize shaders, but haven't yet implemented FBOs or VBOs.  Just a bit of
> context moving forward.
>
> We started out with the example given here:
> http://www.youtube.com/watch?v=s60AljUbpKY
> which goes into dynamic soft shadows via creating an alpha texture,
> rendering a black "ambient darkness" sheet with alpha 1.0f over the scene,
> and then using the alpha map of your lights to "reveal" through the
> darkness.  Downsides to this system were the lack of our ability to do any
> kind of bloom effect without shader language, which led us to...
>
> http://www.youtube.com/watch?v=fsbECSpwtig
Thats a dramatic 'neon' lighting demo, cute.
Their FS is also pretty simple. Sure they use blending,
which is equivalent of compositioning via FBOs, ie multipass.
The latter would be required if the hardware doesn't support blending,
e.g. on some mobile ES2 implementations.
<FS shader attached>

> Finally implementing GLSL and GL2.0 compatibility, we explored the
> possibility of using shader programs for our lights.  Currently we have the
> following simple fragment shader that draws a basic circular light:
>
> fragment shader
>
> /varying vec2 pos;
> uniform vec4 color;
>
> void main()
> {
>     float t = 1.0 - sqrt(pos.x*pos.x + pos.y*pos.y);
>     //Enable this line if you want sigmoid function on the light
> interpolation
>     //t = 1.0 / (1.0 + exp(-(t*12.0 - 6.0)));
>     gl_FragColor = vec4(color.r, color.g, color.b, color.a) * t;
> }/
>
> (The vertices are +/-1, +/-1, so this math works out)
>
> Which is an example from the youtube video.  Using this, we draw the lights
> to the screen as before, create the alpha map, and then blend this alpha map
> into our finished scene at the very end.
looks great (your screenshot too).

>
> Again we run into the problem of being unable to add any sort of bloom
> effect (we assume that this is another type of fragment shader that we would
> have to pass our lights through), and the idea of how to achieve even "hard"
> shadows is somewhat of a mystery (save for finding the dot product of the
> light vector and the normal of the shadow casting surface).  

yup, sorry. indeed you would need to read about the details of such implementations,
which would exceed this thread for sure.

http://fabiensanglard.net/shadowmapping/index.php
http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=279198
http://www.lighthouse3d.com/opengl/docs.shtml

http://www.punkuser.net/vsm/ !!!! (solves AA problems)

http://www.devmaster.net/articles/projectiveshadows/
>
> The context of our project: We are developing a 2D indie title that we hope
> will be as backwards-compatible as possible (I think GL2 is pretty safe).

I would choose GL2ES2, which would ensure your product will be available
on ES2 devices (Android ..) as well.

> However, having good looking shadows gives a game a great "pop" factor.  We
> are complete strangers to VBOs and FBOs, having used the fixed function
> pipeline until now.  However, we understand the basic purposes of these
> objects; we're just lost on implementation and how they might help us arrive
> at a lighting solution without breaking the rest of our working code.
>
> A picture of our current light... uses a shader program, but uses GL_QUADS

There are no GL_QUADS in ES2, use TRIS and TRI_FANS ..
Please check the Gears ES1 and ES2 demos in our unit tests, which
I have described a few days earlier.

> to actually draw it... kind of a bastardization of GL2, but we're working on
> it:
>
> http://forum.jogamp.org/file/n3279698/qLnhx.jpg 

looks great

>
> Hopefully you guys see that we have put a decent amount of effort into

no need to apologize.

and I am sure, you are willing to maybe put back a unit test or demo .. or both :)
all great.

>
> Thanks in advance!

hoped it helped a bit ..

maybe you can try to enhance our gears w/ the
 VSM <http://www.punkuser.net/vsm/>
shadow technique! We maybe able help, plus this would allow us to have
a little unit test and we know what we are talking about.

Thank you in adnvance!

Cheers, Sven

LightFS.txt (565 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: 2D Lighting Tribulations with Shaders

Demoscene Passivist
Administrator
In reply to this post by Rejechted
To get a concrete impression on how various standard shadowmapping techniques work with JOGL2 (with sourcecode) u may have a look at my blog:

- Simple fixed function pipeline "hard"-shadow mapping
- Fake "soft"-shadows using a mix of shaders and fixed function pipeline
- Completely GLSL based "hard"-shadowmapping
- Percentage closer filtering based "soft"-shadow mapping

Reply | Threaded
Open this post in threaded view
|

Re: 2D Lighting Tribulations with Shaders

Rejechted
In reply to this post by Sven Gothel
Thanks for the advice, regarding ES2 we are in no way going for a mobile platform though.  The project is a bit more complicated than I may have let on.  If you can imagine castlevania, diablo 2, and mass effect esque concepts rolled into a side scrolling environment, you essentially have our project.  This means skill trees and an action bar full of various key-bound abilities and the need to be able to have a large and detailed field of vision.  These two things mean that while we would love to have access to that market, our concept simply isn't built for it.

The concepts for graphics that we are trying to achieve are not very complex, and we're going to have to test what features are compatible with various users' hardware so that we can reach as many people as possible.  For this reason we are avoiding ES2 for the time being.  A lot of the techniques used to render shadows in 3D don't even translate to 2D because of the nature of what 2D is and what a light source is.  If you have a light source in 2D on the same Z as the textures, you are not really casting light on them at all because the dot product of the normal and the light source is zero.

Thanks for the tips, we'll look at the links and see what we can do.

Reply | Threaded
Open this post in threaded view
|

Re: 2D Lighting Tribulations with Shaders

Rejechted
In reply to this post by Demoscene Passivist
Sorry for double post but I wanted to reply to Passivist specifically:

Do these concepts translate well into 2 dimensions, where a GL lightsource is not present and we have defined our own?
Reply | Threaded
Open this post in threaded view
|

Re: 2D Lighting Tribulations with Shaders

Demoscene Passivist
Administrator
>Do these concepts translate well into 2 dimensions, where a GL lightsource is not present and we have defined our own?

Sure, defining ur own lightsource is no problem. U are not locked to using GL_LIGHTx when doing shadowmapping. When using the GLSL shader methods u are defining custom lightsources anyway and the fixed function pipeline method just uses the "buildin" lightsources as convenience variables, wich could be easily replaced by custom ones.

Regarding the "translate well into 2 dimensions" issue: Yes and no! When using shadowmapping u definitly have to build some kind of "3D imposters" of ur 2D spirtes/objects. Taking the correct camera view (e.g. from above) and setting the projection matrix to orthographic will then translate ur 3D scene to 2D. Thats what most (pseudo) 2D games do these days.
Reply | Threaded
Open this post in threaded view
|

Re: 2D Lighting Tribulations with Shaders

Rejechted
This post was updated on .
We actually already do use a LightSource wrapper in Java that has a few fields.

This doesn't really seem to make sense given our current model, which is to just render the shader I described above onto a quad, simulating a circular light.  In a TRUE two-dimensional world, a light directly overhead of a wall is really just a light shining down onto a plane whose normal is orthogonal to the light vector.  In the real world you'd see light on the plane but in computer graphics you wont unless light is pointing at the surface in such a way that the angle is between 0 and 90 degrees.  This is the challenge we're facing.

We maintain a list of entities that should cast shadows.  The plan is to use the edges of their polygons to see which edge normal vectors form the correct angle with the light source, and cast the shadow from the vertices of that edge.  This theory we understand.  The issue is creating the correct "pipeline" of shader executions, rendering the world, applying the lightmap, drawing the shadows, etc. all blended with the correct glBlendFunc so that the end result makes sense.  It's not just a challenge in programming and computer graphics but it's a pretty complex logic puzzle as well.
Reply | Threaded
Open this post in threaded view
|

Re: 2D Lighting Tribulations with Shaders

Schierenbeck
In reply to this post by Rejechted