Here's how I do it, the only problem is that depth testing doesn't work because the ZNear and ZFar values of my projection matrix are wrong. I haven't had any luck finding the proper values yet.
If you're not using shaders just replace _shaderConstants->SetMatrix() with IDirect3DDevice9::SetTransform()
Let me know if something is unclear and I'll post more code.
Code:
void D3D::BeginDrawing(const Camera* cam)
{
if(_stateblock)
{
_stateblock->Release();
_stateblock = NULL;
}
//FIXME: Add error checking..
dev->CreateStateBlock(D3DSBT_ALL, &_stateblock);
//FIXME: Needs the real ZNear/ZFar, can't find them in the camera struct any more.
D3DXMatrixPerspectiveFovRH(&_projMatrix, cam->Fov() * 0.6f, cam->AspectRatio(), 0.0f, 10000.0f);
D3DXVECTOR3 eye = cam->Position();
D3DXVECTOR3 at = eye + cam->ForwardVector();
D3DXMatrixLookAtRH(&_viewMatrix, &eye, &at, &D3DXVECTOR3(0, 0, 1));
dev->SetVertexShader(_vs);
dev->SetPixelShader(_ps);
_shaderConstants->SetMatrix(dev, "View", &_viewMatrix);
_shaderConstants->SetMatrix(dev, "Proj", &_projMatrix);
dev->SetVertexDeclaration(_vdecl);
dev->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
dev->SetRenderState(D3DRS_LIGHTING, FALSE);
dev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
dev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
dev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
}
void D3D::EndDrawing()
{
if(_stateblock)
{
_stateblock->Apply();
_stateblock->Release();
_stateblock = NULL;
}
}
void D3D::DrawCube(D3DXVECTOR3 const& targetpos)
{
if(dev->TestCooperativeLevel() != D3D_OK)
return;
D3DXMATRIX world;
D3DXMatrixIdentity(&world);
D3DXMatrixTranslation(&world, targetpos.x, targetpos.y, targetpos.z);
_shaderConstants->SetMatrix(dev, "World", &world);
dev->SetStreamSource(0, _cubeVB, 0, sizeof(D3DVERTEX));
dev->SetTexture(0, _cubeTexture);
dev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 12);
}
In my EndScene hook:
Code:
d3d->BeginDrawing(cam);
std::for_each(Objects.begin(), Objects.end(), [&d3d](std::pair<wowguid, WowObject*> obj) {
if(obj.second->Name() == "Ragged Young Wolf")
{
d3d->DrawCube(obj.second->Position());
}
});
d3d->EndDrawing();