Sign in to follow this  
Sfpiano

Pick function no longer works after applying multiple viewports

Recommended Posts

Here is my pick function which worked fine with one viewport, but doesn't now that I use two viewports. I passed the width of each viewport instead of the entire screen too, but that didn't work.
// Get the ray from mouse pos
	D3DXMATRIXA16 matProj;
    device->GetTransform(D3DTS_PROJECTION, &matProj);

    POINT ptCursor;
	GetCursorPos(&ptCursor);
	ScreenToClient(hWnd, &ptCursor);

	// Compute the vector of the pick ray in screen space
	D3DXVECTOR3 v;
	v.x =  (((2.0f * ptCursor.x) / width) - 1) / matProj._11;
	v.y = -(((2.0f * ptCursor.y) / height) - 1) / matProj._22;
	v.z =  1.0f;

	// Get the inverse of the composite view and world matrix
	D3DXMATRIXA16 matView, matWorld, m;
	device->GetTransform(D3DTS_VIEW, &matView);
	device->GetTransform(D3DTS_WORLD, &matWorld);
        
	m = matWorld * matView;
	D3DXMatrixInverse(&m, NULL, &m);

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

	// Get the picked triangle
	bool collision=false;
	
	// Collect only the closest intersection
	int	index, tempIndex;
	FLOAT fBary1, fBary2, fClosestDist=343.0f;
	D3DXVECTOR3 vTempTri[3];

	if(iRenderPT == D3DPT_TRIANGLELIST){
		for(i=0; i<iNumTriangles; i++){
			vTempTri[0] = pVertices[3*i+0].vPos;
			vTempTri[1] = pVertices[3*i+1].vPos;
			vTempTri[2] = pVertices[3*i+2].vPos;

			if(D3DXIntersectTri(&vTempTri[0], &vTempTri[1], &vTempTri[2], 
								&vPickRayOrig, &vPickRayDir, &fBary1, 
								&fBary2, fDist))
			{
				D3DXVECTOR3 vIntersect = vPickRayOrig+(vPickRayDir*(*fDist));
				collision=true;
				bSelected = true;
				if(*fDist < fClosestDist){
					fClosestDist = *fDist;
					index=i;
				}
			}
		}
	}

Share this post


Link to post
Share on other sites
Quote:
Original post by Sfpiano
...I passed the width of each viewport instead of the entire screen too, but that didn't work...

I agree that it is most likely to be screen coords -> ray which is causing the problem.

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