Hello Guest

Dynamic shadows

  • 8 Replies
  • 4664 Views
Dynamic shadows
« on: October 11, 2015, 18:16:37 »
Hey guys

I really really like to learn how to make a shader for dynamic shadows. There are quite some tutorials on the web but to be honest, I find it a little to hard to understand. All of them are very complicated and when there is a source code, I can't make head nor tails from it.

I've been busy with this for a week now and since I still didn't manage to do it, I thought it'd be a good idea to ask it here. Surely someone knows how to do it. So my question is very simple. Can someone show me how to create dynamic shadows with shaders? I'd love to see a very easy-to-understand example to keep things easy so I could compare results. But otherwise a tutorial you used or think is easy enough might do the trick as well.

Oh and one more thing. I'm unable to use OpenGL 3.1 and above. My OpenGL version is 3.0 so I got to stick to what OpenGL 3.0 and below has to offer for shaders. I already made a shader for point and directional lighting and a shader for normal mapping. Both work fine so I got that going for me which is nice. As for the tutorials I used, here they are:

https://www.opengl.org/discussion_boards/showthread.php/134319-Generate-lightmap-for-triangle-mesh
http://joshbeam.com/articles/dynamic_lightmaps_in_opengl/
http://www.alsprogrammingresource.com/lightmapping_tutorial.html
http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/

Thanks in advance!
TheBoneJarmer

*

Kai

Re: Dynamic shadows
« Reply #1 on: October 11, 2015, 18:51:55 »
This is really not easy to explain.

There are in sum three different techniques I know of to produce shadows:
- simple projection shadows (very unpopular because limited to planar shadow receivers and because of other limitations)
- shadow mapping (most popular, can produce soft shadows)
- stencil shadow volumes (can produce hard shadows, requires complicated static mesh preprocessing or dynamically in a shader)

An example of the first technique can be seen in JOML's demo repository: https://github.com/JOML-CI/joml-lwjgl3-demos/blob/master/src/org/joml/lwjgl/ProjectiveShadowDemo.java (requires OpenGL 1.1)
An example of the second technique is available in LWJGL's demo repository: https://github.com/LWJGL/lwjgl3-demos/blob/master/src/org/lwjgl/demo/opengl/shadow/ShadowMappingDemo20.java (requires OpenGL 2.0 + EXT_framebuffer_object)

But first be clear about which technique you want to use. If you want to use shadow mapping then your last mentioned site is actually a very good reference. Just google more for "OpenGL Shadow Mapping" and you'll find everything you need.
« Last Edit: October 11, 2015, 20:09:11 by Kai »

Re: Dynamic shadows
« Reply #2 on: October 11, 2015, 20:10:54 »
Hey Kai!

Thanks for your reply! So it is as I feared. Oh well, then I guess I should do it the hard way *sigh*. That being said, I compiled all examples but I'm unable to run the ShadowMappingDemo20 because it keeps throwing this GLSL error:

Code: [Select]
0:2(1): preprocessor error: #version must appear on the first line

This is nonsense for all I can tell because both the fragment and vertex have a "#version 110" on the first line. Did this happen to you as well? I am able to run the ShadowMappingDemo no prob. That one looks promising as well but I don't think it will work for the purposes I had in mind. You can see the shadow going through the cube which makes the shadow look less realistic. Are these those stencil shadows you were talking about? But it does work. :)

*

Kai

Re: Dynamic shadows
« Reply #3 on: October 11, 2015, 20:14:26 »
You can see the shadow going through the cube which makes the shadow look less realistic. Are these those stencil shadows you were talking about? But it does work. :)
I don't see any shadow "through" a cube. The shadows are of course projected onto the cube (i.e. a cube is at times a shadow receiver), which seems correct to me.

EDIT: About the shader error you were getting: it should work now with that demo, too.
« Last Edit: October 11, 2015, 20:26:02 by Kai »

Re: Dynamic shadows
« Reply #4 on: October 12, 2015, 09:02:17 »
The shadows are of course projected onto the cube (i.e. a cube is at times a shadow receiver), which seems correct to me.

I still do not see it. haha Time to buy some glasses I guess. Oh well, it does the job.

EDIT: About the shader error you were getting: it should work now with that demo, too.

Yes it does, thanks for the fix!

*

Kai

Re: Dynamic shadows
« Reply #5 on: October 12, 2015, 09:05:40 »
Hehe. :)
Just to be sure, can you maybe please make a screenshot and attach it to a (new) message here, or upload it to some free image hosting platform (tinypic.com, imgur.com, ...) and post the link here for me to verify?
Maybe there is indeed an issue. Wouldn't be the first time that everything worked here with those non-standard-conformant Nvidia drivers and everyone else rightfully get problems. :)
« Last Edit: October 12, 2015, 09:07:22 by Kai »

Re: Dynamic shadows
« Reply #6 on: October 12, 2015, 09:14:49 »
Well, I got an Intel driver. Maybe it is the case haha. Anyway, here is the screenshot.
« Last Edit: October 12, 2015, 09:17:35 by TheBoneJarmer »

*

Kai

Re: Dynamic shadows
« Reply #7 on: October 12, 2015, 09:16:38 »
Okay, thanks.

On the first image:

That looks correct to me. The shadow from the rightmost cube is projected onto the side of the center cube.
And the shadow of that center cube is projected onto the side of the leftmost cube.

On the second image:

That also looks correct to me. If it was "cutting" the cube like butter :) you wouldn't see that the shadow is "lifted up"/"folded up" a bit.

Maybe just play a bit with the light height/position. Make it a function of time to also vary the light's position.
« Last Edit: October 12, 2015, 09:22:42 by Kai »

Re: Dynamic shadows
« Reply #8 on: October 12, 2015, 09:19:38 »
Didn't see you already replied. I added another screenshot that shows the light from another direction. It may be projecting the shadow onto the cube, but it looks to me like it is cutting through it like a knife through butter.