Jump to content
  • Advertisement
Sign in to follow this  

Selecting an Orthographically Projected Object in a Perspective View

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

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,
                  NULL );

Share this post

Link to post
Share on other sites
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).

Share this post

Link to post
Share on other sites
Original post by Evil Steve
Does your picking function work for perspective objects?

Yes, this function works for perspective objects.

Share this post

Link to post
Share on other sites
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.

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!