3

I'm struggling to implement point light shadows using cube shadow maps in DirectX 11. I've searched around and there only really seems to be tutorials in OpenGL or earlier versions of DirectX. I've setup a shadow map texture using CreateTexture2D(), CreateDepthStencilView() and CreateShaderResourceView() using the TextureCube flags where possible. I am then unsure on how to add the different 'camera' positions for each face of the cube map and how I calculate the view/projection matrices. I currently have shadows working for spot/directional lights, but I've never used cube maps before. I just need something to get me started. Thank you in advance.

Edit: I currently have a D3D11_TEXTURE2D_DESC variable to create the shadow texture as a TEXTURECUBE; a depth stencil has been created using D3D11_DEPTH_STENCIL_VIEW_DESC and a shader resource view has been created using D3D11_SHADER_RESOURCE_VIEW_DESC. I'm hoping that his has created the cubemap texture. I'm unsure on how to then initialise it with the view/projection matrix for each face and then pass it to a vertex or possibly geometry shader. Any help is much appreciated.

Jonathan
  • 91
  • 1
  • 8

1 Answers1

0

For each point light you should render the 6 cube map faces as follows: the position for your camera is the position of the point light. You should use a perpective projection with aspect 1 and a 90 degrees field of view. The direction of the camera depends on the cube map face you are rendering. I used sth like this:

switch (faceId)
{
case 0:
    // right
    return glm::vec3(1.0f, 0.0f, 0.0f);
case 1:
    // left
    return glm::vec3(-1.0f, 0.0f, 0.0f);
case 2:
    // up
    return glm::vec3(0.0f, 1.0f, 0.0f);
case 3:
    // down
    return glm::vec3(0.0f, -1.0f, 0.0f);
case 4:
    // front
    return glm::vec3(0.0f, 0.0f, 1.0f);
case 5:
    // back 
    return glm::vec3(0.0f, 0.0f, -1.0f); // ok
}

For the camera up vector:

switch (faceId)
{
case 0:
case 1:
case 4:
case 5:
    return glm::vec3(0.0f, 1.0f, 0.0f);
case 2:
    return glm::vec3(0.0f, 0.0f, -1.0f);
case 3:
    return glm::vec3(0.0f, 0.0f, 1.0f);
}

I would also recommend to write linear depth from you shader. This will make the shadow test a lot easier. For the shadow map just use the direction to the point light for the cubemap sampler and compare the (linear) distance.

Felix Brüll
  • 367
  • 2
  • 13
  • Thanks for your reply. I'm a DirectX 11 novice and Cubemap textures are a new concept to me and I'm struggling to get my head around it. I'm unsure how to create a matrix for each view/projection of each face of the cube and passing it between shaders. I currently only have one view/projection matrix from the light's persepective being passed over to the vertex shader through a constants buffer and the shadow texture being passed to the shaders using PSSetShaderResources(). – Jonathan Jan 02 '20 at 15:38
  • Oh, you only render into a single cubemap face at once. So you only need a single view/projection matrix for one render pass. But you have to do the render pass six times (once for each face). Above I described the required parameters for your view (position direction up) and projection (aspect, fov) for each face – Felix Brüll Jan 03 '20 at 16:12
  • The issue that I am having is that I have no idea where to start, and I've only just learnt how to use geometry shaders. There seems to be limited resources for cubemapping in dx11 and how to do shadowing with point lights. Thanks for trying to help though – Jonathan Jan 06 '20 at 00:32