• Advertisement
Sign in to follow this  

D3DXMatrixOrthoOffCenterLH replacement, 2D graphics without RHW

This topic is 3799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Quite often people face the problem of aligning texels with pixels. There are plenty documents found on the theory behind this, but few providing any solution other than "shift your vertices -0.5 upwards and -0.5 to the left. Many took this literally, and are doing this per vertex. Of course this will cause a severe performance penalty. Another approach would be by using the FVF_XYZRWH parameter (if you can call it that) to bypass all the transformation matrixes. This again translates to a performance penalty. Instead of using FVF_XYZRWH, it would be more beneficial to set your matrixes accordingly. An even better approach would be to include the -0.5 shift as well. When applying this to the projection matrix, you can take things even further by adjusting this matrix to function in a more logical sense. This will make 2D graphics (tile games, side scrollers, UI development) extremely easy. The following code is a modified version of the D3DXMatrixOrthoOffCenterLH library function.
void d3d_2d_ortho(D3DMATRIX * m, const float w, const float h, const float znear, const float zfar) {

	ZeroMemory(m, sizeof(D3DMATRIX));

	m->_11 = 2.0f / w;
	m->_22 = 2.0f / -h;
	m->_33 = 1.0f / (zfar - znear);
	m->_41 = (w + 1.0f) / -w;
	m->_42 = (h + 1.0f) / h;
	m->_43 = znear / (znear - zfar);
	m->_44 = 1.0;
Placing your textures is now as simple as drawing on your desktop. It will give you a simple canvas where the top left is 0, 0 and the bottom right equals w, h.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement