简体   繁体   English

DirectX 11 - 点光阴影

[英]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.我正在努力在 DirectX 11 中使用立方体阴影贴图来实现点光源阴影。我四处搜索,似乎只有 OpenGL 或更早版本的 DirectX 中的教程。 I've setup a shadow map texture using CreateTexture2D(), CreateDepthStencilView() and CreateShaderResourceView() using the TextureCube flags where possible.在可能的情况下,我使用 CreateTexture2D()、CreateDepthStencilView() 和 CreateShaderResourceView() 设置了阴影贴图纹理。 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;编辑:我目前有一个 D3D11_TEXTURE2D_DESC 变量来创建阴影纹理作为 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.深度模板已使用 D3D11_DEPTH_STENCIL_VIEW_DESC 创建,着色器资源视图已使用 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.对于每个点光源,您应该按如下方式渲染 6 个立方体贴图面:相机的位置就是点光源的位置。 You should use a perpective projection with aspect 1 and a 90 degrees field of view.您应该使用具有方面 1 和 90 度视野的透视投影。 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.对于阴影贴图,只需使用立方体贴图采样器的点光源方向并比较(线性)距离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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