Shadow Mapping

There is code to come, this one really baked my noodle, so the code is currently appaling, I’ll post it when I clean it up.

Things to note (hopefully future codea improvements)

  1. z buffering doesn’t work on setContext(image) so dual pass like this doesn’t obey z order, you will notice shadows not falling on all things they should

  2. can’t dynamically switch shader on a mesh, so to make this work I duplicated all my meshes, once for shadow pass, once for final pass

  3. video recording slows things down, on my iPad 2 it runs about 20-30fps.

Did you use a shadow matrix to do this?

Depends what you mean by a shadow matrix… It’s doing 2 pass rendering where the first pass renders an image of depth from the lights point of view ( camera(lightpos…)). Then in the main render pass I’m using the view*projection matrix from the camera settings to lookup against the texture from the first pass and then compare texture encoded depth to the distance from the light.

Wow, looking forward to seeing that code. My shadows look harsher than yours.

Re number 2, is that true? Did you have to reconstruct the whole mesh?

Yeah, I was hoping to just swap shaders, but you can’t so I just init my meshes twice, once for depth, once for lighting. Which is a pain, with better abstraction you could automate it a bit, but I don’t think it’s really usable outside of a little tech demo at the moment.

It may be you can swap shaders and I just wasn’t doing it right :wink:

That looks amazing @spacemonkey. I’ll have to look into the swapping shaders thing — I suspect to swap shaders could cause re-allocation (the shaders may have different attribute buffer requirements, which would need to be allocated in the mesh).

I’ve been trying to swap shaders for a while, i guess I thought it was possible.

Maye you can put a blur on the shader to soften the shadow edge?

Code somewhat tidied here: https://gist.github.com/sp4cemonkey/5112003

.@aciolino I played with a few options on shader swapping with a variety of very odd results. Other things recommend a blur in a 3rd pass, but I guess we could do that just in the lighting pass and read multiple points from the shadowMap.

.@Simeon in the opengl book I have it suggests shader swapping is well supported in opengl itself, because it caches compilations, but I guess it’s the context of your abstractions into lua make it tricky.

I tell ya, that deriveVertexTNB function gets heavily used by you!

@Simeon, can we get the “Paste into” functionality fixed so that the “Feature” of " is remapped to a quote mark?

I just pasted into this code and now have to manually fix the quotes in the “World” tab (at least).

lol, in this case it didn’t really need it because I’m not using textures (so it’s just normals), but all the ADS lighting part was ripped off from my previous lighting work.

Forsome reason when I run it all I get is a black screen. Lots of numbers scrolling on the output window…

EDIT: Found it, was a bunch of < &gt and & in the shader code.

As an aside, you’ve lost the transparency of the object ; when setting a sphere to colors(255,0,0,128), it renders solid.

I went ahead and tested a hack by changing the .a value of the gl_FragColor to vColor.a, and transparency returned. I didn’t change the shadow map, so the sphere shadows are still at 100%.

Heh - changing the pillares to have transperency and a color made a pretty trippy result - lookls like there’s several light sources on each pillar’s shadow.

Updated gist. It’s now iOS 6.0 or above compatible only, but the shadows are now correct using a glsl extension to avoid missing z-buffers.