简体   繁体   中英

DirectX 11 - Point light shadowing

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM