world <-> screen convert

Started by
2 comments, last by qwertyPrince 8 years, 2 months ago

XMMATRIX ViewProj = _view() * _projection();
XMVECTOR pointInWorld  = XMVector3Transform(worldPoint, ViewProj);

	float x = ((XMVectorGetX(pointInWorld) + 1.0f) / 2.0f) *_width;
	float y = ((-XMVectorGetY(pointInWorld) + 1.0f) / 2.0f) *_height;

XMVECTOR pointInScreen = DirectX::XMVectorSet(x, y, 0.0f, 0.0f); // works fine

	float xx = (((2.0f * x) / _width) - 1.0f);
	float yy = -(((2.0f * y) / _height) - 1.0f);
	float zz = DirectX::XMVectorGetZ(pointInWorld);

	XMMATRIX invView = XMMatrixInverse(NULL, ViewProj);
	XMVECTOR mousePos = XMVectorSet(xx, yy, zz, 1.0f);
	mousePos = XMVector3Transform(mousePosition, invView); // incorrect

Hi, i am trying to figure out why i cannot accurately convert between world and screen coordinates. The code above takes a point in world space and converts it into a screen position (). This seems to work fine, but when i do the opposite to get the original world coordinate, the result is wrong. The x coordinate is usually close but the y and z are tottaly off. Anyone care to explain this to me?

Advertisement

i could not figure out where i was going wrong and decided to use the XNA project and unproject which work a charmwink.png

I think you had to divide xyz by w :)

.:vinterberg:.

I think you had to divide xyz by w smile.png

yea, thats what was missing! thanks

This topic is closed to new replies.

Advertisement