2011-08-03 74 views
0

我想在我的3D場景上渲染一些精靈,但是當我啓用D3D精靈時,我的3D場景消失並且我只能看到那些精靈。DirectX中的3D和2D顯示

設置:

LPDIRECT3D9 d3d = NULL; 
LPDIRECT3DDEVICE9 d3ddev = NULL; 
D3DPRESENT_PARAMETERS d3dpp; 
LPD3DXSPRITE d3dspt; 

// Create Direct3D and the Direct3D Device 
void InitDirect3D(GAMEWINDOW* gw) 
{ 
    d3d = Direct3DCreate9(D3D_SDK_VERSION); 

    ZeroMemory(&d3dpp, sizeof(d3dpp)); 
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; 
    d3dpp.Windowed = gw->Windowed; 
    d3dpp.BackBufferWidth = gw->Width; 
    d3dpp.BackBufferHeight = gw->Height; 
    d3dpp.EnableAutoDepthStencil = TRUE; 
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16; 

    d3d->CreateDevice(D3DADAPTER_DEFAULT, 
           D3DDEVTYPE_HAL, 
           gw->hWnd, 
           D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
           &d3dpp, 
           &d3ddev); 

    d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE); 
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); 
    d3ddev->SetRenderState(D3DRS_CULLMODE, TRUE); 

    D3DXCreateSprite(d3ddev, &d3dspt); 

    return; 
} 

渲染:

// Start rendering 
void StartRender() 
{ 

    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); 
    d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); 
    d3ddev->BeginScene(); 
    d3dspt->Begin(D3DXSPRITE_ALPHABLEND); // when enabled, 3d scene dissapears 

    return; 
} 

// Stop rendering 
void EndRender() 
{ 
    d3dspt->End(); // disabling sprites 
    d3ddev->EndScene(); 
    d3ddev->Present(NULL, NULL, NULL, NULL); 

    return; 
} 

渲染功能:

void Render() 
{ 
    static int frame = 0; 
    if (frame == 36) frame = 0; 

    StartRender(); 

    DrawSprite(&interceptor, frame++, 100, 100, 0); 

    DrawModel(&a, 0.0f, 0.0f, 0.0f); 



    EndRender(); 

    return; 
} 

回答

0

嘗試從場景中的其餘部分分開處理你的精靈。你可以創建一個單一的渲染功能,將讓你的生活變得更輕鬆:

void Render() 
{ 
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 
        D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); 
    d3ddev->BeginScene(); 
    d3dspt->Begin(D3DXSPRITE_ALPHABLEND); 

    My3DRenderingFunction(); 

    MySpriteRenderingFunction(); 

    d3dspt->End(); // disabling sprites 
    d3ddev->EndScene(); 
    d3ddev->Present(NULL, NULL, NULL, NULL); 

} 

的My3DRenderingRunction()和MySpriteRenderingFunction()將是你的自定義功能,你會令一切。您甚至可以將回調函數(函數指針)傳遞給渲染函數。另外,請注意您不需要兩次Clear()調用。你可以用一個:

d3ddev->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 
        D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); 
+0

你是什麼意思分離?我正在做你如何張貼。更新了包含更多源代碼的主題。 – Neomex