Clipplanes, vertex shaders and hardware vertex processing in Direct3D 9

Posted by Igor on Stack Overflow See other posts from Stack Overflow or by Igor
Published on 2010-06-13T23:27:11Z Indexed on 2010/06/13 23:32 UTC
Read the original article Hit count: 433

Filed under:
|

Hi,

I have an issue with clipplanes in my application that I can reproduce in a sample from DirectX SDK (February 2010).

I added a clipplane to the HLSLwithoutEffects sample:

...
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() );
    }
}

When I rotate the camera I have different visual results when using hardware and software vertex processing. In software vertex processing mode or when using the reference device the clipping plane works fine as expected. In hardware mode it seems to rotate with the camera.

If I remove the call to RenderText(); from OnFrameRender then hardware rendering also works fine. Further debugging reveals that the problem is in ID3DXFont::DrawText.

I have this issue in Windows Vista and Windows 7 but not in Windows XP. I tested the code with the latest NVidia and ATI drivers in all three OSes on different PCs. Is it a DirectX issue? Or incorrect usage of clipplanes?

Thanks

Igor

© Stack Overflow or respective owner

Related posts about direct3d

Related posts about direct3d9