Книга: DirectX 8 Programming Tutorial
2D Camera
2D Camera
To setup the camera for 2D objects we need to change the projection matrix from a perspective one used in 3D to an orthogonal projection. When using a perspective projection, objects that are further away from the camera are smaller than objects that are closer to the camera. When using an orthogonal projection, an object will be the same size no matter how far it is from the camera. For example, when you use your 3D modelling program, you normally get three orthogonal projections (top, side and front) and a perspective projection (3D). The code for setting the camera for 2D is shown below:
void CGame::Setup2DCamera() {
D3DXMATRIX matOrtho;
D3DXMATRIX matIdentity;
//Setup the orthogonal projection matrix and the default world/view matrix
D3DXMatrixOrthoLH(&matOrtho, (float)m_nScreenWidth, (float)m_nScreenHeight, 0.0f, 1.0f);
D3DXMatrixIdentity(&matIdentity);
m_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matOrtho);
m_pD3DDevice->SetTransform(D3DTS_WORLD, &matIdentity);
m_pD3DDevice->SetTransform(D3DTS_VIEW, &matIdentity);
//Make sure that the z-buffer and lighting are disabled
m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
}
In addition to changing the projection matrix, we also set the world and view matrices to an identity matrix and disable the z-buffer and lighting. We disable the z-buffer so that everything that is rendered from then on will appear on top of objects that have already been rendered (3D objects).