# DirectX Unproject Problem

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

## Recommended Posts

I am trying to write my own Unproject function, but I am getting results close, but different to the D3DXVec3Unproject function. This function returns... + _D3DVECTOR {x=31.804205 y=90.251999 z=-56.938885 } _D3DVECTOR my function returns... + nearPoint {x=31.804203 y=89.938942 z=-57.251949 } Vector3 I have coded my function using Direct3D's own routines, and it reads....
Vector3 Camera::Get3DFrom2D(Renderer* in_renderer, Vector3 in_vector)
{
Vector4 retVector;
Matrix projection, view;

dev->GetTransform(D3DTS_PROJECTION, (D3DMATRIX*)&projection);
dev->GetTransform(D3DTS_VIEW, (D3DMATRIX*)&view);

// first work out as a value between -1.0f and 1.0f
retVector.x=((in_vector.x/m_outputWidth)*2.0f)-1.0f;
retVector.y=((in_vector.y/m_outputHeight)*2.0f)-1.0f;
retVector.z=in_vector.z;
retVector.w=1.0f;

// reverse projection
D3DXMATRIX invCheck;
D3DXMatrixInverse(&invCheck, NULL, (D3DXMATRIX*)&projection);
D3DXVec4Transform((D3DXVECTOR4*)&retVector, (D3DXVECTOR4*)&retVector, (D3DXMATRIX*)&invCheck);

retVector.x/=retVector.w;
retVector.y/=retVector.w;
retVector.z/=retVector.w;
retVector.w/=retVector.w;

// reverse view transform
D3DXMatrixInverse(&invCheck, NULL, (D3DXMATRIX*)&view);
D3DXVec4Transform((D3DXVECTOR4*)&retVector, (D3DXVECTOR4*)&retVector, (D3DXMATRIX*)&invCheck);

return Vector3(retVector.x, retVector.y, retVector.z);
}


What could the D3DX routine be doing differently to get different results? I have checked output width and height are the same as the directx viewport setting using GetViewport. My world matrix is simply identity. I am really stumped. I can understand some decimal inaccuracy like in the x component, but the y and z are so far out that the result is skewing the functionality of my application. Thanks

##### Share on other sites
It could be multiplying both inversed matrices together, then transforming the vector and dividing by its w.

Also, are you sure this is the exact formula used by original function:
((in_vector.x/m_outputWidth)*2.0f)-1.0f
It could be substracting first and then multiplying, or God-knows-what else.

PS: You also got my response it the other thread.

##### Share on other sites
Quote:
 Original post by defferIt could be multiplying both inversed matrices together, then transforming the vector and dividing by its w.Also, are you sure this is the exact formula used by original function:((in_vector.x/m_outputWidth)*2.0f)-1.0fIt could be substracting first and then multiplying, or God-knows-what else.PS: You also got my response it the other thread.

OMG, I think I found the problem. It should be...

retVector.y=1.0f-in_vector.y/(m_outputHeight/2.0f);

not

retVector.y=in_vector.y/(m_outputHeight/2.0f)-1.0f;

I assume this is because screen space y runs downwards, and 3d space y runs upwards?

Regards

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 24
• 42
• 75