【问题标题】:DirectX 9 engine Z-buffer not working with D3DImageDirectX 9 引擎 Z 缓冲区不适用于 D3DImage
【发布时间】:2014-12-29 21:54:01
【问题描述】:

我正在尝试通过 D3DImage 将我的 Direct9 引擎集成到我的新 WPF 应用程序中。除了 Z 缓冲区外,一切正常。使用 FALSE 的 AutoDepthStencil,我可以渲染我的网格,但深度不起作用。当我激活 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;

渲染状态:

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

【问题讨论】:

  • 最好提供图片。
  • 遗憾的是,我需要 10 个代表才能这样做。你可以在 gamedev 网站上查看我的 x-post:gamedev.stackexchange.com/questions/90290/…
  • 你已经掌握了使用z缓冲区的所有步骤,我唯一能猜到的是单词/视图矩阵不正确。
  • 我添加了矩阵。我还尝试了 D3DXMatrixPerspectiveFovRH 和 D3DXMatrixLookAtRH。我还尝试了来自 msdn msdn.microsoft.com/en-us/library/cc656716(v=vs.110).aspx 的 D3DImage 新教程,您只需复制粘贴文件并按照步骤操作即可。该教程效果很好。但是只要添加 m_pp->EnableAutoDepthStencil = TRUE; m_pp->AutoDepthStencilFormat = D3DFMT_D16; m_graphics->GetDeviceD3D()->SetRenderState(D3DRS_ZENABLE, TRUE);什么都没有呈现...非常感谢您的帮助!
  • 您是否收到任何错误消息?您是否尝试过不同的深度缓冲区格式?

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


【解决方案1】:

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

【讨论】:

  • 非常感谢您的帮助!我更改了演示参数的宽度/高度,并使用 D3DFMT_D24S8 作为模板格式,它起作用了!
猜你喜欢
  • 1970-01-01
  • 2016-03-05
  • 1970-01-01
  • 1970-01-01
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多