Jump to content
  • Advertisement
Sign in to follow this  
Sfpiano

Pick function no longer works after applying multiple viewports

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

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!