DirectX 9引擎Z-buffer不能与D3DImage一起使用

时间:2014-12-29 21:54:02

标签: c++ wpf directx depth-buffer d3dimage

我试图通过D3DImage将我的Direct9引擎集成到我的新WPF应用程序中。除Z缓冲区外,一切正常。 AutoDepthStencil为FALSE我可以渲染我的网格,但深度不起作用。当我激活AutoDepthStencil时,不会绘制任何内容。我一直在寻找几天,没有找到任何帮助meq

她的演示参数

ZeroMemory( m_pp, sizeof(m_pp) );

m_pp->AutoDepthStencilFormat = D3DFMT_D16;
m_pp->BackBufferCount            = 1;
m_pp->MultiSampleQuality         = 0;
m_pp->Windowed                   = Windowed;
m_pp->MultiSampleType            = D3DMULTISAMPLE_8_SAMPLES;
m_pp->EnableAutoDepthStencil     = FALSE;
m_pp->PresentationInterval       = D3DPRESENT_INTERVAL_ONE;
m_pp->Windowed                   = TRUE;
m_pp->BackBufferHeight           = 1;
m_pp->BackBufferWidth            = 1;
m_pp->SwapEffect                 = D3DSWAPEFFECT_DISCARD;
m_pp->BackBufferFormat           = D3DFMT_A8R8G8B8;

renderStates:

m_graphics->GetDeviceD3D()->SetRenderState(D3DRS_LIGHTING, TRUE);    // turn off the 3D lighting
m_graphics->GetDeviceD3D()->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50,50,50));
m_graphics->GetDeviceD3D()->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD); 
m_graphics->GetDeviceD3D()->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
m_graphics->GetDeviceD3D()->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);
m_graphics->GetDeviceD3D()->SetRenderState(D3DRS_SPECULARENABLE, TRUE );
m_graphics->GetDeviceD3D()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);    // both sides of the triangles
m_graphics->GetDeviceD3D()->SetRenderState(D3DRS_ZENABLE, TRUE);    // turn on the z-buffer
m_graphics->GetDeviceD3D()->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
m_graphics->GetDeviceD3D()->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
m_graphics->GetDeviceD3D()->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
m_graphics->GetDeviceD3D()->LightEnable( 0, TRUE );

复位:

m_graphics->GetDeviceD3D()->Clear(0, NULL, D3DCLEAR_TARGET , D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);
m_graphics->GetDeviceD3D()->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

表面和设备创建:

if (FAILED(m_pD3D9->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd,
    dwVertexProcessing,
    m_pp, NULL, GetDevicePtr())))
{
    return NULL;
}

// obtain the standard D3D device interface
if (FAILED(GetDeviceD3D()->QueryInterface(__uuidof(IDirect3DDevice9), reinterpret_cast<void **>(GetDevicePtr()))))
{
    return NULL;
}

// create and set the render target surface
// it should be lockable on XP and nonlockable on Vista

if (FAILED(GetDeviceD3D()->CreateRenderTarget(Width, Height, 
    D3DFMT_A8R8G8B8, D3DMULTISAMPLE_8_SAMPLES, 0, 
    false, // lockable
    GetSurfacePtr(), NULL)))
{
    return NULL;
}
GetDeviceD3D()->SetRenderTarget(0, m_surface);

矩阵

    // set the projection transform
    D3DXMATRIX matProjection;    // the projection transform matrix
    D3DXMatrixPerspectiveFovLH(&matProjection,
                                D3DXToRadian(45),    // the horizontal field of view
                                1.f, // aspect ratio TODO
                                0.001f,    // the near view-plane
                                1000.0f);    // the far view-plane
    m_graphics->GetDeviceD3D()->SetTransform(D3DTS_PROJECTION, &matProjection);     // set the projection

    //CAMERA
    D3DXMATRIX matView;
    D3DXMatrixLookAtLH(&matView,
        &D3DXVECTOR3 (m_scene->Cam->Position.X, m_scene->Cam->Position.Y, m_scene->Cam->Position.Z),   // the camera position
        &D3DXVECTOR3 (m_scene->Cam->Target.X, m_scene->Cam->Target.Y, m_scene->Cam->Target.Z),    // the look-at position
        &D3DXVECTOR3 (m_scene->Cam->UpVector.X, m_scene->Cam->UpVector.Y, m_scene->Cam->UpVector.Z));    // the up direction
    d3ddev->m_device->SetTransform(D3DTS_VIEW, &matView);    // set the view transform to matView

1 个答案:

答案 0 :(得分:1)

将EnableAutoDepthStencil设置为TRUE,确保CreateRenderTarget使用与演示参数中设置相同的宽度和高度