Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / 3D Theory & Graphics / D3DXIntersect (a solution).... Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
shields

May 08, 2005, 08:44 AM

For anyone still stuck on this issue here is my working c++ code...

For D3DXIntersect to work with your mesh, your mesh must be indiced...

  1.  
  2. BOOL GetRayAtCursor(LPDIRECT3DDEVICE9 lD3DDevice, LPD3DXMESH lD3DMesh, HWND lhWnd)
  3. {
  4. //Mouse variables
  5.     POINT pt;
  6.  
  7. //UnProjection variables
  8.     D3DVIEWPORT9 vpScreen;
  9.     D3DXMATRIX matProj;
  10.     D3DXMATRIX matView;
  11.     D3DXMATRIX matWorld;
  12.     D3DXVECTOR3 vPickOrigin;
  13.     D3DXVECTOR3 vPickDir;
  14.  
  15. //D3DXIntersect variables
  16.     BOOL bHit=0;
  17.     DWORD uFaceIndex=0;
  18.     DWORD uFaceCount=0;
  19.     float fU=0;
  20.     float fV=0;
  21.     float fG=0;
  22.     float fDist=0;
  23.  
  24. //Get the screen cursor
  25.     GetCursorPos(&pt);
  26.     ScreenToClient(lhWnd, &pt);
  27.  
  28.  
  29. //Get all the 3D UnProjection variables
  30.         lD3DDevice->GetViewport(&vpScreen);
  31.         lD3DDevice->GetTransform(D3DTS_PROJECTION, &matProj);
  32.         lD3DDevice->GetTransform(D3DTS_VIEW, &matView);
  33.         lD3DDevice->GetTransform(D3DTS_WORLD, &matWorld);
  34.  
  35.  
  36. //Compute UnProjection
  37.         D3DXVec3Unproject(&vPickDir, &D3DXVECTOR3((float)pt.x, (float)pt.y, 0), &vpScreen, &matProj, &matView, &matWorld);
  38.         D3DXVec3Unproject(&vPickOrigin, &D3DXVECTOR3((float)pt.x, (float)pt.y, 1.f), &vpScreen, &matProj, &matView, &matWorld);
  39.         D3DXVec3Subtract(&vPickDir, &vPickDir, &vPickOrigin);
  40.  
  41.  
  42. //Compute D3DXIntersect
  43.     D3DXIntersect(lD3DMesh,
  44.                         &vPickOrigin,
  45.                         &vPickDir,
  46.                         &bHit,
  47.                         &uFaceIndex,
  48.                         &fV,
  49.                         &fG,
  50.                         &fDist,
  51.                         NULL,
  52.                         &uFaceCount);
  53.  
  54.         return bHit;
  55. }
  56.  

 
shields

May 08, 2005, 02:25 PM

whoops... I made a mistake...
I got the vPickOrigin and vPickDir the wrong way round!.

It should read like this..

  1.  
  2. D3DXVec3Unproject(&vPickOrigin, &D3DXVECTOR3((float)pt.x, (float)pt.y, 0), &vpScreen, &matProj, &matView, &matWorld);
  3.        
  4. D3DXVec3Unproject(&vPickDir, &D3DXVECTOR3((float)pt.x, (float)pt.y, 1.f), &vpScreen, &matProj, &matView, &matWorld);
  5.  

 
This thread contains 2 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.