Shadows of Dominium

Hmm, cool book title maybe… 🙂

This past week – I’ve taken a “Mental Holiday” from UI, Maps et al. My subconcious (being a nearly separate personality in it’s own right it seems) decided that ‘Shadows’ were the order of the week.

So, taking a grateful break from UI work, I decided to dive into ‘pretty’ work as a diversion. It’s been a while since I tinkered with the rendering code, and this has long been ‘on the list’ – so I didn’t need much self-persuasion!

WARNING : This wanders off into techy-talk 🙂

Real-time shadows (and self-shadowing) is a major aspect of the art of rendering, and not to be taken lightly. I have almost been putting this off, as I know ‘what’ to do, and I know ‘how’ hard it can be – and I’ve never really dabbled. 666 has projective planar shadows at the moment, and whilst that works for the model viewer – it doesn’t cut the mustard for games, particularly Dom.

Models need to self-shadow as (by and large) the only thing casting shadows onto them will be themselves. Sure, a passing ship may cast a shadow over it every now and then – or if parked behind a station – both of these can be handled (clunkily) by quick n’ dirty occlusion testing (is object blocking sun from my position – yes, then render me in “shadow”).

For Dom to stand amongst equals, it has to have per-pixel self-shadow mapping at the shader level.

I’ve done lots of research in the past, collated some papers together, and I know the underlying principle. It’s actually remarkably straightforward. I knew the challenge would be “getting it to work”!

There are lots of techniques – I’ve plumped for Shadow Mapping. I have plans to make it optimal and scene context aware, but right now I just want to get the basics to work.

1) Render the object to be “shadowed” from the point of view of the light. This is easy – setup a camera where the light is, aim it at the object and then render the view into a texture. There are two additional things to do. One – we only need “depth” – that is the distance from the “light” to the “pixel” being drawn for the object. Two – we use an orthographic camera, as our light source is (in this instance) directional – so it’s direction is identical at every pixel on the object being rendered.

2) Pass that depth map texture, and the matrix used for the “light camera” into our shader.

3) In the vertex shader, transform the vertex by the “light camera” matrix (I call it the Shadow Caster Matrix). This gives us the position of the vertex in the object, but in the same space as the light camera. Then we do perspective division on this new vertex, to get what would normally be “screen space”. Basically we get a 2D vertex in the range -/+1. This then is mapped into the depth map texture (0 -> 1) and then we can look up the depth of the pixel being rendered from the lights point of view.

4) We pass this to the fragment shader as UV co-ord, and then it does the “is this pixel behind the one in the depth map? if so – it’s getting no light, and is therefore in shadow.”

That’s all done. But – right now – it no workey 🙁

It’s nearly there… but I’m doing something wrong somewhere – and it’s proving to be a ***** to track down. So, either I put this aside and ‘forget’ what I did and try to pick it back up again at a later date… or carry on and fix the thing – delaying the UI work… urf.



  • Facebook
  • Twitter
  • Myspace
  • Google Buzz
  • Reddit
  • Stumnleupon
  • Delicious
  • Digg
  • Technorati
Author: Mak View all posts by

Comments are closed.

Subscribe to The Dominium Observer Newsletter!