简体   繁体   English

DirectX9和HLSL的阴影映射问题

[英]Shadow Mapping Issue with DirectX9 and HLSL

I am attempting to teach myself shadow mapping to integrate it into my game, and I am currently using this tutorial (without the soft shaders for now): 我试图自学阴影映射以将其集成到我的游戏中,并且我目前正在使用本教程(暂时没有软着色器):

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

I have successfully implemented most of the code into my game. 我已将大多数代码成功实现到我的游戏中。 I can form the matrix needed to create the depth buffer for the shadow map, and have it render so I know it looks correct. 我可以形成为阴影贴图创建深度缓冲区所需的矩阵,并对其进行渲染,因此我知道它看起来正确。 Then I enter that into the final render loop and that is where I have run into my issues. 然后,将其输入到最终的渲染循环中,这就是我遇到的问题所在。 I cannot seem to implement the final matrix calculation correctly, or I have have done something wrong along the way. 我似乎无法正确实现最终矩阵计算,或者在此过程中做错了什么。 I am basically one step away from this working hopefully, but my matrix calculations do not seem to be correct, I am hoping that is the case and it is not something that is in another part of the implementation. 我基本上距离完成这项工作只有一步之遥,但是我的矩阵计算似乎并不正确,我希望情况确实如此,并且不是实现的另一部分。

Here is the initialization code (no known issues here): (removed for simplification) 这是初始化代码(此处没有已知问题):(为简化起见,已删除)

Here is the initialization code to setup for the depth buffer render (no known issues here): (removed for simplification) 这是为深度缓冲区渲染设置的初始化代码(此处没有已知问题):(为简化起见,已删除)

All of this code successfully created a depth map and puts it into g_pShadowMap 所有这些代码均成功创建了深度图并将其放入g_pShadowMap

Here is the HLSL Code used to render the depth map (no known problems here): (removed for simplification) 这是用于渲染深度图的HLSL代码(此处没有已知问题):(为简化起见,已删除)

I save the image of the depth map out into a file and look at it, since the values are 0.5 and larger the image is white, however if I divide the depth value by 100, I see the depth image more accurately, and it is correctly rendering a top-down light source from 20 units above the character, facing in the +x direction as planned. 我将深度图的图像保存到文件中并进行查看,因为值是0.5或更大,所以图像是白色的,但是,如果将深度值除以100,则可以更准确地看到深度图,并且从角色上方的20个单位正确渲染一个自顶向下的光源,并按计划朝向+ x方向。 I am not sure if the z-distances stored within the map need to be a certain scale, but again, the image is white unless I divide the z values by 100 in order to see my "blue" depth map. 我不确定地图中存储的z距离是否需要一定比例,但是同样,除非我将z值除以100才能看到“蓝色”深度图,否则图像为白色。 (I do not use the divided values as my final image). (我不使用除法的值作为最终图像)。

Here is the code for the final render of my terrain (this is where there is most likely a problem): 这是最终地形渲染的代码(这是最有可能出现问题的地方):

// 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

Now for the final HLSL render code, I am assuming I am not dealing with the Shadow Map's matrix 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;
}

Any help would be greatly appreciated. 任何帮助将不胜感激。 I can only assume that I am not calculating a matrix upon the Shadow Map correctly. 我只能假设我没有正确计算阴影贴图上的矩阵。 The example uses LH Projection, while I use RH projections. 该示例使用LH投影,而我使用RH投影。 Also, since I already had my game's matrices already set up, I did not use the examples's D3DXMatrixOrthoLH() call to calculate my final matLightViewProj and I am not sure if i needed to. 另外,由于我已经设置了游戏矩阵,因此我没有使用示例的D3DXMatrixOrthoLH()调用来计算最终的matLightViewProj并且不确定是否需matLightViewProj I believe I have posted all the relevant code for this issue. 我相信我已经发布了与此问题相关的所有代码。 Again I would greatly appreciate any help, it has been stumping me for several days now. 再次,我将不胜感激任何帮助,它已经困扰了我几天。

I have found the answer, apparently I needed to reverse my multiplication of the position and modified light matrix, using the code OUT.vProjCoord = mul( float4(IN.position, 1), g_matTexture ); 我找到了答案,显然我需要使用代码OUT.vProjCoord = mul( float4(IN.position, 1), g_matTexture );反转位置和修改后的光矩阵的乘法OUT.vProjCoord = mul( float4(IN.position, 1), g_matTexture );

It confused me since I multiply it in the opposite order for the normal rendering of the scene. 因为我以相反的顺序将其乘以场景的正常渲染,所以这让我感到困惑。

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

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