I thot the parameters were like parameter 0, 1, 2, 3, 4 not 1, 2, 3, 4
So yes I fixed that
So I unprojected p1 as well after changing v.z to one, then did the subtraction like you said
So is this correct?
D3DXVECTOR3 v;
v.x = float(pd3dsdBackBuffer->Width /2);
v.y = -(float(pd3dsdBackBuffer->Height /2));
v.z = 0.0f;
const D3DXMATRIX* pmatProj = g_Camera.GetProjMatrix();
const D3DXMATRIX matView = *g_Camera.GetViewMatrix();
const D3DXMATRIX matWorld = *g_Camera.GetWorldMatrix();
D3DVIEWPORT9 tempmatViewPort;
g_pd3dDevice->GetViewport( &tempmatViewPort );
const D3DVIEWPORT9 matViewPort = tempmatViewPort;
D3DXVECTOR3 p0, p1;
D3DXVec3Unproject(&p0, &v, &matViewPort, pmatProj, &matView, &matWorld );// origin
v.z = 1.0f;
D3DXVec3Unproject(&p1, &v, &matViewPort, pmatProj, &matView, &matWorld );// direction
p1 -= p0;
Thank you all very much