Selecting an Orthographically Projected Object in a Perspective View

Started by
2 comments, last by G-Man9566 14 years ago
I have move and rotate type gizmos in my engine to be able to move objects around in the world. I wanted these gizmo to be the same screen size no matter how far away the camera is from the screen. What I then did was to project the position of the gizmo to screen space(from world space) using D3DXVec3Project and my perspective projection matrix. I then project the position back into world space (from screen space) using D3DXVec3Unproject and an orthographic projection matrix. I then created a world matrix from this position and drew the gizmo using that world matrix. This works very nicely and the gizmo is always the same screen size. The thing I'm finding extremely difficult at this stage is to "pick" or "select" the gizmo in this newly transformed space/view. The following piece of code is a function that I wrote to do the selection, but its not working. Can anyone give me some advise on this matter.


void GetSelected( GizmoAxis* l_pGizmoAxis, /*The gizmo axis im testing for seleciton*/
                  const D3DXMATRIX& l_d3dWorld, /*The world matrix that was transormed as described above*/
                  const D3DXMATRIX& l_d3dmProjectionMatrix, /*Orthographic Projection*/
                  const D3DXMATRIX& l_d3dmViewMatrix,  /*Camera's view matrix*/
                  int l_iMouseX, 
                  int l_iMouseY,
                  int l_iWidth,   /*Viewport Width*/
                  int l_iHeight ) /*Viewport Height*/
{
  D3DXVECTOR3 l_d3dvWorldPosition;
  D3DXMATRIX  l_d3dmInverseView;
  D3DXVECTOR3 l_d3dvRayOrigin;
  D3DXVECTOR3 l_d3dvRayDirection;
  D3DXMATRIX  l_d3dmInverseWorld;
  D3DXVECTOR3 l_d3dvRayObjectOrigin;    /* Ray origin in object/local space*/
  D3DXVECTOR3 l_d3dvRayObjectDirection;	/* Ray direction in object/local space*/

  /*Step 1:*/ 
   l_d3dvWorldPosition.x =  ((( 2.0f *  l_iMouseX) / l_iWidth  ) - 1 ) / l_d3dmProjectionMatrix._11;
  l_d3dvWorldPosition.y = -((( 2.0f *  l_iMouseY) / l_iHeight ) - 1 ) / l_d3dmProjectionMatrix._22;
  l_d3dvWorldPosition.z = 1.0f;

  /*Step 2:*/
  D3DXMatrixInverse( &l_d3dmInverseView, NULL, &l_d3dmViewMatrix);

  l_d3dvRayDirection.x = l_d3dvWorldPosition.x * l_d3dmInverseView._11 + l_d3dvWorldPosition.y * l_d3dmInverseView._21 + l_d3dvWorldPosition.z * l_d3dmInverseView._31; 
  l_d3dvRayDirection.y = l_d3dvWorldPosition.x * l_d3dmInverseView._12 + l_d3dvWorldPosition.y * l_d3dmInverseView._22 + l_d3dvWorldPosition.z * l_d3dmInverseView._32;
  l_d3dvRayDirection.z = l_d3dvWorldPosition.x * l_d3dmInverseView._13 + l_d3dvWorldPosition.y * l_d3dmInverseView._23 + l_d3dvWorldPosition.z * l_d3dmInverseView._33;

  l_d3dvRayOrigin.x = l_d3dmInverseView._41;
  l_d3dvRayOrigin.y = l_d3dmInverseView._42;
  l_d3dvRayOrigin.z = l_d3dmInverseView._43;
    
  /*Step 3: Transform the rays to gizmo's object/local space*/
  D3DXMatrixInverse( &l_d3dmInverseWorld, NULL, &l_d3dWorld );

  D3DXVec3TransformCoord( &l_d3dvRayObjectOrigin, &l_d3dvRayOrigin, &l_d3dmInverseWorld);
  D3DXVec3TransformNormal(&l_d3dvRayObjectDirection, &l_d3dvRayDirection, &l_d3dmInverseWorld);

  //Step 4: Test intersection
  D3DXIntersect( l_pGizmoAxis->d3dMesh,
                 &l_d3dvRayObjectOrigin, 
                 &l_d3dvRayObjectDirection,
		 &l_pGizmoAxis->bSelected,
                  NULL,
                  NULL,
                  NULL,
		 &l_pGizmoAxis->fDistanceToCollision,
                  NULL,
                  NULL );
    }


Advertisement
Does your picking function work for perspective objects? So long as you do the picking in the same coordinate space as you render the object, it should "just work" (Assuming the pick function is generally correct).
Quote:Original post by Evil Steve
Does your picking function work for perspective objects?


Yes, this function works for perspective objects.
Well apparently picking differs for perspective view than for orthographic views. I thought that you can just replace the perspective proj matrix with you ortho proj matrix. But according to this forum, that is not the case; ortho view picking works differently from perspective picking.

This topic is closed to new replies.

Advertisement