我想在我的3D场景上渲染一些精灵,但是当我启用D3D精灵时,我的3D场景消失了,我只能看到那些精灵。
设定:
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 :(得分: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()将是您自定义函数,您可以在其中呈现所有内容。您甚至可以将回调函数(函数指针)传递给render函数。另外,请注意您不需要两个Clear()调用。你可以使用一个:
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);