Hi All,
Kinda new to XNA and Computer Graphics in general, trying to do shadow mapping for a point light (as opposed to a directional light). I got it semi working rendering six separate textures, and then projectively texturing them into my main view over six passes - as you can imagine it was incredibly slow (12 passes in all just to get to a lightmap of the scene!!). So I decided that I should use a cubemap instead of 6 separate textures, thus allowing me to do the projective texturing in a single pass as opposed to six.
Herein lies my problem - I am at a complete loss as to how I actually make the cubemap.
What I am doing at the moment is something like the following (not at my development machine right now, so treat this more like psuedo code):
|
// Fields
|
| RenderTargetCube cubeRT; |
| TextureCube cubeTex; |
| ... |
| |
| // Constuctor |
| cubeRT = new RenderTargetCube( device, device.PresentationParameters.BackBufferWidth, SurfaceFormat.Single ); |
| |
| ... |
| |
| // Draw Method |
| |
| device.SetRenderTarget(0, cubeRT, CubeFace.PositiveX ); |
| drawMySceneUsingShadowMapHLSLEffect(positiveXViewProjectionMatrix); // I don't really have that method, it is an illustrative example |
| |
| device.SetRenderTarget(0, cubeRT, CubeFace.PositiveY ); |
| drawMySceneUsingShadowMapHLSLEffect(positiveYViewProjectionMatrix); // I don't really have that method, it is an illustrative example |
| |
| device.SetRenderTarget(0, cubeRT, CubeFace.PositiveZ ); |
| drawMySceneUsingShadowMapHLSLEffect(positiveZViewProjectionMatrix); // I don't really have that method, it is an illustrative example |
| |
| // ... and so on for each face. |
| |
| device.SetRenderTarget(0, null); |
| cubeTex = cubeRT.GetTexture(); |
| |
| |
| |
After getting that texture I draw the scene again, using my DiffuseLightMap HLSL technique. I'll explain my algorithm:
The cube texture *should* contain depth maps for each of its views. Working under this assumption, the pixel shader of my DiffuseLightMap shader calcualtes the distance between each pixel and the light, on one hand, and looks up the light's perceived depth to that pixel (calculate vector from pixel to light, and then use that as the second argument to texCUBE( CubeMapSampler, vectorToPixel) ). I just realised that I probably need to normalize that vector - I will have to try that. Anyhow, continuing on. With the two distances calculated, I compare them to decide if the pixel is in shadow or not.
I have got shadow mapping working for a directional light, so I know that my shadow map shader, and my projective texturing (the latter in principle) shader work. What I don't know is if I am generating the cubemap right, and whether I am consuming it correctly in my projective texturing shader.
I will try to post the actual code when I get home, but perhaps if someone could post a simple example on using a cube map (needn't be shadow mapping, I am sure that if I can get even a basic cube map for reflections or something working I can extend it to shadow mapping without too much trouble).
Thanks in advance.
I'm too busy writing computer graphics for my day job to have any games in play-test or peer-review.
Twitter:
twitter/guyshermanBlog:
www.guysherman.com