【问题标题】:Shadow Mapping Issue with DirectX9 and HLSLDirectX9 和 HLSL 的阴影映射问题
【发布时间】:2014-09-06 00:13:23
【问题描述】:

我正在尝试自学阴影映射以将其集成到我的游戏中,并且我目前正在使用本教程(目前没有软着色器):

http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/soft-edged-shadows-r2193

我已经成功地将大部分代码实现到我的游戏中。我可以形成为阴影贴图创建深度缓冲区所需的矩阵,并让它渲染,这样我就知道它看起来是正确的。然后我将它输入到最终的渲染循环中,这就是我遇到问题的地方。我似乎无法正确实现最终的矩阵计算,或者我在此过程中做错了什么。希望我基本上离这项工作只有一步之遥,但我的矩阵计算似乎不正确,我希望是这样,它不是实现的另一部分。

这里是初始化代码(这里没有已知问题): (为简化起见已删除)

这是设置深度缓冲区渲染的初始化代码(此处没有已知问题): (为简化起见已删除)

所有这些代码都成功创建了深度图并放入g_pShadowMap

这是用于渲染深度图的 HLSL 代码(此处没有已知问题): (为简化起见已删除)

我将深度图的图像保存到一个文件中并查看它,因为值是 0.5 和更大的图像是白色的,但是如果我将深度值除以 100,我可以更准确地看到深度图像,并且它正确地从角色上方 20 个单位渲染自上而下的光源,按计划面向 +x 方向。我不确定存储在地图中的 z 距离是否需要一定的比例,但同样,图像是白色的,除非我将 z 值除以 100 以查看我的“蓝色”深度图。 (我不使用分割后的值作为我的最终图像)。

这是我的地形最终渲染的代码(这是最有可能出现问题的地方):

// Restore the render target and depth buffer
g_pd3dDevice->SetRenderTarget( 0, g_pOldColorRT );
g_pd3dDevice->SetDepthStencilSurface( g_pOldDepthRT );
SAFE_RELEASE( g_pOldColorRT );
SAFE_RELEASE( g_pOldDepthRT );

// Compute the texture matrix
float fTexOffs = 0.5 + (0.5 / (float)VGlobal::SHADOW_MAP_SIZE);
D3DXMATRIX matTexAdj( 0.5f,     0.0f,   0.0f,   0.0f,
                          0.0f,    -0.5f,   0.0f,   0.0f,
                          0.0f,     0.0f,   1.0f,   0.0f,
                          fTexOffs, fTexOffs,  0.0f, 1.0f );

D3DXMATRIX matTexture = matLightViewProj * matTexAdj;
g_pEffect->SetMatrix( "g_matTexture", &matTexture );
g_pEffect->SetTexture( "tShadowMap", g_pShadowMap );

// Begin the scene
// g_pd3dDevice->BeginScene();

// Setup the world, view, and projection matrices
SetupMatrices();

// Extract the 6 Viewing Frustrum planes for culling calculations
D3DXMATRIX tWorld, tView, tProj, tFinal;
g_pd3dDevice->GetTransform( D3DTS_PROJECTION, &tProj );
g_pd3dDevice->GetTransform( D3DTS_VIEW, &tView );
D3DXMatrixMultiply( &tFinal, &tView, &tProj);
gFrustrum.ExtractPlanesD3D( gFrustrum.frPlanes, tFinal, true );


D3DXMATRIX tempMat = matInfo.ReturnWorld() * matInfo.ReturnView() * matInfo.ReturnProj();
D3DXMatrixTranspose( &tempMat, &tempMat );

g_pEffect->SetMatrix( "worldViewProj", &tempMat );
g_pEffect->SetTechnique( "Technique0" );

//The main render code continues from here, and renders the non-shaded aspects correctly

现在对于最终的 HLSL 渲染代码,我假设我没有正确处理阴影贴图的矩阵...

fragment TerrainVS( vertex IN )
{
    fragment OUT;

    OUT.hposition = mul( worldViewProj, float4(IN.position, 1) );

    //Removed non-relevant code (textures/color calculation)
    //...

    //Shadow mapping
    // Output the projective texture coordinates
    OUT.vProjCoord = mul( g_matTexture, float4(IN.position, 1) );

return OUT;
}

pixel TerrainPS( fragment IN )
{
    pixel OUT;

    //Removed non-relevant code (normal/lighting/color calculations)
    //...

    // Grab the shadow term
    float fShadowTerm = 0.0f;
    fShadowTerm = tex2Dproj( ShadowSampler, IN.vProjCoord ) < (IN.vProjCoord.z - 0.001f) ? 0.1f : 1.0f;


    OUT.color = max( normColor1, normColor2 ) * fShadowTerm;

    OUT.color.a = 1.0f;

    return OUT;
}

任何帮助将不胜感激。我只能假设我没有正确计算阴影贴图上的矩阵。该示例使用 LH 投影,而我使用 RH 投影。另外,由于我已经设置了我的游戏矩阵,我没有使用示例的D3DXMatrixOrthoLH() 调用来计算我的最终matLightViewProj,我不确定是否需要。我相信我已经发布了这个问题的所有相关代码。再次感谢任何帮助,这已经困扰了我好几天了。

【问题讨论】:

  • 是否可以将这个庞大的代码示例剪掉一点,以便您描述的only the part that produces the error 出现在问题中?
  • @APerson 当然,我只是想确保我发布了任何相关代码,以防我遗漏了一些东西,但如果我在这里放了太多信息,我会删除它。跨度>

标签: c++ hlsl directx-9 shadows


【解决方案1】:

我找到了答案,显然我需要使用代码OUT.vProjCoord = mul( float4(IN.position, 1), g_matTexture );来反转我的位置乘法和修改后的光矩阵

这让我很困惑,因为我以相反的顺序将它相乘以正常渲染场景。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 2014-01-16
    相关资源
    最近更新 更多