Sign in to follow this  
Coldon

Mouse Picking Help - Vec3Unproject not working as intended

Recommended Posts

Coldon    100
So I've spent several days scouring the web in regards to this with most links leading back to this forum. I've been attempting to do mouse picking and from everything i've read using the d3dx10Vec3unproject function seemed to be the best option (it looks to me as they simply wrapped the pick example in the d3d 9 sdk in a function). Unfortunately I've lost several days trying to get it to work to no avail. rayObjDirection and RayObjOrigin are completely different to the r and rDir i got from the unproject function. here is my pick code, using both the sdk and the unproject version but the results of unproject are completely different to the sdk method. So what the hell does unproject do internally? I can only assume its something to do with the viewport as that is the only structure that is not used in the sdk version (where i manually specify window width and height)... i've been pulling my hair out, any help would be appreciated. Thanks in advance.
case WM_LBUTTONDOWN: 
		{
			lDown = true;
			UINT hc;

			D3DXMATRIX w;
			D3DXMatrixIdentity(&w);

			D3DXVECTOR3 v;
			v.x =  ( ( ( 2.0f * mouseX ) / windowWidth  ) - 1 ) / ProjectionMatrix._11;
			v.y = -( ( ( 2.0f * mouseY ) / windowHeight ) - 1 ) / ProjectionMatrix._22;
			v.z =  1.0f;

			D3DXMATRIX m;
			D3DXVECTOR3 rayOrigin,rayDir;

			D3DXMatrixInverse( &m, NULL, &ViewMatrix );

			// Transform the screen space pick ray into 3D space
			rayDir.x  = v.x*m._11 + v.y*m._21 + v.z*m._31;
			rayDir.y  = v.x*m._12 + v.y*m._22 + v.z*m._32;
			rayDir.z  = v.x*m._13 + v.y*m._23 + v.z*m._33;
			rayOrigin.x = m._41;
			rayOrigin.y = m._42;
			rayOrigin.z = m._43;

			// Use inverse of matrix
			D3DXMATRIX matInverse;
			D3DXMatrixInverse(&matInverse,NULL,&cp.WorldMatrix);

			// Transform ray origin and direction by inv matrix
			D3DXVECTOR3 rayObjOrigin,rayObjDirection;

			D3DXVec3TransformCoord(&rayObjOrigin,&rayOrigin,&matInverse);
			D3DXVec3TransformNormal(&rayObjDirection,&rayDir,&matInverse);
			D3DXVec3Normalize(&rayObjDirection,&rayObjDirection);

			//unproject test
			D3DXVECTOR3 r, rDir;

			D3DXVec3Unproject( &r, &D3DXVECTOR3( mouseX, mouseY, 0 ), &viewPort, &ProjectionMatrix, &ViewMatrix, &cp.WorldMatrix);
			D3DXVec3Unproject( &rDir, &D3DXVECTOR3( mouseX, mouseY, 1 ), &viewPort, &ProjectionMatrix, &ViewMatrix, &cp.WorldMatrix);
			
			//ray direction
			rDir = rDir - r;

			BOOL hasHit;
			float distanceToCollision;

			cp.mesh->Intersect(&rayObjOrigin, &rayObjDirection, &hc, NULL, NULL, NULL, &distanceToCollision, NULL);
			
			if ( hc > 0) MessageBox(hWnd, "Hit!", "Hit!", MB_OK);
		}
		break;



Share this post


Link to post
Share on other sites
ET3D    810
Looks to me like you're mixing together two unprojection methods. A simple way is to unproject the point with z=0 and z=1. The ray is then v1-v0 and origin v0.

The Pick code does such calculations itself, which may be a little more efficient (it's less calculation, but I can't say how SSE and other optimisations will affect optimality).

Looks to me like you're doing both here. I'd suggest the first method, because IMO it results in clearer code.

Share this post


Link to post
Share on other sites
Buckeye    10747
Where do you get the mouse point? If you use GetCursorPos(), you need to convert the point from screen to client coordinates, i.e., ScreenToClient(hwnd,&pt); or something similar.

Share this post


Link to post
Share on other sites
Coldon    100
no you guys are missing the point, I'm using the first method to just illustrate the brokenness of the unproject function, i am converting my mouse co-ordinates, the first method works, but the second method, returns completely wrong unprojected co-ordinates, i think the dx10 guys broke something.

does any one have a working example of picking using vec3Unproject with the june2k8 sdk?

Share this post


Link to post
Share on other sites
Coldon    100
"my" method is the method used in the d3d pick sample, so it is correct (it also returns the correct co-ordinates and so picking actually works), the vec3unproject tho does not return the correct co-ordinates. the return values from it are around 10+ times smaller than the expected values. Something is broken and i'd love to know what it is.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this