Jump to content
  • Advertisement
Sign in to follow this  
Raeldor

DirectX Unproject Problem

This topic is 4883 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

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by deffer
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.


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

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!