我的应用程序中存在剪贴平面问题,我可以在 DirectX SDK(2010 年 2 月)的示例中重现该问题。
我在 HLSLwithoutEffects 示例中添加了一个剪贴平面:
...
D3DXPLANE g_Plane( 0.0f, 1.0f, 0.0f, 0.0f );
...
void SetupClipPlane(const D3DXMATRIXA16 & view, const D3DXMATRIXA16 & proj)
{
D3DXMATRIXA16 m = view * proj;
D3DXMatrixInverse( &m, NULL, &m );
D3DXMatrixTranspose( &m, &m );
D3DXPLANE plane;
D3DXPlaneNormalize( &plane, &g_Plane );
D3DXPLANE clipSpacePlane;
D3DXPlaneTransform( &clipSpacePlane, &plane, &m );
DXUTGetD3D9Device()->SetClipPlane( 0, clipSpacePlane );
}
void CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext )
{
// Update the camera's position based on user input
g_Camera.FrameMove( fElapsedTime );
// Set up the vertex shader constants
D3DXMATRIXA16 mWorldViewProj;
D3DXMATRIXA16 mWorld;
D3DXMATRIXA16 mView;
D3DXMATRIXA16 mProj;
mWorld = *g_Camera.GetWorldMatrix();
mView = *g_Camera.GetViewMatrix();
mProj = *g_Camera.GetProjMatrix();
mWorldViewProj = mWorld * mView * mProj;
g_pConstantTable->SetMatrix( DXUTGetD3D9Device(), "mWorldViewProj", &mWorldViewProj );
g_pConstantTable->SetFloat( DXUTGetD3D9Device(), "fTime", ( float )fTime );
SetupClipPlane( mView, mProj );
}
void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
// If the settings dialog is being shown, then
// render it instead of rendering the app's scene
if( g_SettingsDlg.IsActive() )
{
g_SettingsDlg.OnRender( fElapsedTime );
return;
}
HRESULT hr;
// Clear the render target and the zbuffer
V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 45, 50, 170 ), 1.0f, 0 ) );
// Render the scene
if( SUCCEEDED( pd3dDevice->BeginScene() ) )
{
pd3dDevice->SetVertexDeclaration( g_pVertexDeclaration );
pd3dDevice->SetVertexShader( g_pVertexShader );
pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( D3DXVECTOR2 ) );
pd3dDevice->SetIndices( g_pIB );
pd3dDevice->SetRenderState( D3DRS_CLIPPLANEENABLE, D3DCLIPPLANE0 );
V( pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, g_dwNumVertices,
0, g_dwNumIndices / 3 ) );
pd3dDevice->SetRenderState( D3DRS_CLIPPLANEENABLE, 0 );
RenderText();
V( g_HUD.OnRender( fElapsedTime ) );
V( pd3dDevice->EndScene() );
}
}
当我旋转相机时,我在使用硬件和软件顶点处理时会有不同的视觉结果。在软件顶点处理模式或使用参考设备时,剪裁平面按预期工作。在硬件模式下,它似乎随着相机旋转。
如果我删除对 RenderText(); 的调用 从 OnFrameRender 然后硬件渲染也可以正常工作。进一步调试表明问题出在 ID3DXFont::DrawText 中。
我在 Windows Vista 和 Windows 7 中遇到了这个问题,但在 Windows XP 中没有。我在不同 PC 上的所有三个操作系统中使用最新的 NVidia 和 ATI 驱动程序测试了代码。是 DirectX 的问题吗?或者剪贴平面的使用不正确?
谢谢
伊戈尔