Testing shadow mapping

In Alien Blitz lights are computed using a mix between a 2d light texture and some custom code to get smooth lights are avoid artifacts on walls.

It’s a good solution as it is requires some time when loading level but it is quite quick afterwards.

I’ve decided to try to implement shadow mapping, I don’t know if I will apply it to Alien Blitz, maybe just the PC version. We will see… but it is still a good exercise and I’d love to see what it could look like.

I’ve not at all completed the task, but I’ve decided to post some “Work in progress” screenshots.

The scene

I’ve made a custom level, with just some basic walls and one light, here is the current result

capture_2014-12-12_18-15-01_0474_Alien_Blitz_

Light point of view

In order to generate the shadow map we need to set a camera on the light itself (what the light can “see”). For testing purposes it is easier to actually use the game camera first.

For the first tests I’ve decided to use a directional light, point lights need to generate 6 textures (surrounding cube), so I will do that later. This light “looks at” and follows the player

capture_2014-12-12_18-58-36_0973_Alien_Blitz_

Depth map

And now the shaders need to be changed to only get a depth map

capture_2014-12-12_20-06-50_0364_Alien_Blitz_

(the UI elements are added after the 3d rendering, so they will not be saved when using a framebuffer later)

The black & white are inverted in this screenshot, else the wall in the back would be almost invisible/pitch black.

Using a frame buffer

Next step is to revert view to its original state and use a frame buffer to render the light’s camera.

screenshot1

Black and whites are now correctly rendered, I’ve changed the FOV on the camera to be able to view the pillar with the 1:1 ratio (1024×1024)

(I’ve saved the texture using the screenshot code from libgdx, it just needs to be called before ending the frame buffer)

Applying shadow/depth map

Removing previous lighting

capture_2014-12-13_12-15-39_0107_Alien_Blitz_

First try

 

capture_2014-12-13_12-31-28_0440_Alien_Blitz_

Something is happening, it doesn’t crash, and lines move when player moves, we’re getting there

 

After some correction

capture_2014-12-13_15-12-24_0604_Alien_Blitz_

Hmmm it looks like letters and signs… Oops forgot to bind the texture, so it’s using another one (the one used by the font apparently)

 

capture_2014-12-13_15-28-37_0042_Alien_Blitz_

Something is obviously off

 

capture_2014-12-13_15-30-09_0483_Alien_Blitz_

Hey! it looks like there’s a small mecha there…

 

capture_2014-12-13_15-40-17_0790_Alien_Blitz_

Ok, there’s obviously a mecha there, and it moves accordingly. Good, but there are way too much artifacts…

 

capture_2014-12-13_16-25-05_0090_Alien_Blitz_

After a bunch of tests and corrections, pillars’ shadows seem correctly placed, but the light does not go far away (should illuminate walls), there doesn’t seem to be any shadow for the mecha and there are still artifacts…

 

capture_2014-12-13_16-29-50_0694_Alien_Blitz_

Some more tests, still a lot of artifacts but I’m beginning to like what is displayed…

 

capture_2014-12-13_16-32-22_0829_Alien_Blitz_

Haa a lot better, let’s remove the right part

 

capture_2014-12-13_16-34-57_0287_Alien_Blitz_

Nice, I also have the “expected” artifacts near the center of the light. They are normal artifact due to distortion, they are to be expected at this point (and corrected afterwards of course).

 

A small video

What’s next ?

This was a simple example with lots of limitations

  • Shadows are computed each frame
  • Only one light
  • No light color
  • Badly written code

So there’s lots of code to do… but it’s a start and I’m pleased I’ve been able to get some shadow mapping so fast.

Comments are closed.